Skip to content

Commit

Permalink
fix: refactor & temporarily use gaianet
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenArcher committed Oct 10, 2024
1 parent 1401ee4 commit e41cbff
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 29 deletions.
40 changes: 14 additions & 26 deletions app/api/ai/route.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,28 @@
import OpenAI from "openai";
import { getPrompt } from '@/lib/chat';
import { createOpenAI } from '@ai-sdk/openai';
import { generateText } from 'ai';

const openai = new OpenAI({
baseURL: "https://openrouter.ai/api/v1",
const baseURL = "https://openrouter.ai/api/v1";

const openai = createOpenAI({
baseURL,
apiKey: process.env.NEXT_PUBLIC_OPENROUTER_API_KEY,
defaultHeaders: {
"HTTP-Referer": "https://punky.app",
"X-Title": "Punky Telegram Mini App",
},
});
compatibility: "compatible",
})

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

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. 8. Try to limit the reply to within 40 words, unless it's need to generate context exceed 40. 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: ${message}`;
const promptText = getPrompt(message);

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

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

// 返回一个新的 Response 对象,包含响应体和头部
return new Response(JSON.stringify({ message: messageContent.content }), {
return new Response(JSON.stringify({ message: text }), {
headers: { "Content-Type": "application/json" },
});
}
7 changes: 6 additions & 1 deletion app/api/chat.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getPrompt } from '@/lib/chat';
import { createOpenAI } from "@ai-sdk/openai";
import { generateText } from "ai";

Expand All @@ -11,7 +12,7 @@ const openai = createOpenAI({
});

export async function getGaiaNetResponse(message: string) {
const prompt = `Your name is punky, you are a faithful pet friend of the user, and you are a helpful assistant. Now the user says: "${message}"; Please give your response.`;
const prompt = getPrompt(message);
const { text } = await generateText({
model: openai("llama"),
prompt: prompt,
Expand All @@ -33,3 +34,7 @@ export async function getChatResponse(message: string) {
const data = await res.json();
return data.message as string;
}

export async function getResponse(message: string) {
return getGaiaNetResponse(message);
}
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 { getChatResponse } from "@/app/api/chat";
import { getChatResponse, getResponse } 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);

getChatResponse(content)
getResponse(content)
.then((reply) => {
setMessages((prevMessages) => {
prevMessages[prevMessages.length - 1].role = "ai";
Expand Down
4 changes: 4 additions & 0 deletions lib/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ export interface ChatMessage {
role: MessageRole;
content: any;
type?: string
}

export function getPrompt(message: string) {
return `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. 8. Try to limit the reply to within 40 words, unless it's need to generate context exceed 40. 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: ${message}`;
}

0 comments on commit e41cbff

Please sign in to comment.