-
Notifications
You must be signed in to change notification settings - Fork 345
/
Copy pathtest_callback.py
80 lines (59 loc) · 2.3 KB
/
test_callback.py
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
import os
import pytest
from modelscope_agent.agents.role_play import RolePlay
from modelscope_agent.callbacks.run_state import RunStateCallback
from modelscope_agent.memory import MemoryWithRag
from modelscope_agent.tools.base import TOOL_REGISTRY
from .ut_utils import MockTool
IS_FORKED_PR = os.getenv('IS_FORKED_PR', 'false') == 'true'
@pytest.mark.skipif(IS_FORKED_PR, reason='only run modelscope-agent main repo')
def test_llm_run_state(mocker):
llm_config = {
'model': 'qwen-max',
'model_server': 'dashscope',
'api_key': 'test'
}
callback = RunStateCallback()
agent = RolePlay(llm=llm_config, callbacks=[callback], stream=False)
mocker.patch.object(agent.llm, '_chat_no_stream', return_value='hello')
response = agent.run('hello')
for r in response:
print(r)
assert callback.run_states[1][0].type == 'llm'
assert callback.run_states[1][0].content == 'hello'
def test_tool_exec_run_state(mocker):
TOOL_REGISTRY['mock_tool'] = {'class': MockTool}
llm_config = {
'model': 'qwen-max',
'model_server': 'dashscope',
'api_key': 'test'
}
function_list = ['mock_tool']
callback = RunStateCallback()
agent = RolePlay(
llm=llm_config,
callbacks=[callback],
function_list=function_list,
stream=False)
mocker.patch.object(
agent.llm,
'_chat_no_stream',
return_value='Action: mock_tool\nAction Input: {"test": "test_value"}')
response = agent.run('hello')
for r in response:
print(r)
assert callback.run_states[1][1].type == 'tool_input'
assert callback.run_states[1][1].name == 'mock_tool'
assert callback.run_states[1][1].content == '{"test": "test_value"}'
assert callback.run_states[1][2].type == 'tool_output'
assert callback.run_states[1][2].name == 'mock_tool'
assert callback.run_states[1][2].content == '{"test": "test_value"}'
@pytest.mark.skipif(IS_FORKED_PR, reason='only run modelscope-agent main repo')
def test_rag_run_state(mocker):
callback = RunStateCallback()
memory = MemoryWithRag(
urls=['tests/samples/常见QA.pdf'],
use_knowledge_cache=False,
callbacks=[callback])
memory.run(query='高德天气api怎么申请')
assert callback.run_states[1][0].type == 'rag'