-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathollama_client.py
More file actions
197 lines (173 loc) · 6.74 KB
/
ollama_client.py
File metadata and controls
197 lines (173 loc) · 6.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# Copyright (c) 2025 ByteDance Ltd. and/or its affiliates
# SPDX-License-Identifier: MIT
"""
Ollama API client wrapper with tool integration
"""
import json
import uuid
from typing import override
import openai
from ollama import chat as ollama_chat # pyright: ignore[reportUnknownVariableType]
from openai.types.responses import (
FunctionToolParam,
ResponseFunctionToolCallParam,
ResponseInputParam,
)
from openai.types.responses.response_input_param import FunctionCallOutput
from trae_agent.tools.base import Tool, ToolCall, ToolResult
from trae_agent.utils.config import ModelConfig
from trae_agent.utils.llm_clients.base_client import BaseLLMClient
from trae_agent.utils.llm_clients.llm_basics import LLMMessage, LLMResponse
from trae_agent.utils.llm_clients.retry_utils import retry_with
class OllamaClient(BaseLLMClient):
def __init__(self, model_config: ModelConfig):
super().__init__(model_config)
self.client: openai.OpenAI = openai.OpenAI(
# by default ollama doesn't require any api key. It should set to be "ollama".
api_key=self.api_key,
base_url=model_config.model_provider.base_url
if model_config.model_provider.base_url
else "http://localhost:11434/v1",
)
self.message_history: ResponseInputParam = []
@override
def set_chat_history(self, messages: list[LLMMessage]) -> None:
self.message_history = self.parse_messages(messages)
def _create_ollama_response(
self,
model_config: ModelConfig,
tool_schemas: list[FunctionToolParam] | None,
):
"""Create a response using Ollama API. This method will be decorated with retry logic."""
tools_param = None
if tool_schemas:
tools_param = [
{
"type": "function",
"function": {
"name": tool["name"],
"description": tool.get("description", ""),
"parameters": tool["parameters"],
},
}
for tool in tool_schemas
]
return ollama_chat(
messages=self.message_history,
model=model_config.model,
tools=tools_param,
)
@override
def chat(
self,
messages: list[LLMMessage],
model_config: ModelConfig,
tools: list[Tool] | None = None,
reuse_history: bool = True,
) -> LLMResponse:
"""
A rewritten version of ollama chan
"""
msgs: ResponseInputParam = self.parse_messages(messages)
tool_schemas = None
if tools:
tool_schemas = [
FunctionToolParam(
name=tool.name,
description=tool.description,
parameters=tool.get_input_schema(),
strict=True,
type="function",
)
for tool in tools
]
if reuse_history:
self.message_history = self.message_history + msgs
else:
self.message_history = msgs
# Apply retry decorator to the API call
retry_decorator = retry_with(
func=self._create_ollama_response,
provider_name="Ollama",
max_retries=model_config.max_retries,
)
response = retry_decorator(model_config, tool_schemas)
content = ""
tool_calls: list[ToolCall] = []
if response.message.tool_calls:
for tool in response.message.tool_calls:
tool_calls.append(
ToolCall(
call_id=self._id_generator(),
name=tool.function.name,
arguments=dict(tool.function.arguments),
id=self._id_generator(),
)
)
else:
# consider response is not a tool call
content = str(response.message.content)
llm_response = LLMResponse(
content=content,
usage=None,
model=model_config.model,
finish_reason=None, # seems can't get finish reason will check docs soon
tool_calls=tool_calls if len(tool_calls) > 0 else None,
)
if self.trajectory_recorder:
self.trajectory_recorder.record_llm_interaction(
messages=messages,
response=llm_response,
provider="ollama",
model=model_config.model,
tools=tools,
)
return llm_response
def parse_messages(self, messages: list[LLMMessage]) -> ResponseInputParam:
"""
Ollama parse messages should be compatible with openai handling
"""
openai_messages: ResponseInputParam = []
for msg in messages:
if msg.tool_result:
openai_messages.append(self.parse_tool_call_result(msg.tool_result))
elif msg.tool_call:
openai_messages.append(self.parse_tool_call(msg.tool_call))
else:
if not msg.content:
raise ValueError("Message content is required")
if msg.role == "system":
openai_messages.append({"role": "system", "content": msg.content})
elif msg.role == "user":
openai_messages.append({"role": "user", "content": msg.content})
elif msg.role == "assistant":
openai_messages.append({"role": "assistant", "content": msg.content})
else:
raise ValueError(f"Invalid message role: {msg.role}")
return openai_messages
def parse_tool_call(self, tool_call: ToolCall) -> ResponseFunctionToolCallParam:
"""Parse the tool call from the LLM response."""
return ResponseFunctionToolCallParam(
call_id=tool_call.call_id,
name=tool_call.name,
arguments=json.dumps(tool_call.arguments),
type="function_call",
)
def parse_tool_call_result(self, tool_call_result: ToolResult) -> FunctionCallOutput:
"""Parse the tool call result from the LLM response."""
result: str = ""
if tool_call_result.result:
result = result + tool_call_result.result + "\n"
if tool_call_result.error:
result += tool_call_result.error
result = result.strip()
return FunctionCallOutput(
role="function",
call_id=tool_call_result.call_id,
id=tool_call_result.id,
output=result,
type="function_call_output",
)
def _id_generator(self) -> str:
"""Generate a random ID string"""
return str(uuid.uuid4())