-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from wing0night/wcy
gemini api
- Loading branch information
Showing
7 changed files
with
285 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,4 +39,7 @@ next-env.d.ts | |
|
||
certificates | ||
|
||
.env | ||
.env | ||
|
||
.vscode/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }, | ||
}); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.