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.
| 43 44 45 46 47 | # File 'lib/ollama/commands/chat.rb', line 43 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.
| 99 100 101 | # File 'lib/ollama/commands/chat.rb', line 99 def client=(value) @client = value end | 
#format ⇒ String? (readonly)
The format attribute reader returns the response format associated with the object.
| 67 68 69 | # File 'lib/ollama/commands/chat.rb', line 67 def format @format end | 
#keep_alive ⇒ String? (readonly)
The keep_alive attribute reader returns the keep-alive duration associated with the object.
| 84 85 86 | # File 'lib/ollama/commands/chat.rb', line 84 def keep_alive @keep_alive end | 
#messages ⇒ Array<Ollama::Message>? (readonly)
The messages attribute reader returns the conversation history associated with the object.
| 57 58 59 | # File 'lib/ollama/commands/chat.rb', line 57 def @messages end | 
#model ⇒ String (readonly)
The model attribute reader returns the model name associated with the object.
| 52 53 54 | # File 'lib/ollama/commands/chat.rb', line 52 def model @model end | 
#options ⇒ Ollama::Options? (readonly)
The options attribute reader returns the model configuration parameters associated with the object.
| 72 73 74 | # File 'lib/ollama/commands/chat.rb', line 72 def @options end | 
#stream ⇒ TrueClass, ... (readonly)
The stream attribute reader returns the streaming behavior setting associated with the object.
| 79 80 81 | # File 'lib/ollama/commands/chat.rb', line 79 def stream @stream end | 
#think ⇒ Boolean? (readonly)
The think attribute reader returns the thinking mode setting associated with the object.
| 89 90 91 | # File 'lib/ollama/commands/chat.rb', line 89 def think @think end | 
#tools ⇒ Array<Ollama::Tool>? (readonly)
The tools attribute reader returns the available tools associated with the object.
| 62 63 64 | # File 'lib/ollama/commands/chat.rb', line 62 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 |