const schema = defSchema("CITY_SCHEMA", { type: "array", description: "A list of cities with population and elevation information.", items: { type: "object", description: "A city with population and elevation information.", properties: { name: { type: "string", description: "The name of the city." }, population: { type: "number", description: "The population of the city." }, url: { type: "string", description: "The URL of the city's Wikipedia page." } }, required: ["name", "population", "url"] } })
$`Generate data using JSON compliant with ${schema}.`
根据 TypeChat 的 “All You Need Is Types” 方法,架构会在插入 LLM 提示前转换成 TypeScript 类型。例如,上面的 CITY_SCHEMA 类型如下:
// A list of cities with population and elevation information. type CITY_SCHEMA = Array// The name of the city. name: string, // The population of the city. population: number, // The URL of the city's Wikipedia page. url: string, }>
const schema = defSchema("CITY_SCHEMA", { type: "array", description: "A list of cities with population and elevation information.", items: { type: "object", description: "A city with population and elevation information.", properties: { name: { type: "string", description: "The name of the city." }, population: { type: "number", description: "The population of the city." }, url: { type: "string", description: "The URL of the city's Wikipedia page." } }, required: ["name", "population", "url"] } }) $`Generate data using JSON compliant with ${schema}.`
defTool( "weather", "query a weather web api", { location: "string" }, async (args) => await fetch(`https://weather.api.api/?location=${args.location}`) )
注册 JavaScript 函数作为工具并结合 prompt 成为代理。
defAgent( "git", "Query a repository using Git to accomplish tasks.", `Your are a helpful LLM agent that can use the git tools to query the current repository. Answer the question in QUERY. - The current repository is the same as github repository.`, { model, system: ["system.github_info"], tools: ["git"] } ) then use it as a tool
let choice let message do { // 生成一个符合规范的 Git 提交信息 const res = await runPrompt((_) => { _.def("GIT_DIFF", diff, { maxTokens: 20000, language: "diff" }) _.$`Generate a git conventional commit message for the changes in GIT_DIFF. - do NOT add quotes - maximum 50 characters - use emojis` }) message = res.text } while (choice !== "commit")
for (const file of files) { const { filename, content } = file const matches = content.matchAll(rx)
for (const match of matches) { const url = match[1] const resolvedUrl = resolveUrl(filename, url) const { text } = await runPrompt( (_) => { _.defImages(resolvedUrl) _.$` You are an expert in assistive technology. You will analyze the image and generate a description alt text for the image. - Do not include alt text in the description. - Keep it short but descriptive. - Do not generate the [ character.` }, { system: ["system.safety_harmful_content"], model: "openai:gpt-4o", } ) imgs[url] = text } }