Skip to content

Commit

Permalink
wanto-to-doのAI分割機能
Browse files Browse the repository at this point in the history
  • Loading branch information
yuto-trd committed Oct 27, 2024
1 parent dc839e0 commit 05513d6
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 10 deletions.
155 changes: 146 additions & 9 deletions task_yell/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion task_yell/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@
"googleapis": "^144.0.0",
"lucide-react": "^0.453.0",
"next": "14.2.15",
"openai": "^4.68.4",
"react": "^18",
"react-day-picker": "^8.10.1",
"react-dom": "^18",
"recharts": "^2.13.0",
"tailwind-merge": "^2.5.4",
"tailwindcss-animate": "^1.0.7"
"tailwindcss-animate": "^1.0.7",
"zod": "^3.23.8"
},
"devDependencies": {
"@types/node": "^20",
Expand Down
10 changes: 10 additions & 0 deletions task_yell/src/app/api/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// import { splitWantToDo } from "@/lib/ai";
// import { NextRequest, NextResponse } from "next/server";

// export async function GET(req: NextRequest) {
// const res = await splitWantToDo({
// title: "キーボードを買う"
// });

// return NextResponse.json(res);
// }
47 changes: 47 additions & 0 deletions task_yell/src/lib/ai.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import OpenAI from 'openai';
import { WantTodo } from './types';
import { z } from "zod";
import { zodResponseFormat } from "openai/helpers/zod";

const client = new OpenAI({
apiKey: process.env.OPENAI_API_KEY
});

const TasksScheme = z.object({
tasks: z.array(z.object({
content: z.string()
}))
});

const prompt = `
ユーザーがやりたいことを送信するのでそれを実現するためにタスクに分割してください。
出力は元の言語である日本語で出力してください。
`;

export async function splitWantToDo(wantTodo: WantTodo): Promise<WantTodo[] | null> {
try {
const result = await client.chat.completions.create({
model: 'gpt-4o',
messages: [
{ role: 'system', content: prompt },
{
role: 'user', content: `
{
"content": "${wantTodo.title}"
}
`
}
],
response_format: zodResponseFormat(TasksScheme, 'tasks')
});

console.log(result.choices[0].message.content);
const content = result.choices[0].message.content;
if (content) {
return JSON.parse(content).tasks.map((item: { content: string }) => ({ title: item.content }));
}
} catch (e: unknown) {
console.error(e);
}
return null;
}

0 comments on commit 05513d6

Please sign in to comment.