Skip to content

Extract URI parser compatibility logic #994

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
5 changes: 1 addition & 4 deletions lib/langchain/assistant/messages/ollama_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ class OllamaMessage < Base
def initialize(role:, content: nil, image_url: nil, tool_calls: [], tool_call_id: nil)
raise ArgumentError, "Role must be one of #{ROLES.join(", ")}" unless ROLES.include?(role)
raise ArgumentError, "Tool calls must be an array of hashes" unless tool_calls.is_a?(Array) && tool_calls.all? { |tool_call| tool_call.is_a?(Hash) }

# Ensure compatibility with `URI::RFC2396_PARSER` (Ruby 3.4+) and `URI::DEFAULT_PARSER` (<= 3.3).
uri_parser = defined?(URI::RFC2396_PARSER) ? URI::RFC2396_PARSER : URI::DEFAULT_PARSER
raise ArgumentError, "image_url must be a valid url" if image_url && !uri_parser.make_regexp.match?(image_url)
raise ArgumentError, "image_url must be a valid url" if image_url && !Langchain::Utils::UriParser.make_regexp.match?(image_url)

@role = role
# Some Tools return content as a JSON hence `.to_s`
Expand Down
5 changes: 1 addition & 4 deletions lib/langchain/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,7 @@ def load(&block)

def load_from_url
unescaped_url = URI.decode_www_form_component(@path)

# Ensure compatibility with `URI::RFC2396_PARSER` (Ruby 3.4+) and `URI::DEFAULT_PARSER` (<= 3.3).
uri_parser = defined?(URI::RFC2396_PARSER) ? URI::RFC2396_PARSER : URI::DEFAULT_PARSER
escaped_url = uri_parser.escape(unescaped_url)
escaped_url = Langchain::Utils::UriParser.escape(unescaped_url)

URI.parse(escaped_url).open
end
Expand Down
5 changes: 1 addition & 4 deletions lib/langchain/processors/eml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,9 @@ def extract_text_content(mail)

# Clean and format the extracted content
def clean_content(content)
# Ensure compatibility with `URI::RFC2396_PARSER` (Ruby 3.4+) and `URI::DEFAULT_PARSER` (<= 3.3).
uri_parser = defined?(URI::RFC2396_PARSER) ? URI::RFC2396_PARSER : URI::DEFAULT_PARSER

content
.gsub(/\[cid:[^\]]+\]/, "") # Remove embedded image references
.gsub(uri_parser.make_regexp(%w[http https])) { |match| "<#{match}>" } # Format URLs
.gsub(Langchain::Utils::UriParser.make_regexp(%w[http https])) { |match| "<#{match}>" } # Format URLs
.gsub(/\r\n?/, "\n") # Normalize line endings to Unix style
.gsub(/[\u200B-\u200D\uFEFF]/, "") # Remove zero width spaces and similar characters
.gsub(/<\/?[^>]+>/, "") # Remove any HTML tags that might have sneaked in
Expand Down
8 changes: 8 additions & 0 deletions lib/langchain/utils.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

module Langchain
module Utils
# Ensure compatibility with `URI::RFC2396_PARSER` (Ruby 3.4+) and `URI::DEFAULT_PARSER` (<= 3.3).
UriParser = defined?(URI::RFC2396_PARSER) ? URI::RFC2396_PARSER : URI::DEFAULT_PARSER
end
end