Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add chat.messages(format='internal') #1532

Closed
wants to merge 2 commits into from
Closed

Add chat.messages(format='internal') #1532

wants to merge 2 commits into from

Conversation

wch
Copy link
Collaborator

@wch wch commented Jul 15, 2024

This changes PR changes chat.messages() so that:

  • A value for format is required. Previously the default value was MISSING.
  • To get the internal/native format, the user calls it with chat.messages(format="internal") (instead of format=MISSING, which was the previously the default).

Previously, the default MISSING would result in it returning the messages in the internal ChatMessage format. However, this format is actually the least likely format that a user would want -- it is used internally in Shiny, but is not directly usable by any LLMs out there. I think that we don't want to have a default format which is not useful.

Note that this builds on PR #1530.

Also, I'm open to ideas other than "internal" for the name.

@@ -406,7 +406,7 @@ def messages(
def messages(
self,
*,
format: MISSING_TYPE | ProviderMessageFormat = MISSING,
format: ProviderMessageFormat | Literal["internal"],
Copy link
Collaborator

@cpsievert cpsievert Jul 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, this format is actually the least likely format that a user would want -- it is used internally in Shiny, but is not directly usable by any LLMs out there.

That's not actually true. This format can be passed directly OpenAI, LangChain, Anthropic, Ollama, and probably others without modification. It won't pass a type checker, but that doesn't seem like reason enough to force everyone to have to specify a format.

Suggested change
format: ProviderMessageFormat | Literal["internal"],
format: ProviderMessageFormat | Literal["internal"] = "internal",

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wait, I think I misunderstood what the default format was -- I thought it was the actual internal format that the Chat class uses, but that's not right. The internal format is StoredMessage, not ChatMessage. That's why I thought it wouldn't make sense to return that format by default.

So to summarize, prior to this PR, the default format was ChatMessage, which is sort of a lowest-common-denominator format that has role and content.

So it might make sense to just drop this PR entirely. That said, it could still be useful to be able to access the StoredMessages in the chat object. That's something I actually wanted to do when I was trying to get a handle on the sizes of messages being sent to the LLM (but then I got sidetracked before I went in depth on it).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see. I'd be interested to know more about why you want access to StoredMessage.

I'll close this PR and create another one for the format="anthropic" thing

Comment on lines +878 to +879
while messages2[-1]["role"] != "user":
messages2.pop()
Copy link
Collaborator

@cpsievert cpsievert Jul 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's technically possible that messages2 is of length 0 here, so we'll need to be more careful about that.

from shiny import reactive
from shiny.express import ui

chat = ui.Chat(id="chat")
chat.ui()

@reactive.effect
def _():
    print(chat.messages(format="anthropic"))

@cpsievert cpsievert closed this Jul 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants