-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmock_llm_server.py
More file actions
583 lines (506 loc) · 20.4 KB
/
mock_llm_server.py
File metadata and controls
583 lines (506 loc) · 20.4 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
"""统一的 Mock LLM Server 模块
为所有 integration 测试提供统一的 Mock LLM 能力,支持:
- HTTP 请求拦截和响应模拟
- litellm 函数 mock
- 请求参数捕获和验证
- 流式/非流式响应模拟
- 场景配置(简单对话、工具调用、多轮对话)
"""
from dataclasses import dataclass, field
import json
from typing import Any, Callable, Dict, List, Optional
from litellm.files.main import ModelResponse
import pydash
import respx
from agentrun.model.api.data import BaseInfo
from agentrun.utils.log import logger
from .scenarios import MockScenario, Scenarios
@dataclass
class CapturedRequest:
"""捕获的请求信息"""
messages: List[Dict[str, Any]]
tools: Optional[List[Dict[str, Any]]]
stream: Optional[bool]
stream_options: Optional[Dict[str, Any]]
model: Optional[str]
all_kwargs: Dict[str, Any]
@dataclass
class MockLLMServer:
"""统一的 Mock LLM Server
提供 HTTP 和 litellm 的 mock 能力,支持捕获请求参数和场景配置。
使用方式:
# 基本用法
server = MockLLMServer()
server.install(monkeypatch, respx_mock) # 需要传入 respx_mock
# 添加自定义场景
server.add_scenario(Scenarios.simple_chat("你好", "你好!"))
server.add_scenario(Scenarios.single_tool_call(
"天气", "weather_lookup", {"city": "上海"}, "晴天"
))
# 测试代码...
# 验证捕获的请求
assert server.captured_requests[0].stream is True
"""
base_url: str = "https://mock-llm.local/v1"
expect_tools: bool = True
captured_requests: List[CapturedRequest] = field(default_factory=list)
scenarios: List[MockScenario] = field(default_factory=list)
response_builder: Optional[Callable[[List[Dict], Optional[List]], Dict]] = (
None
)
validate_tools: bool = True
"""是否验证工具格式(默认 True)"""
_respx_router: Any = field(default=None, init=False, repr=False)
"""内部使用的 respx router 实例"""
def install(
self, monkeypatch: Any, respx_mock: Any = None
) -> "MockLLMServer":
"""安装所有 mock
Args:
monkeypatch: pytest monkeypatch fixture
respx_mock: pytest respx_mock fixture(必须传入以确保 mock 生效)
Returns:
self: 返回自身以便链式调用
"""
self._respx_router = respx_mock
self._patch_model_info(monkeypatch)
self._patch_litellm(monkeypatch)
self._setup_respx()
return self
def add_scenario(self, scenario: MockScenario) -> "MockLLMServer":
"""添加测试场景
Args:
scenario: 场景配置
Returns:
self: 返回自身以便链式调用
"""
self.scenarios.append(scenario)
return self
def add_default_scenarios(self) -> "MockLLMServer":
"""添加默认测试场景
包括:
- 多工具调用场景(触发词:上海)
- 简单对话场景(触发词:你好)
Returns:
self: 返回自身以便链式调用
"""
self.add_scenario(Scenarios.default_multi_tool_scenario())
self.add_scenario(Scenarios.simple_chat("你好", "你好!我是AI助手。"))
return self
def clear_scenarios(self) -> "MockLLMServer":
"""清除所有场景
Returns:
self: 返回自身以便链式调用
"""
self.scenarios.clear()
return self
def reset_scenarios(self) -> "MockLLMServer":
"""重置所有场景状态(用于多次测试)
Returns:
self: 返回自身以便链式调用
"""
for scenario in self.scenarios:
scenario.reset()
return self
def clear_captured_requests(self) -> "MockLLMServer":
"""清除捕获的请求
Returns:
self: 返回自身以便链式调用
"""
self.captured_requests.clear()
return self
def get_last_request(self) -> Optional[CapturedRequest]:
"""获取最后一个捕获的请求"""
return self.captured_requests[-1] if self.captured_requests else None
def assert_no_stream_options_when_not_streaming(self):
"""断言非流式请求不包含 stream_options
这是检测 stream_options 使用错误的核心断言。
"""
for req in self.captured_requests:
if req.stream is False or req.stream is None:
# 非流式模式不应该传递 stream_options
if req.stream_options is not None:
include_usage = pydash.get(
req.stream_options, "include_usage"
)
if include_usage is True:
raise AssertionError(
"非流式请求不应包含"
" stream_options.include_usage=True,"
f"但收到: stream={req.stream}, "
f"stream_options={req.stream_options}"
)
def assert_stream_options_when_streaming(self):
"""断言流式请求包含正确的 stream_options"""
for req in self.captured_requests:
if req.stream is True:
include_usage = pydash.get(
req.stream_options, "include_usage", False
)
assert include_usage is True, (
"流式请求应包含 stream_options.include_usage=True,"
f"但收到: stream={req.stream}, "
f"stream_options={req.stream_options}"
)
def _capture_request(self, **kwargs: Any) -> CapturedRequest:
"""捕获请求参数"""
captured = CapturedRequest(
messages=kwargs.get("messages", []),
tools=kwargs.get("tools"),
stream=kwargs.get("stream"),
stream_options=kwargs.get("stream_options"),
model=kwargs.get("model"),
all_kwargs=kwargs,
)
self.captured_requests.append(captured)
logger.debug(
"Captured request: stream=%s, stream_options=%s, messages=%d",
captured.stream,
captured.stream_options,
len(captured.messages),
)
return captured
def _patch_model_info(self, monkeypatch: Any):
"""Mock ModelDataAPI.model_info 方法"""
def fake_model_info(inner_self: Any, config: Any = None) -> BaseInfo:
return BaseInfo(
api_key="mock-api-key",
base_url=self.base_url,
model=inner_self.model_name or "mock-model",
headers={
"Authorization": "Bearer mock-token",
"Agentrun-Access-Token": "mock-token",
},
)
monkeypatch.setattr(
"agentrun.model.api.data.ModelDataAPI.model_info",
fake_model_info,
)
def _patch_litellm(self, monkeypatch: Any):
"""Mock litellm.completion 和 litellm.acompletion"""
def fake_completion(*args: Any, **kwargs: Any) -> ModelResponse:
self._capture_request(**kwargs)
messages = kwargs.get("messages") or []
tools_payload = kwargs.get("tools")
return self._build_model_response(messages, tools_payload)
async def fake_acompletion(*args: Any, **kwargs: Any) -> ModelResponse:
self._capture_request(**kwargs)
messages = kwargs.get("messages") or []
tools_payload = kwargs.get("tools")
return self._build_model_response(messages, tools_payload)
monkeypatch.setattr("litellm.completion", fake_completion)
monkeypatch.setattr("litellm.acompletion", fake_acompletion)
# Patch Google ADK 的 litellm 导入
try:
import google.adk.models.lite_llm as lite_llm_module
monkeypatch.setattr(
lite_llm_module, "acompletion", fake_acompletion
)
monkeypatch.setattr(lite_llm_module, "completion", fake_completion)
except ImportError:
pass # google.adk not installed
def _setup_respx(self):
"""设置 respx HTTP mock
关键修复:使用 pytest-respx fixture 提供的 router 而不是全局 respx
问题背景:
- 之前直接使用全局 respx.route() 在 CI 环境中不生效
- 全局 respx router 在某些环境中可能没有正确初始化
- 导致 HTTP 请求没有被拦截,Google ADK 发送真实请求
解决方案:
- 使用 pytest-respx 提供的 respx_mock fixture
- 通过 install() 方法传入 respx_mock
- 确保 mock 在所有环境中一致生效
"""
def extract_payload(request: Any) -> Dict[str, Any]:
try:
if request.content:
body = request.content
if isinstance(body, (bytes, bytearray)):
body = body.decode()
if isinstance(body, str) and body.strip():
return json.loads(body)
except (json.JSONDecodeError, AttributeError):
pass
return {}
def build_response(request: Any, route: Any) -> respx.MockResponse:
payload = extract_payload(request)
# 捕获 HTTP 请求
self._capture_request(**payload)
is_stream = payload.get("stream", False)
response_json = self._build_response(
payload.get("messages") or [],
payload.get("tools"),
)
if is_stream:
return respx.MockResponse(
status_code=200,
content=self._build_sse_stream(response_json),
headers={"content-type": "text/event-stream"},
)
return respx.MockResponse(status_code=200, json=response_json)
# 关键修复:使用传入的 respx_router 而不是全局 respx
# 如果没有传入 respx_router,回退到全局 respx(向后兼容)
router = self._respx_router if self._respx_router is not None else respx
router.route(url__startswith=self.base_url).mock(
side_effect=build_response
)
def _find_matching_scenario(
self, messages: List[Dict]
) -> Optional[MockScenario]:
"""查找匹配的场景"""
for scenario in self.scenarios:
if scenario.match(messages):
return scenario
return None
def _build_response(
self, messages: List[Dict], tools_payload: Optional[List]
) -> Dict[str, Any]:
"""构建响应 JSON
优先使用场景配置,如果没有匹配的场景则使用默认逻辑。
"""
# 使用自定义 response_builder
if self.response_builder:
return self.response_builder(messages, tools_payload)
logger.debug(
"Building response for %d messages, tools=%s",
len(messages),
tools_payload is not None,
)
# 添加详细的消息日志,帮助调试框架的消息格式
for i, msg in enumerate(messages):
role = msg.get("role", "unknown")
content_preview = str(msg.get("content", ""))[:100]
logger.debug(
"Message[%d] role=%s, content_preview=%s",
i,
role,
content_preview,
)
if "tool_calls" in msg:
logger.debug(
"Message[%d] has tool_calls: %s", i, msg.get("tool_calls")
)
if "tool_call_id" in msg:
logger.debug(
"Message[%d] has tool_call_id: %s",
i,
msg.get("tool_call_id"),
)
# 验证工具格式
if self.validate_tools and self.expect_tools and tools_payload:
self._assert_tools(tools_payload)
elif tools_payload is not None:
assert isinstance(tools_payload, list)
if not messages:
raise AssertionError("messages payload cannot be empty")
# 查找匹配的场景
scenario = self._find_matching_scenario(messages)
if scenario:
turn = scenario.get_response(messages)
return turn.to_response()
# 默认逻辑:未匹配场景时使用
return self._build_default_response(messages, tools_payload)
def _build_default_response(
self, messages: List[Dict], tools_payload: Optional[List]
) -> Dict[str, Any]:
"""构建默认响应(无场景匹配时使用)"""
# 检查消息历史中是否已经有 tool 结果
# 这是关键修复:不只检查最后一条消息,而是检查整个历史
has_tool_results = any(msg.get("role") == "tool" for msg in messages)
if has_tool_results:
# 已经有 tool 结果,应该返回最终答案而不是再次调用工具
return {
"id": "chatcmpl-mock-final",
"object": "chat.completion",
"created": 1234567890,
"model": "mock-model",
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": "final result",
},
"finish_reason": "stop",
}],
"usage": {
"prompt_tokens": 3,
"completion_tokens": 2,
"total_tokens": 5,
},
}
# 如果有工具且未调用过,返回工具调用
if tools_payload:
return {
"id": "chatcmpl-mock-tools",
"object": "chat.completion",
"created": 1234567890,
"model": "mock-model",
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": None,
"tool_calls": [
{
"id": "tool_call_1",
"type": "function",
"function": {
"name": "weather_lookup",
"arguments": '{"city": "上海"}',
},
},
{
"id": "tool_call_2",
"type": "function",
"function": {
"name": "get_time_now",
"arguments": "{}",
},
},
],
},
"finish_reason": "tool_calls",
}],
"usage": {
"prompt_tokens": 5,
"completion_tokens": 1,
"total_tokens": 6,
},
}
# 无工具时返回简单文本响应
return {
"id": "chatcmpl-mock-simple",
"object": "chat.completion",
"created": 1234567890,
"model": "mock-model",
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello! I'm a helpful assistant.",
},
"finish_reason": "stop",
}],
"usage": {
"prompt_tokens": 3,
"completion_tokens": 5,
"total_tokens": 8,
},
}
def _build_model_response(
self, messages: List[Dict], tools_payload: Optional[List]
) -> ModelResponse:
"""构建 ModelResponse 对象"""
response_dict = self._build_response(messages, tools_payload)
return ModelResponse(**response_dict)
def _build_sse_stream(self, response_json: Dict[str, Any]) -> bytes:
"""构建 SSE 流式响应"""
chunks = []
choice = response_json.get("choices", [{}])[0]
message = choice.get("message", {})
tool_calls = message.get("tool_calls")
# First chunk with role
first_chunk = {
"id": response_json.get("id", "chatcmpl-mock"),
"object": "chat.completion.chunk",
"created": response_json.get("created", 1234567890),
"model": response_json.get("model", "mock-model"),
"choices": [{
"index": 0,
"delta": {"role": "assistant", "content": ""},
"finish_reason": None,
}],
}
chunks.append(f"data: {json.dumps(first_chunk)}\n\n")
if tool_calls:
for i, tool_call in enumerate(tool_calls):
tc_chunk = {
"id": response_json.get("id", "chatcmpl-mock"),
"object": "chat.completion.chunk",
"created": response_json.get("created", 1234567890),
"model": response_json.get("model", "mock-model"),
"choices": [{
"index": 0,
"delta": {
"tool_calls": [{
"index": i,
"id": tool_call.get("id"),
"type": "function",
"function": tool_call.get("function"),
}],
},
"finish_reason": None,
}],
}
chunks.append(f"data: {json.dumps(tc_chunk)}\n\n")
else:
content = message.get("content", "")
if content:
content_chunk = {
"id": response_json.get("id", "chatcmpl-mock"),
"object": "chat.completion.chunk",
"created": response_json.get("created", 1234567890),
"model": response_json.get("model", "mock-model"),
"choices": [{
"index": 0,
"delta": {"content": content},
"finish_reason": None,
}],
}
chunks.append(f"data: {json.dumps(content_chunk)}\n\n")
# Final chunk with finish_reason
finish_reason = "tool_calls" if tool_calls else "stop"
final_chunk = {
"id": response_json.get("id", "chatcmpl-mock"),
"object": "chat.completion.chunk",
"created": response_json.get("created", 1234567890),
"model": response_json.get("model", "mock-model"),
"choices": [{
"index": 0,
"delta": {},
"finish_reason": finish_reason,
}],
}
chunks.append(f"data: {json.dumps(final_chunk)}\n\n")
chunks.append("data: [DONE]\n\n")
return "".join(chunks).encode("utf-8")
def _assert_tools(self, tools_payload: List[Dict]):
"""验证工具参数格式"""
assert isinstance(tools_payload, list)
assert (
pydash.get(tools_payload, "[0].function.name") == "weather_lookup"
)
assert (
pydash.get(tools_payload, "[0].function.description")
== "查询城市天气"
)
assert (
pydash.get(
tools_payload,
"[0].function.parameters.properties.city.type",
)
== "string"
)
assert (
pydash.get(tools_payload, "[0].function.parameters.type")
== "object"
)
assert "city" in (
pydash.get(tools_payload, "[0].function.parameters.required", [])
or []
)
assert pydash.get(tools_payload, "[1].function.name") == "get_time_now"
assert (
pydash.get(tools_payload, "[1].function.description")
== "返回当前时间"
)
assert pydash.get(
tools_payload, "[1].function.parameters.properties"
) in (
{},
None,
)
assert (
pydash.get(tools_payload, "[1].function.parameters.type")
== "object"
)