Skip to content

Commit 7cf55fe

Browse files
committed
チャットAPIをNext.jsに移行
1 parent 43d5020 commit 7cf55fe

File tree

2 files changed

+36
-50
lines changed

2 files changed

+36
-50
lines changed

app/chat/route.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// npm install @google/generative-ai
2+
import { NextResponse } from 'next/server';
3+
import { GoogleGenerativeAI } from '@google/generative-ai';
4+
5+
6+
const genAI = new GoogleGenerativeAI(process.env.API_KEY);
7+
8+
export async function POST(request) {
9+
const { message } = await request.json();
10+
11+
12+
if (!message) {
13+
return NextResponse.json(
14+
{ error: "メッセージがありません。" },
15+
{ status: 400 }
16+
);
17+
}
18+
19+
try {
20+
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" });
21+
22+
const result = await model.generateContent(message);
23+
const response = result.response;
24+
const text = response.text();
25+
26+
return NextResponse.json({ response: text });
27+
28+
} catch (e) {
29+
30+
console.error("Error:", e);
31+
return NextResponse.json(
32+
{ response: "エラーが発生しました。" },
33+
{ status: 500 }
34+
);
35+
}
36+
}

chat.py

Lines changed: 0 additions & 50 deletions
This file was deleted.

0 commit comments

Comments
 (0)