Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wanto-to-doのAI分割機能 #74

Merged
merged 3 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
54 changes: 54 additions & 0 deletions task_yell/src/lib/ai.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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;
}
Loading