Skip to content

Commit

Permalink
Merge pull request #74 from jphacks/feat/openai
Browse files Browse the repository at this point in the history
wanto-to-doのAI分割機能
  • Loading branch information
Inlet-back authored Oct 27, 2024
2 parents d91bc07 + 6dd9555 commit 92e761b
Show file tree
Hide file tree
Showing 3 changed files with 203 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
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;
}

0 comments on commit 92e761b

Please sign in to comment.