Skip to content

Commit 3c25358

Browse files
authored
Merge pull request #76 from SystemEngineeringTeam/issue/57-feat-title-from-ai
feat: AIでタイトルと説明を作る機能
2 parents 7ec29d3 + 757cba8 commit 3c25358

File tree

4 files changed

+82
-1
lines changed

4 files changed

+82
-1
lines changed

Diff for: package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212
"preseed": "yarn supabase db reset"
1313
},
1414
"dependencies": {
15+
"@google/generative-ai": "^0.21.0",
1516
"@supabase/ssr": "0.4.0",
1617
"@supabase/supabase-js": "2.45.1",
1718
"jotai": "2.9.2",
1819
"next": "14.2.4",
1920
"react": "^18",
2021
"react-dom": "^18",
21-
"react-syntax-highlighter": "15.6.1"
22+
"react-syntax-highlighter": "15.6.1",
23+
"zod": "^3.23.8"
2224
},
2325
"devDependencies": {
2426
"@types/node": "^20",

Diff for: src/shema/index.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import z from 'zod';
2+
3+
export const zAIResponse = z.object({
4+
title: z.string(),
5+
description: z.string(),
6+
});
7+
8+
export type AIResponse = z.infer<typeof zAIResponse>;

Diff for: src/utils/ai.ts

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { GoogleGenerativeAI, type GenerativeModel } from '@google/generative-ai';
2+
import { type AIResponse, zAIResponse } from '@/shema';
3+
4+
const apiKey = process.env.NEXT_PUBLIC_GEMINI_API_KEY ?? '';
5+
const ai = new GoogleGenerativeAI(apiKey);
6+
7+
export async function askAIResponse(sourceCode: string, lang?: string): Promise<AIResponse | undefined> {
8+
const model: GenerativeModel = await ai.getGenerativeModel({
9+
model: 'gemini-1.5-flash',
10+
generationConfig: {
11+
responseMimeType: 'application/json',
12+
},
13+
});
14+
15+
const prompt = `
16+
あなたはユーモアのある熟練のITエンジニアです。
17+
あなたは以下のよくないとされるコードを受け取ります。
18+
\`\`\`${lang}
19+
${sourceCode}
20+
\`\`\`
21+
22+
受け取ったコードについて説明を行います。
23+
ユーモアのあるソースコードのタイトルを30文字以内で考えてください。
24+
その後ソースコードのダメな点を指摘してください。
25+
26+
その後、どのようなソースコードを記述するべきなのか説明してください。
27+
28+
では, 次の形式で返答してください:
29+
\`\`\`jsonschema
30+
{
31+
"titile: "説明のタイトル",
32+
"description": "説明の内容"
33+
}
34+
\`\`\`
35+
`;
36+
37+
const response = await model.generateContent(prompt);
38+
const text: string = response.response.text();
39+
const res = zAIResponse.safeParse(JSON.parse(text));
40+
if (res.success) {
41+
return res.data;
42+
}
43+
return undefined;
44+
}
45+
46+
export async function AskAnyAIResponse(prompt: string): Promise<AIResponse | undefined> {
47+
const model: GenerativeModel = await ai.getGenerativeModel({
48+
model: 'gemini-1.5-flash',
49+
generationConfig: {
50+
responseMimeType: 'application/json',
51+
},
52+
});
53+
54+
const response = await model.generateContent(prompt);
55+
const text: string = response.response.text();
56+
const res = zAIResponse.safeParse(JSON.parse(text));
57+
if (res.success) {
58+
return res.data;
59+
}
60+
return undefined;
61+
}

Diff for: yarn.lock

+10
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@
4141
resolved "https://registry.yarnpkg.com/@eslint/js/-/js-8.57.1.tgz#de633db3ec2ef6a3c89e2f19038063e8a122e2c2"
4242
integrity sha512-d9zaMRSTIKDLhctzH12MtXvJKSSUhaHcjV+2Z+GK+EEY7XKpP5yR4x+N3TAcHTcu963nIr+TMcCb4DBCYX1z6Q==
4343

44+
"@google/generative-ai@^0.21.0":
45+
version "0.21.0"
46+
resolved "https://registry.yarnpkg.com/@google/generative-ai/-/generative-ai-0.21.0.tgz#a5011aab9e6082e706937b26ef23445933fa0d15"
47+
integrity sha512-7XhUbtnlkSEZK15kN3t+tzIMxsbKm/dSkKBFalj+20NvPKe1kBY7mR2P7vuijEn+f06z5+A8bVGKO0v39cr6Wg==
48+
4449
"@humanwhocodes/config-array@^0.13.0":
4550
version "0.13.0"
4651
resolved "https://registry.yarnpkg.com/@humanwhocodes/config-array/-/config-array-0.13.0.tgz#fb907624df3256d04b9aa2df50d7aa97ec648748"
@@ -3444,3 +3449,8 @@ yocto-queue@^0.1.0:
34443449
version "0.1.0"
34453450
resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-0.1.0.tgz#0294eb3dee05028d31ee1a5fa2c556a6aaf10a1b"
34463451
integrity sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==
3452+
3453+
zod@^3.23.8:
3454+
version "3.23.8"
3455+
resolved "https://registry.yarnpkg.com/zod/-/zod-3.23.8.tgz#e37b957b5d52079769fb8097099b592f0ef4067d"
3456+
integrity sha512-XBx9AXhXktjUqnepgTiE5flcKIYWi/rme0Eaj+5Y0lftuGBq+jyRu/md4WnuxqgP1ubdpNCsYEYPxrzVHD8d6g==

0 commit comments

Comments
 (0)