Class: Ollama::Providers::OpenAI

Inherits:
Base
  • Object
show all
Defined in:
lib/ollama/providers/openai.rb

Overview

OpenAI-compatible API provider (e.g. llama.cpp, vLLM, OpenAI)

Instance Attribute Summary

Attributes inherited from Base

#config, #transport

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Ollama::Providers::Base

Instance Method Details

#chat_endpointObject



9
10
11
12
# File 'lib/ollama/providers/openai.rb', line 9

def chat_endpoint
  path = config.base_url.end_with?("/") ? "chat/completions" : "/chat/completions"
  URI("#{config.base_url}#{path}")
end

#embeddings_endpointObject



19
20
21
22
# File 'lib/ollama/providers/openai.rb', line 19

def embeddings_endpoint
  path = config.base_url.end_with?("/") ? "embeddings" : "/embeddings"
  URI("#{config.base_url}#{path}")
end

#format_chat_request(params) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ollama/providers/openai.rb', line 29

def format_chat_request(params)
  # Flatten options for OpenAI
  options = params.delete(:options) || {}
  params[:temperature] = options[:temperature] if options[:temperature]
  params[:top_p] = options[:top_p] if options[:top_p]
  params[:max_tokens] = options[:num_ctx] if options[:num_ctx]

  # Ollama 'format' can be 'json' or a schema.
  # OpenAI uses 'response_format'
  format = params.delete(:format)
  if format == "json"
    params[:response_format] = { type: "json_object" }
  elsif format.is_a?(Hash)
    params[:response_format] = { type: "json_schema", json_schema: format }
  end

  params
end

#format_embeddings_request(params) ⇒ Object



53
54
55
# File 'lib/ollama/providers/openai.rb', line 53

def format_embeddings_request(params)
  params
end

#format_generate_request(params) ⇒ Object



48
49
50
51
# File 'lib/ollama/providers/openai.rb', line 48

def format_generate_request(params)
  # OpenAI completions API is similar to chat but uses 'prompt'
  format_chat_request(params)
end

#generate_endpointObject



14
15
16
17
# File 'lib/ollama/providers/openai.rb', line 14

def generate_endpoint
  path = config.base_url.end_with?("/") ? "completions" : "/completions"
  URI("#{config.base_url}#{path}")
end

#models_endpointObject



24
25
26
27
# File 'lib/ollama/providers/openai.rb', line 24

def models_endpoint
  path = config.base_url.end_with?("/") ? "models" : "/models"
  URI("#{config.base_url}#{path}")
end

#normalize_chat_response(response_data) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ollama/providers/openai.rb', line 57

def normalize_chat_response(response_data)
  return response_data unless response_data.is_a?(Hash) && response_data.key?("choices")

  # Translate OpenAI format to Ollama format for Ollama::Response
  choice = response_data["choices"][0]
  message = choice["message"]

  {
    "model" => response_data["model"],
    "message" => {
      "role" => message["role"],
      "content" => message["content"],
      "tool_calls" => translate_tool_calls(message["tool_calls"])
    },
    "done" => choice["finish_reason"] == "stop",
    "done_reason" => choice["finish_reason"],
    "usage" => response_data["usage"]
  }
end

#normalize_embeddings_response(response_data) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/ollama/providers/openai.rb', line 89

def normalize_embeddings_response(response_data)
  return response_data unless response_data.is_a?(Hash) && response_data.key?("data")

  # OpenAI format: {"data": [{"embedding": [...]}, ...]}
  # Ollama format: {"embeddings": [[...], ...]}
  {
    "model" => response_data["model"],
    "embeddings" => response_data["data"].map { |d| d["embedding"] }
  }
end

#normalize_generate_response(response_data) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ollama/providers/openai.rb', line 77

def normalize_generate_response(response_data)
  return response_data unless response_data.is_a?(Hash) && response_data.key?("choices")

  choice = response_data["choices"][0]
  {
    "model" => response_data["model"],
    "response" => choice["text"],
    "done" => choice["finish_reason"] == "stop",
    "usage" => response_data["usage"]
  }
end

#normalize_models_response(response_data) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/ollama/providers/openai.rb', line 100

def normalize_models_response(response_data)
  return response_data unless response_data.is_a?(Hash) && response_data.key?("data")

  # OpenAI format: {"data": [{"id": "model-name", ...}, ...]}
  # Ollama format: {"models": [{"name": "model-name", ...}, ...]}
  {
    "models" => response_data["data"].map do |m|
      {
        "name" => m["id"],
        "model" => m["id"],
        "details" => { "family" => m["owned_by"] }
      }
    end
  }
end