Class: LLM::Google::RequestAdapter::Completion
- Inherits:
-
Object
- Object
- LLM::Google::RequestAdapter::Completion
- Defined in:
- lib/llm/providers/google/request_adapter/completion.rb
Instance Method Summary collapse
-
#adapt ⇒ Hash
Adapts the message for the Gemini chat completions API.
- #adapt_content(content) ⇒ Object
- #adapt_function_response(value) ⇒ Object
- #adapt_object(object) ⇒ Object
- #adapt_remote_file(file) ⇒ Object
- #content ⇒ Object
-
#initialize(message) ⇒ Completion
constructor
A new instance of Completion.
- #message ⇒ Object
- #prompt_error!(object) ⇒ Object
Constructor Details
#initialize(message) ⇒ Completion
Returns a new instance of Completion.
10 11 12 |
# File 'lib/llm/providers/google/request_adapter/completion.rb', line 10 def initialize() @message = end |
Instance Method Details
#adapt ⇒ Hash
Adapts the message for the Gemini chat completions API
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/llm/providers/google/request_adapter/completion.rb', line 17 def adapt catch(:abort) do if Hash === {role: [:role], parts: adapt_content([:content])} elsif .tool_call? {role: .role, parts: .extra.original_tool_calls} else {role: .role, parts: adapt_content(.content)} end end end |
#adapt_content(content) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/llm/providers/google/request_adapter/completion.rb', line 29 def adapt_content(content) case content when Array content.empty? ? throw(:abort, nil) : content.flat_map { adapt_content(_1) } when String [{text: content}] when LLM::Response adapt_remote_file(content) when LLM::Message adapt_content(content.content) when LLM::Function::Return [{functionResponse: {name: content.name, response: adapt_function_response(content.value)}}] when LLM::Object adapt_object(content) else prompt_error!(content) end end |
#adapt_function_response(value) ⇒ Object
67 68 69 |
# File 'lib/llm/providers/google/request_adapter/completion.rb', line 67 def adapt_function_response(value) Hash === value ? value : {result: value} end |
#adapt_object(object) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/llm/providers/google/request_adapter/completion.rb', line 48 def adapt_object(object) case object.kind when :image_url [{file_data: {file_uri: object.value.to_s}}] when :local_file file = object.value [{inline_data: {mime_type: file.mime_type, data: file.to_b64}}] when :remote_file adapt_remote_file(object.value) else prompt_error!(object) end end |
#adapt_remote_file(file) ⇒ Object
62 63 64 65 |
# File 'lib/llm/providers/google/request_adapter/completion.rb', line 62 def adapt_remote_file(file) return prompt_error!(file) unless file.file? [{file_data: {mime_type: file.mime_type, file_uri: file.uri}}] end |
#content ⇒ Object
82 |
# File 'lib/llm/providers/google/request_adapter/completion.rb', line 82 def content = .content |
#message ⇒ Object
81 |
# File 'lib/llm/providers/google/request_adapter/completion.rb', line 81 def = @message |
#prompt_error!(object) ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/llm/providers/google/request_adapter/completion.rb', line 71 def prompt_error!(object) if LLM::Object === object raise LLM::PromptError, "The given LLM::Object with kind '#{content.kind}' is not " \ "supported by the Gemini API" else raise LLM::PromptError, "The given object (an instance of #{object.class}) " \ "is not supported by the Gemini API" end end |