Skip to content

Commit

Permalink
Merge pull request #76 from SystemEngineeringTeam/issue/57-feat-title…
Browse files Browse the repository at this point in the history
…-from-ai

feat: AIでタイトルと説明を作る機能
  • Loading branch information
SatooRu65536 authored Nov 12, 2024
2 parents 7ec29d3 + 757cba8 commit 3c25358
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
"preseed": "yarn supabase db reset"
},
"dependencies": {
"@google/generative-ai": "^0.21.0",
"@supabase/ssr": "0.4.0",
"@supabase/supabase-js": "2.45.1",
"jotai": "2.9.2",
"next": "14.2.4",
"react": "^18",
"react-dom": "^18",
"react-syntax-highlighter": "15.6.1"
"react-syntax-highlighter": "15.6.1",
"zod": "^3.23.8"
},
"devDependencies": {
"@types/node": "^20",
Expand Down
8 changes: 8 additions & 0 deletions src/shema/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import z from 'zod';

export const zAIResponse = z.object({
title: z.string(),
description: z.string(),
});

export type AIResponse = z.infer<typeof zAIResponse>;
61 changes: 61 additions & 0 deletions src/utils/ai.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { GoogleGenerativeAI, type GenerativeModel } from '@google/generative-ai';
import { type AIResponse, zAIResponse } from '@/shema';

const apiKey = process.env.NEXT_PUBLIC_GEMINI_API_KEY ?? '';
const ai = new GoogleGenerativeAI(apiKey);

export async function askAIResponse(sourceCode: string, lang?: string): Promise<AIResponse | undefined> {
const model: GenerativeModel = await ai.getGenerativeModel({

Check failure on line 8 in src/utils/ai.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/utils/ai.ts#L8

Unexpected `await` of a non-Promise (non-"Thenable") value (@typescript-eslint/await-thenable)
model: 'gemini-1.5-flash',
generationConfig: {
responseMimeType: 'application/json',
},
});

const prompt = `
あなたはユーモアのある熟練のITエンジニアです。
あなたは以下のよくないとされるコードを受け取ります。
\`\`\`${lang}
${sourceCode}
\`\`\`
受け取ったコードについて説明を行います。
ユーモアのあるソースコードのタイトルを30文字以内で考えてください。
その後ソースコードのダメな点を指摘してください。
その後、どのようなソースコードを記述するべきなのか説明してください。
では, 次の形式で返答してください:
\`\`\`jsonschema
{
"titile: "説明のタイトル",
"description": "説明の内容"
}
\`\`\`
`;

const response = await model.generateContent(prompt);
const text: string = response.response.text();
const res = zAIResponse.safeParse(JSON.parse(text));
if (res.success) {
return res.data;
}
return undefined;
}

export async function AskAnyAIResponse(prompt: string): Promise<AIResponse | undefined> {
const model: GenerativeModel = await ai.getGenerativeModel({

Check failure on line 47 in src/utils/ai.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/utils/ai.ts#L47

Unexpected `await` of a non-Promise (non-"Thenable") value (@typescript-eslint/await-thenable)
model: 'gemini-1.5-flash',
generationConfig: {
responseMimeType: 'application/json',
},
});

const response = await model.generateContent(prompt);
const text: string = response.response.text();
const res = zAIResponse.safeParse(JSON.parse(text));
if (res.success) {
return res.data;
}
return undefined;
}
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
resolved "https://registry.yarnpkg.com/@eslint/js/-/js-8.57.1.tgz#de633db3ec2ef6a3c89e2f19038063e8a122e2c2"
integrity sha512-d9zaMRSTIKDLhctzH12MtXvJKSSUhaHcjV+2Z+GK+EEY7XKpP5yR4x+N3TAcHTcu963nIr+TMcCb4DBCYX1z6Q==

"@google/generative-ai@^0.21.0":
version "0.21.0"
resolved "https://registry.yarnpkg.com/@google/generative-ai/-/generative-ai-0.21.0.tgz#a5011aab9e6082e706937b26ef23445933fa0d15"
integrity sha512-7XhUbtnlkSEZK15kN3t+tzIMxsbKm/dSkKBFalj+20NvPKe1kBY7mR2P7vuijEn+f06z5+A8bVGKO0v39cr6Wg==

"@humanwhocodes/config-array@^0.13.0":
version "0.13.0"
resolved "https://registry.yarnpkg.com/@humanwhocodes/config-array/-/config-array-0.13.0.tgz#fb907624df3256d04b9aa2df50d7aa97ec648748"
Expand Down Expand Up @@ -3444,3 +3449,8 @@ yocto-queue@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-0.1.0.tgz#0294eb3dee05028d31ee1a5fa2c556a6aaf10a1b"
integrity sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==

zod@^3.23.8:
version "3.23.8"
resolved "https://registry.yarnpkg.com/zod/-/zod-3.23.8.tgz#e37b957b5d52079769fb8097099b592f0ef4067d"
integrity sha512-XBx9AXhXktjUqnepgTiE5flcKIYWi/rme0Eaj+5Y0lftuGBq+jyRu/md4WnuxqgP1ubdpNCsYEYPxrzVHD8d6g==

0 comments on commit 3c25358

Please sign in to comment.