File size: 9,038 Bytes
8cc6fec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import time

from lagent.agents.aggregator import InternLMToolAggregator
from lagent.agents.stream import PLUGIN_CN, AgentForInternLM, MathCoder, get_plugin_prompt
from lagent.llms import INTERNLM2_META, LMDeployPipeline
from lagent.prompts.parsers import PluginParser

model = LMDeployPipeline(
    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 -----------------------
print('-' * 80, 'interpreter', '-' * 80)
coder = MathCoder(
    llm=model,
    aggregator=InternLMToolAggregator(few_shot=[[
        {
            'role':
            'user',
            'content':
            "Mike paid $\\$1.25$ for a stamp three years ago. He was just offered double that amount for the stamp. Assuming the stamp's offer price doubles every three years, how many dollars will he be offered in 12 more years?"
        },
        {
            'role':
            'language',
            'content':
            "Let's break down the problem into smaller steps to understand how to approach it.\n\n**Step 1:** Determine the initial amount paid for the stamp, which is $1.25.\n\n**Step 2:** Understand the pattern of the offer price increase. The offer price doubles every three years.\n\n**Step 3:** Calculate the offer price in 12 more years.\n\nSince the offer price doubles every three years, in 12 years, the price will double 12 / 3 = 4 times.\n\nWe can represent the offer price after 12 more years as:\n\\[ \\text{Offer Price} = \\text{Initial Price} \\times 2^{(\\text{Number of Doublings})} \\]\n\nLet's calculate this using Python to get the exact figure.\n\n"
        },
        {
            'role': 'tool',
            'content':
            '```python\nfrom sympy import symbols, Eq, solve\n\n# Define the variables\ninitial_price = 1.25\ndoublings_in_12_years = 12 / 3\n\n# Calculate the offer price after 12 years\noffer_price_12_years = initial_price * 2**doublings_in_12_years\n\noffer_price_12_years\n```',
            'name': 'interpreter',
        },
        {
            'role': 'environment',
            'content': '20.0',
            'name': 'interpreter'
        },
        {
            'role':
            'language',
            'content':
            'After calculating the given formula, the offer price for the stamp in 12 more years is found to be $\\boxed{20.0}$ dollars.'
        },
    ]]))
query = (
    '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.')
res = coder(query, session_id=0)
# print(res.model_dump_json())
# print('-' * 120)
print(coder.get_steps(0))
import json

print(
    json.dumps(
        coder.agent.aggregator.aggregate(coder.agent.memory, coder.agent.name,
                                         coder.agent.output_format),
        ensure_ascii=False,
        indent=4))

state_dict = {
    'memory': [{
        '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.',
        'formatted': None,
        'type': None,
        'receiver': None
    }, {
        'sender': 'Agent',
        'content':
        'The product of the two numbers, \\(63\\) and \\(-63\\), is \\(-3969\\). Therefore, the product of the two numbers that Marie and Jay are thinking of is \\(\\boxed{-3969}\\).',
        'formatted': {
            'tool_type': None,
            'thought':
            'The product of the two numbers, \\(63\\) and \\(-63\\), is \\(-3969\\). Therefore, the product of the two numbers that Marie and Jay are thinking of is \\(\\boxed{-3969}\\).',
            'action': None,
            'status': 0
        },
        'type': None,
        'receiver': None
    }],
    'agent.memory': [{
        '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.',
        'formatted': None,
        'type': None,
        'receiver': None
    }, {
        'sender': 'Agent',
        'content':
        "To solve this problem, we first need to identify the factors of 63. Since Marie is thinking of a multiple of 63 and Jay is thinking of a factor of 63, and they are thinking of the same number, we need to find the factors of 63 and then determine the multiples of 63 that match these factors.\n\nLet's start by finding the factors of 63 using Python.<|action_start|><|interpreter|>\n```python\nfrom sympy import divisors\n\n# Find the factors of 63\nfactors_of_63 = divisors(63)\nfactors_of_63\n```",
        'formatted': {
            'tool_type': 'interpreter',
            'thought':
            "To solve this problem, we first need to identify the factors of 63. Since Marie is thinking of a multiple of 63 and Jay is thinking of a factor of 63, and they are thinking of the same number, we need to find the factors of 63 and then determine the multiples of 63 that match these factors.\n\nLet's start by finding the factors of 63 using Python.",
            'action':
            '```python\nfrom sympy import divisors\n\n# Find the factors of 63\nfactors_of_63 = divisors(63)\nfactors_of_63\n```',
            'status': 1
        },
        'type': None,
        'receiver': None
    }, {
        'sender': 'ActionExecutor',
        'content': '[1, 3, 7, 9, 21, 63]',
        'formatted': None,
        'type': None,
        'receiver': None
    }, {
        'sender': 'Agent',
        'content':
        "The factors of 63 are \\(1, 3, 7, 9, 21,\\) and \\(63\\). Since Marie is thinking of a multiple of 63 and Jay is thinking of a factor of 63, and they are thinking of the same number, the number they are thinking of must be one of the factors of 63.\n\nGiven that there are two possibilities for the number they are thinking of, one positive and one negative, we need to consider both \\(63\\) and \\(-63\\) as the number they might be thinking of.\n\nNow, let's calculate the product of these two numbers, \\(63\\) and \\(-63\\).<|action_start|><|interpreter|>\n```python\n# Calculate the product of 63 and -63\nproduct = 63 * (-63)\nproduct\n```",
        'formatted': {
            'tool_type': 'interpreter',
            'thought':
            "The factors of 63 are \\(1, 3, 7, 9, 21,\\) and \\(63\\). Since Marie is thinking of a multiple of 63 and Jay is thinking of a factor of 63, and they are thinking of the same number, the number they are thinking of must be one of the factors of 63.\n\nGiven that there are two possibilities for the number they are thinking of, one positive and one negative, we need to consider both \\(63\\) and \\(-63\\) as the number they might be thinking of.\n\nNow, let's calculate the product of these two numbers, \\(63\\) and \\(-63\\).",
            'action':
            '```python\n# Calculate the product of 63 and -63\nproduct = 63 * (-63)\nproduct\n```',
            'status': 1
        },
        'type': None,
        'receiver': None
    }, {
        'sender': 'ActionExecutor',
        'content': '-3969',
        'formatted': None,
        'type': None,
        'receiver': None
    }, {
        'sender': 'Agent',
        'content':
        'The product of the two numbers, \\(63\\) and \\(-63\\), is \\(-3969\\). Therefore, the product of the two numbers that Marie and Jay are thinking of is \\(\\boxed{-3969}\\).',
        'formatted': {
            'tool_type': None,
            'thought':
            'The product of the two numbers, \\(63\\) and \\(-63\\), is \\(-3969\\). Therefore, the product of the two numbers that Marie and Jay are thinking of is \\(\\boxed{-3969}\\).',
            'action': None,
            'status': 0
        },
        'type': None,
        'receiver': None
    }]
}
coder.load_state_dict(state_dict)
print(coder.state_dict() == state_dict)
print(coder.state_dict())

# ----------------------- plugin -----------------------
print('-' * 80, 'plugin', '-' * 80)
plugins = [dict(type='lagent.actions.ArxivSearch')]
agent = AgentForInternLM(
    llm=model,
    plugins=plugins,
    output_format=dict(
        type=PluginParser,
        template=PLUGIN_CN,
        prompt=get_plugin_prompt(plugins)))

tic = time.time()
query = 'LLM智能体方向的最新论文有哪些?'
res = agent(query, session_id=0)
print(res.model_dump_json())
print('-' * 120)
print(agent.get_steps(0))
print(f'time elapsed: {time.time() - tic}')