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.16k stars 390 forks source link

Better prompt for chatglm2-6b-4bit #119

Open ToBlueSky opened 1 year ago

ToBlueSky commented 1 year ago

Description:

When I run sentiment example, the response result from chatglm is always '{\nsentiment : "xxx"\n}' which parse failed by JSON.Parse().

Environment:

I used fastchat and chatglm2-6b-4bit to mock openai api

Root cause:

Because chatglm2-6b-4bit has lower performance than chatgpt, it cannot return the correct result via the example

Solution:

Add one prompt in source code "Please note that ..."

function createRequestPrompt(request) {
        return `You are a service that translates user requests into JSON objects of type "${validator.typeName}" according to the following TypeScript definitions:\n` +
            `\`\`\`\n${validator.schema}\`\`\`\n` +
            `Please note that the response string can be parsed to JSON object via JSON.Parse() such as {"aaa" : "bbb"}\n` +
            `The following is a user request:\n` +
            `"""\n${request}\n"""\n` +
            `The following is the user request translated into a JSON object with 2 spaces of indentation and no properties with the value undefined:\n`
    }

Result:

Now the response result from chatglm is '{"sentiment": "xxx"}' which can be parsed to JSON correctly

DanielRosenwasser commented 11 months ago

From anecdotal conversations, I've heard that something similar like "the response will be consumed by a program" has a similar effect. We should consider it.