Module: Raif::Concerns::LlmResponseParsing
- Extended by:
- ActiveSupport::Concern
- Included in:
- Raif::Conversation, ModelCompletion, Task
- Defined in:
- app/models/raif/concerns/llm_response_parsing.rb
Constant Summary collapse
- ASCII_CONTROL_CHARS =
/[\x00-\x1f\x7f]/
Instance Method Summary collapse
- #parse_html_response ⇒ Object
- #parse_json_response ⇒ Object
-
#parsed_response(force_reparse: false) ⇒ Object
Parses the response from the LLM into a structured format, based on the response_format.
Instance Method Details
#parse_html_response ⇒ Object
61 62 63 64 65 66 67 68 69 70 |
# File 'app/models/raif/concerns/llm_response_parsing.rb', line 61 def parse_html_response html = raw_response.strip.gsub("```html", "").chomp("```") html_with_converted_links = Raif::Utils::HtmlFragmentProcessor.convert_markdown_links_to_html(html) Raif::Utils::HtmlFragmentProcessor.clean_html_fragment( html_with_converted_links, allowed_tags: , allowed_attributes: allowed_attributes ) end |
#parse_json_response ⇒ Object
53 54 55 56 57 58 59 |
# File 'app/models/raif/concerns/llm_response_parsing.rb', line 53 def parse_json_response json = raw_response.gsub(/#{ASCII_CONTROL_CHARS}|^```json|```$/, "").strip raise JSON::ParserError, "Invalid JSON" if json.blank? JSON.parse(json) end |
#parsed_response(force_reparse: false) ⇒ Object
Parses the response from the LLM into a structured format, based on the response_format. If the response format is JSON, it will be parsed using JSON.parse. If the response format is HTML, it will be sanitized via ActionController::Base.helpers.sanitize.
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'app/models/raif/concerns/llm_response_parsing.rb', line 40 def parsed_response(force_reparse: false) return if raw_response.blank? return @parsed_response if @parsed_response.present? && !force_reparse @parsed_response = if response_format_json? parse_json_response elsif response_format_html? parse_html_response else raw_response.strip end end |