Skip to content

Repository files navigation

type_chat

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.

Installation

npm install

Usage

Step one: define your lib interface in typescript

Let'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}`}}

Step two: generate your lib interface in python

npm run build

Step three: pack your python lib

npm run package

Step four: install and use your lib in python

Look at details in examples directory.

cd examples
pip install -r requirements.txt --force-reinstall
python chat.py
The sentiment is neutral
The sentiment is positive
The sentiment is positive
The sentiment is neutral

That's it. Enjoy it.

Acknowledgements

@wleven 👍

License

MIT

About

a python version of TypeChat taking a shortcut

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages