langchainrb
langchainrb copied to clipboard
Add streaming support for Anthropic
@chalmagean I came across this error when it uses a tool:
irb(main):015* anthropic.chat messages:[{role:"user", content:"What are the news today?"}], tools: Langchain::Tool::NewsRetriever.function_schemas.to_anthropic_format do |chunk|
irb(main):016* puts chunk
irb(main):017> end
{"type"=>"message_start", "message"=>{"id"=>"msg_01H7z5m5YnYo54ZynbvyyAgz", "type"=>"message", "role"=>"assistant", "model"=>"claude-3-sonnet-20240229", "content"=>[], "stop_reason"=>nil, "stop_sequence"=>nil, "usage"=>{"input_tokens"=>1660, "output_tokens"=>1}}}
{"type"=>"content_block_start", "index"=>0, "content_block"=>{"type"=>"text", "text"=>""}}
{"type"=>"ping"}
{"type"=>"content_block_delta", "index"=>0, "delta"=>{"type"=>"text_delta", "text"=>"Here"}}
{"type"=>"content_block_delta", "index"=>0, "delta"=>{"type"=>"text_delta", "text"=>" is how"}}
{"type"=>"content_block_delta", "index"=>0, "delta"=>{"type"=>"text_delta", "text"=>" we"}}
{"type"=>"content_block_delta", "index"=>0, "delta"=>{"type"=>"text_delta", "text"=>" can get"}}
{"type"=>"content_block_delta", "index"=>0, "delta"=>{"type"=>"text_delta", "text"=>" the top"}}
{"type"=>"content_block_delta", "index"=>0, "delta"=>{"type"=>"text_delta", "text"=>" headlines for news"}}
{"type"=>"content_block_delta", "index"=>0, "delta"=>{"type"=>"text_delta", "text"=>" today using"}}
{"type"=>"content_block_delta", "index"=>0, "delta"=>{"type"=>"text_delta", "text"=>" the News"}}
{"type"=>"content_block_delta", "index"=>0, "delta"=>{"type"=>"text_delta", "text"=>" Retriever tool"}}
{"type"=>"content_block_delta", "index"=>0, "delta"=>{"type"=>"text_delta", "text"=>":"}}
{"type"=>"content_block_stop", "index"=>0}
{"type"=>"content_block_start", "index"=>1, "content_block"=>{"type"=>"tool_use", "id"=>"toolu_01DusTL7UBsky2XWigBW7K8R", "name"=>"langchain_tool_news_retriever__get_top_headlines", "input"=>{}}}
/Users/andrei/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/anthropic-0.3.0/lib/anthropic/http.rb:102:in `concat': no implicit conversion of nil into String (TypeError)
response["content"][0]["text"].concat(delta)
^^^^^
from /Users/andrei/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/anthropic-0.3.0/lib/anthropic/http.rb:102:in `_handle_message_type'
from /Users/andrei/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/anthropic-0.3.0/lib/anthropic/http.rb:81:in `block (2 levels) in to_json_stream'
Looks like there's an issue in the anthropic gem and there's a fix in a PR waiting to be merged: https://github.com/alexrudall/anthropic/pull/25
Thanks @andreibondarev - released in anthropic v0.3.1