@@ -1,29 +1,47 @@
11 package provider
22
33 import (
4+ "bytes"
45 "context"
56 "encoding/json"
67 "errors"
8+ "fmt"
79 "io"
10+ "net/http"
11+ "strings"
812
913 openai "github.com/sashabaranov/go-openai"
14+ "golang.org/x/time/rate"
1015 )
1116
1217 // OllamaProvider implements the Provider interface for Ollama.
1318 type OllamaProvider struct {
14- client *openai.Client
15- model string
19+ client *openai.Client
20+ baseURL string
21+ httpClient *http.Client
22+ model string
23+ temperature float64
24+ limiter *rate.Limiter
1625 }
1726
1827 // NewOllama creates a new Ollama provider.
1928 // Ollama exposes an OpenAI-compatible API at /v1.
2029 func NewOllama(endpoint, model string) *OllamaProvider {
30+ return NewOllamaWithTemp(endpoint, model, 0.7, nil)
31+}
32+
33+func NewOllamaWithTemp(endpoint, model string, temperature float64, limiter *rate.Limiter) *OllamaProvider {
2134 config := openai.DefaultConfig("")
22- config.BaseURL = endpoint + "/v1"
35+ baseURL := strings.TrimRight(endpoint, "/") + "/v1"
36+ config.BaseURL = baseURL
2337
2438 return &OllamaProvider{
25- client: openai.NewClientWithConfig(config),
26- model: model,
39+ client: openai.NewClientWithConfig(config),
40+ baseURL: baseURL,
41+ httpClient: &http.Client{},
42+ model: model,
43+ temperature: temperature,
44+ limiter: limiter,
2745 }
2846 }
2947
@@ -34,9 +52,16 @@ func (p *OllamaProvider) Name() string {
3452
3553 // Chat sends messages and returns the complete response.
3654 func (p *OllamaProvider) Chat(ctx context.Context, messages []Message) (string, error) {
37- resp, err := p.client.CreateChatCompletion(ctx, openai.ChatCompletionRequest{
38- Model: p.model,
39- Messages: toOpenAIMessages(messages),
55+ if p.limiter != nil {
56+ if err := p.limiter.Wait(ctx); err != nil {
57+ return "", err
58+ }
59+ }
60+
61+ resp, err := p.createChatCompletion(ctx, ollamaChatRequest{
62+ Model: p.model,
63+ Messages: toOllamaMessages(messages),
64+ Temperature: float32(p.temperature),
4065 })
4166 if err != nil {
4267 return "", err
@@ -51,34 +76,17 @@ func (p *OllamaProvider) Chat(ctx context.Context, messages []Message) (string,
5176
5277 // ChatWithTools sends messages with available tools and returns response with potential tool calls.
5378 func (p *OllamaProvider) ChatWithTools(ctx context.Context, messages []Message, tools []Tool) (*ChatResponse, error) {
54- // Convert tools to OpenAI format
55- openaiTools := make([]openai.Tool, len(tools))
56- for i, t := range tools {
57- var params map[string]interface{}
58- if len(t.Parameters) > 0 {
59- json.Unmarshal(t.Parameters, ¶ms)
60- }
61- if params == nil {
62- params = map[string]interface{}{
63- "type": "object",
64- "properties": map[string]interface{}{},
65- }
66- }
67-
68- openaiTools[i] = openai.Tool{
69- Type: openai.ToolTypeFunction,
70- Function: &openai.FunctionDefinition{
71- Name: t.Name,
72- Description: t.Description,
73- Parameters: params,
74- },
79+ if p.limiter != nil {
80+ if err := p.limiter.Wait(ctx); err != nil {
81+ return nil, err
7582 }
7683 }
7784
78- resp, err := p.client.CreateChatCompletion(ctx, openai.ChatCompletionRequest{
79- Model: p.model,
80- Messages: toOpenAIMessages(messages),
81- Tools: openaiTools,
85+ resp, err := p.createChatCompletion(ctx, ollamaChatRequest{
86+ Model: p.model,
87+ Messages: toOllamaMessages(messages),
88+ Tools: toOllamaTools(tools),
89+ Temperature: float32(p.temperature),
8290 })
8391 if err != nil {
8492 return nil, err
@@ -90,7 +98,8 @@ func (p *OllamaProvider) ChatWithTools(ctx context.Context, messages []Message,
9098
9199 choice := resp.Choices[0]
92100 result := &ChatResponse{
93- Content: choice.Message.Content,
101+ Content: choice.Message.Content,
102+ Reasoning: choice.Message.reasoning(),
94103 }
95104
96105 // Extract tool calls if present
@@ -108,11 +117,120 @@ func (p *OllamaProvider) ChatWithTools(ctx context.Context, messages []Message,
108117 return result, nil
109118 }
110119
120+type chatCompletionResponse struct {
121+ Choices []chatCompletionChoice `json:"choices"`
122+}
123+
124+type chatCompletionChoice struct {
125+ Message chatCompletionMessage `json:"message"`
126+}
127+
128+type chatCompletionMessage struct {
129+ Role string `json:"role"`
130+ Content string `json:"content"`
131+ Reasoning string `json:"reasoning,omitempty"`
132+ ReasoningContent string `json:"reasoning_content,omitempty"`
133+ ToolCalls []chatCompletionToolCall `json:"tool_calls,omitempty"`
134+}
135+
136+type chatCompletionToolCall struct {
137+ ID string `json:"id"`
138+ Type string `json:"type"`
139+ Function chatCompletionFunction `json:"function"`
140+}
141+
142+type chatCompletionFunction struct {
143+ Name string `json:"name"`
144+ Arguments string `json:"arguments"`
145+}
146+
147+type ollamaChatRequest struct {
148+ Model string `json:"model"`
149+ Messages []ollamaReqMessage `json:"messages"`
150+ Tools []ollamaReqTool `json:"tools,omitempty"`
151+ Temperature float32 `json:"temperature,omitempty"`
152+}
153+
154+type ollamaReqMessage struct {
155+ Role string `json:"role"`
156+ Content string `json:"content"`
157+ ToolCallID string `json:"tool_call_id,omitempty"`
158+ ToolCalls []ollamaReqToolCall `json:"tool_calls,omitempty"`
159+}
160+
161+type ollamaReqTool struct {
162+ Type string `json:"type"`
163+ Function ollamaReqFunction `json:"function"`
164+}
165+
166+type ollamaReqFunction struct {
167+ Name string `json:"name"`
168+ Description string `json:"description"`
169+ Parameters map[string]interface{} `json:"parameters"`
170+}
171+
172+type ollamaReqToolCall struct {
173+ ID string `json:"id"`
174+ Type string `json:"type"`
175+ Function ollamaReqFuncCall `json:"function"`
176+}
177+
178+type ollamaReqFuncCall struct {
179+ Name string `json:"name"`
180+ Arguments string `json:"arguments"`
181+}
182+
183+func (m chatCompletionMessage) reasoning() string {
184+ if m.Reasoning != "" {
185+ return m.Reasoning
186+ }
187+ return m.ReasoningContent
188+}
189+
190+func (p *OllamaProvider) createChatCompletion(ctx context.Context, req ollamaChatRequest) (*chatCompletionResponse, error) {
191+ body, err := json.Marshal(req)
192+ if err != nil {
193+ return nil, err
194+ }
195+
196+ url := p.baseURL + "/chat/completions"
197+ httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body))
198+ if err != nil {
199+ return nil, err
200+ }
201+ httpReq.Header.Set("Content-Type", "application/json")
202+
203+ resp, err := p.httpClient.Do(httpReq)
204+ if err != nil {
205+ return nil, err
206+ }
207+ defer resp.Body.Close()
208+
209+ if resp.StatusCode < 200 || resp.StatusCode >= 300 {
210+ payload, _ := io.ReadAll(resp.Body)
211+ return nil, fmt.Errorf("chat completion status %d: %s", resp.StatusCode, strings.TrimSpace(string(payload)))
212+ }
213+
214+ var decoded chatCompletionResponse
215+ if err := json.NewDecoder(resp.Body).Decode(&decoded); err != nil {
216+ return nil, err
217+ }
218+
219+ return &decoded, nil
220+}
221+
111222 // Stream sends messages and returns a channel that streams response chunks.
112223 func (p *OllamaProvider) Stream(ctx context.Context, messages []Message) (<-chan StreamChunk, error) {
224+ if p.limiter != nil {
225+ if err := p.limiter.Wait(ctx); err != nil {
226+ return nil, err
227+ }
228+ }
229+
113230 stream, err := p.client.CreateChatCompletionStream(ctx, openai.ChatCompletionRequest{
114- Model: p.model,
115- Messages: toOpenAIMessages(messages),
231+ Model: p.model,
232+ Messages: toOpenAIMessages(messages),
233+ Temperature: float32(p.temperature),
116234 })
117235 if err != nil {
118236 return nil, err
@@ -175,3 +293,65 @@ func toOpenAIMessages(messages []Message) []openai.ChatCompletionMessage {
175293 }
176294 return result
177295 }
296+
297+func toOllamaMessages(messages []Message) []ollamaReqMessage {
298+ result := make([]ollamaReqMessage, len(messages))
299+ for i, m := range messages {
300+ msg := ollamaReqMessage{
301+ Role: m.Role,
302+ Content: m.Content,
303+ }
304+
305+ if m.ToolCallID != "" {
306+ msg.ToolCallID = m.ToolCallID
307+ }
308+
309+ if len(m.ToolCalls) > 0 {
310+ msg.ToolCalls = make([]ollamaReqToolCall, len(m.ToolCalls))
311+ for j, tc := range m.ToolCalls {
312+ msg.ToolCalls[j] = ollamaReqToolCall{
313+ ID: tc.ID,
314+ Type: "function",
315+ Function: ollamaReqFuncCall{
316+ Name: tc.Name,
317+ Arguments: string(tc.Arguments),
318+ },
319+ }
320+ }
321+ }
322+
323+ result[i] = msg
324+ }
325+ return result
326+}
327+
328+func toOllamaTools(tools []Tool) []ollamaReqTool {
329+ result := make([]ollamaReqTool, len(tools))
330+ for i, t := range tools {
331+ var params map[string]interface{}
332+ if len(t.Parameters) > 0 {
333+ if err := json.Unmarshal(t.Parameters, ¶ms); err != nil {
334+ params = map[string]interface{}{
335+ "type": "object",
336+ "properties": map[string]interface{}{},
337+ }
338+ }
339+ }
340+ if params == nil {
341+ params = map[string]interface{}{
342+ "type": "object",
343+ "properties": map[string]interface{}{},
344+ }
345+ }
346+
347+ result[i] = ollamaReqTool{
348+ Type: "function",
349+ Function: ollamaReqFunction{
350+ Name: t.Name,
351+ Description: t.Description,
352+ Parameters: params,
353+ },
354+ }
355+ }
356+ return result
357+}