-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from SystemEngineeringTeam/issue/57-feat-title…
…-from-ai feat: AIでタイトルと説明を作る機能
- Loading branch information
Showing
4 changed files
with
82 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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({ | ||
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({ | ||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters