60 lines
1.5 KiB
TypeScript
60 lines
1.5 KiB
TypeScript
import { parseJsonString } from "@egg/hooks"
|
||
import { z } from "zod"
|
||
import { zodToJsonSchema } from "zod-to-json-schema"
|
||
|
||
import initAppConfig from "../../constant/config"
|
||
import llm from "../../utils/llm"
|
||
import { cleanLLMRes } from "../../utils/llm/base"
|
||
|
||
await initAppConfig()
|
||
|
||
const BaseIntentSchema = z.object({
|
||
intent: z.number().min(1).max(13),
|
||
})
|
||
|
||
const BriefingSchema = BaseIntentSchema.extend({
|
||
intent: z.literal(3),
|
||
link: z.string().url(),
|
||
userDescription: z.string().min(1),
|
||
})
|
||
|
||
const CommonResponseSchema = BaseIntentSchema.extend({
|
||
intent: z.literal(12),
|
||
message: z.string().min(1),
|
||
})
|
||
|
||
const IntentSchema = z.union([
|
||
BriefingSchema,
|
||
CommonResponseSchema,
|
||
BaseIntentSchema,
|
||
])
|
||
|
||
const jsonSchema = zodToJsonSchema(IntentSchema)
|
||
|
||
const res = await llm.invoke(
|
||
"intentRecognitionNext",
|
||
{
|
||
userInput:
|
||
"https://mp.weixin.qq.com/s/-0J8XbXJU6Bu-UihRtgGAQ Airbnb死磕React Native惨败,微软却玩出花!Office、Outlook全线接入,Copilot成最大赢家 推荐大家看一下,rn助力微软copilot 跨平台实现",
|
||
time: new Date().toLocaleString("zh-CN", { timeZone: "Asia/Shanghai" }),
|
||
jsonSchema,
|
||
},
|
||
"test",
|
||
0,
|
||
true
|
||
)
|
||
|
||
const rawIntent = cleanLLMRes(res as string)
|
||
|
||
const parsedIntent = parseJsonString(rawIntent, null)
|
||
console.log("🚀 ~ parsedIntent:", parsedIntent)
|
||
|
||
try {
|
||
IntentSchema.parse(parsedIntent)
|
||
console.log("Intent is valid:", parsedIntent)
|
||
} catch (e: any) {
|
||
console.error("Invalid intent:", e.errors)
|
||
}
|
||
|
||
console.log(cleanLLMRes(res as string))
|