initial commit
Browse files- app/api/chat/route.ts +18 -0
- app/page.tsx +32 -60
- package-lock.json +11 -0
- package.json +1 -0
app/api/chat/route.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { GoogleGenerativeAI } from "@google/generative-ai";
|
| 2 |
+
|
| 3 |
+
export async function POST(req: Request) {
|
| 4 |
+
const { message } = await req.json();
|
| 5 |
+
|
| 6 |
+
const genAI = new GoogleGenerativeAI(
|
| 7 |
+
process.env.GEMINI_API_KEY!
|
| 8 |
+
);
|
| 9 |
+
|
| 10 |
+
const model = genAI.getGenerativeModel({
|
| 11 |
+
model: "gemini-1.5-flash"
|
| 12 |
+
});
|
| 13 |
+
|
| 14 |
+
const result = await model.generateContent(message);
|
| 15 |
+
const text = result.response.text();
|
| 16 |
+
|
| 17 |
+
return Response.json({ reply: text });
|
| 18 |
+
}
|
app/page.tsx
CHANGED
|
@@ -1,65 +1,37 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
|
| 3 |
export default function Home() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
return (
|
| 5 |
-
<
|
| 6 |
-
<
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
Looking for a starting point or more instructions? Head over to{" "}
|
| 21 |
-
<a
|
| 22 |
-
href="https://vercel.com/templates?framework=next.js&utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
|
| 23 |
-
className="font-medium text-zinc-950 dark:text-zinc-50"
|
| 24 |
-
>
|
| 25 |
-
Templates
|
| 26 |
-
</a>{" "}
|
| 27 |
-
or the{" "}
|
| 28 |
-
<a
|
| 29 |
-
href="https://nextjs.org/learn?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
|
| 30 |
-
className="font-medium text-zinc-950 dark:text-zinc-50"
|
| 31 |
-
>
|
| 32 |
-
Learning
|
| 33 |
-
</a>{" "}
|
| 34 |
-
center.
|
| 35 |
-
</p>
|
| 36 |
-
</div>
|
| 37 |
-
<div className="flex flex-col gap-4 text-base font-medium sm:flex-row">
|
| 38 |
-
<a
|
| 39 |
-
className="flex h-12 w-full items-center justify-center gap-2 rounded-full bg-foreground px-5 text-background transition-colors hover:bg-[#383838] dark:hover:bg-[#ccc] md:w-[158px]"
|
| 40 |
-
href="https://vercel.com/new?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
|
| 41 |
-
target="_blank"
|
| 42 |
-
rel="noopener noreferrer"
|
| 43 |
-
>
|
| 44 |
-
<Image
|
| 45 |
-
className="dark:invert"
|
| 46 |
-
src="/vercel.svg"
|
| 47 |
-
alt="Vercel logomark"
|
| 48 |
-
width={16}
|
| 49 |
-
height={16}
|
| 50 |
-
/>
|
| 51 |
-
Deploy Now
|
| 52 |
-
</a>
|
| 53 |
-
<a
|
| 54 |
-
className="flex h-12 w-full items-center justify-center rounded-full border border-solid border-black/[.08] px-5 transition-colors hover:border-transparent hover:bg-black/[.04] dark:border-white/[.145] dark:hover:bg-[#1a1a1a] md:w-[158px]"
|
| 55 |
-
href="https://nextjs.org/docs?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
|
| 56 |
-
target="_blank"
|
| 57 |
-
rel="noopener noreferrer"
|
| 58 |
-
>
|
| 59 |
-
Documentation
|
| 60 |
-
</a>
|
| 61 |
-
</div>
|
| 62 |
-
</main>
|
| 63 |
-
</div>
|
| 64 |
);
|
| 65 |
}
|
|
|
|
| 1 |
+
"use client";
|
| 2 |
+
import { useState } from "react";
|
| 3 |
|
| 4 |
export default function Home() {
|
| 5 |
+
const [input, setInput] = useState("");
|
| 6 |
+
const [messages, setMessages] = useState<string[]>([]);
|
| 7 |
+
|
| 8 |
+
const sendMessage = async () => {
|
| 9 |
+
const res = await fetch("/api/chat", {
|
| 10 |
+
method: "POST",
|
| 11 |
+
headers: { "Content-Type": "application/json" },
|
| 12 |
+
body: JSON.stringify({ message: input }),
|
| 13 |
+
});
|
| 14 |
+
|
| 15 |
+
const data = await res.json();
|
| 16 |
+
setMessages([...messages, "You: " + input, "AI: " + data.reply]);
|
| 17 |
+
setInput("");
|
| 18 |
+
};
|
| 19 |
+
|
| 20 |
return (
|
| 21 |
+
<main style={{ padding: 20 }}>
|
| 22 |
+
<h1>Gemini Chatbot</h1>
|
| 23 |
+
|
| 24 |
+
<div>
|
| 25 |
+
{messages.map((m, i) => (
|
| 26 |
+
<p key={i}>{m}</p>
|
| 27 |
+
))}
|
| 28 |
+
</div>
|
| 29 |
+
|
| 30 |
+
<input
|
| 31 |
+
value={input}
|
| 32 |
+
onChange={(e) => setInput(e.target.value)}
|
| 33 |
+
/>
|
| 34 |
+
<button onClick={sendMessage}>送信</button>
|
| 35 |
+
</main>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
);
|
| 37 |
}
|
package-lock.json
CHANGED
|
@@ -8,6 +8,7 @@
|
|
| 8 |
"name": "my-gemini-chatbot",
|
| 9 |
"version": "0.1.0",
|
| 10 |
"dependencies": {
|
|
|
|
| 11 |
"next": "16.1.6",
|
| 12 |
"react": "19.2.3",
|
| 13 |
"react-dom": "19.2.3"
|
|
@@ -454,6 +455,15 @@
|
|
| 454 |
"node": "^18.18.0 || ^20.9.0 || >=21.1.0"
|
| 455 |
}
|
| 456 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 457 |
"node_modules/@humanfs/core": {
|
| 458 |
"version": "0.19.1",
|
| 459 |
"resolved": "https://registry.npmjs.org/@humanfs/core/-/core-0.19.1.tgz",
|
|
@@ -3217,6 +3227,7 @@
|
|
| 3217 |
"integrity": "sha512-whOE1HFo/qJDyX4SnXzP4N6zOWn79WhnCUY/iDR0mPfQZO8wcYE4JClzI2oZrhBnnMUCBCHZhO6VQyoBU95mZA==",
|
| 3218 |
"dev": true,
|
| 3219 |
"license": "MIT",
|
|
|
|
| 3220 |
"dependencies": {
|
| 3221 |
"@rtsao/scc": "^1.1.0",
|
| 3222 |
"array-includes": "^3.1.9",
|
|
|
|
| 8 |
"name": "my-gemini-chatbot",
|
| 9 |
"version": "0.1.0",
|
| 10 |
"dependencies": {
|
| 11 |
+
"@google/generative-ai": "^0.24.1",
|
| 12 |
"next": "16.1.6",
|
| 13 |
"react": "19.2.3",
|
| 14 |
"react-dom": "19.2.3"
|
|
|
|
| 455 |
"node": "^18.18.0 || ^20.9.0 || >=21.1.0"
|
| 456 |
}
|
| 457 |
},
|
| 458 |
+
"node_modules/@google/generative-ai": {
|
| 459 |
+
"version": "0.24.1",
|
| 460 |
+
"resolved": "https://registry.npmjs.org/@google/generative-ai/-/generative-ai-0.24.1.tgz",
|
| 461 |
+
"integrity": "sha512-MqO+MLfM6kjxcKoy0p1wRzG3b4ZZXtPI+z2IE26UogS2Cm/XHO+7gGRBh6gcJsOiIVoH93UwKvW4HdgiOZCy9Q==",
|
| 462 |
+
"license": "Apache-2.0",
|
| 463 |
+
"engines": {
|
| 464 |
+
"node": ">=18.0.0"
|
| 465 |
+
}
|
| 466 |
+
},
|
| 467 |
"node_modules/@humanfs/core": {
|
| 468 |
"version": "0.19.1",
|
| 469 |
"resolved": "https://registry.npmjs.org/@humanfs/core/-/core-0.19.1.tgz",
|
|
|
|
| 3227 |
"integrity": "sha512-whOE1HFo/qJDyX4SnXzP4N6zOWn79WhnCUY/iDR0mPfQZO8wcYE4JClzI2oZrhBnnMUCBCHZhO6VQyoBU95mZA==",
|
| 3228 |
"dev": true,
|
| 3229 |
"license": "MIT",
|
| 3230 |
+
"peer": true,
|
| 3231 |
"dependencies": {
|
| 3232 |
"@rtsao/scc": "^1.1.0",
|
| 3233 |
"array-includes": "^3.1.9",
|
package.json
CHANGED
|
@@ -9,6 +9,7 @@
|
|
| 9 |
"lint": "eslint"
|
| 10 |
},
|
| 11 |
"dependencies": {
|
|
|
|
| 12 |
"next": "16.1.6",
|
| 13 |
"react": "19.2.3",
|
| 14 |
"react-dom": "19.2.3"
|
|
|
|
| 9 |
"lint": "eslint"
|
| 10 |
},
|
| 11 |
"dependencies": {
|
| 12 |
+
"@google/generative-ai": "^0.24.1",
|
| 13 |
"next": "16.1.6",
|
| 14 |
"react": "19.2.3",
|
| 15 |
"react-dom": "19.2.3"
|