微软开源TypeChat库,给大语言模型换种提示,一行代码安装
机器之心报道
借助 TypeChat,可以很容易地获得类型良好的结构化数据。
//./src/sentimentSchema.ts
// The following is a schema definition for determining the sentiment of a some user input.
export interface SentimentResponse {
/** The sentiment of the text. */
sentiment: "negative" | "neutral" | "positive";
}
//./src/main.ts
import * as fs from "fs";
import * as path from "path";
import dotenv from "dotenv";
import * as typechat from "typechat";
import {SentimentResponse} from "./sentimentSchema";
// Load environment variables.
dotenv.config ({ path: path.join (__dirname, "../.env") });
// Create a language model based on the environment variables.
const model = typechat.createLanguageModel (process.env);
// Load up the contents of our "Response" schema.
const schema = fs.readFileSync (path.join (__dirname, "sentimentSchema.ts"), "utf8");
const translator = typechat.createJsonTranslator<SentimentResponse>(model, schema, "SentimentResponse");
// Process requests interactively.
typechat.processRequests ("😀>", /*inputFile*/undefined, async (request) => {
const response = await translator.translate (request);
if (!response.success) {
console.log (response.message);
return;
}
console.log (`The sentiment is ${response.data.sentiment}`);
});
© THE END
转载请联系本公众号获得授权
投稿或寻求报道:[email protected]
微信扫码关注该文公众号作者
戳这里提交新闻线索和高质量文章给我们。
来源: qq
点击查看作者最近其他文章