From 40f2e1883763b66b5c26ce168e9d820ecab0edc8 Mon Sep 17 00:00:00 2001 From: YunYouJun Date: Mon, 5 Feb 2024 00:06:40 +0800 Subject: [PATCH] refactor: optimze text --- components/SpringFestivalCouplets.vue | 1 + packages/ai/src/api.ts | 4 ++-- server/api/generate.ts | 10 +++++++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/components/SpringFestivalCouplets.vue b/components/SpringFestivalCouplets.vue index a5f20f8..fecd934 100644 --- a/components/SpringFestivalCouplets.vue +++ b/components/SpringFestivalCouplets.vue @@ -155,5 +155,6 @@ async function shareLink() { padding: 1.5rem 0.4rem 1.4rem 0.4rem; // 文字 gap letter-spacing: 1rem; + white-space: nowrap; } diff --git a/packages/ai/src/api.ts b/packages/ai/src/api.ts index 81c3a0d..e427216 100644 --- a/packages/ai/src/api.ts +++ b/packages/ai/src/api.ts @@ -24,8 +24,8 @@ export async function getCouplets(couplet: string) { '请根据我的提示生成一组春联,包含上联、下联各一句,每句字数在五到十三字之间,并附上一个恰当的横批。', '并给出一个字总结。', '不需要标点符号,尽量不要使用生僻字。', - '以下述 JSON 给出:', - `export interface SprintFestivalCouplets { + '以可以被 JSON.parse 解析的字符串格式给出,务必不要生成多余文本。', + `参考 TS 类型:export interface SprintFestivalCouplets { 上联: string 下联: string 横批: string diff --git a/server/api/generate.ts b/server/api/generate.ts index 7fd64f5..f54cf43 100644 --- a/server/api/generate.ts +++ b/server/api/generate.ts @@ -7,7 +7,15 @@ export default defineEventHandler(async (event) => { const { content } = data const unwrapperContent = (content || '{}')?.replace('```json\n', '').replace('```', '') - const coupletData = JSON.parse(unwrapperContent) as SprintFestivalCouplets + let coupletData: SprintFestivalCouplets | undefined + try { + coupletData = JSON.parse(unwrapperContent) as SprintFestivalCouplets + } + catch (e) { + // eslint-disable-next-line no-console + console.log(content) + console.error(e) + } return coupletData })