-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanswer.py
More file actions
305 lines (251 loc) · 10.6 KB
/
Copy pathanswer.py
File metadata and controls
305 lines (251 loc) · 10.6 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import importlib
import configparser
import load
from colorama import Fore
import base64
import os
from typing import List, Optional, Dict, Any, Tuple
import time
import requests
import json
import re
from chatContent import ChatContent
# ================= 配置加载(对齐 Answer.cs 构造函数) =================
config = configparser.ConfigParser()
config.read('config.ini', encoding='utf-8')
def _cfg(key: str) -> str:
return config['general'][key]
modelName: str = _cfg('modelname')
serverUrl: str = _cfg('server_url')
isVisionModel: bool = _cfg('isvisionmodel').lower() == 'true'
maxImageCount: int = int(_cfg('maximagecount'))
remoteServerTimeout: int = int(_cfg('remote_server_timeout'))
forceOllamaAPI: bool = config.getboolean('general', 'forceollamaapi', fallback=False)
apiKey: str = _cfg('api_key')
# 系统提示从 config.ini 的 system 键读取(沿用旧版方式)
sysPmpt: str = config.get('general', 'system', fallback='')
useOllama = False
builtInLanguageModel = False
tinylm = None
# 后端选择(对齐 Answer.cs 构造函数)
if forceOllamaAPI:
useOllama = True
serverUrl+='/api/chat'
elif serverUrl.lower() == 'ollama':
serverUrl = 'http://localhost:11434/api/chat'
useOllama = True
elif serverUrl.lower() == 'builtin':
builtInLanguageModel = True
# 否则 server_url 是用户自定义的 base URL(如 http://192.168.1.100:8000/v1)
MAX_LENGTH = 2048 # 对齐 C#;非流式请求,未用于截断
# extra.json 中的字段会合并进请求体并覆盖同名键(对齐 C#)
extra: Dict[str, Any] = {}
try:
with open('extra.json', 'r', encoding='utf-8') as f:
extra = json.loads(f.read())
except Exception:
pass
# ================= 工具函数 =================
def _image_to_base64(path: str) -> str:
with open(path, 'rb') as f:
return base64.b64encode(f.read()).decode('utf-8')
def isTime(text: str) -> Tuple[bool, List[str]]:
"""对齐 Answer.cs 的 IsTime:校验小时 0-23、分钟 0-59"""
pattern1 = r'\(?([0-2]?[0-9]):([0-5][0-9])\)?'
pattern2 = r'\(?([0-2]?[0-9])\.([0-5][0-9])\)?'
valid_times: List[str] = []
for pattern in (pattern1, pattern2):
for m in re.finditer(pattern, text):
try:
h = int(m.group(1))
minute = int(m.group(2))
except ValueError:
continue
if 0 <= h <= 23 and 0 <= minute <= 59:
valid_times.append(f'{h:02d}:{minute:02d}')
return len(valid_times) > 0, valid_times
def _concatenate_text(text_list: List[ChatContent], images: List[str]) -> List[Dict[str, Any]]:
"""对齐 Answer.cs 的 ConcatenateText。
- 只收集同时出现在全局 images 列表(已按 MaxImageCount 截断)中的图片;
- 只有用户消息携带图片,assistant 消息只带文本;
- 既没有文本、也没有可发送的图片 → 整条消息跳过(纯图片消息必须保留);
- Ollama 用纯 base64 数组;OpenAI 兼容 API 用 data: URI 分段数组。
"""
messages: List[Dict[str, Any]] = []
for t in text_list:
image_b64: List[str] = [] # 纯 base64 —— Ollama 使用
image_data_urls: List[str] = [] # data: URI —— OpenAI 兼容 API 使用
for img in t.imagePaths:
if img not in images:
continue
b64 = _image_to_base64(img)
mime = 'image/png' if img.lower().endswith('.png') else 'image/jpeg'
image_b64.append(b64)
image_data_urls.append(f'data:{mime};base64,{b64}')
has_text = bool(t.text)
attach_images = (not t.ownByMyself) and len(image_b64) > 0
if not has_text and not attach_images:
continue
text = str(t) if has_text else ''
message: Dict[str, Any] = {
'role': 'assistant' if t.ownByMyself else 'user',
}
if useOllama:
# Ollama /api/chat 格式:images 是与 content 平级的【纯 base64】数组(不带 data: 前缀)
message['content'] = text
if attach_images:
message['images'] = image_b64
else:
# OpenAI 兼容格式:content 可以是字符串,也可以是分段数组
if not attach_images:
message['content'] = text
else:
parts: List[Any] = [{'type': 'text', 'text': text}]
for url in image_data_urls:
parts.append({'type': 'image_url', 'image_url': {'url': url}})
message['content'] = parts
messages.append(message)
return messages
# ================= 核心推理函数 =================
def getAnswer(text: List[ChatContent], systemPrompt: str = 'auto') -> Tuple[Optional[str], int]:
totalTokens = 0
if text is None or len(text) == 0:
return '', 0
# 内置模型(对齐 Answer.cs Builtin 分支:从后往前找第一条非空、非自己发的消息)
if builtInLanguageModel:
global tinylm
for t in reversed(text):
if not t.text or t.ownByMyself:
continue
if tinylm is None:
tinylm = importlib.import_module('TinyLangJaccard')
return tinylm.answer(t.text), 0
return '', 0
# 系统提示("auto" → config.ini 的 system;"" / "None" → 空;否则用传入值)
if systemPrompt == 'auto':
final_system_prompt = sysPmpt
elif systemPrompt == '' or systemPrompt == 'None':
final_system_prompt = ''
else:
final_system_prompt = systemPrompt
# 收集图片:从后往前,跳过自己的消息,直到 MaxImageCount(对齐 C#)
image_list: List[str] = []
if isVisionModel:
for t in reversed(text):
if not t.ownByMyself:
for img in t.imagePaths:
if os.path.exists(img):
image_list.append(img)
if len(image_list) >= maxImageCount:
break
else:
print(f'× 没有找到图片 {img}')
if len(image_list) >= maxImageCount:
break
# 构建 messages
messages: List[Dict[str, Any]] = []
if final_system_prompt:
messages.append({'role': 'system', 'content': final_system_prompt})
messages += _concatenate_text(text, image_list)
# 构造请求体(对齐 C#)
request_body: Dict[str, Any] = {
'model': modelName,
'messages': messages,
'stream': False,
}
for k, v in extra.items():
request_body[k] = v
# 调试:写出完整请求体(对齐 C# dest.json)
try:
with open('dest.json', 'w', encoding='utf-8') as f:
json.dump(request_body, f, ensure_ascii=False, indent=2)
except Exception:
pass
# Headers(对齐 C#:API_KEY 非空且不是 localhost 时才带 Bearer)
headers = {'Content-Type': 'application/json'}
if apiKey and 'localhost' not in serverUrl and '127.0.0.1' not in serverUrl:
headers['Authorization'] = f'Bearer {apiKey}'
url = serverUrl if useOllama else f'{serverUrl}/chat/completions'
start_time = time.time()
try:
print(f'Sending request to: {url}')
resp = requests.post(url, json=request_body, headers=headers, timeout=remoteServerTimeout)
response_body = resp.content.decode('utf-8', errors='replace')
if resp.status_code < 200 or resp.status_code >= 300:
print(f'API Error: {resp.status_code} - {response_body}')
return None, 0
print('\n\nResponse:\n')
print(response_body)
doc = json.loads(response_body)
if useOllama:
answer_text = doc.get('message', {}).get('content')
else:
answer_text = doc.get('choices', [{}])[0].get('message', {}).get('content')
# Token 用量(对齐 C#:只读 prompt_tokens / completion_tokens / total_tokens,缺失为 0)
usage = doc.get('usage')
if usage is not None:
prompt_tokens = usage.get('prompt_tokens', 0) or 0
completion_tokens = usage.get('completion_tokens', 0) or 0
total_tokens = usage.get('total_tokens', 0) or 0
print(f'Token 用量: 输入 {prompt_tokens} | 输出 {completion_tokens} | 总计 {total_tokens}')
totalTokens = total_tokens
# 推理内容(Deepseek reasoning_content / Ollama thinking,对齐 C#)
try:
reason = doc.get('choices', [{}])[0].get('message', {}).get('reasoning_content')
if reason is None:
reason = doc.get('message', {}).get('thinking')
except Exception:
reason = None
if reason is not None:
print(f'{Fore.LIGHTBLACK_EX}<think>\n{reason}\n</think>{Fore.RESET}')
elapsed = time.time() - start_time
print(f'用时 {elapsed:.2f}s')
if answer_text is not None:
answer_text = answer_text.strip()
print(answer_text if answer_text is not None else '')
return answer_text, totalTokens
except Exception as e:
print(f'HTTP request failed: {e}')
return None, 0
def get_answer_as_string(text: str, system_prompt):
return getAnswer([ChatContent(username='', imagePaths=[], text=text, time='', ownByMyself=False)], system_prompt)
# ================= 测试入口 =================
if __name__ == '__main__':
c = ChatContent(
username='', imagePaths=[],
text='解释图片\n2\n3\n4\n5',
time=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),
ownByMyself=False,
)
c2 = ChatContent(
username='', imagePaths=[r'D:\Pictures\111.PNG'],
text='12345',
time=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),
ownByMyself=True,
)
c3 = ChatContent(
username='', imagePaths=[],
text='678910',
time=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),
ownByMyself=False,
)
# 测试 Ollama(对齐 C# Test())
useOllama = True
serverUrl = 'http://localhost:8080/api/chat'
modelName = 'qwen3.5:0.8b'
print('\n=== Testing Ollama ===')
answer = getAnswer([c, c2, c3])
print(f'Answer: {answer}')
# 测试 Built-in
print('\n=== Testing Built-in Model ===')
builtInLanguageModel = True
answer = getAnswer([c, c2, c3])
print(f'Answer: {answer}')
# 测试 OpenAI Compatible
print('\n=== Testing OpenAI Compatible API ===')
builtInLanguageModel = False
useOllama = False
serverUrl = 'http://localhost:8000/v1'
apiKey = '21r234242'
answer = getAnswer([c, c2, c3])
print(f'Answer: {answer}')