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 378 forks source link

Proposed alternate prompting scheme #241

Open gvanrossum opened 2 months ago

gvanrossum commented 2 months ago

This is my proposal to address #39. It also fixes the issue I flagged in #240 about the user input being repeated twice. I tried it with my demo from #238 and that seems to work.

The prompt structure is as follows:

I don't understand how your test tools work (I'm a dinosaur, I don't know how hatch or toml or pipenv work, I can handle pytest though).

@DanielRosenwasser

DanielRosenwasser commented 2 months ago

So this matches my intuition around how to put all of this together. While we have no way of testing for a regression, I'd be okay with running with something like this for now. The thing I'll note is that when @ahejlsberg tried something like this with the system prompt, he got slightly worse results.

That said, he guessed part of this was probably tied to the schema falling further and further behind in history. So maybe something to alleviate this would be to instead do:

Basically, move the system prompt immediately before the user request.


I don't understand how your test tools work

No worries. To most quickly fix the build:

cd TypeChat/python
pip install -e .[dev,examples]
pytest --snapshot-update
git add tests
git commit -m "Update snapshots."
DanielRosenwasser commented 2 months ago

It probably also makes sense to make the repair prompt a system message as well.

DanielRosenwasser commented 2 months ago

One of the things I tried in https://github.com/microsoft/TypeChat/tree/systemPromptPython (ea0e0d8a3a4e2d5ea1982f35ae8cc730468eec73) was to take the approach here after fixing some issues. As @ahejlsberg mentioned, OpenAI unfortunately doesn't do a great job when you switch to the system prompts. Trying things out on the coffee shop example input file, it generally got more "wrong".

One idea we could try is to expose the (very basic) logic we use to parse out JSON so that people can override translate and do whatever they want.

But otherwise, the only other approaches I could advise to implement something like https://github.com/microsoft/TypeChat/pull/238 is to experiment with preambles to talk about past state.

gvanrossum commented 2 months ago

Thanks for the feedback!

Preamble issues

My problem with the preamble is that it is prefixed to the prompt containing the schema, which makes the conversation a bit awkward, as it goes like this (ignoring system messages):

What I want is this (because my intuition tells me that it's a more natural dialog):

About system messages:

My (rudimentary) understanding of system messages is that it's meant to set a behavior or personality that's hard(er) to subvert by users trying to be clever -- e.g. the system prompt would tell the model not to be racist. In TypeChat's case, I think that at best the prompt that sets the schema should be a system prompt, but the repair prompts should probably just be user prompts.

The docs say a little bit about this. It makes me think that it's not worth experimenting too much with this in particular.

I also just noticed "JSON Mode" in the docs, which may or may not be useful for TypeChat.

ahejlsberg commented 2 months ago

@gvanrossum FWIW, a while back I did a multi-turn experiment where I didn't include previous replies in the prompt, but rather just repeated the request with the additional user input concatenated to the the previous user input (separated by newlines). So, your example would have a single user prompt section with text like:

Please translate user requests into JSON using this schema:
{schema JSON}.
Here is a user question:
draw three squares
make them bigger
make them red

It seemed to work quite well. Wonder if that would work in your case?

ahejlsberg commented 2 months ago

I also just noticed "JSON Mode" in the docs, which may or may not be useful for TypeChat.

The reason we aren't using JSON Mode is that there is no good way to know upfront if a model supports it, and, unfortunately, older models don't know to ignore it, but instead error on the request.

Also, not getting JSON back from requests hasn't really been an issue.

gvanrossum commented 2 months ago

I tried concatenating the user inputs, and it works okay — I’m not completely happy, but I wasn’t with my old approach either.

I’d think that it would not work well with ChatGPT’s caching scheme; though I think that TypeChat makes that hard anyway by not giving the user the exact output from the model.

It makes me wonder what the prompt_preamble is intended for, though.


From: Anders Hejlsberg @.> Sent: Wednesday, May 1, 2024 6:15:34 PM To: microsoft/TypeChat @.> Cc: Guido van Rossum (PYTHON) @.>; Mention @.> Subject: Re: [microsoft/TypeChat] Proposed alternate prompting scheme (PR #241)

@gvanrossumhttps://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fgvanrossum&data=05%7C02%7Cgvanrossum%40microsoft.com%7C93078619311d483366b408dc6a455e55%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C638502093394267737%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=SswLMU8Cbb78fxukGgRfT56pOp0R5vZ9CyYsrNI%2BtvQ%3D&reserved=0 FWIW, a while back I did a multi-turn experiment where I didn't include previous replies in the prompt, but rather just repeated the request with the additional user input concatenated to the the previous user input (separated by newlines). So, your example would have a single user prompt section with text like:

Please translate user requests into JSON using this schema: {schema JSON}. Here is a user question: draw three squares make them bigger make them red

It seemed to work quite well. Wonder if that would work in your case?

— Reply to this email directly, view it on GitHubhttps://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeChat%2Fpull%2F241%23issuecomment-2089370024&data=05%7C02%7Cgvanrossum%40microsoft.com%7C93078619311d483366b408dc6a455e55%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C638502093394278168%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=gbFrYyfMpU%2FBgd89FeY8TdHdQtfo3OD3QkFWEeolWqk%3D&reserved=0, or unsubscribehttps://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAAWCWMW7TOR4W33QRYXU2YTZAGHTNAVCNFSM6AAAAABGTVDORGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAOBZGM3TAMBSGQ&data=05%7C02%7Cgvanrossum%40microsoft.com%7C93078619311d483366b408dc6a455e55%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C638502093394285794%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=dRUqv6RO1k4BTXv%2FnAjV6E%2Bw9lPin2ANeItW4azFYkM%3D&reserved=0. You are receiving this because you were mentioned.Message ID: @.***>