diff --git a/app/actions/chatActions.ts b/app/actions/chatActions.ts index 1fc2e6f..bd2c14b 100644 --- a/app/actions/chatActions.ts +++ b/app/actions/chatActions.ts @@ -1,7 +1,7 @@ 'use server'; -import { GoogleGenerativeAI } from "@google/generative-ai"; import { z } from "zod"; +import { generateContent } from "./gemini"; interface FormState { response: string; @@ -13,8 +13,6 @@ const ChatSchema = z.object({ documentContent: z.string().min(1, { message: "コンテキストとなるドキュメントがありません。"}), }); -const genAI = new GoogleGenerativeAI(process.env.API_KEY!); - type ChatParams = z.input; export async function askAI(params: ChatParams): Promise { @@ -30,7 +28,7 @@ export async function askAI(params: ChatParams): Promise { const { userQuestion, documentContent } = parseResult.data; try { - const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" }); + const prompt = ` 以下のPythonチュートリアルのドキュメントの内容を正確に理解し、ユーザーからの質問に対して、初心者にも分かりやすく、丁寧な解説を提供してください。 @@ -48,7 +46,7 @@ ${userQuestion} - `; - const result = await model.generateContent(prompt); + const result = await generateContent(prompt); const response = result.response; const text = response.text(); return { response: text, error: null }; diff --git a/app/actions/gemini.ts b/app/actions/gemini.ts new file mode 100644 index 0000000..3af3a16 --- /dev/null +++ b/app/actions/gemini.ts @@ -0,0 +1,24 @@ +"use server"; + +import { GoogleGenerativeAI, ModelParams } from "@google/generative-ai"; + +export async function generateContent(prompt: string) { + const params: ModelParams = { + model: "gemini-1.5-flash", + }; + + const genAI = new GoogleGenerativeAI(process.env.API_KEY!); + try { + const model = genAI.getGenerativeModel(params); + return await model.generateContent(prompt); + } catch (e: unknown) { + if (String(e).includes("User location is not supported")) { + const model = genAI.getGenerativeModel(params, { + baseUrl: "https://gemini-proxy.utcode.net", + }); + return await model.generateContent(prompt); + } else { + throw e; + } + } +} diff --git a/app/actions/questionExample.ts b/app/actions/questionExample.ts index 846a13a..6b0bf87 100644 --- a/app/actions/questionExample.ts +++ b/app/actions/questionExample.ts @@ -1,15 +1,13 @@ "use server"; -import { GoogleGenerativeAI } from "@google/generative-ai"; import { z } from "zod"; +import { generateContent } from "./gemini"; const QuestionExampleSchema = z.object({ lang: z.string().min(1), documentContent: z.string().min(1), }); -const genAI = new GoogleGenerativeAI(process.env.API_KEY!); - type QuestionExampleParams = z.input; export async function getQuestionExample( @@ -28,7 +26,6 @@ export async function getQuestionExample( const { lang, documentContent } = parseResult.data; - const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" }); const prompt = ` 以下の${lang}チュートリアルのドキュメントに対して、想定される初心者のユーザーからの質問の例を箇条書きで複数挙げてください。 強調などはせずテキストだけで1行ごとに1つ出力してください。 @@ -36,7 +33,7 @@ export async function getQuestionExample( # ドキュメント ${documentContent} `; - const result = await model.generateContent(prompt); + const result = await generateContent(prompt); const response = result.response; const text = response.text(); return text.trim().split("\n");