Class: Ollama::Commands::Chat
- Inherits:
-
Object
- Object
- Ollama::Commands::Chat
- Includes:
- DTO
- Defined in:
- lib/ollama/commands/chat.rb
Overview
A command class that represents the chat API endpoint for Ollama.
This class is used to interact with the Ollama API’s chat endpoint, which generates conversational responses using a specified model. It inherits from the base command structure and provides the necessary functionality to execute chat requests for interactive conversations with language models.
Instance Attribute Summary collapse
-
#client ⇒ Object
writeonly
The client attribute writer allows setting the client instance associated with the object.
-
#format ⇒ String?
readonly
The format attribute reader returns the response format associated with the object.
-
#keep_alive ⇒ String?
readonly
The keep_alive attribute reader returns the keep-alive duration associated with the object.
-
#messages ⇒ Array<Ollama::Message>?
readonly
The messages attribute reader returns the conversation history associated with the object.
-
#model ⇒ String
readonly
The model attribute reader returns the model name associated with the object.
-
#options ⇒ Ollama::Options?
readonly
The options attribute reader returns the model configuration parameters associated with the object.
-
#stream ⇒ TrueClass, ...
readonly
The stream attribute reader returns the streaming behavior setting associated with the object.
-
#think ⇒ Boolean, ...
readonly
The think attribute reader returns the thinking mode setting associated with the object.
-
#tools ⇒ Array<Ollama::Tool>?
readonly
The tools attribute reader returns the available tools associated with the object.
Class Method Summary collapse
-
.path ⇒ String
The path method returns the API endpoint path for chat requests.
Instance Method Summary collapse
-
#initialize(model:, messages:, tools: nil, format: nil, options: nil, stream: nil, keep_alive: nil, think: nil) ⇒ Chat
constructor
The initialize method sets up a new instance with streaming behavior.
-
#perform(handler) ⇒ self
The perform method executes a command request using the specified handler.
Methods included from DTO
#==, #as_array, #as_array_of_hashes, #as_hash, #as_json, #empty?, #to_json
Constructor Details
#initialize(model:, messages:, tools: nil, format: nil, options: nil, stream: nil, keep_alive: nil, think: nil) ⇒ Chat
The initialize method sets up a new instance with streaming behavior.
This method is responsible for initializing a new object instance and configuring it with parameters required for chat interactions. It sets up the model, conversation messages, tools, format, options, streaming behavior, keep-alive duration, and thinking mode.
44 45 46 47 48 |
# File 'lib/ollama/commands/chat.rb', line 44 def initialize(model:, messages:, tools: nil, format: nil, options: nil, stream: nil, keep_alive: nil, think: nil) @model, @messages, @tools, @format, @options, @stream, @keep_alive, @think = model, as_array_of_hashes(), as_array_of_hashes(tools), format, , stream, keep_alive, think end |
Instance Attribute Details
#client=(value) ⇒ Object (writeonly)
The client attribute writer allows setting the client instance associated with the object.
This method assigns the client that will be used to perform requests and handle responses for this command. It is typically called internally when a command is executed through a client instance.
100 101 102 |
# File 'lib/ollama/commands/chat.rb', line 100 def client=(value) @client = value end |
#format ⇒ String? (readonly)
The format attribute reader returns the response format associated with the object.
68 69 70 |
# File 'lib/ollama/commands/chat.rb', line 68 def format @format end |
#keep_alive ⇒ String? (readonly)
The keep_alive attribute reader returns the keep-alive duration associated with the object.
85 86 87 |
# File 'lib/ollama/commands/chat.rb', line 85 def keep_alive @keep_alive end |
#messages ⇒ Array<Ollama::Message>? (readonly)
The messages attribute reader returns the conversation history associated with the object.
58 59 60 |
# File 'lib/ollama/commands/chat.rb', line 58 def @messages end |
#model ⇒ String (readonly)
The model attribute reader returns the model name associated with the object.
53 54 55 |
# File 'lib/ollama/commands/chat.rb', line 53 def model @model end |
#options ⇒ Ollama::Options? (readonly)
The options attribute reader returns the model configuration parameters associated with the object.
73 74 75 |
# File 'lib/ollama/commands/chat.rb', line 73 def @options end |
#stream ⇒ TrueClass, ... (readonly)
The stream attribute reader returns the streaming behavior setting associated with the object.
80 81 82 |
# File 'lib/ollama/commands/chat.rb', line 80 def stream @stream end |
#think ⇒ Boolean, ... (readonly)
The think attribute reader returns the thinking mode setting associated with the object.
90 91 92 |
# File 'lib/ollama/commands/chat.rb', line 90 def think @think end |
#tools ⇒ Array<Ollama::Tool>? (readonly)
The tools attribute reader returns the available tools associated with the object.
63 64 65 |
# File 'lib/ollama/commands/chat.rb', line 63 def tools @tools end |
Class Method Details
.path ⇒ String
The path method returns the API endpoint path for chat requests.
This class method provides the specific URL path used to interact with the Ollama API’s chat endpoint. It is utilized internally by the command structure to determine the correct API route for conversational interactions.
24 25 26 |
# File 'lib/ollama/commands/chat.rb', line 24 def self.path '/api/chat' end |
Instance Method Details
#perform(handler) ⇒ self
The perform method executes a command request using the specified handler.
This method initiates a POST request to the Ollama API’s chat endpoint, utilizing the client instance to send the request and process responses through the provided handler. It handles both streaming and non-streaming scenarios based on the command’s configuration.
responses
113 114 115 |
# File 'lib/ollama/commands/chat.rb', line 113 def perform(handler) @client.request(method: :post, path: self.class.path, body: to_json, stream:, handler:) end |