Skip to content

Commit

Permalink
feat: slice length when call generate api
Browse files Browse the repository at this point in the history
  • Loading branch information
YunYouJun committed Feb 4, 2024
1 parent e188a26 commit 4a6cbed
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
3 changes: 2 additions & 1 deletion components/AiPrompt.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import consola from 'consola'
import { useMagicKeys } from '@vueuse/core'
import { config } from '~/config'
const app = useAppStore()
Expand Down Expand Up @@ -42,7 +43,7 @@ watch(() => [Cmd_enter.value, Ctrl_enter.value], (v) => {
placeholder="想要什么样的春联?"
class="w-full rounded-lg p-4 shadow dark:bg-dark-800 outline-none!"
border="~ gray focus:(yellow-500)"
maxlength="200"
:maxlength="config.inputMaxLength"
/>

<button
Expand Down
3 changes: 3 additions & 0 deletions config/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export const suggestedCoupletsFilename = 'AI 春联.png'
export const config = {
inputMaxLength: 200,
}
22 changes: 15 additions & 7 deletions packages/ai/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import consola from 'consola'
import type OpenAI from 'openai'
import { baseChatCompletionCreateParams, baseModel, openai } from './config'

// TODO: pass params
import { config } from '~/config'

export async function getCompletion(msg: string) {
const chatCompletion = await openai.chat.completions.create({
...baseChatCompletionCreateParams,
Expand All @@ -19,18 +22,23 @@ export interface SprintFestivalCouplets {
总结: string
}

export async function getCouplets(couplet: string) {
export async function getCouplets(prompt: string) {
/**
* 限制输入长度
*/
prompt = prompt.trim().slice(0, config.inputMaxLength)

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

const messages: OpenAI.ChatCompletionMessageParam[] = [
Expand All @@ -40,13 +48,13 @@ export async function getCouplets(couplet: string) {
},
]

if (couplet)
messages.push({ role: 'user', content: `我的提示是:${couplet}` })
if (prompt)
messages.push({ role: 'user', content: `我的提示是:${prompt}` })

const chatCompletion = await openai.chat.completions.create({
...baseChatCompletionCreateParams,
messages,
model: 'deepseek-chat',
max_tokens: 300,
model: baseModel,
// stream: true
})

Expand Down
2 changes: 1 addition & 1 deletion packages/ai/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ export const openai = new OpenAI({
export const baseModel = 'deepseek-chat'

export const baseChatCompletionCreateParams: Partial<OpenAI.ChatCompletionCreateParamsNonStreaming> = {
max_tokens: 300,
max_tokens: 100,
// stream: true
}

0 comments on commit 4a6cbed

Please sign in to comment.