-
Notifications
You must be signed in to change notification settings - Fork 69
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
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -347,7 +347,7 @@ async def _raise_exception( | |
def messages( | ||
self, | ||
*, | ||
format: Literal["anthropic"] = "anthropic", | ||
format: Literal["anthropic"], | ||
token_limits: tuple[int, int] | None = (4096, 1000), | ||
transform_user: Literal["all", "last", "none"] = "all", | ||
transform_assistant: bool = False, | ||
|
@@ -357,7 +357,7 @@ def messages( | |
def messages( | ||
self, | ||
*, | ||
format: Literal["google"] = "google", | ||
format: Literal["google"], | ||
token_limits: tuple[int, int] | None = (4096, 1000), | ||
transform_user: Literal["all", "last", "none"] = "all", | ||
transform_assistant: bool = False, | ||
|
@@ -367,7 +367,7 @@ def messages( | |
def messages( | ||
self, | ||
*, | ||
format: Literal["langchain"] = "langchain", | ||
format: Literal["langchain"], | ||
token_limits: tuple[int, int] | None = (4096, 1000), | ||
transform_user: Literal["all", "last", "none"] = "all", | ||
transform_assistant: bool = False, | ||
|
@@ -377,7 +377,7 @@ def messages( | |
def messages( | ||
self, | ||
*, | ||
format: Literal["openai"] = "openai", | ||
format: Literal["openai"], | ||
token_limits: tuple[int, int] | None = (4096, 1000), | ||
transform_user: Literal["all", "last", "none"] = "all", | ||
transform_assistant: bool = False, | ||
|
@@ -387,7 +387,7 @@ def messages( | |
def messages( | ||
self, | ||
*, | ||
format: Literal["ollama"] = "ollama", | ||
format: Literal["ollama"], | ||
token_limits: tuple[int, int] | None = (4096, 1000), | ||
transform_user: Literal["all", "last", "none"] = "all", | ||
transform_assistant: bool = False, | ||
|
@@ -397,7 +397,7 @@ def messages( | |
def messages( | ||
self, | ||
*, | ||
format: MISSING_TYPE = MISSING, | ||
format: Literal["internal"], | ||
token_limits: tuple[int, int] | None = (4096, 1000), | ||
transform_user: Literal["all", "last", "none"] = "all", | ||
transform_assistant: bool = False, | ||
|
@@ -406,7 +406,7 @@ def messages( | |
def messages( | ||
self, | ||
*, | ||
format: MISSING_TYPE | ProviderMessageFormat = MISSING, | ||
format: ProviderMessageFormat | Literal["internal"], | ||
token_limits: tuple[int, int] | None = (4096, 1000), | ||
transform_user: Literal["all", "last", "none"] = "all", | ||
transform_assistant: bool = False, | ||
|
@@ -466,7 +466,7 @@ def messages( | |
|
||
messages = self._messages() | ||
if token_limits is not None: | ||
messages = self._trim_messages(messages, token_limits) | ||
messages = self._trim_messages(messages, token_limits, format) | ||
|
||
res: list[ChatMessage | ProviderMessage] = [] | ||
for i, m in enumerate(messages): | ||
|
@@ -479,7 +479,7 @@ def messages( | |
) | ||
content_key = m["transform_key" if transform else "pre_transform_key"] | ||
chat_msg = ChatMessage(content=m[content_key], role=m["role"]) | ||
if not isinstance(format, MISSING_TYPE): | ||
if format != "internal": | ||
chat_msg = as_provider_message(chat_msg, format) | ||
res.append(chat_msg) | ||
|
||
|
@@ -827,7 +827,8 @@ def _store_message( | |
@staticmethod | ||
def _trim_messages( | ||
messages: tuple[StoredMessage, ...], | ||
token_limits: tuple[int, int] = (4096, 1000), | ||
token_limits: tuple[int, int], | ||
format: ProviderMessageFormat | Literal["internal"], | ||
) -> tuple[StoredMessage, ...]: | ||
|
||
n_total, n_reserve = token_limits | ||
|
@@ -872,6 +873,11 @@ def _trim_messages( | |
if remaining_non_system_tokens >= 0: | ||
messages2.append(m) | ||
|
||
if format == "anthropic": | ||
# For anthropic, the first message must be a user message. | ||
while messages2[-1]["role"] != "user": | ||
messages2.pop() | ||
Comment on lines
+878
to
+879
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's technically possible 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")) |
||
|
||
messages2.reverse() | ||
|
||
if len(messages2) == n_system_messages and n_other_messages > 0: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
.There was a problem hiding this comment.
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 isStoredMessage
, notChatMessage
. 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 hasrole
andcontent
.So it might make sense to just drop this PR entirely. That said, it could still be useful to be able to access the
StoredMessage
s 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).There was a problem hiding this comment.
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