diff --git a/README.md b/README.md index 042aac4..7204b3d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -# Personal AI +# Siri Ultra -This is a personal AI assistant that works with Apple Shortcuts removing the need for a dedicated hardware device. +This is a Siri Ultra that works with Apple Shortcuts removing the need for a dedicated hardware device. ## How it works @@ -27,18 +27,27 @@ The assistant is run on Cloudflare Workers and can work with any LLM model. The - Update `wrangler.toml` with the namespace IDs: ```toml - kv_namespaces = [ - { binding = "personal_ai_chats", id = "", preview_id = ""} - ] + [[kv_namespaces]] + binding = "personal_ai_chats" + id = "" + preview_id = "" ``` 6. **Set up API keys**: -- Run `npx wrangler secret put GROQ_API_KEY` to set the GROQ API key. +- Run `npx wrangler secret put API_KEY` to set the GROQ or OpenAI API key. - Run `npx wrangler secret put OPENWEATHERMAP_API_KEY` to set the OpenWeather API key. - Run `npx wrangler secret put SEARCH1API_KEY` to set the SEARCH1API_KEY API key. - > **Note**: You can get these keys by signing up on [GroqCloud](https://console.groq.com/login) and [OpenWeather](https://home.openweathermap.org/users/sign_up) and and [Search1API](https://www.search1api.com/) respectively. + > **Note**: You can get these keys by signing up on [GroqCloud](https://console.groq.com/login) or [OpenAI](https://openai.com/) and [OpenWeather](https://home.openweathermap.org/users/sign_up) and [Search1API](https://www.search1api.com/) respectively. + +7. **Update the LLMs Vars**: + ```toml + API_BASE= "https://api.groq.com/openai/v1/" + MODEL="llama3-70b-8192" + SYSTEM_PROMPT="You are Siri Pro. Answer in 1-2 sentences. Be friendly, helpful and concise. Default to metric units when possible. Keep the conversation short and sweet. You only answer in text. Don't include links or any other extras. Don't respond with computer code, for example don't return user longitude." + ``` + ### Deploying the Worker diff --git a/src/chat.ts b/src/chat.ts index 7aed855..50dd3cb 100644 --- a/src/chat.ts +++ b/src/chat.ts @@ -18,33 +18,34 @@ export interface IRequest { } export const getClient = (req: IRequest): { client: OpenAI; model: string } => { - const url = "https://api.groq.com/openai/v1/"; - + const url = req.env.API_BASE || "https://api.groq.com/openai/v1/"; const client = new OpenAI({ - apiKey: req.env.GROQ_API_KEY, + apiKey: req.env.API_KEY, }); - client.baseURL = url; - - return { client, model: "llama3-70b-8192" }; + const model = req.env.MODEL || "llama3-70b-8192"; + return { client, model }; }; export const handle = async (req: IRequest): Promise => { const openai = getClient(req); + const defaultSystemPrompt = ` + You are Siri Pro. Answer in 1-2 sentences. Be friendly, helpful and concise. + Default to metric units when possible. Keep the conversation short and sweet. + You only answer in text. Don't include links or any other extras. + Don't respond with computer code, for example don't return user longitude. + `; - const system = ` - You are Siri Pro. Answer in 1-2 sentences. Be friendly, helpful and concise. - Default to metric units when possible. Keep the conversation short and sweet. - You only answer in text. Don't include links or any other extras. - Don't respond with computer code, for example don't return user longitude. - + const customSystemPrompt = req.env.SYSTEM_PROMPT || defaultSystemPrompt; - User's current info: - date: ${req.request.date} - lat:${req.request.location.latitude}, lon:${req.request.location.longitude} + const system = ` + ${customSystemPrompt} + User's current info: + date: ${req.request.date} + lat:${req.request.location.latitude}, lon:${req.request.location.longitude} `; - console.log("system", system); + const chat = ChatHistory.getInstance(req.env.personal_ai_chats); await chat.add(req.request.chat_id, { role: "user", @@ -61,22 +62,20 @@ export const handle = async (req: IRequest): Promise => { ], tools: FunctionHandler.functions, }); - console.log("ask", JSON.stringify(ask, null, 2)); + if (ask.choices[0].message.tool_calls) { chat.add(req.request.chat_id, { role: "assistant", name: "tool", tool_calls: ask.choices[0].message.tool_calls, }); - for (const tool of ask.choices[0].message.tool_calls) { const result = await FunctionHandler.handle( tool.function.name, JSON.parse(tool.function.arguments), req ); - console.log("result", result); await chat.add(req.request.chat_id, { role: "tool", @@ -85,7 +84,6 @@ export const handle = async (req: IRequest): Promise => { }); } } - if (ask.choices[0].finish_reason === "stop") { response = ask.choices[0].message.content; await chat.add(req.request.chat_id, { @@ -95,6 +93,5 @@ export const handle = async (req: IRequest): Promise => { break; } } - return response; -}; +}; \ No newline at end of file diff --git a/wrangler.toml b/wrangler.toml index a6d58e2..f68afbc 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -3,6 +3,13 @@ main="src/index.ts" compatibility_date = "2022-11-22" -kv_namespaces = [ - { binding = "personal_ai_chats", id = "", preview_id = ""} -] + +[vars] +API_BASE= "https://pro.sum4all.site/v1/" +MODEL="gpt-3.5-turbo-0125" +SYSTEM_PROMPT="你是 Siri Ultra。请你跟我对话时,务必使用中文,不要夹杂英文单词,甚至英语短语也不能随意使用,但类似于 llama3 这样的专属名词除外。回答时请尽量简洁,使用1-2句话。保持友好、乐于助人的态度。尽可能使用公制单位。保持对话简短精悍。只使用文字回答,不要包含链接或其他额外内容,尤其是emoji。不要使用计算机代码回答,例如不要返回用户的经度。" + +[[kv_namespaces]] +binding = "personal_ai_chats" +id = "" +preview_id = ""