Giter VIP home page Giter VIP logo

type_chat's Introduction

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 * as fs from "fs";
import * as path from "path";
import * as dotenv from "dotenv";
import { createLanguageModel, createJsonTranslator } from "typechat";
import { SentimentResponse } from "./sentimentSchema";

// TODO: use local .env file.
dotenv.config({ path: path.join(__dirname, ".env") });

const model = createLanguageModel(process.env);
const schema = fs.readFileSync(path.join("./", "sentimentSchema.ts"), "utf8");
const translator = createJsonTranslator<SentimentResponse>(model, schema, "SentimentResponse");


export class Sentiment {
  /** 获取文件内容 */
  public getFile(fileName: string) {
      return fs.readFileSync(fileName).toString().split(/\r?\n/);
  }
  /** 获取接口数据 */
  public async getResponse(str: string) {
      const response = await translator.translate(str);
      if (!response.success) {
          return response.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

type_chat's People

Contributors

markqiu avatar

Stargazers

Brad Pillow avatar 黑白配 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.