Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions app/actions/chatActions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use server';

import { GoogleGenerativeAI } from "@google/generative-ai";
import { z } from "zod";
import { generateContent } from "./gemini";

interface FormState {
response: string;
Expand All @@ -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<typeof ChatSchema>;

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

try {
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" });

const prompt = `
以下のPythonチュートリアルのドキュメントの内容を正確に理解し、ユーザーからの質問に対して、初心者にも分かりやすく、丁寧な解説を提供してください。

Expand All @@ -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 };
Expand Down
24 changes: 24 additions & 0 deletions app/actions/gemini.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
7 changes: 2 additions & 5 deletions app/actions/questionExample.ts
Original file line number Diff line number Diff line change
@@ -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<typeof QuestionExampleSchema>;

export async function getQuestionExample(
Expand All @@ -28,15 +26,14 @@ export async function getQuestionExample(

const { lang, documentContent } = parseResult.data;

const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" });
const prompt = `
以下の${lang}チュートリアルのドキュメントに対して、想定される初心者のユーザーからの質問の例を箇条書きで複数挙げてください。
強調などはせずテキストだけで1行ごとに1つ出力してください。

# ドキュメント
${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");
Expand Down