Class: Ollama::Providers::OpenAI
- Inherits:
-
Base
- Object
- Base
- Ollama::Providers::OpenAI
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
Instance Method Details
#chat_endpoint ⇒ Object
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_endpoint ⇒ Object
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
|
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)
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]
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
|
53
54
55
|
# File 'lib/ollama/providers/openai.rb', line 53
def format_embeddings_request(params)
params
end
|
48
49
50
51
|
# File 'lib/ollama/providers/openai.rb', line 48
def format_generate_request(params)
format_chat_request(params)
end
|
#generate_endpoint ⇒ Object
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_endpoint ⇒ Object
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")
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")
{
"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")
{
"models" => response_data["data"].map do |m|
{
"name" => m["id"],
"model" => m["id"],
"details" => { "family" => m["owned_by"] }
}
end
}
end
|