Skip to content

Commit 021ebf9

Browse files
committed
geminiへのリクエストに失敗した場合proxyを使う
1 parent 5ad930f commit 021ebf9

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

app/actions/chatActions.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use server';
22

3-
import { GoogleGenerativeAI } from "@google/generative-ai";
43
import { z } from "zod";
4+
import { generateContent } from "./gemini";
55

66
interface FormState {
77
response: string;
@@ -13,8 +13,6 @@ const ChatSchema = z.object({
1313
documentContent: z.string().min(1, { message: "コンテキストとなるドキュメントがありません。"}),
1414
});
1515

16-
const genAI = new GoogleGenerativeAI(process.env.API_KEY!);
17-
1816
type ChatParams = z.input<typeof ChatSchema>;
1917

2018
export async function askAI(params: ChatParams): Promise<FormState> {
@@ -30,7 +28,7 @@ export async function askAI(params: ChatParams): Promise<FormState> {
3028
const { userQuestion, documentContent } = parseResult.data;
3129

3230
try {
33-
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" });
31+
3432
const prompt = `
3533
以下のPythonチュートリアルのドキュメントの内容を正確に理解し、ユーザーからの質問に対して、初心者にも分かりやすく、丁寧な解説を提供してください。
3634
@@ -48,7 +46,7 @@ ${userQuestion}
4846
-
4947
5048
`;
51-
const result = await model.generateContent(prompt);
49+
const result = await generateContent(prompt);
5250
const response = result.response;
5351
const text = response.text();
5452
return { response: text, error: null };

app/actions/gemini.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"use server";
2+
3+
import { GoogleGenerativeAI, ModelParams } from "@google/generative-ai";
4+
5+
export async function generateContent(prompt: string) {
6+
const params: ModelParams = {
7+
model: "gemini-1.5-flash",
8+
};
9+
10+
const genAI = new GoogleGenerativeAI(process.env.API_KEY!);
11+
try {
12+
const model = genAI.getGenerativeModel(params);
13+
return await model.generateContent(prompt);
14+
} catch (e: unknown) {
15+
if (String(e).includes("User location is not supported")) {
16+
const model = genAI.getGenerativeModel(params, {
17+
baseUrl: "https://gemini-proxy.natrium144.org",
18+
customHeaders: {
19+
"x-goog-api-key": process.env.API_KEY!,
20+
},
21+
});
22+
return await model.generateContent(prompt);
23+
} else {
24+
throw e;
25+
}
26+
}
27+
}

app/actions/questionExample.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
"use server";
22

3-
import { GoogleGenerativeAI } from "@google/generative-ai";
43
import { z } from "zod";
4+
import { generateContent } from "./gemini";
55

66
const QuestionExampleSchema = z.object({
77
lang: z.string().min(1),
88
documentContent: z.string().min(1),
99
});
1010

11-
const genAI = new GoogleGenerativeAI(process.env.API_KEY!);
12-
1311
type QuestionExampleParams = z.input<typeof QuestionExampleSchema>;
1412

1513
export async function getQuestionExample(
@@ -28,15 +26,14 @@ export async function getQuestionExample(
2826

2927
const { lang, documentContent } = parseResult.data;
3028

31-
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" });
3229
const prompt = `
3330
以下の${lang}チュートリアルのドキュメントに対して、想定される初心者のユーザーからの質問の例を箇条書きで複数挙げてください。
3431
強調などはせずテキストだけで1行ごとに1つ出力してください。
3532
3633
# ドキュメント
3734
${documentContent}
3835
`;
39-
const result = await model.generateContent(prompt);
36+
const result = await generateContent(prompt);
4037
const response = result.response;
4138
const text = response.text();
4239
return text.trim().split("\n");

0 commit comments

Comments
 (0)