microsoft / TypeChat

TypeChat is a library that makes it easy to build natural language interfaces using types.
https://microsoft.github.io/TypeChat/
MIT License
8.06k stars 379 forks source link

How to customize typechat for my own model and personalized scenarios (exp. for Chinese users)? #86

Open qftie opened 11 months ago

qftie commented 11 months ago

I am currently deploying the model and exposing a callable OpenAI API. I am modifying model.ts to fit my own model, but I'm experiencing instability when using mpt-30b-chat and mpt-30b-instruct. Math functions are frequently being called incorrectly. JSON templates often fail to validate in complex projects. Do you have any suggestions, such as adding a system prompt or modifying the template to accommodate Chinese?

DanielRosenwasser commented 11 months ago

I am modifying model.ts to fit my own model

I'll just note that TypeChat has a bring-your-own-model approach. You don't need to fork the library, you just need to provide an object with a complete method.

Here's an example

```ts const model: TypeChatLanguageModel = { complete: async function complete(prompt) { const response = await fetch("https://api.openai.com/v1/chat/completions", { method: "POST", headers: { Authorization: `Bearer ${apiKey}`, "Content-Type": "application/json", }, body: JSON.stringify({ model: modelName, temperature: 0, n: 1, "messages": [ { "role": "user", "content": prompt } ], }) }); assert(response.body); try { return success(await response.text()); } catch (e) { return error(e instanceof Error ? e.message : String(e)); } } } ```

About other languages like Chinese - I'm not so sure. Maybe others have tips and ideas?

weykon commented 11 months ago

Hi! I've completed the first level of the experiment, which involved making additional translation requests based on the returned results. It seems that we need to obtain its output before we can begin the transformation. I have tried adding new requirements for translation in the createRequestPrompt method in typechat, but I have attempted multiple prompts to construct a structured processing flow, even with GPT-4, but have not been successful. So, in order to ensure a good result, here are a few suggestions:

  1. Before executing typechat, you can translate your schema file (using GPT or other simple translation services), where all string types are translated into Chinese. I have tried this, and if the content of your schema is in Chinese or another language, the values in the output JSON data will be translated accordingly.

  2. After executing typechat, you can perform translation on the resulting output. I achieved this using a simple prompt.

    Prompt --
const prompt =`${JSON.stringify(cart, undefined, 2)}\n above is json string all of the following JSON key and value, the values of string need to translate into Chinese, but the JSON key name not change, such like "items": [
        {
          "type": "lineitem",
          "product": {
            "type": "LatteDrinks",
            "name": "latte"
          },
          "quantity": 1
        }
      ] will translate to chinese: 
        [{
          "type": "行项目",
          "product": {
            "type": "拿铁饮料",
            "name": "拿铁"
          },
          "quantity": 1
        }]
     and keep JSON data structure`;

This way, you can always translate the current output, reducing the amount of translation needed. However, please note that translating the data may not preserve the same types as defined in the schema file. Therefore, this method is more suitable for displaying text data rather than precise data.

  1. Another approach, which I haven't achieved yet, is to granularly parse each JSON key and value within typechat and handle the required actions. This method should also be feasible but requires more experimentation time. For example, in my project, there is a section for translating Markdown. A one-shot experiment similar to this prompt can be found in this https://github.com/weykon/translate-md-by-gpt/blob/main/pmt_set.ts . (the original code is quite long, so please check the link for details.)

Using this method of enabling translation options may result in two completely different sets of prompts, which increases complexity.

  1. I believe a better approach would be to use the i18n (internationalization) concept and create multiple language schema files, such as schema.en.ts, schema.zh.ts, schema.it.ts, to obtain more accurate outputs.

So, currently, I think the fourth method is a better choice.

ybsun0215 commented 10 months ago

Hello, have you encountered an error after mounting vpn? How was it resolved?