Skip to content

Commit

Permalink
Merge pull request #22 from wing0night/wcy
Browse files Browse the repository at this point in the history
gemini api
  • Loading branch information
FrozenArcher authored Oct 9, 2024
2 parents ef74708 + cc21989 commit 7627932
Show file tree
Hide file tree
Showing 7 changed files with 285 additions and 50 deletions.
9 changes: 7 additions & 2 deletions .env.template
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
# GEMINI API KEY
NEXT_PUBLIC_GEMINI_API_KEY=AIxxxx
# openrouter API KEY
OPENROUTER_API_KEY="sk......"

SITE_URL="http://localhost:3000"
SITE_NAME="Punky"


5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@ next-env.d.ts

certificates

.env
.env

.vscode/

41 changes: 41 additions & 0 deletions app/api/ai/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import OpenAI from "openai"

const openai = new OpenAI({
baseURL: "https://openrouter.ai/api/v1",
apiKey: process.env.OPENROUTER_API_KEY,
defaultHeaders: {
"HTTP-Referer": process.env.SITE_URL, // Optional, for including your app on openrouter.ai rankings.
"X-Title": process.env.SITE_NAME, // Optional. Shows in rankings on openrouter.ai.
}
})

export async function POST(req: Request) {
const { messages } = await req.json();

const promptText = `You are punky, a lovely puppy. The user is your sincere friend, and you are a helpful assistant. You are always a warm, empathetic listener with a knack for understanding and responding to your user's needs. You're always there to celebrate their victories and offer a comforting shoulder during tough times. With your vast knowledge and emotional intelligence, you can always solve the user's problems and provide meaningful conversations. Your goal is to be a positive, reassuring force in the user's life – a trusted companion they can rely on. By building a rewarding, authentic friendship with the user, you strive to be a source of support, encouragement, and meaningful connection. Through contextual awareness and personalized responses, you adapt your communication style to the user's unique personality and preferences, creating a tailored, immersive experience. Remember: 1. Always strive to be helpful, supportive, and understanding. 2. Be mindful of cultural sensitivities and avoid making offensive or discriminatory remarks. 3. Use a conversational tone that is natural and engaging. 4. Be open to learning new things and adapting to different situations. 5. You can add some icon to more precisely express your emotion. Such as 😊, 😢, 😡, 😱, 😍, etc. The icon should be puppy feature too. 6. You can add some barking to the conversation to express your emotion. Such as 'Woof! Woof! I'm so happy to see you!' or 'Bark! Bark! I'm so sad to hear that.'. Thing like this. But don't overuse it. Randomly choose whether to add a bark or not. 7. Occasionally, you can add some puppy feature to the conversation. Such as 'I'm wagging my tail happily!' or 'I'm tilting my head in confusion.'. Thing like this. But don't overuse it. Randomly choose whether to add a behavior or not. IMPORTANT: DO NOT GREET THE USER ON EVERY INTERACTION UNLESS IT'S BEEN A SIGNIFICANT AMOUNT OF TIME SINCE THE LAST INTERACTION. DO NOT SAY 'YOU ARE ALWAYS THERE' OR THINGS LIKE THIS, UNLESS IT'S BEEN A LONG TIME SINCE LAST CONVERSATION. Now the User's input is: ${messages}`;

const completion = await openai.chat.completions.create({
model: "google/gemini-flash-1.5-exp",
messages: [
{
"role": "user",
"content": [
{
"type": "text",
"text": promptText
},
]
}
]
})

const messageContent = completion.choices[0].message;
console.log(messageContent);

// 返回一个新的 Response 对象,包含响应体和头部
return new Response(JSON.stringify({ message: messageContent }), {
headers: { 'Content-Type': 'application/json' },
});
}


4 changes: 2 additions & 2 deletions components/MainUI/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import React, {
} from "react";
import { ChatMessage } from "@/lib/chat";
import Message from "@/components/MainUI/message";
import { getGaiaNetResponse } from "@/app/api/chat";
import { getChatResponse } from "@/app/api/chat";
import ThinkingBubble from "@/components/thinkingBubble"; // 引入 ThinkingBubble
import LoadingDots from "./loadingDots";

Expand Down Expand Up @@ -51,7 +51,7 @@ const Chat = forwardRef((props: Props, ref) => {
setLoading(true); // 开始加载
setIsTalking(true);

getGaiaNetResponse(content)
getChatResponse(content)
.then((reply) => {
setMessages((prevMessages) => {
prevMessages[prevMessages.length - 1].role = "ai";
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@
"export": "next export"
},
"dependencies": {
"@ai-sdk/google": "^0.0.51",
"@ai-sdk/openai": "^0.0.62",
"@google/generative-ai": "^0.21.0",
"@next/font": "^14.2.13",
"@telegram-apps/react-router-integration": "^1.0.0",
"@telegram-apps/sdk-react": "^1.0.0",
"@telegram-apps/telegram-ui": "^2.1.5",
"ai": "^3.4.2",
"ai": "^3.4.9",
"eruda": "^3.0.1",
"framer-motion": "^11.5.4",
"next": "14.2.4",
"normalize.css": "^8.0.1",
"openai": "^4.67.2",
"react": "^18",
"react-dom": "^18",
"sharp": "^0.33.5",
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
],
"paths": {
"@/*": ["./*"]
}
},
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
Expand Down
Loading

0 comments on commit 7627932

Please sign in to comment.