Skip to content

Commit 4a6cbed

Browse files
committed
feat: slice length when call generate api
1 parent e188a26 commit 4a6cbed

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

components/AiPrompt.vue

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import consola from 'consola'
33
44
import { useMagicKeys } from '@vueuse/core'
5+
import { config } from '~/config'
56
67
const app = useAppStore()
78
@@ -42,7 +43,7 @@ watch(() => [Cmd_enter.value, Ctrl_enter.value], (v) => {
4243
placeholder="想要什么样的春联?"
4344
class="w-full rounded-lg p-4 shadow dark:bg-dark-800 outline-none!"
4445
border="~ gray focus:(yellow-500)"
45-
maxlength="200"
46+
:maxlength="config.inputMaxLength"
4647
/>
4748

4849
<button

config/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
export const suggestedCoupletsFilename = 'AI 春联.png'
2+
export const config = {
3+
inputMaxLength: 200,
4+
}

packages/ai/src/api.ts

+15-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import consola from 'consola'
22
import type OpenAI from 'openai'
33
import { baseChatCompletionCreateParams, baseModel, openai } from './config'
44

5+
// TODO: pass params
6+
import { config } from '~/config'
7+
58
export async function getCompletion(msg: string) {
69
const chatCompletion = await openai.chat.completions.create({
710
...baseChatCompletionCreateParams,
@@ -19,18 +22,23 @@ export interface SprintFestivalCouplets {
1922
总结: string
2023
}
2124

22-
export async function getCouplets(couplet: string) {
25+
export async function getCouplets(prompt: string) {
26+
/**
27+
* 限制输入长度
28+
*/
29+
prompt = prompt.trim().slice(0, config.inputMaxLength)
30+
2331
const tooltip = [
2432
'请根据我的提示生成一组春联,包含上联、下联各一句,每句字数在五到十三字之间,上下联字数相同,并附上一个恰当的不超过五个字的横批。',
2533
'并给出一个字总结。',
26-
'不需要标点符号,尽量不要使用生僻字。',
34+
'不需要标点符号,不要使用生僻字。',
2735
`格式类型:{
2836
"上联": "",
2937
"下联": "",
3038
"横批": "",
3139
"总结": ""
3240
}`,
33-
'直接给出可以被 JSON.parse 解析的字符串,不需要解释内容。',
41+
'直接给出可以被 JSON.parse 解析的字符串,不要解释内容。',
3442
]
3543

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

43-
if (couplet)
44-
messages.push({ role: 'user', content: `我的提示是:${couplet}` })
51+
if (prompt)
52+
messages.push({ role: 'user', content: `我的提示是:${prompt}` })
4553

4654
const chatCompletion = await openai.chat.completions.create({
55+
...baseChatCompletionCreateParams,
4756
messages,
48-
model: 'deepseek-chat',
49-
max_tokens: 300,
57+
model: baseModel,
5058
// stream: true
5159
})
5260

packages/ai/src/config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ export const openai = new OpenAI({
1212
export const baseModel = 'deepseek-chat'
1313

1414
export const baseChatCompletionCreateParams: Partial<OpenAI.ChatCompletionCreateParamsNonStreaming> = {
15-
max_tokens: 300,
15+
max_tokens: 100,
1616
// stream: true
1717
}

0 commit comments

Comments
 (0)