-
-
Notifications
You must be signed in to change notification settings - Fork 142
feat(anthropic): text stream support #266
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
Conversation
This reverts commit 2316c30.
Preview deployments for prism ⚡️
Commit: Deployment ID: Static site name: |
Hi @ChrisB-TL Thanks for the feedback. I’ve made some changes, but I could use some guidance on the remaining parts.
This turned out to be more complex than I anticipated, but with some guidance I believe I can crack it |
I applied some fixes and refactoring based on your feedback, and fixed the formatting. The implementation now handles rate limits correctly, and properly processes citations and thinking signatures. Any feedback would be very helpful. |
…nthropic-stream-0.51
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.
Changes made directly.
I think I am done with this. In summary I have:
- Shared error handling code across the text/structured base handler and streaming.
- Added the concept of a meta chunk - the first and last - which the dev probably won't want to forward to the frontend. This should lay the groundwork as/when we add usage data also.
- Refactored the
processStream
method to use a match statement and defer to separate handler methods, and added comments to explain what is going on given its complexity. - Clarified what is going on with state.
- Added yielding of thinking chunks with a chunkType of Thinking (so dev can decide whether or not to forward to frontend).
- Fixed citations - this was knarly as it does not work quite the same way as a standard response. I've added a note to the docs to explain.
- Re-used the text handler's build payload method, as its the same other than the stream property.
It is a pretty complex feature for Anthropic given citations, thinking etc. and I think it could do with further cleaning up for readability / maintenance. But I think good enough for now if @sixlive agrees?
Thanks a lot for all the refactoring on the PR! You've really shaped the code to align well with the rest of the library. Sorry if I caused some pain due to my lack of knowledge with the rest of the library (and I did underestimate the complexity of Anthropic Stream) 🤦♂️ The ChunkType approach you implemented is quite smart. I'm now thinking about how we could extend this to provide better signalling for tool execution to the frontend - specifically for "tool_started" and "tool_finished" events that would help to display tool execution status in the frontend. Would ChunkType be the foundation bricks to achieve this? |
Not at all - it's a tricky one as Anthropic is so feature rich! That's a great idea on using the chunkType for tools. You could add metadata about the tool - name, input data etc. - to additionalData. I'd suggest getting this PR done first, then we can loop back over the stream handlers to do that if @sixlive likes the idea too! |
@ChrisB-TL I finally had chance to test it further. I realised an issue with the handleMessageStop's Chunk. The Also within FinishReasonMap |
I found some more room for improvements. I was trying to setup history and I had difficulty with multiple tool-call case. Specifically;
|
src/Enums/ChunkType.php
Outdated
|
||
enum ChunkType | ||
{ | ||
case Message; |
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.
Does this make better sense as Text
or Message
?
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.
I'm thinking this makes more sense being Text
.
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.
One feedback comment. Ready to merge this shortly.
I've been tinkering with this branch for some time.
![]() I tried implementing the Steps approach here: https://github.com/sentiasa/prism-anthropic-stream/pull/2/files. This might be useful for other models' streaming as well. (this branch is half baked and just wanted to show you my approach) My thoughts:
|
@sentiasa I think we should probably do something about point 1. Lets punt on point 2 for now. I definitely want to implement chunk types for tools more broadly. |
Description
Breaking Changes
none