Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions lib/langchain/llm/ollama.rb
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,28 @@ def auth_headers
end

def json_responses_chunk_handler(&block)
incomplete_chunk_line = nil
proc do |chunk, _size|
chunk.split("\n").each do |chunk_line|
parsed_chunk = JSON.parse(chunk_line)
block.call(parsed_chunk)
if incomplete_chunk_line
chunk_line = incomplete_chunk_line + chunk_line
incomplete_chunk_line = nil
end

parsed_chunk = begin
JSON.parse(chunk_line)

# In some instance the chunk exceeds the buffer size and the JSON parser fails
rescue JSON::ParserError
if chunk_line.end_with?("}")
raise
else
incomplete_chunk_line = chunk_line
nil
end
end

block.call(parsed_chunk) unless parsed_chunk.nil?
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions lib/langchain/tool/weather.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ def fetch_current_weather(city:, state_code:, country_code:, units:)
return tool_response(content: location_response) if location_response.is_a?(String) # Error occurred

location = location_response.first
if !location && country_code.nil?
return tool_response(content: "Location not found. Please provide a valid country code too and try again.")
else
return tool_response(content: "Location not found") unless location
end
return tool_response(content: "Location not found") unless location

params = params.merge(lat: location["lat"], lon: location["lon"]).except(:q)
Expand Down
2 changes: 1 addition & 1 deletion lib/langchain/version.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

module Langchain
VERSION = "0.19.5"
VERSION = "0.19.5-fix"
Version = VERSION
end
2 changes: 1 addition & 1 deletion spec/lib/langchain/tool/weather_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
it "returns an error message" do
result = weather_tool.get_current_weather(city: city, state_code: state_code)
expect(result).to be_a(Langchain::ToolResponse)
expect(result.content).to eq("Location not found")
expect(result.content).to start_with("Location not found")
end
end

Expand Down