-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLlm.py
More file actions
291 lines (223 loc) · 9.17 KB
/
Llm.py
File metadata and controls
291 lines (223 loc) · 9.17 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
"""
Llm Function Script
"""
from ollama import Client
from ollama._types import ResponseError
import json
import conf_module
from web_search import browse
# Constant:
LINK = conf_module.load_conf('LINK')
DEFAULT_MODEL = conf_module.load_conf('DEFAULT_MODEL')
# Base Value (if hosting):
# os.environ["OLLAMA_MAX_LOADED_MODELS"] = "3"
# os.environ["OLLAMA_KEEP_ALIVE"] = "-1"
# os.environ["OLLAMA_FLASH_ATTENTION"] = "true"
ollama_client = Client(
host=LINK
)
tools = [{
'type': 'function',
'function': {
'name': 'browse',
'description': 'Get online information',
'parameters': {
'type': 'object',
'properties': {
'reply': {
'type': 'string',
'description': 'Respond to the user about what you will browse.'
},
'query': {
'type': 'string',
'description': 'The browse query or a direct link.'
}
},
'required': ['reply', 'query']
}
}
},
]
context = []
logs = []
def load(model: str = DEFAULT_MODEL):
"""Infer with a model in streaming to load it. Returns when the model output the first token."""
if model is None:
model = DEFAULT_MODEL
# start a streaming chat
stream = ollama_client.chat(
model=model,
messages=[{'role': 'user', 'content': 'Hi'}],
stream=True
)
try:
first = next(stream) # Fails if no stream
stream.close()
except StopIteration:
pass
return "model loaded"
def get_model_capabilities(model: str = DEFAULT_MODEL):
info = ollama_client._request_raw("POST", "/api/show",json={"name":model}).json()
capabilities = info["capabilities"]
return capabilities
def summarize_chat(num: int = 10, model: str = DEFAULT_MODEL):
"""Summarize the first `num` messages (after system prompt) and replace them with a summary."""
if len(context) <= num + 1:
print("Not enough messages to summarize.")
return
system_msg = context[0]
to_summarize = context[1:num+1]
keep_rest = context[num+1:]
summarization_prompt = (
"Summarize the following conversation in a concise but clear way. "
"Keep important details, but remove fluff. Make it short enough to fit in one message.\n\n"
f"{json.dumps(to_summarize, ensure_ascii=False, indent=2)}"
)
response = ollama_client.chat(
model=model,
messages=[
{"role": "system", "content": "You are a summarizer. Do not tell what you're about to do, summarize only."},
{"role": "user", "content": summarization_prompt}
],
think=False
)
summary_text = response["message"]["content"].strip()
# Build new summarized context
summarized_msg = {
"role": "system",
"content": f"(Summary of earlier conversation)\n{summary_text}"
}
new_context = [system_msg, summarized_msg] + keep_rest
context.clear()
context.extend(new_context)
with open("/home/neo_luigi/Documents/Python_Llm/context.json", "w", encoding="utf-8") as f:
json.dump(context, f, ensure_ascii=False, indent=2)
def get_tool_type(tool_call):
"""Run the proper tool called and output its result."""
tool_name = tool_call['function'].get('name')
reply = tool_call['function']['arguments'].get('reply')
if reply:
save_context(reply, 'assistant')
send_message(reply)
if tool_name == 'browse':
query = tool_call['function']['arguments'].get('query')
return browse(str(query))
else:
error_msg = f"Unknown tool type: {tool_name}"
print(error_msg)
return error_msg
def save_context(content, role='user', image_path: list = None):
if image_path:
context.append({
'role': role,
'content': content,
'images': image_path
})
else:
context.append({
'role': role,
'content': content
})
try:
data_str = json.dumps(context, ensure_ascii=False, indent=2)
json.loads(data_str)
path = "context.json"
with open(path, "w", encoding="utf-8") as f:
f.write(data_str)
except Exception as e:
raise RuntimeError(f"Couldn't save context: {e}")
def chat(content: str, role: str = 'user', model: str = DEFAULT_MODEL, thinking: str = 'auto', streaming: bool = False, num_retry_fail: int = 5):
"""Generate a reply from the LLM with optional multimodal tool calling.
Args:
content (str): The prompt given to the model.
role (str, optional): The role to label the message with.
Options: 'user', 'assistant', 'system'. Defaults to 'user'.
model (str, optional): The model to use for generation. Defaults to 'qwen3:8b'.
thinking (str, optional): Whether to enable tool calling.
Options: 'auto', 'true', 'false'. Defaults to 'auto'.
streaming (str, optional): The reply type, either wait till full reply, or show progress over each token. Defaults to 'False'.
Returns:
str: The generated reply from the model.
"""
if num_retry_fail >= 0:
try:
if model == None:
model = DEFAULT_MODEL
generate = True
tool_calling = False
save_context(content, role=role)
while generate == True:
if thinking.lower() == 'auto':
pass
elif thinking.lower() == 'true':
tool_calling = True
elif thinking.lower() == 'false':
tool_calling = False
if streaming:
response = ollama_client.chat(
model=model,
messages=context,
tools=tools,
think=tool_calling,
stream=True,
)
buffer = ""
response_chunk = []
for part in response:
response_chunk.append(part.model_dump())
if 'message' in part and 'tool_calls' in part['message']:
for tool_call in part['message']['tool_calls']:
tool_result = get_tool_type(tool_call)
save_context(str(tool_result), 'tool')
with open("context.json", "w", encoding="utf-8") as f:
json.dump(context, f, ensure_ascii=False, indent=2)
generate = True
tool_calling = True
elif 'message' in part and part['message'].get('content'):
buffer += part['message']['content']
yield(part['message']['content'])
generate = False
tool_calling = False
if not generate:
yield("")
if buffer.strip():
save_context(buffer, role='assistant')
logs.append(response_chunk)
with open("logs.json", "w", encoding="utf-8") as f:
json.dump(logs, f, ensure_ascii=False, indent=2)
else:
response = ollama_client.chat(
model=model,
messages=context,
tools=tools,
think=tool_calling,
stream=False,
)
logs.append(response.model_dump(mode='json'))
with open("logs.json", "w", encoding="utf-8") as f:
json.dump(logs, f, ensure_ascii=False, indent=2)
if response['message'].get('content'):
save_context(response['message']['content'], 'assistant')
yield(response['message']['content'])
elif 'tool_calls' in response['message']:
for tool_call in response['message']['tool_calls']:
tool_result = get_tool_type(tool_call)
save_context(str(tool_result), 'tool')
with open("context.json", "w", encoding="utf-8") as f:
json.dump(context, f, ensure_ascii=False, indent=2)
generate = True
tool_calling = True
except ResponseError as e:
if e.status_code == "524":
num_retry_fail -= 1
else:
return("couldn't generate the message. Please retry with streaming activated `/stream True`.")
def get_model_list():
"""Return a list of all available models on the Ollama server."""
models_info = ollama_client.list()
return [model["model"] for model in models_info.get("models", [])]
def send_message(msg: str, flush: bool = False):
if flush:
print(msg, end="", flush=True)
else:
print(msg)