Skip to content

Commit

Permalink
support constom model, apibase and system prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
fatwang2 committed May 9, 2024
1 parent 2c87947 commit ddf5a97
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 32 deletions.
23 changes: 16 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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 = "<id>", preview_id = "<preview_id>"}
]
[[kv_namespaces]]
binding = "personal_ai_chats"
id = "<id>"
preview_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

Expand Down
41 changes: 19 additions & 22 deletions src/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> => {
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",
Expand All @@ -61,22 +62,20 @@ export const handle = async (req: IRequest): Promise<string> => {
],
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",
Expand All @@ -85,7 +84,6 @@ export const handle = async (req: IRequest): Promise<string> => {
});
}
}

if (ask.choices[0].finish_reason === "stop") {
response = ask.choices[0].message.content;
await chat.add(req.request.chat_id, {
Expand All @@ -95,6 +93,5 @@ export const handle = async (req: IRequest): Promise<string> => {
break;
}
}

return response;
};
};
13 changes: 10 additions & 3 deletions wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ main="src/index.ts"

compatibility_date = "2022-11-22"

kv_namespaces = [
{ binding = "personal_ai_chats", id = "<id>", preview_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 = "<id>"
preview_id = "<preview_id>"

0 comments on commit ddf5a97

Please sign in to comment.