File tree Expand file tree Collapse file tree 3 files changed +32
-10
lines changed
Expand file tree Collapse file tree 3 files changed +32
-10
lines changed Original file line number Diff line number Diff line change 11'use server' ;
22
3- import { GoogleGenerativeAI } from "@google/generative-ai" ;
43import { z } from "zod" ;
4+ import { generateContent } from "./gemini" ;
55
66interface 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-
1816type ChatParams = z . input < typeof ChatSchema > ;
1917
2018export 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 } ;
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 11"use server" ;
22
3- import { GoogleGenerativeAI } from "@google/generative-ai" ;
43import { z } from "zod" ;
4+ import { generateContent } from "./gemini" ;
55
66const 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-
1311type QuestionExampleParams = z . input < typeof QuestionExampleSchema > ;
1412
1513export 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" ) ;
You can’t perform that action at this time.
0 commit comments