Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom AI service (INCL. url or model name) and optimize prompt #2

Merged
merged 3 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
OPENAI_API_KEY=
MODEL_NAME="gpt-3.5-turbo-1106"
AI_SERVICE_URL="https://api.openai.com/v1"
2 changes: 2 additions & 0 deletions packages/ai/.env.example
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
OPENAI_API_KEY=
MODEL_NAME="gpt-3.5-turbo-1106"
AI_SERVICE_URL="https://api.openai.com/v1"
18 changes: 9 additions & 9 deletions packages/ai/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,24 @@
*/
prompt = prompt.trim().slice(0, config.inputMaxLength)

const tooltip = [
'请根据我的提示生成一组春联,包含上联、下联各一句,每句字数在五到十三字之间,上下联字数相同,并附上一个恰当的不超过五个字的横批。',
'并给出一个字总结。',
'不需要标点符号,不要使用生僻字。',
`格式类型:{
// 尽可能少的 token
const tooltip = `
请根据我的提示生成一组春联,包含上联、下联各一句,每句字数在五到十三字之间,上下联字数相同,并附上一个恰当的不超过五个字的横批。
并给出一个字总结。
不要使用生僻字和标点符号。
格式类型:{
"上联": "",
"下联": "",
"横批": "",
"总结": ""
}`,
'直接给出可以被 JSON.parse 解析的字符串,不要解释内容。',
]
}
直接给出可以被 JSON.parse 解析的字符串,不需要解释内容。`

const messages: OpenAI.ChatCompletionMessageParam[] = [
{
role: 'system',
content: tooltip.join('\n'),

Check failure on line 47 in packages/ai/src/api.ts

View workflow job for this annotation

GitHub Actions / typecheck

Property 'join' does not exist on type '"\n请根据我的提示生成一组春联,包含上联、下联各一句,每句字数在五到十三字之间,上下联字数相同,并附上一个恰当的不超过五个字的横批。\n并给出一个字总结。\n不要使用生僻字和标点符号。\n格式类型:{\n \"上联\": \"\",\n \"下联\": \"\",\n \"横批\": \"\",\n \"总结\": \"\"\n}\n直接给出可以被 JSON.parse 解析的字符串,不需要解释内容。"'.
},
}

Check failure on line 48 in packages/ai/src/api.ts

View workflow job for this annotation

GitHub Actions / lint

Missing trailing comma
]

if (prompt)
Expand Down
6 changes: 4 additions & 2 deletions packages/ai/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import OpenAI from 'openai'

const deepseekApiUrl = 'https://api.deepseek.com/v1'

const aiServiceUrl = process.env.AI_SERVICE_URL || deepseekApiUrl

export const openai = new OpenAI({
baseURL: deepseekApiUrl,
baseURL: aiServiceUrl,
apiKey: process.env.OPENAI_API_KEY, // This is the default and can be omitted
})

export const baseModel = 'deepseek-chat'
export const baseModel = process.env.MODEL_NAME || 'deepseek-chat'

export const baseChatCompletionCreateParams: Partial<OpenAI.ChatCompletionCreateParamsNonStreaming> = {
max_tokens: 100,
Expand Down
Loading