A python version of TypeChat taking a shortcut. It's in fact a typescript tool. Because you define you python lib interface by yourself and it will try to translate whatever you defined in typescript to python version.
npm installLet's use an example of TypeChat: Sentiment
import*asfsfrom"fs";import*aspathfrom"path";import*asdotenvfrom"dotenv";import{createLanguageModel,createJsonTranslator}from"typechat";import{SentimentResponse}from"./sentimentSchema";// TODO: use local .env file.dotenv.config({path: path.join(__dirname,".env")});constmodel=createLanguageModel(process.env);constschema=fs.readFileSync(path.join("./","sentimentSchema.ts"),"utf8");consttranslator=createJsonTranslator<SentimentResponse>(model,schema,"SentimentResponse");exportclassSentiment{/** 获取文件内容 */publicgetFile(fileName: string){returnfs.readFileSync(fileName).toString().split(/\r?\n/);}/** 获取接口数据 */publicasyncgetResponse(str: string){constresponse=awaittranslator.translate(str);if(!response.success){returnresponse.message+" - "+str;}return`The sentiment is ${response.data.sentiment}`}}npm run buildnpm run packageLook at details in examples directory.
cd examples
pip install -r requirements.txt --force-reinstall
python chat.pyThe sentiment is neutral
The sentiment is positive
The sentiment is positive
The sentiment is neutral
That's it. Enjoy it.
@wleven 👍