egg_server/test/llm/intentRecognition.ts

60 lines
1.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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))