File size: 3,309 Bytes
f8d0193
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import asyncio
import time

from lagent.agents.stream import PLUGIN_CN, get_plugin_prompt
from lagent.distributed import AsyncHTTPAgentClient, AsyncHTTPAgentServer, HTTPAgentClient, HTTPAgentServer
from lagent.llms import INTERNLM2_META
from lagent.schema import AgentMessage
from lagent.utils import create_object

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

server = HTTPAgentServer(
    '1',
    {
        'type': 'lagent.agents.AsyncAgent',
        'llm': {
            'type': 'lagent.llms.AsyncLMDeployPipeline',
            'path': 'internlm/internlm2_5-7b-chat',
            'meta_template': INTERNLM2_META,
        }
    },
    port=8090,
)
print(server.is_alive)
message = AgentMessage(sender='user', content='hello')
result = server(message)
print(result)
server.shutdown()

# math coder
server = AsyncHTTPAgentServer(
    '1',
    {
        'type': 'lagent.agents.AsyncMathCoder',
        'llm': {
            'type': 'lagent.llms.AsyncLMDeployPipeline',
            'path': 'internlm/internlm2_5-7b-chat',
            'meta_template': INTERNLM2_META,
            'tp': 1,
            'top_k': 1,
            'temperature': 1.0,
            'stop_words': ['<|im_end|>', '<|action_end|>'],
            'max_new_tokens': 1024,
        },
        'interpreter': {
            'type': 'lagent.actions.AsyncIPythonInterpreter',
            'max_kernels': 100
        },
    },
    port=8091,
)
message = AgentMessage(
    sender='user',
    content=
    ('Marie is thinking of a multiple of 63, while Jay is thinking of a factor '
     'of 63. They happen to be thinking of the same number. There are two '
     'possibilities for the number that each of them is thinking of, one '
     'positive and one negative. Find the product of these two numbers.'))
result = server(message)
print(loop.run_until_complete(result))
print(server.state_dict())

client = AsyncHTTPAgentClient(port=8091)
result = client('hello', session_id=1)
print(loop.run_until_complete(result))
print(client.state_dict(1))

client = HTTPAgentClient(port=8091)
print(client.state_dict(1))
print(client('introduce yourself', session_id=1))
print(client.state_dict(1))
server.shutdown()

# plugins
plugins = [dict(type='lagent.actions.AsyncArxivSearch')]
server_cfg = dict(
    type='lagent.distributed.AsyncHTTPAgentServer',
    gpu_id='1',
    config={
        'type': 'lagent.agents.AsyncAgentForInternLM',
        'llm': {
            'type': 'lagent.llms.AsyncLMDeployPipeline',
            'path': 'internlm/internlm2_5-7b-chat',
            'meta_template': INTERNLM2_META,
            'tp': 1,
            'top_k': 1,
            'temperature': 1.0,
            'stop_words': ['<|im_end|>', '<|action_end|>'],
            'max_new_tokens': 1024,
        },
        'plugins': plugins,
        'output_format': {
            'type': 'lagent.prompts.parsers.PluginParser',
            'template': PLUGIN_CN,
            'prompt': get_plugin_prompt(plugins),
        }
    },
    port=8091,
)
server = create_object(server_cfg)
tic = time.time()
coros = [
    server(query, session_id=i)
    for i, query in enumerate(['LLM智能体方向的最新论文有哪些?'] * 50)
]
res = loop.run_until_complete(asyncio.gather(*coros))
print('-' * 120)
print(f'time elapsed: {time.time() - tic}')
server.shutdown()