Spaces:
Running
Running
xiaoxishui
commited on
Commit
·
3ecb541
1
Parent(s):
b2ad1a6
Add LAGENT source code files
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- lagent/examples/.ipynb_checkpoints/agent_api_web_demo-checkpoint.py +198 -0
- lagent/examples/.ipynb_checkpoints/multi_agents_api_web_demo-checkpoint.py +198 -0
- lagent/lagent.egg-info/PKG-INFO +600 -0
- lagent/lagent.egg-info/SOURCES.txt +71 -0
- lagent/lagent.egg-info/dependency_links.txt +1 -0
- lagent/lagent.egg-info/requires.txt +59 -0
- lagent/lagent.egg-info/top_level.txt +1 -0
- lagent/lagent/__pycache__/__init__.cpython-310.pyc +0 -0
- lagent/lagent/__pycache__/schema.cpython-310.pyc +0 -0
- lagent/lagent/__pycache__/version.cpython-310.pyc +0 -0
- lagent/lagent/actions/.ipynb_checkpoints/__init__-checkpoint.py +26 -0
- lagent/lagent/actions/.ipynb_checkpoints/weather_query-checkpoint.py +71 -0
- lagent/lagent/actions/__pycache__/__init__.cpython-310.pyc +0 -0
- lagent/lagent/actions/__pycache__/action_executor.cpython-310.pyc +0 -0
- lagent/lagent/actions/__pycache__/arxiv_search.cpython-310.pyc +0 -0
- lagent/lagent/actions/__pycache__/base_action.cpython-310.pyc +0 -0
- lagent/lagent/actions/__pycache__/bing_map.cpython-310.pyc +0 -0
- lagent/lagent/actions/__pycache__/builtin_actions.cpython-310.pyc +0 -0
- lagent/lagent/actions/__pycache__/google_scholar_search.cpython-310.pyc +0 -0
- lagent/lagent/actions/__pycache__/google_search.cpython-310.pyc +0 -0
- lagent/lagent/actions/__pycache__/ipython_interactive.cpython-310.pyc +0 -0
- lagent/lagent/actions/__pycache__/ipython_interpreter.cpython-310.pyc +0 -0
- lagent/lagent/actions/__pycache__/ipython_manager.cpython-310.pyc +0 -0
- lagent/lagent/actions/__pycache__/parser.cpython-310.pyc +0 -0
- lagent/lagent/actions/__pycache__/ppt.cpython-310.pyc +0 -0
- lagent/lagent/actions/__pycache__/python_interpreter.cpython-310.pyc +0 -0
- lagent/lagent/actions/__pycache__/weather_query.cpython-310.pyc +0 -0
- lagent/lagent/actions/__pycache__/web_browser.cpython-310.pyc +0 -0
- lagent/lagent/agents/__pycache__/__init__.cpython-310.pyc +0 -0
- lagent/lagent/agents/__pycache__/agent.cpython-310.pyc +0 -0
- lagent/lagent/agents/__pycache__/react.cpython-310.pyc +0 -0
- lagent/lagent/agents/__pycache__/stream.cpython-310.pyc +0 -0
- lagent/lagent/agents/aggregator/__pycache__/__init__.cpython-310.pyc +0 -0
- lagent/lagent/agents/aggregator/__pycache__/default_aggregator.cpython-310.pyc +0 -0
- lagent/lagent/agents/aggregator/__pycache__/tool_aggregator.cpython-310.pyc +0 -0
- lagent/lagent/hooks/__pycache__/__init__.cpython-310.pyc +0 -0
- lagent/lagent/hooks/__pycache__/action_preprocessor.cpython-310.pyc +0 -0
- lagent/lagent/hooks/__pycache__/hook.cpython-310.pyc +0 -0
- lagent/lagent/hooks/__pycache__/logger.cpython-310.pyc +0 -0
- lagent/lagent/llms/__pycache__/__init__.cpython-310.pyc +0 -0
- lagent/lagent/llms/__pycache__/base_api.cpython-310.pyc +0 -0
- lagent/lagent/llms/__pycache__/base_llm.cpython-310.pyc +0 -0
- lagent/lagent/llms/__pycache__/huggingface.cpython-310.pyc +0 -0
- lagent/lagent/llms/__pycache__/lmdeploy_wrapper.cpython-310.pyc +0 -0
- lagent/lagent/llms/__pycache__/meta_template.cpython-310.pyc +0 -0
- lagent/lagent/llms/__pycache__/openai.cpython-310.pyc +0 -0
- lagent/lagent/llms/__pycache__/sensenova.cpython-310.pyc +0 -0
- lagent/lagent/llms/__pycache__/vllm_wrapper.cpython-310.pyc +0 -0
- lagent/lagent/memory/__pycache__/__init__.cpython-310.pyc +0 -0
- lagent/lagent/memory/__pycache__/base_memory.cpython-310.pyc +0 -0
lagent/examples/.ipynb_checkpoints/agent_api_web_demo-checkpoint.py
ADDED
@@ -0,0 +1,198 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import copy
|
2 |
+
import os
|
3 |
+
from typing import List
|
4 |
+
import streamlit as st
|
5 |
+
from lagent.actions import ArxivSearch
|
6 |
+
from lagent.prompts.parsers import PluginParser
|
7 |
+
from lagent.agents.stream import INTERPRETER_CN, META_CN, PLUGIN_CN, AgentForInternLM, get_plugin_prompt
|
8 |
+
from lagent.llms import GPTAPI
|
9 |
+
from lagent.actions import ArxivSearch, WeatherQuery
|
10 |
+
|
11 |
+
|
12 |
+
class SessionState:
|
13 |
+
"""管理会话状态的类。"""
|
14 |
+
|
15 |
+
def init_state(self):
|
16 |
+
"""初始化会话状态变量。"""
|
17 |
+
st.session_state['assistant'] = [] # 助手消息历史
|
18 |
+
st.session_state['user'] = [] # 用户消息历史
|
19 |
+
# 初始化插件列表
|
20 |
+
action_list = [
|
21 |
+
ArxivSearch(),
|
22 |
+
WeatherQuery(),
|
23 |
+
]
|
24 |
+
st.session_state['plugin_map'] = {action.name: action for action in action_list}
|
25 |
+
st.session_state['model_map'] = {} # 存储模型实例
|
26 |
+
st.session_state['model_selected'] = None # 当前选定模型
|
27 |
+
st.session_state['plugin_actions'] = set() # 当前激活插件
|
28 |
+
st.session_state['history'] = [] # 聊天历史
|
29 |
+
st.session_state['api_base'] = None # 初始化API base地址
|
30 |
+
|
31 |
+
def clear_state(self):
|
32 |
+
"""清除当前会话状态。"""
|
33 |
+
st.session_state['assistant'] = []
|
34 |
+
st.session_state['user'] = []
|
35 |
+
st.session_state['model_selected'] = None
|
36 |
+
|
37 |
+
|
38 |
+
class StreamlitUI:
|
39 |
+
"""管理 Streamlit 界面的类。"""
|
40 |
+
|
41 |
+
def __init__(self, session_state: SessionState):
|
42 |
+
self.session_state = session_state
|
43 |
+
self.plugin_action = [] # 当前选定的插件
|
44 |
+
# 初始化提示词
|
45 |
+
self.meta_prompt = META_CN
|
46 |
+
self.plugin_prompt = PLUGIN_CN
|
47 |
+
self.init_streamlit()
|
48 |
+
|
49 |
+
def init_streamlit(self):
|
50 |
+
"""初始化 Streamlit 的 UI 设置。"""
|
51 |
+
st.set_page_config(
|
52 |
+
layout='wide',
|
53 |
+
page_title='lagent-web',
|
54 |
+
page_icon='./docs/imgs/lagent_icon.png'
|
55 |
+
)
|
56 |
+
st.header(':robot_face: :blue[Lagent] Web Demo ', divider='rainbow')
|
57 |
+
|
58 |
+
def setup_sidebar(self):
|
59 |
+
"""设置侧边栏,选择模型和插件。"""
|
60 |
+
# 模型名称和 API Base 输入框
|
61 |
+
model_name = st.sidebar.text_input('模型名称:', value='internlm2.5-latest')
|
62 |
+
|
63 |
+
# ================================== 硅基流动的API ==================================
|
64 |
+
# 注意,如果采用硅基流动API,模型名称需要更改为:internlm/internlm2_5-7b-chat 或者 internlm/internlm2_5-20b-chat
|
65 |
+
# api_base = st.sidebar.text_input(
|
66 |
+
# 'API Base 地址:', value='https://api.siliconflow.cn/v1/chat/completions'
|
67 |
+
# )
|
68 |
+
# ================================== 浦语官方的API ==================================
|
69 |
+
api_base = st.sidebar.text_input(
|
70 |
+
'API Base 地址:', value='https://internlm-chat.intern-ai.org.cn/puyu/api/v1/chat/completions'
|
71 |
+
)
|
72 |
+
# ==================================================================================
|
73 |
+
# 插件选择
|
74 |
+
plugin_name = st.sidebar.multiselect(
|
75 |
+
'插件选择',
|
76 |
+
options=list(st.session_state['plugin_map'].keys()),
|
77 |
+
default=[],
|
78 |
+
)
|
79 |
+
|
80 |
+
# 根据选择的插件生成插件操作列表
|
81 |
+
self.plugin_action = [st.session_state['plugin_map'][name] for name in plugin_name]
|
82 |
+
|
83 |
+
# 动态生成插件提示
|
84 |
+
if self.plugin_action:
|
85 |
+
self.plugin_prompt = get_plugin_prompt(self.plugin_action)
|
86 |
+
|
87 |
+
# 清空对话按钮
|
88 |
+
if st.sidebar.button('清空对话', key='clear'):
|
89 |
+
self.session_state.clear_state()
|
90 |
+
|
91 |
+
return model_name, api_base, self.plugin_action
|
92 |
+
|
93 |
+
def initialize_chatbot(self, model_name, api_base, plugin_action):
|
94 |
+
"""初始化 GPTAPI 实例作为 chatbot。"""
|
95 |
+
token = os.getenv("token")
|
96 |
+
if not token:
|
97 |
+
st.error("未检测到环境变量 `token`,请设置环境变量,例如 `export token='your_token_here'` 后重新运行 X﹏X")
|
98 |
+
st.stop() # 停止运行应用
|
99 |
+
|
100 |
+
# 创建完整的 meta_prompt,保留原始结构并动态插入侧边栏配置
|
101 |
+
meta_prompt = [
|
102 |
+
{"role": "system", "content": self.meta_prompt, "api_role": "system"},
|
103 |
+
{"role": "user", "content": "", "api_role": "user"},
|
104 |
+
{"role": "assistant", "content": self.plugin_prompt, "api_role": "assistant"},
|
105 |
+
{"role": "environment", "content": "", "api_role": "environment"}
|
106 |
+
]
|
107 |
+
|
108 |
+
api_model = GPTAPI(
|
109 |
+
model_type=model_name,
|
110 |
+
api_base=api_base,
|
111 |
+
key=token, # 从环境变量中获取授权令牌
|
112 |
+
meta_template=meta_prompt,
|
113 |
+
max_new_tokens=512,
|
114 |
+
temperature=0.8,
|
115 |
+
top_p=0.9
|
116 |
+
)
|
117 |
+
return api_model
|
118 |
+
|
119 |
+
def render_user(self, prompt: str):
|
120 |
+
"""渲染用户输入内容。"""
|
121 |
+
with st.chat_message('user'):
|
122 |
+
st.markdown(prompt)
|
123 |
+
|
124 |
+
def render_assistant(self, agent_return):
|
125 |
+
"""渲染助手响应内容。"""
|
126 |
+
with st.chat_message('assistant'):
|
127 |
+
content = getattr(agent_return, "content", str(agent_return))
|
128 |
+
st.markdown(content if isinstance(content, str) else str(content))
|
129 |
+
|
130 |
+
|
131 |
+
def main():
|
132 |
+
"""主函数,运行 Streamlit 应用。"""
|
133 |
+
if 'ui' not in st.session_state:
|
134 |
+
session_state = SessionState()
|
135 |
+
session_state.init_state()
|
136 |
+
st.session_state['ui'] = StreamlitUI(session_state)
|
137 |
+
else:
|
138 |
+
st.set_page_config(
|
139 |
+
layout='wide',
|
140 |
+
page_title='lagent-web',
|
141 |
+
page_icon='./docs/imgs/lagent_icon.png'
|
142 |
+
)
|
143 |
+
st.header(':robot_face: :blue[Lagent] Web Demo ', divider='rainbow')
|
144 |
+
|
145 |
+
# 设置侧边栏并获取模型和插件信息
|
146 |
+
model_name, api_base, plugin_action = st.session_state['ui'].setup_sidebar()
|
147 |
+
plugins = [dict(type=f"lagent.actions.{plugin.__class__.__name__}") for plugin in plugin_action]
|
148 |
+
|
149 |
+
if (
|
150 |
+
'chatbot' not in st.session_state or
|
151 |
+
model_name != st.session_state['chatbot'].model_type or
|
152 |
+
'last_plugin_action' not in st.session_state or
|
153 |
+
plugin_action != st.session_state['last_plugin_action'] or
|
154 |
+
api_base != st.session_state['api_base']
|
155 |
+
):
|
156 |
+
# 更新 Chatbot
|
157 |
+
st.session_state['chatbot'] = st.session_state['ui'].initialize_chatbot(model_name, api_base, plugin_action)
|
158 |
+
st.session_state['last_plugin_action'] = plugin_action # 更新插件状态
|
159 |
+
st.session_state['api_base'] = api_base # 更新 API Base 地址
|
160 |
+
|
161 |
+
# 初始化 AgentForInternLM
|
162 |
+
st.session_state['agent'] = AgentForInternLM(
|
163 |
+
llm=st.session_state['chatbot'],
|
164 |
+
plugins=plugins,
|
165 |
+
output_format=dict(
|
166 |
+
type=PluginParser,
|
167 |
+
template=PLUGIN_CN,
|
168 |
+
prompt=get_plugin_prompt(plugin_action)
|
169 |
+
)
|
170 |
+
)
|
171 |
+
# 清空对话历史
|
172 |
+
st.session_state['session_history'] = []
|
173 |
+
|
174 |
+
if 'agent' not in st.session_state:
|
175 |
+
st.session_state['agent'] = None
|
176 |
+
|
177 |
+
agent = st.session_state['agent']
|
178 |
+
for prompt, agent_return in zip(st.session_state['user'], st.session_state['assistant']):
|
179 |
+
st.session_state['ui'].render_user(prompt)
|
180 |
+
st.session_state['ui'].render_assistant(agent_return)
|
181 |
+
|
182 |
+
# 处理用户输入
|
183 |
+
if user_input := st.chat_input(''):
|
184 |
+
st.session_state['ui'].render_user(user_input)
|
185 |
+
|
186 |
+
# 调用模型时确保侧边栏的系统提示词和插件提示词生效
|
187 |
+
res = agent(user_input, session_id=0)
|
188 |
+
st.session_state['ui'].render_assistant(res)
|
189 |
+
|
190 |
+
# 更新会话状态
|
191 |
+
st.session_state['user'].append(user_input)
|
192 |
+
st.session_state['assistant'].append(copy.deepcopy(res))
|
193 |
+
|
194 |
+
st.session_state['last_status'] = None
|
195 |
+
|
196 |
+
|
197 |
+
if __name__ == '__main__':
|
198 |
+
main()
|
lagent/examples/.ipynb_checkpoints/multi_agents_api_web_demo-checkpoint.py
ADDED
@@ -0,0 +1,198 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import asyncio
|
3 |
+
import json
|
4 |
+
import re
|
5 |
+
import requests
|
6 |
+
import streamlit as st
|
7 |
+
|
8 |
+
from lagent.agents import Agent
|
9 |
+
from lagent.prompts.parsers import PluginParser
|
10 |
+
from lagent.agents.stream import PLUGIN_CN, get_plugin_prompt
|
11 |
+
from lagent.schema import AgentMessage
|
12 |
+
from lagent.actions import ArxivSearch
|
13 |
+
from lagent.hooks import Hook
|
14 |
+
from lagent.llms import GPTAPI
|
15 |
+
|
16 |
+
YOUR_TOKEN_HERE = os.getenv("token")
|
17 |
+
if not YOUR_TOKEN_HERE:
|
18 |
+
raise EnvironmentError("未找到环境变量 'token',请设置后再运行程序。")
|
19 |
+
|
20 |
+
# Hook类,用于对消息添加前缀
|
21 |
+
class PrefixedMessageHook(Hook):
|
22 |
+
def __init__(self, prefix, senders=None):
|
23 |
+
"""
|
24 |
+
初始化Hook
|
25 |
+
:param prefix: 消息前缀
|
26 |
+
:param senders: 指定发送者列表
|
27 |
+
"""
|
28 |
+
self.prefix = prefix
|
29 |
+
self.senders = senders or []
|
30 |
+
|
31 |
+
def before_agent(self, agent, messages, session_id):
|
32 |
+
"""
|
33 |
+
在代理处理消息前修改消息内容
|
34 |
+
:param agent: 当前代理
|
35 |
+
:param messages: 消息列表
|
36 |
+
:param session_id: 会话ID
|
37 |
+
"""
|
38 |
+
for message in messages:
|
39 |
+
if message.sender in self.senders:
|
40 |
+
message.content = self.prefix + message.content
|
41 |
+
|
42 |
+
class AsyncBlogger:
|
43 |
+
"""博客生成类,整合写作者和批评者。"""
|
44 |
+
|
45 |
+
def __init__(self, model_type, api_base, writer_prompt, critic_prompt, critic_prefix='', max_turn=2):
|
46 |
+
"""
|
47 |
+
初始化博客生成器
|
48 |
+
:param model_type: 模型类型
|
49 |
+
:param api_base: API 基地址
|
50 |
+
:param writer_prompt: 写作者提示词
|
51 |
+
:param critic_prompt: 批评者提示词
|
52 |
+
:param critic_prefix: 批评消息前缀
|
53 |
+
:param max_turn: 最大轮次
|
54 |
+
"""
|
55 |
+
self.model_type = model_type
|
56 |
+
self.api_base = api_base
|
57 |
+
self.llm = GPTAPI(
|
58 |
+
model_type=model_type,
|
59 |
+
api_base=api_base,
|
60 |
+
key=YOUR_TOKEN_HERE,
|
61 |
+
max_new_tokens=4096,
|
62 |
+
)
|
63 |
+
self.plugins = [dict(type='lagent.actions.ArxivSearch')]
|
64 |
+
self.writer = Agent(
|
65 |
+
self.llm,
|
66 |
+
writer_prompt,
|
67 |
+
name='写作者',
|
68 |
+
output_format=dict(
|
69 |
+
type=PluginParser,
|
70 |
+
template=PLUGIN_CN,
|
71 |
+
prompt=get_plugin_prompt(self.plugins)
|
72 |
+
)
|
73 |
+
)
|
74 |
+
self.critic = Agent(
|
75 |
+
self.llm,
|
76 |
+
critic_prompt,
|
77 |
+
name='批评者',
|
78 |
+
hooks=[PrefixedMessageHook(critic_prefix, ['写作者'])]
|
79 |
+
)
|
80 |
+
self.max_turn = max_turn
|
81 |
+
|
82 |
+
async def forward(self, message: AgentMessage, update_placeholder):
|
83 |
+
"""
|
84 |
+
执行多阶段博客生成流程
|
85 |
+
:param message: 初始消息
|
86 |
+
:param update_placeholder: Streamlit占位符
|
87 |
+
:return: 最终优化的博客内容
|
88 |
+
"""
|
89 |
+
step1_placeholder = update_placeholder.container()
|
90 |
+
step2_placeholder = update_placeholder.container()
|
91 |
+
step3_placeholder = update_placeholder.container()
|
92 |
+
|
93 |
+
# 第一步:生成初始内容
|
94 |
+
step1_placeholder.markdown("**Step 1: 生成初始内容...**")
|
95 |
+
message = self.writer(message)
|
96 |
+
if message.content:
|
97 |
+
step1_placeholder.markdown(f"**生成的初始内容**:\n\n{message.content}")
|
98 |
+
else:
|
99 |
+
step1_placeholder.markdown("**生成的初始内容为空,请检查生成逻辑。**")
|
100 |
+
|
101 |
+
# 第二步:批评者提供反馈
|
102 |
+
step2_placeholder.markdown("**Step 2: 批评者正在提供反馈和文献推荐...**")
|
103 |
+
message = self.critic(message)
|
104 |
+
if message.content:
|
105 |
+
# 解析批评者反馈
|
106 |
+
suggestions = re.search(r"1\. 批评建议:\n(.*?)2\. 推荐的关键词:", message.content, re.S)
|
107 |
+
keywords = re.search(r"2\. 推荐的关键词:\n- (.*)", message.content)
|
108 |
+
feedback = suggestions.group(1).strip() if suggestions else "未提供批评建议"
|
109 |
+
keywords = keywords.group(1).strip() if keywords else "未提供关键词"
|
110 |
+
|
111 |
+
# Arxiv 文献查询
|
112 |
+
arxiv_search = ArxivSearch()
|
113 |
+
arxiv_results = arxiv_search.get_arxiv_article_information(keywords)
|
114 |
+
|
115 |
+
# 显示批评内容和文献推荐
|
116 |
+
message.content = f"**批评建议**:\n{feedback}\n\n**推荐的文献**:\n{arxiv_results}"
|
117 |
+
step2_placeholder.markdown(f"**批评和文献推荐**:\n\n{message.content}")
|
118 |
+
else:
|
119 |
+
step2_placeholder.markdown("**批评内容为空,请检查批评逻辑。**")
|
120 |
+
|
121 |
+
# 第三步:写作者根据反馈优化内容
|
122 |
+
step3_placeholder.markdown("**Step 3: 根据反馈改进内容...**")
|
123 |
+
improvement_prompt = AgentMessage(
|
124 |
+
sender="critic",
|
125 |
+
content=(
|
126 |
+
f"根据以下批评建议和推荐文献对内容进行改进:\n\n"
|
127 |
+
f"批评建议:\n{feedback}\n\n"
|
128 |
+
f"推荐文献:\n{arxiv_results}\n\n"
|
129 |
+
f"请优化��始内容,使其更加清晰、丰富,并符合专业水准。"
|
130 |
+
),
|
131 |
+
)
|
132 |
+
message = self.writer(improvement_prompt)
|
133 |
+
if message.content:
|
134 |
+
step3_placeholder.markdown(f"**最终优化的博客内容**:\n\n{message.content}")
|
135 |
+
else:
|
136 |
+
step3_placeholder.markdown("**最终优化的博客内容为空,请检查生成逻辑。**")
|
137 |
+
|
138 |
+
return message
|
139 |
+
|
140 |
+
def setup_sidebar():
|
141 |
+
"""设置侧边栏,选择模型。"""
|
142 |
+
model_name = st.sidebar.text_input('模型名称:', value='internlm2.5-latest')
|
143 |
+
api_base = st.sidebar.text_input(
|
144 |
+
'API Base 地址:', value='https://internlm-chat.intern-ai.org.cn/puyu/api/v1/chat/completions'
|
145 |
+
)
|
146 |
+
|
147 |
+
return model_name, api_base
|
148 |
+
|
149 |
+
def main():
|
150 |
+
"""
|
151 |
+
主函数:构建Streamlit界面并处理用户交互
|
152 |
+
"""
|
153 |
+
st.set_page_config(layout='wide', page_title='Lagent Web Demo', page_icon='🤖')
|
154 |
+
st.title("多代理博客优化助手")
|
155 |
+
|
156 |
+
model_type, api_base = setup_sidebar()
|
157 |
+
topic = st.text_input('输入一个话题:', 'Self-Supervised Learning')
|
158 |
+
generate_button = st.button('生成博客内容')
|
159 |
+
|
160 |
+
if (
|
161 |
+
'blogger' not in st.session_state or
|
162 |
+
st.session_state['model_type'] != model_type or
|
163 |
+
st.session_state['api_base'] != api_base
|
164 |
+
):
|
165 |
+
st.session_state['blogger'] = AsyncBlogger(
|
166 |
+
model_type=model_type,
|
167 |
+
api_base=api_base,
|
168 |
+
writer_prompt="你是一位优秀的AI内容写作者,请撰写一篇有吸引力且信息丰富的博客内容。",
|
169 |
+
critic_prompt="""
|
170 |
+
作为一位严谨的批评者,请给出建设性的批评和改进建议,并基于相关主题使用已有的工具推荐一些参考文献,推荐的关键词应该是英语形式,简洁且切题。
|
171 |
+
请按照以下格式提供反馈:
|
172 |
+
1. 批评建议:
|
173 |
+
- (具体建议)
|
174 |
+
2. 推荐的关键词:
|
175 |
+
- (关键词1, 关键词2, ...)
|
176 |
+
""",
|
177 |
+
critic_prefix="请批评以下内容,并提供改进建议:\n\n"
|
178 |
+
)
|
179 |
+
st.session_state['model_type'] = model_type
|
180 |
+
st.session_state['api_base'] = api_base
|
181 |
+
|
182 |
+
if generate_button:
|
183 |
+
update_placeholder = st.empty()
|
184 |
+
|
185 |
+
async def run_async_blogger():
|
186 |
+
message = AgentMessage(
|
187 |
+
sender='user',
|
188 |
+
content=f"请撰写一篇关于{topic}的博客文章,要求表达专业,生动有趣,并且易于理解。"
|
189 |
+
)
|
190 |
+
result = await st.session_state['blogger'].forward(message, update_placeholder)
|
191 |
+
return result
|
192 |
+
|
193 |
+
loop = asyncio.new_event_loop()
|
194 |
+
asyncio.set_event_loop(loop)
|
195 |
+
loop.run_until_complete(run_async_blogger())
|
196 |
+
|
197 |
+
if __name__ == '__main__':
|
198 |
+
main()
|
lagent/lagent.egg-info/PKG-INFO
ADDED
@@ -0,0 +1,600 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Metadata-Version: 2.1
|
2 |
+
Name: lagent
|
3 |
+
Version: 0.5.0rc1
|
4 |
+
Summary: A lightweight framework for building LLM-based agents
|
5 |
+
Home-page: https://github.com/InternLM/lagent
|
6 |
+
License: Apache 2.0
|
7 |
+
Keywords: artificial general intelligence,agent,agi,llm
|
8 |
+
Description-Content-Type: text/markdown
|
9 |
+
License-File: LICENSE
|
10 |
+
Requires-Dist: aiohttp
|
11 |
+
Requires-Dist: arxiv
|
12 |
+
Requires-Dist: asyncache
|
13 |
+
Requires-Dist: asyncer
|
14 |
+
Requires-Dist: distro
|
15 |
+
Requires-Dist: duckduckgo_search==5.3.1b1
|
16 |
+
Requires-Dist: filelock
|
17 |
+
Requires-Dist: func_timeout
|
18 |
+
Requires-Dist: griffe<1.0
|
19 |
+
Requires-Dist: json5
|
20 |
+
Requires-Dist: jsonschema
|
21 |
+
Requires-Dist: jupyter==1.0.0
|
22 |
+
Requires-Dist: jupyter_client==8.6.2
|
23 |
+
Requires-Dist: jupyter_core==5.7.2
|
24 |
+
Requires-Dist: pydantic==2.6.4
|
25 |
+
Requires-Dist: requests
|
26 |
+
Requires-Dist: termcolor
|
27 |
+
Requires-Dist: tiktoken
|
28 |
+
Requires-Dist: timeout-decorator
|
29 |
+
Requires-Dist: typing-extensions
|
30 |
+
Provides-Extra: all
|
31 |
+
Requires-Dist: google-search-results; extra == "all"
|
32 |
+
Requires-Dist: lmdeploy>=0.2.5; extra == "all"
|
33 |
+
Requires-Dist: pillow; extra == "all"
|
34 |
+
Requires-Dist: python-pptx; extra == "all"
|
35 |
+
Requires-Dist: timeout_decorator; extra == "all"
|
36 |
+
Requires-Dist: torch; extra == "all"
|
37 |
+
Requires-Dist: transformers<=4.40,>=4.34; extra == "all"
|
38 |
+
Requires-Dist: vllm>=0.3.3; extra == "all"
|
39 |
+
Requires-Dist: aiohttp; extra == "all"
|
40 |
+
Requires-Dist: arxiv; extra == "all"
|
41 |
+
Requires-Dist: asyncache; extra == "all"
|
42 |
+
Requires-Dist: asyncer; extra == "all"
|
43 |
+
Requires-Dist: distro; extra == "all"
|
44 |
+
Requires-Dist: duckduckgo_search==5.3.1b1; extra == "all"
|
45 |
+
Requires-Dist: filelock; extra == "all"
|
46 |
+
Requires-Dist: func_timeout; extra == "all"
|
47 |
+
Requires-Dist: griffe<1.0; extra == "all"
|
48 |
+
Requires-Dist: json5; extra == "all"
|
49 |
+
Requires-Dist: jsonschema; extra == "all"
|
50 |
+
Requires-Dist: jupyter==1.0.0; extra == "all"
|
51 |
+
Requires-Dist: jupyter_client==8.6.2; extra == "all"
|
52 |
+
Requires-Dist: jupyter_core==5.7.2; extra == "all"
|
53 |
+
Requires-Dist: pydantic==2.6.4; extra == "all"
|
54 |
+
Requires-Dist: requests; extra == "all"
|
55 |
+
Requires-Dist: termcolor; extra == "all"
|
56 |
+
Requires-Dist: tiktoken; extra == "all"
|
57 |
+
Requires-Dist: timeout-decorator; extra == "all"
|
58 |
+
Requires-Dist: typing-extensions; extra == "all"
|
59 |
+
Provides-Extra: optional
|
60 |
+
Requires-Dist: google-search-results; extra == "optional"
|
61 |
+
Requires-Dist: lmdeploy>=0.2.5; extra == "optional"
|
62 |
+
Requires-Dist: pillow; extra == "optional"
|
63 |
+
Requires-Dist: python-pptx; extra == "optional"
|
64 |
+
Requires-Dist: timeout_decorator; extra == "optional"
|
65 |
+
Requires-Dist: torch; extra == "optional"
|
66 |
+
Requires-Dist: transformers<=4.40,>=4.34; extra == "optional"
|
67 |
+
Requires-Dist: vllm>=0.3.3; extra == "optional"
|
68 |
+
|
69 |
+
<div id="top"></div>
|
70 |
+
<div align="center">
|
71 |
+
<img src="docs/imgs/lagent_logo.png" width="450"/>
|
72 |
+
|
73 |
+
[![docs](https://img.shields.io/badge/docs-latest-blue)](https://lagent.readthedocs.io/en/latest/)
|
74 |
+
[![PyPI](https://img.shields.io/pypi/v/lagent)](https://pypi.org/project/lagent)
|
75 |
+
[![license](https://img.shields.io/github/license/InternLM/lagent.svg)](https://github.com/InternLM/lagent/tree/main/LICENSE)
|
76 |
+
[![issue resolution](https://img.shields.io/github/issues-closed-raw/InternLM/lagent)](https://github.com/InternLM/lagent/issues)
|
77 |
+
[![open issues](https://img.shields.io/github/issues-raw/InternLM/lagent)](https://github.com/InternLM/lagent/issues)
|
78 |
+
![Visitors](https://api.visitorbadge.io/api/visitors?path=InternLM%2Flagent%20&countColor=%23263759&style=flat)
|
79 |
+
![GitHub forks](https://img.shields.io/github/forks/InternLM/lagent)
|
80 |
+
![GitHub Repo stars](https://img.shields.io/github/stars/InternLM/lagent)
|
81 |
+
![GitHub contributors](https://img.shields.io/github/contributors/InternLM/lagent)
|
82 |
+
|
83 |
+
</div>
|
84 |
+
|
85 |
+
<p align="center">
|
86 |
+
👋 join us on <a href="https://twitter.com/intern_lm" target="_blank">𝕏 (Twitter)</a>, <a href="https://discord.gg/xa29JuW87d" target="_blank">Discord</a> and <a href="https://r.vansin.top/?r=internwx" target="_blank">WeChat</a>
|
87 |
+
</p>
|
88 |
+
|
89 |
+
## Installation
|
90 |
+
|
91 |
+
Install from source:
|
92 |
+
|
93 |
+
```bash
|
94 |
+
git clone https://github.com/InternLM/lagent.git
|
95 |
+
cd lagent
|
96 |
+
pip install -e .
|
97 |
+
```
|
98 |
+
|
99 |
+
## Usage
|
100 |
+
|
101 |
+
Lagent is inspired by the design philosophy of PyTorch. We expect that the analogy of neural network layers will make the workflow clearer and more intuitive, so users only need to focus on creating layers and defining message passing between them in a Pythonic way. This is a simple tutorial to get you quickly started with building multi-agent applications.
|
102 |
+
|
103 |
+
### Models as Agents
|
104 |
+
|
105 |
+
Agents use `AgentMessage` for communication.
|
106 |
+
|
107 |
+
```python
|
108 |
+
from typing import Dict, List
|
109 |
+
from lagent.agents import Agent
|
110 |
+
from lagent.schema import AgentMessage
|
111 |
+
from lagent.llms import VllmModel, INTERNLM2_META
|
112 |
+
|
113 |
+
llm = VllmModel(
|
114 |
+
path='Qwen/Qwen2-7B-Instruct',
|
115 |
+
meta_template=INTERNLM2_META,
|
116 |
+
tp=1,
|
117 |
+
top_k=1,
|
118 |
+
temperature=1.0,
|
119 |
+
stop_words=['<|im_end|>'],
|
120 |
+
max_new_tokens=1024,
|
121 |
+
)
|
122 |
+
system_prompt = '你的回答只能从“典”、“孝”、“急”三个字中选一个。'
|
123 |
+
agent = Agent(llm, system_prompt)
|
124 |
+
|
125 |
+
user_msg = AgentMessage(sender='user', content='今天天气情况')
|
126 |
+
bot_msg = agent(user_msg)
|
127 |
+
print(bot_msg)
|
128 |
+
```
|
129 |
+
|
130 |
+
```
|
131 |
+
content='急' sender='Agent' formatted=None extra_info=None type=None receiver=None stream_state=<AgentStatusCode.END: 0>
|
132 |
+
```
|
133 |
+
|
134 |
+
### Memory as State
|
135 |
+
|
136 |
+
Both input and output messages will be added to the memory of `Agent` in each forward pass. This is performed in `__call__` rather than `forward`. See the following pseudo code
|
137 |
+
|
138 |
+
```python
|
139 |
+
def __call__(self, *message):
|
140 |
+
message = pre_hooks(message)
|
141 |
+
add_memory(message)
|
142 |
+
message = self.forward(*message)
|
143 |
+
add_memory(message)
|
144 |
+
message = post_hooks(message)
|
145 |
+
return message
|
146 |
+
```
|
147 |
+
|
148 |
+
Inspect the memory in two ways
|
149 |
+
|
150 |
+
```python
|
151 |
+
memory: List[AgentMessage] = agent.memory.get_memory()
|
152 |
+
print(memory)
|
153 |
+
print('-' * 120)
|
154 |
+
dumped_memory: Dict[str, List[dict]] = agent.state_dict()
|
155 |
+
print(dumped_memory['memory'])
|
156 |
+
```
|
157 |
+
|
158 |
+
```
|
159 |
+
[AgentMessage(content='今天天气情况', sender='user', formatted=None, extra_info=None, type=None, receiver=None, stream_state=<AgentStatusCode.END: 0>), AgentMessage(content='急', sender='Agent', formatted=None, extra_info=None, type=None, receiver=None, stream_state=<AgentStatusCode.END: 0>)]
|
160 |
+
------------------------------------------------------------------------------------------------------------------------
|
161 |
+
[{'content': '今天天气情况', 'sender': 'user', 'formatted': None, 'extra_info': None, 'type': None, 'receiver': None, 'stream_state': <AgentStatusCode.END: 0>}, {'content': '急', 'sender': 'Agent', 'formatted': None, 'extra_info': None, 'type': None, 'receiver': None, 'stream_state': <AgentStatusCode.END: 0>}]
|
162 |
+
```
|
163 |
+
|
164 |
+
Clear the memory of this session(`session_id=0` by default):
|
165 |
+
|
166 |
+
```python
|
167 |
+
agent.memory.reset()
|
168 |
+
```
|
169 |
+
|
170 |
+
### Custom Message Aggregation
|
171 |
+
|
172 |
+
`DefaultAggregator` is called under the hood to assemble and convert `AgentMessage` to OpenAI message format.
|
173 |
+
|
174 |
+
```python
|
175 |
+
def forward(self, *message: AgentMessage, session_id=0, **kwargs) -> Union[AgentMessage, str]:
|
176 |
+
formatted_messages = self.aggregator.aggregate(
|
177 |
+
self.memory.get(session_id),
|
178 |
+
self.name,
|
179 |
+
self.output_format,
|
180 |
+
self.template,
|
181 |
+
)
|
182 |
+
llm_response = self.llm.chat(formatted_messages, **kwargs)
|
183 |
+
...
|
184 |
+
```
|
185 |
+
|
186 |
+
Implement a simple aggregator that can receive few-shots
|
187 |
+
|
188 |
+
```python
|
189 |
+
from typing import List, Union
|
190 |
+
from lagent.memory import Memory
|
191 |
+
from lagent.prompts import StrParser
|
192 |
+
from lagent.agents.aggregator import DefaultAggregator
|
193 |
+
|
194 |
+
class FewshotAggregator(DefaultAggregator):
|
195 |
+
def __init__(self, few_shot: List[dict] = None):
|
196 |
+
self.few_shot = few_shot or []
|
197 |
+
|
198 |
+
def aggregate(self,
|
199 |
+
messages: Memory,
|
200 |
+
name: str,
|
201 |
+
parser: StrParser = None,
|
202 |
+
system_instruction: Union[str, dict, List[dict]] = None) -> List[dict]:
|
203 |
+
_message = []
|
204 |
+
if system_instruction:
|
205 |
+
_message.extend(
|
206 |
+
self.aggregate_system_intruction(system_instruction))
|
207 |
+
_message.extend(self.few_shot)
|
208 |
+
messages = messages.get_memory()
|
209 |
+
for message in messages:
|
210 |
+
if message.sender == name:
|
211 |
+
_message.append(
|
212 |
+
dict(role='assistant', content=str(message.content)))
|
213 |
+
else:
|
214 |
+
user_message = message.content
|
215 |
+
if len(_message) > 0 and _message[-1]['role'] == 'user':
|
216 |
+
_message[-1]['content'] += user_message
|
217 |
+
else:
|
218 |
+
_message.append(dict(role='user', content=user_message))
|
219 |
+
return _message
|
220 |
+
|
221 |
+
agent = Agent(
|
222 |
+
llm,
|
223 |
+
aggregator=FewshotAggregator(
|
224 |
+
[
|
225 |
+
{"role": "user", "content": "今天天气"},
|
226 |
+
{"role": "assistant", "content": "【晴】"},
|
227 |
+
]
|
228 |
+
)
|
229 |
+
)
|
230 |
+
user_msg = AgentMessage(sender='user', content='昨天天气')
|
231 |
+
bot_msg = agent(user_msg)
|
232 |
+
print(bot_msg)
|
233 |
+
```
|
234 |
+
|
235 |
+
```
|
236 |
+
content='【多云转晴,夜间有轻微降温】' sender='Agent' formatted=None extra_info=None type=None receiver=None stream_state=<AgentStatusCode.END: 0>
|
237 |
+
```
|
238 |
+
|
239 |
+
### Flexible Response Formatting
|
240 |
+
|
241 |
+
In `AgentMessage`, `formatted` is reserved to store information parsed by `output_format` from the model output.
|
242 |
+
|
243 |
+
```python
|
244 |
+
def forward(self, *message: AgentMessage, session_id=0, **kwargs) -> Union[AgentMessage, str]:
|
245 |
+
...
|
246 |
+
llm_response = self.llm.chat(formatted_messages, **kwargs)
|
247 |
+
if self.output_format:
|
248 |
+
formatted_messages = self.output_format.parse_response(llm_response)
|
249 |
+
return AgentMessage(
|
250 |
+
sender=self.name,
|
251 |
+
content=llm_response,
|
252 |
+
formatted=formatted_messages,
|
253 |
+
)
|
254 |
+
...
|
255 |
+
```
|
256 |
+
|
257 |
+
Use a tool parser as follows
|
258 |
+
|
259 |
+
````python
|
260 |
+
from lagent.prompts.parsers import ToolParser
|
261 |
+
|
262 |
+
system_prompt = "逐步分析并编写Python代码解决以下问题。"
|
263 |
+
parser = ToolParser(tool_type='code interpreter', begin='```python\n', end='\n```\n')
|
264 |
+
llm.gen_params['stop_words'].append('\n```\n')
|
265 |
+
agent = Agent(llm, system_prompt, output_format=parser)
|
266 |
+
|
267 |
+
user_msg = AgentMessage(
|
268 |
+
sender='user',
|
269 |
+
content='Marie is thinking of a multiple of 63, while Jay is thinking of a '
|
270 |
+
'factor of 63. They happen to be thinking of the same number. There are '
|
271 |
+
'two possibilities for the number that each of them is thinking of, one '
|
272 |
+
'positive and one negative. Find the product of these two numbers.')
|
273 |
+
bot_msg = agent(user_msg)
|
274 |
+
print(bot_msg.model_dump_json(indent=4))
|
275 |
+
````
|
276 |
+
|
277 |
+
````
|
278 |
+
{
|
279 |
+
"content": "首先,我们需要找出63的所有正因数和负因数。63的正因数可以通过分解63的质因数来找出,即\\(63 = 3^2 \\times 7\\)。因此,63的正因数包括1, 3, 7, 9, 21, 和 63。对于负因数,我们只需将上述正因数乘以-1。\n\n接下来,我们需要找出与63的正因数相乘的结果为63的数,以及与63的负因数相乘的结果为63的数。这可以通过将63除以每个正因数和负因数来实现。\n\n最后,我们将找到的两个数相乘得到最终答案。\n\n下面是Python代码实现:\n\n```python\ndef find_numbers():\n # 正因数\n positive_factors = [1, 3, 7, 9, 21, 63]\n # 负因数\n negative_factors = [-1, -3, -7, -9, -21, -63]\n \n # 找到与正因数相乘的结果为63的数\n positive_numbers = [63 / factor for factor in positive_factors]\n # 找到与负因数相乘的结果为63的数\n negative_numbers = [-63 / factor for factor in negative_factors]\n \n # 计算两个数的乘积\n product = positive_numbers[0] * negative_numbers[0]\n \n return product\n\nresult = find_numbers()\nprint(result)",
|
280 |
+
"sender": "Agent",
|
281 |
+
"formatted": {
|
282 |
+
"tool_type": "code interpreter",
|
283 |
+
"thought": "首先,我们需要找出63的所有正因数和负因数。63的正因数可以通过分解63的质因数来找出,即\\(63 = 3^2 \\times 7\\)。因此,63的正因数包括1, 3, 7, 9, 21, 和 63。对于负因数,我们只需将上述正因数乘以-1。\n\n接下来,我们需要找出与63的正因数相乘的结果为63的数,以及与63的负因数相乘的结果为63的数。这可以通过将63除以每个正因数和负因数来实现。\n\n最后,我们将找到的两个数相乘得到最终答案。\n\n下面是Python代码实现:\n\n",
|
284 |
+
"action": "def find_numbers():\n # 正因数\n positive_factors = [1, 3, 7, 9, 21, 63]\n # 负因数\n negative_factors = [-1, -3, -7, -9, -21, -63]\n \n # 找到与正因数相乘的结果为63的数\n positive_numbers = [63 / factor for factor in positive_factors]\n # 找到与负因数相乘的结果为63的数\n negative_numbers = [-63 / factor for factor in negative_factors]\n \n # 计算两个数的乘积\n product = positive_numbers[0] * negative_numbers[0]\n \n return product\n\nresult = find_numbers()\nprint(result)",
|
285 |
+
"status": 1
|
286 |
+
},
|
287 |
+
"extra_info": null,
|
288 |
+
"type": null,
|
289 |
+
"receiver": null,
|
290 |
+
"stream_state": 0
|
291 |
+
}
|
292 |
+
````
|
293 |
+
|
294 |
+
### Consistency of Tool Calling
|
295 |
+
|
296 |
+
`ActionExecutor` uses the same communication data structure as `Agent`, but requires the content of input `AgentMessage` to be a dict containing:
|
297 |
+
|
298 |
+
- `name`: tool name, e.g. `'IPythonInterpreter'`, `'WebBrowser.search'`.
|
299 |
+
- `parameters`: keyword arguments of the tool API, e.g. `{'command': 'import math;math.sqrt(2)'}`, `{'query': ['recent progress in AI']}`.
|
300 |
+
|
301 |
+
You can register custom hooks for message conversion.
|
302 |
+
|
303 |
+
```python
|
304 |
+
from lagent.hooks import Hook
|
305 |
+
from lagent.schema import ActionReturn, ActionStatusCode, AgentMessage
|
306 |
+
from lagent.actions import ActionExecutor, IPythonInteractive
|
307 |
+
|
308 |
+
class CodeProcessor(Hook):
|
309 |
+
def before_action(self, executor, message, session_id):
|
310 |
+
message = message.copy(deep=True)
|
311 |
+
message.content = dict(
|
312 |
+
name='IPythonInteractive', parameters={'command': message.formatted['action']}
|
313 |
+
)
|
314 |
+
return message
|
315 |
+
|
316 |
+
def after_action(self, executor, message, session_id):
|
317 |
+
action_return = message.content
|
318 |
+
if isinstance(action_return, ActionReturn):
|
319 |
+
if action_return.state == ActionStatusCode.SUCCESS:
|
320 |
+
response = action_return.format_result()
|
321 |
+
else:
|
322 |
+
response = action_return.errmsg
|
323 |
+
else:
|
324 |
+
response = action_return
|
325 |
+
message.content = response
|
326 |
+
return message
|
327 |
+
|
328 |
+
executor = ActionExecutor(actions=[IPythonInteractive()], hooks=[CodeProcessor()])
|
329 |
+
bot_msg = AgentMessage(
|
330 |
+
sender='Agent',
|
331 |
+
content='首先,我们需要...',
|
332 |
+
formatted={
|
333 |
+
'tool_type': 'code interpreter',
|
334 |
+
'thought': '首先,我们需要...',
|
335 |
+
'action': 'def find_numbers():\n # 正因数\n positive_factors = [1, 3, 7, 9, 21, 63]\n # 负因数\n negative_factors = [-1, -3, -7, -9, -21, -63]\n \n # 找到与正因数相乘的结果为63的数\n positive_numbers = [63 / factor for factor in positive_factors]\n # 找到与负因数相乘的结果为63的数\n negative_numbers = [-63 / factor for factor in negative_factors]\n \n # 计算两个数的乘积\n product = positive_numbers[0] * negative_numbers[0]\n \n return product\n\nresult = find_numbers()\nprint(result)',
|
336 |
+
'status': 1
|
337 |
+
})
|
338 |
+
executor_msg = executor(bot_msg)
|
339 |
+
print(executor_msg)
|
340 |
+
```
|
341 |
+
|
342 |
+
```
|
343 |
+
content='3969.0' sender='ActionExecutor' formatted=None extra_info=None type=None receiver=None stream_state=<AgentStatusCode.END: 0>
|
344 |
+
```
|
345 |
+
|
346 |
+
**For convenience, Lagent provides `InternLMActionProcessor` which is adapted to messages formatted by `ToolParser` as mentioned above.**
|
347 |
+
|
348 |
+
### Dual Interfaces
|
349 |
+
|
350 |
+
Lagent adopts dual interface design, where almost every component(LLMs, actions, action executors...) has the corresponding asynchronous variant by prefixing its identifier with 'Async'. It is recommended to use synchronous agents for debugging and asynchronous ones for large-scale inference to make the most of idle CPU and GPU resources.
|
351 |
+
|
352 |
+
However, make sure the internal consistency of agents, i.e. asynchronous agents should be equipped with asynchronous LLMs and asynchronous action executors that drive asynchronous tools.
|
353 |
+
|
354 |
+
```python
|
355 |
+
from lagent.llms import VllmModel, AsyncVllmModel, LMDeployPipeline, AsyncLMDeployPipeline
|
356 |
+
from lagent.actions import ActionExecutor, AsyncActionExecutor, WebBrowser, AsyncWebBrowser
|
357 |
+
from lagent.agents import Agent, AsyncAgent, AgentForInternLM, AsyncAgentForInternLM
|
358 |
+
```
|
359 |
+
|
360 |
+
______________________________________________________________________
|
361 |
+
|
362 |
+
## Practice
|
363 |
+
|
364 |
+
- **Try to implement `forward` instead of `__call__` of subclasses unless necessary.**
|
365 |
+
- **Always include the `session_id` argument explicitly, which is designed for isolation of memory, LLM requests and tool invocation(e.g. maintain multiple independent IPython environments) in concurrency.**
|
366 |
+
|
367 |
+
### Single Agent
|
368 |
+
|
369 |
+
Math agents that solve problems by programming
|
370 |
+
|
371 |
+
````python
|
372 |
+
from lagent.agents.aggregator import InternLMToolAggregator
|
373 |
+
|
374 |
+
class Coder(Agent):
|
375 |
+
def __init__(self, model_path, system_prompt, max_turn=3):
|
376 |
+
super().__init__()
|
377 |
+
llm = VllmModel(
|
378 |
+
path=model_path,
|
379 |
+
meta_template=INTERNLM2_META,
|
380 |
+
tp=1,
|
381 |
+
top_k=1,
|
382 |
+
temperature=1.0,
|
383 |
+
stop_words=['\n```\n', '<|im_end|>'],
|
384 |
+
max_new_tokens=1024,
|
385 |
+
)
|
386 |
+
self.agent = Agent(
|
387 |
+
llm,
|
388 |
+
system_prompt,
|
389 |
+
output_format=ToolParser(
|
390 |
+
tool_type='code interpreter', begin='```python\n', end='\n```\n'
|
391 |
+
),
|
392 |
+
# `InternLMToolAggregator` is adapted to `ToolParser` for aggregating
|
393 |
+
# messages with tool invocations and execution results
|
394 |
+
aggregator=InternLMToolAggregator(),
|
395 |
+
)
|
396 |
+
self.executor = ActionExecutor([IPythonInteractive()], hooks=[CodeProcessor()])
|
397 |
+
self.max_turn = max_turn
|
398 |
+
|
399 |
+
def forward(self, message: AgentMessage, session_id=0) -> AgentMessage:
|
400 |
+
for _ in range(self.max_turn):
|
401 |
+
message = self.agent(message, session_id=session_id)
|
402 |
+
if message.formatted['tool_type'] is None:
|
403 |
+
return message
|
404 |
+
message = self.executor(message, session_id=session_id)
|
405 |
+
return message
|
406 |
+
|
407 |
+
coder = Coder('Qwen/Qwen2-7B-Instruct', 'Solve the problem step by step with assistance of Python code')
|
408 |
+
query = AgentMessage(
|
409 |
+
sender='user',
|
410 |
+
content='Find the projection of $\\mathbf{a}$ onto $\\mathbf{b} = '
|
411 |
+
'\\begin{pmatrix} 1 \\\\ -3 \\end{pmatrix}$ if $\\mathbf{a} \\cdot \\mathbf{b} = 2.$'
|
412 |
+
)
|
413 |
+
answer = coder(query)
|
414 |
+
print(answer.content)
|
415 |
+
print('-' * 120)
|
416 |
+
for msg in coder.state_dict()['agent.memory']:
|
417 |
+
print('*' * 80)
|
418 |
+
print(f'{msg["sender"]}:\n\n{msg["content"]}')
|
419 |
+
````
|
420 |
+
|
421 |
+
### Multiple Agents
|
422 |
+
|
423 |
+
Asynchronous blogging agents that improve writing quality by self-refinement ([original AutoGen example](https://microsoft.github.io/autogen/0.2/docs/topics/prompting-and-reasoning/reflection/))
|
424 |
+
|
425 |
+
```python
|
426 |
+
import asyncio
|
427 |
+
import os
|
428 |
+
from lagent.llms import AsyncGPTAPI
|
429 |
+
from lagent.agents import AsyncAgent
|
430 |
+
os.environ['OPENAI_API_KEY'] = 'YOUR_API_KEY'
|
431 |
+
|
432 |
+
class PrefixedMessageHook(Hook):
|
433 |
+
def __init__(self, prefix: str, senders: list = None):
|
434 |
+
self.prefix = prefix
|
435 |
+
self.senders = senders or []
|
436 |
+
|
437 |
+
def before_agent(self, agent, messages, session_id):
|
438 |
+
for message in messages:
|
439 |
+
if message.sender in self.senders:
|
440 |
+
message.content = self.prefix + message.content
|
441 |
+
|
442 |
+
class AsyncBlogger(AsyncAgent):
|
443 |
+
def __init__(self, model_path, writer_prompt, critic_prompt, critic_prefix='', max_turn=3):
|
444 |
+
super().__init__()
|
445 |
+
llm = AsyncGPTAPI(model_type=model_path, retry=5, max_new_tokens=2048)
|
446 |
+
self.writer = AsyncAgent(llm, writer_prompt, name='writer')
|
447 |
+
self.critic = AsyncAgent(
|
448 |
+
llm, critic_prompt, name='critic', hooks=[PrefixedMessageHook(critic_prefix, ['writer'])]
|
449 |
+
)
|
450 |
+
self.max_turn = max_turn
|
451 |
+
|
452 |
+
async def forward(self, message: AgentMessage, session_id=0) -> AgentMessage:
|
453 |
+
for _ in range(self.max_turn):
|
454 |
+
message = await self.writer(message, session_id=session_id)
|
455 |
+
message = await self.critic(message, session_id=session_id)
|
456 |
+
return await self.writer(message, session_id=session_id)
|
457 |
+
|
458 |
+
blogger = AsyncBlogger(
|
459 |
+
'gpt-4o-2024-05-13',
|
460 |
+
writer_prompt="You are an writing assistant tasked to write engaging blogpost. You try to generate the best blogpost possible for the user's request. "
|
461 |
+
"If the user provides critique, then respond with a revised version of your previous attempts",
|
462 |
+
critic_prompt="Generate critique and recommendations on the writing. Provide detailed recommendations, including requests for length, depth, style, etc..",
|
463 |
+
critic_prefix='Reflect and provide critique on the following writing. \n\n',
|
464 |
+
)
|
465 |
+
user_prompt = (
|
466 |
+
"Write an engaging blogpost on the recent updates in {topic}. "
|
467 |
+
"The blogpost should be engaging and understandable for general audience. "
|
468 |
+
"Should have more than 3 paragraphes but no longer than 1000 words.")
|
469 |
+
bot_msgs = asyncio.get_event_loop().run_until_complete(
|
470 |
+
asyncio.gather(
|
471 |
+
*[
|
472 |
+
blogger(AgentMessage(sender='user', content=user_prompt.format(topic=topic)), session_id=i)
|
473 |
+
for i, topic in enumerate(['AI', 'Biotechnology', 'New Energy', 'Video Games', 'Pop Music'])
|
474 |
+
]
|
475 |
+
)
|
476 |
+
)
|
477 |
+
print(bot_msgs[0].content)
|
478 |
+
print('-' * 120)
|
479 |
+
for msg in blogger.state_dict(session_id=0)['writer.memory']:
|
480 |
+
print('*' * 80)
|
481 |
+
print(f'{msg["sender"]}:\n\n{msg["content"]}')
|
482 |
+
print('-' * 120)
|
483 |
+
for msg in blogger.state_dict(session_id=0)['critic.memory']:
|
484 |
+
print('*' * 80)
|
485 |
+
print(f'{msg["sender"]}:\n\n{msg["content"]}')
|
486 |
+
```
|
487 |
+
|
488 |
+
A multi-agent workflow that performs information retrieval, data collection and chart plotting ([original LangGraph example](https://vijaykumarkartha.medium.com/multiple-ai-agents-creating-multi-agent-workflows-using-langgraph-and-langchain-0587406ec4e6))
|
489 |
+
|
490 |
+
<div align="center">
|
491 |
+
<img src="https://miro.medium.com/v2/resize:fit:1400/format:webp/1*ffzadZCKXJT7n4JaRVFvcQ.jpeg" width="850" />
|
492 |
+
</div>
|
493 |
+
|
494 |
+
````python
|
495 |
+
import json
|
496 |
+
from lagent.actions import IPythonInterpreter, WebBrowser, ActionExecutor
|
497 |
+
from lagent.agents.stream import get_plugin_prompt
|
498 |
+
from lagent.llms import GPTAPI
|
499 |
+
from lagent.hooks import InternLMActionProcessor
|
500 |
+
|
501 |
+
TOOL_TEMPLATE = (
|
502 |
+
"You are a helpful AI assistant, collaborating with other assistants. Use the provided tools to progress"
|
503 |
+
" towards answering the question. If you are unable to fully answer, that's OK, another assistant with"
|
504 |
+
" different tools will help where you left off. Execute what you can to make progress. If you or any of"
|
505 |
+
" the other assistants have the final answer or deliverable, prefix your response with {finish_pattern}"
|
506 |
+
" so the team knows to stop. You have access to the following tools:\n{tool_description}\nPlease provide"
|
507 |
+
" your thought process when you need to use a tool, followed by the call statement in this format:"
|
508 |
+
"\n{invocation_format}\\\\n**{system_prompt}**"
|
509 |
+
)
|
510 |
+
|
511 |
+
class DataVisualizer(Agent):
|
512 |
+
def __init__(self, model_path, research_prompt, chart_prompt, finish_pattern="Final Answer", max_turn=10):
|
513 |
+
super().__init__()
|
514 |
+
llm = GPTAPI(model_path, key='YOUR_OPENAI_API_KEY', retry=5, max_new_tokens=1024, stop_words=["```\n"])
|
515 |
+
interpreter, browser = IPythonInterpreter(), WebBrowser("BingSearch", api_key="YOUR_BING_API_KEY")
|
516 |
+
self.researcher = Agent(
|
517 |
+
llm,
|
518 |
+
TOOL_TEMPLATE.format(
|
519 |
+
finish_pattern=finish_pattern,
|
520 |
+
tool_description=get_plugin_prompt(browser),
|
521 |
+
invocation_format='```json\n{"name": {{tool name}}, "parameters": {{keyword arguments}}}\n```\n',
|
522 |
+
system_prompt=research_prompt,
|
523 |
+
),
|
524 |
+
output_format=ToolParser(
|
525 |
+
"browser",
|
526 |
+
begin="```json\n",
|
527 |
+
end="\n```\n",
|
528 |
+
validate=lambda x: json.loads(x.rstrip('`')),
|
529 |
+
),
|
530 |
+
aggregator=InternLMToolAggregator(),
|
531 |
+
name="researcher",
|
532 |
+
)
|
533 |
+
self.charter = Agent(
|
534 |
+
llm,
|
535 |
+
TOOL_TEMPLATE.format(
|
536 |
+
finish_pattern=finish_pattern,
|
537 |
+
tool_description=interpreter.name,
|
538 |
+
invocation_format='```python\n{{code}}\n```\n',
|
539 |
+
system_prompt=chart_prompt,
|
540 |
+
),
|
541 |
+
output_format=ToolParser(
|
542 |
+
"interpreter",
|
543 |
+
begin="```python\n",
|
544 |
+
end="\n```\n",
|
545 |
+
validate=lambda x: x.rstrip('`'),
|
546 |
+
),
|
547 |
+
aggregator=InternLMToolAggregator(),
|
548 |
+
name="charter",
|
549 |
+
)
|
550 |
+
self.executor = ActionExecutor([interpreter, browser], hooks=[InternLMActionProcessor()])
|
551 |
+
self.finish_pattern = finish_pattern
|
552 |
+
self.max_turn = max_turn
|
553 |
+
|
554 |
+
def forward(self, message, session_id=0):
|
555 |
+
for _ in range(self.max_turn):
|
556 |
+
message = self.researcher(message, session_id=session_id, stop_words=["```\n", "```python"]) # override llm stop words
|
557 |
+
while message.formatted["tool_type"]:
|
558 |
+
message = self.executor(message, session_id=session_id)
|
559 |
+
message = self.researcher(message, session_id=session_id, stop_words=["```\n", "```python"])
|
560 |
+
if self.finish_pattern in message.content:
|
561 |
+
return message
|
562 |
+
message = self.charter(message)
|
563 |
+
while message.formatted["tool_type"]:
|
564 |
+
message = self.executor(message, session_id=session_id)
|
565 |
+
message = self.charter(message, session_id=session_id)
|
566 |
+
if self.finish_pattern in message.content:
|
567 |
+
return message
|
568 |
+
return message
|
569 |
+
|
570 |
+
visualizer = DataVisualizer(
|
571 |
+
"gpt-4o-2024-05-13",
|
572 |
+
research_prompt="You should provide accurate data for the chart generator to use.",
|
573 |
+
chart_prompt="Any charts you display will be visible by the user.",
|
574 |
+
)
|
575 |
+
user_msg = AgentMessage(
|
576 |
+
sender='user',
|
577 |
+
content="Fetch the China's GDP over the past 5 years, then draw a line graph of it. Once you code it up, finish.")
|
578 |
+
bot_msg = visualizer(user_msg)
|
579 |
+
print(bot_msg.content)
|
580 |
+
json.dump(visualizer.state_dict(), open('visualizer.json', 'w'), ensure_ascii=False, indent=4)
|
581 |
+
````
|
582 |
+
|
583 |
+
## Citation
|
584 |
+
|
585 |
+
If you find this project useful in your research, please consider cite:
|
586 |
+
|
587 |
+
```latex
|
588 |
+
@misc{lagent2023,
|
589 |
+
title={{Lagent: InternLM} a lightweight open-source framework that allows users to efficiently build large language model(LLM)-based agents},
|
590 |
+
author={Lagent Developer Team},
|
591 |
+
howpublished = {\url{https://github.com/InternLM/lagent}},
|
592 |
+
year={2023}
|
593 |
+
}
|
594 |
+
```
|
595 |
+
|
596 |
+
## License
|
597 |
+
|
598 |
+
This project is released under the [Apache 2.0 license](LICENSE).
|
599 |
+
|
600 |
+
<p align="right"><a href="#top">🔼 Back to top</a></p>
|
lagent/lagent.egg-info/SOURCES.txt
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
LICENSE
|
2 |
+
MANIFEST.in
|
3 |
+
README.md
|
4 |
+
setup.cfg
|
5 |
+
setup.py
|
6 |
+
lagent/__init__.py
|
7 |
+
lagent/schema.py
|
8 |
+
lagent/version.py
|
9 |
+
lagent.egg-info/PKG-INFO
|
10 |
+
lagent.egg-info/SOURCES.txt
|
11 |
+
lagent.egg-info/dependency_links.txt
|
12 |
+
lagent.egg-info/requires.txt
|
13 |
+
lagent.egg-info/top_level.txt
|
14 |
+
lagent/actions/__init__.py
|
15 |
+
lagent/actions/action_executor.py
|
16 |
+
lagent/actions/arxiv_search.py
|
17 |
+
lagent/actions/base_action.py
|
18 |
+
lagent/actions/bing_map.py
|
19 |
+
lagent/actions/builtin_actions.py
|
20 |
+
lagent/actions/google_scholar_search.py
|
21 |
+
lagent/actions/google_search.py
|
22 |
+
lagent/actions/ipython_interactive.py
|
23 |
+
lagent/actions/ipython_interpreter.py
|
24 |
+
lagent/actions/ipython_manager.py
|
25 |
+
lagent/actions/parser.py
|
26 |
+
lagent/actions/ppt.py
|
27 |
+
lagent/actions/python_interpreter.py
|
28 |
+
lagent/actions/web_browser.py
|
29 |
+
lagent/agents/__init__.py
|
30 |
+
lagent/agents/agent.py
|
31 |
+
lagent/agents/react.py
|
32 |
+
lagent/agents/stream.py
|
33 |
+
lagent/agents/aggregator/__init__.py
|
34 |
+
lagent/agents/aggregator/default_aggregator.py
|
35 |
+
lagent/agents/aggregator/tool_aggregator.py
|
36 |
+
lagent/distributed/__init__.py
|
37 |
+
lagent/distributed/http_serve/__init__.py
|
38 |
+
lagent/distributed/http_serve/api_server.py
|
39 |
+
lagent/distributed/http_serve/app.py
|
40 |
+
lagent/distributed/ray_serve/__init__.py
|
41 |
+
lagent/distributed/ray_serve/ray_warpper.py
|
42 |
+
lagent/hooks/__init__.py
|
43 |
+
lagent/hooks/action_preprocessor.py
|
44 |
+
lagent/hooks/hook.py
|
45 |
+
lagent/hooks/logger.py
|
46 |
+
lagent/llms/__init__.py
|
47 |
+
lagent/llms/base_api.py
|
48 |
+
lagent/llms/base_llm.py
|
49 |
+
lagent/llms/huggingface.py
|
50 |
+
lagent/llms/lmdeploy_wrapper.py
|
51 |
+
lagent/llms/meta_template.py
|
52 |
+
lagent/llms/openai.py
|
53 |
+
lagent/llms/sensenova.py
|
54 |
+
lagent/llms/vllm_wrapper.py
|
55 |
+
lagent/memory/__init__.py
|
56 |
+
lagent/memory/base_memory.py
|
57 |
+
lagent/memory/manager.py
|
58 |
+
lagent/prompts/__init__.py
|
59 |
+
lagent/prompts/prompt_template.py
|
60 |
+
lagent/prompts/parsers/__init__.py
|
61 |
+
lagent/prompts/parsers/custom_parser.py
|
62 |
+
lagent/prompts/parsers/json_parser.py
|
63 |
+
lagent/prompts/parsers/str_parser.py
|
64 |
+
lagent/prompts/parsers/tool_parser.py
|
65 |
+
lagent/utils/__init__.py
|
66 |
+
lagent/utils/gen_key.py
|
67 |
+
lagent/utils/package.py
|
68 |
+
lagent/utils/util.py
|
69 |
+
requirements/docs.txt
|
70 |
+
requirements/optional.txt
|
71 |
+
requirements/runtime.txt
|
lagent/lagent.egg-info/dependency_links.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
|
lagent/lagent.egg-info/requires.txt
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
aiohttp
|
2 |
+
arxiv
|
3 |
+
asyncache
|
4 |
+
asyncer
|
5 |
+
distro
|
6 |
+
duckduckgo_search==5.3.1b1
|
7 |
+
filelock
|
8 |
+
func_timeout
|
9 |
+
griffe<1.0
|
10 |
+
json5
|
11 |
+
jsonschema
|
12 |
+
jupyter==1.0.0
|
13 |
+
jupyter_client==8.6.2
|
14 |
+
jupyter_core==5.7.2
|
15 |
+
pydantic==2.6.4
|
16 |
+
requests
|
17 |
+
termcolor
|
18 |
+
tiktoken
|
19 |
+
timeout-decorator
|
20 |
+
typing-extensions
|
21 |
+
|
22 |
+
[all]
|
23 |
+
google-search-results
|
24 |
+
lmdeploy>=0.2.5
|
25 |
+
pillow
|
26 |
+
python-pptx
|
27 |
+
timeout_decorator
|
28 |
+
torch
|
29 |
+
transformers<=4.40,>=4.34
|
30 |
+
vllm>=0.3.3
|
31 |
+
aiohttp
|
32 |
+
arxiv
|
33 |
+
asyncache
|
34 |
+
asyncer
|
35 |
+
distro
|
36 |
+
duckduckgo_search==5.3.1b1
|
37 |
+
filelock
|
38 |
+
func_timeout
|
39 |
+
griffe<1.0
|
40 |
+
json5
|
41 |
+
jsonschema
|
42 |
+
jupyter==1.0.0
|
43 |
+
jupyter_client==8.6.2
|
44 |
+
jupyter_core==5.7.2
|
45 |
+
pydantic==2.6.4
|
46 |
+
requests
|
47 |
+
termcolor
|
48 |
+
tiktoken
|
49 |
+
typing-extensions
|
50 |
+
|
51 |
+
[optional]
|
52 |
+
google-search-results
|
53 |
+
lmdeploy>=0.2.5
|
54 |
+
pillow
|
55 |
+
python-pptx
|
56 |
+
timeout_decorator
|
57 |
+
torch
|
58 |
+
transformers<=4.40,>=4.34
|
59 |
+
vllm>=0.3.3
|
lagent/lagent.egg-info/top_level.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
lagent
|
lagent/lagent/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (231 Bytes). View file
|
|
lagent/lagent/__pycache__/schema.cpython-310.pyc
ADDED
Binary file (3.46 kB). View file
|
|
lagent/lagent/__pycache__/version.cpython-310.pyc
ADDED
Binary file (744 Bytes). View file
|
|
lagent/lagent/actions/.ipynb_checkpoints/__init__-checkpoint.py
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from .action_executor import ActionExecutor, AsyncActionExecutor
|
2 |
+
from .arxiv_search import ArxivSearch, AsyncArxivSearch
|
3 |
+
from .base_action import BaseAction, tool_api
|
4 |
+
from .bing_map import AsyncBINGMap, BINGMap
|
5 |
+
from .builtin_actions import FinishAction, InvalidAction, NoAction
|
6 |
+
from .google_scholar_search import AsyncGoogleScholar, GoogleScholar
|
7 |
+
from .google_search import AsyncGoogleSearch, GoogleSearch
|
8 |
+
from .ipython_interactive import AsyncIPythonInteractive, IPythonInteractive
|
9 |
+
from .ipython_interpreter import AsyncIPythonInterpreter, IPythonInterpreter
|
10 |
+
from .ipython_manager import IPythonInteractiveManager
|
11 |
+
from .parser import BaseParser, JsonParser, TupleParser
|
12 |
+
from .ppt import PPT, AsyncPPT
|
13 |
+
from .python_interpreter import AsyncPythonInterpreter, PythonInterpreter
|
14 |
+
from .web_browser import AsyncWebBrowser, WebBrowser
|
15 |
+
from .weather_query import WeatherQuery
|
16 |
+
|
17 |
+
__all__ = [
|
18 |
+
'BaseAction', 'ActionExecutor', 'AsyncActionExecutor', 'InvalidAction',
|
19 |
+
'FinishAction', 'NoAction', 'BINGMap', 'AsyncBINGMap', 'ArxivSearch',
|
20 |
+
'AsyncArxivSearch', 'GoogleSearch', 'AsyncGoogleSearch', 'GoogleScholar',
|
21 |
+
'AsyncGoogleScholar', 'IPythonInterpreter', 'AsyncIPythonInterpreter',
|
22 |
+
'IPythonInteractive', 'AsyncIPythonInteractive',
|
23 |
+
'IPythonInteractiveManager', 'PythonInterpreter', 'AsyncPythonInterpreter',
|
24 |
+
'PPT', 'AsyncPPT', 'WebBrowser', 'AsyncWebBrowser', 'BaseParser',
|
25 |
+
'JsonParser', 'TupleParser', 'tool_api', 'WeatherQuery'
|
26 |
+
]
|
lagent/lagent/actions/.ipynb_checkpoints/weather_query-checkpoint.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import requests
|
3 |
+
from lagent.actions.base_action import BaseAction, tool_api
|
4 |
+
from lagent.schema import ActionReturn, ActionStatusCode
|
5 |
+
|
6 |
+
class WeatherQuery(BaseAction):
|
7 |
+
def __init__(self):
|
8 |
+
super().__init__()
|
9 |
+
self.api_key = os.getenv("weather_token")
|
10 |
+
print(self.api_key)
|
11 |
+
if not self.api_key:
|
12 |
+
raise EnvironmentError("未找到环境变量 'token'。请设置你的和风天气 API Key 到 'weather_token' 环境变量中,比如export weather_token='xxx' ")
|
13 |
+
|
14 |
+
@tool_api
|
15 |
+
def run(self, location: str) -> dict:
|
16 |
+
"""
|
17 |
+
查询实时天气信息。
|
18 |
+
|
19 |
+
Args:
|
20 |
+
location (str): 要查询的地点名称、LocationID 或经纬度坐标(如 "101010100" 或 "116.41,39.92")。
|
21 |
+
|
22 |
+
Returns:
|
23 |
+
dict: 包含天气信息的字典
|
24 |
+
* location: 地点名称
|
25 |
+
* weather: 天气状况
|
26 |
+
* temperature: 当前温度
|
27 |
+
* wind_direction: 风向
|
28 |
+
* wind_speed: 风速(公里/小时)
|
29 |
+
* humidity: 相对湿度(%)
|
30 |
+
* report_time: 数据报告时间
|
31 |
+
"""
|
32 |
+
try:
|
33 |
+
# 如果 location 不是坐标格式(例如 "116.41,39.92"),则调用 GeoAPI 获取 LocationID
|
34 |
+
if not ("," in location and location.replace(",", "").replace(".", "").isdigit()):
|
35 |
+
# 使用 GeoAPI 获取 LocationID
|
36 |
+
geo_url = f"https://geoapi.qweather.com/v2/city/lookup?location={location}&key={self.api_key}"
|
37 |
+
geo_response = requests.get(geo_url)
|
38 |
+
geo_data = geo_response.json()
|
39 |
+
|
40 |
+
if geo_data.get("code") != "200" or not geo_data.get("location"):
|
41 |
+
raise Exception(f"GeoAPI 返回错误码:{geo_data.get('code')} 或未找到位置")
|
42 |
+
|
43 |
+
location = geo_data["location"][0]["id"]
|
44 |
+
|
45 |
+
# 构建天气查询的 API 请求 URL
|
46 |
+
weather_url = f"https://devapi.qweather.com/v7/weather/now?location={location}&key={self.api_key}"
|
47 |
+
response = requests.get(weather_url)
|
48 |
+
data = response.json()
|
49 |
+
|
50 |
+
# 检查 API 响应码
|
51 |
+
if data.get("code") != "200":
|
52 |
+
raise Exception(f"Weather API 返回错误码:{data.get('code')}")
|
53 |
+
|
54 |
+
# 解析和组织天气信息
|
55 |
+
weather_info = {
|
56 |
+
"location": location,
|
57 |
+
"weather": data["now"]["text"],
|
58 |
+
"temperature": data["now"]["temp"] + "°C",
|
59 |
+
"wind_direction": data["now"]["windDir"],
|
60 |
+
"wind_speed": data["now"]["windSpeed"] + " km/h",
|
61 |
+
"humidity": data["now"]["humidity"] + "%",
|
62 |
+
"report_time": data["updateTime"]
|
63 |
+
}
|
64 |
+
|
65 |
+
return {"result": weather_info}
|
66 |
+
|
67 |
+
except Exception as exc:
|
68 |
+
return ActionReturn(
|
69 |
+
errmsg=f"WeatherQuery 异常:{exc}",
|
70 |
+
state=ActionStatusCode.HTTP_ERROR
|
71 |
+
)
|
lagent/lagent/actions/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (1.47 kB). View file
|
|
lagent/lagent/actions/__pycache__/action_executor.cpython-310.pyc
ADDED
Binary file (5.84 kB). View file
|
|
lagent/lagent/actions/__pycache__/arxiv_search.cpython-310.pyc
ADDED
Binary file (3.19 kB). View file
|
|
lagent/lagent/actions/__pycache__/base_action.cpython-310.pyc
ADDED
Binary file (11.6 kB). View file
|
|
lagent/lagent/actions/__pycache__/bing_map.cpython-310.pyc
ADDED
Binary file (7.79 kB). View file
|
|
lagent/lagent/actions/__pycache__/builtin_actions.cpython-310.pyc
ADDED
Binary file (3.89 kB). View file
|
|
lagent/lagent/actions/__pycache__/google_scholar_search.cpython-310.pyc
ADDED
Binary file (13 kB). View file
|
|
lagent/lagent/actions/__pycache__/google_search.cpython-310.pyc
ADDED
Binary file (6.93 kB). View file
|
|
lagent/lagent/actions/__pycache__/ipython_interactive.cpython-310.pyc
ADDED
Binary file (8.41 kB). View file
|
|
lagent/lagent/actions/__pycache__/ipython_interpreter.cpython-310.pyc
ADDED
Binary file (16.6 kB). View file
|
|
lagent/lagent/actions/__pycache__/ipython_manager.cpython-310.pyc
ADDED
Binary file (7.11 kB). View file
|
|
lagent/lagent/actions/__pycache__/parser.cpython-310.pyc
ADDED
Binary file (5.48 kB). View file
|
|
lagent/lagent/actions/__pycache__/ppt.cpython-310.pyc
ADDED
Binary file (6.81 kB). View file
|
|
lagent/lagent/actions/__pycache__/python_interpreter.cpython-310.pyc
ADDED
Binary file (5.38 kB). View file
|
|
lagent/lagent/actions/__pycache__/weather_query.cpython-310.pyc
ADDED
Binary file (2.66 kB). View file
|
|
lagent/lagent/actions/__pycache__/web_browser.cpython-310.pyc
ADDED
Binary file (28.8 kB). View file
|
|
lagent/lagent/agents/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (547 Bytes). View file
|
|
lagent/lagent/agents/__pycache__/agent.cpython-310.pyc
ADDED
Binary file (12.9 kB). View file
|
|
lagent/lagent/agents/__pycache__/react.cpython-310.pyc
ADDED
Binary file (4.85 kB). View file
|
|
lagent/lagent/agents/__pycache__/stream.cpython-310.pyc
ADDED
Binary file (8.95 kB). View file
|
|
lagent/lagent/agents/aggregator/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (305 Bytes). View file
|
|
lagent/lagent/agents/aggregator/__pycache__/default_aggregator.cpython-310.pyc
ADDED
Binary file (1.6 kB). View file
|
|
lagent/lagent/agents/aggregator/__pycache__/tool_aggregator.cpython-310.pyc
ADDED
Binary file (2.71 kB). View file
|
|
lagent/lagent/hooks/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (387 Bytes). View file
|
|
lagent/lagent/hooks/__pycache__/action_preprocessor.cpython-310.pyc
ADDED
Binary file (2.29 kB). View file
|
|
lagent/lagent/hooks/__pycache__/hook.cpython-310.pyc
ADDED
Binary file (1.53 kB). View file
|
|
lagent/lagent/hooks/__pycache__/logger.cpython-310.pyc
ADDED
Binary file (1.77 kB). View file
|
|
lagent/lagent/llms/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (925 Bytes). View file
|
|
lagent/lagent/llms/__pycache__/base_api.cpython-310.pyc
ADDED
Binary file (5.29 kB). View file
|
|
lagent/lagent/llms/__pycache__/base_llm.cpython-310.pyc
ADDED
Binary file (7.96 kB). View file
|
|
lagent/lagent/llms/__pycache__/huggingface.cpython-310.pyc
ADDED
Binary file (9.67 kB). View file
|
|
lagent/lagent/llms/__pycache__/lmdeploy_wrapper.cpython-310.pyc
ADDED
Binary file (22.5 kB). View file
|
|
lagent/lagent/llms/__pycache__/meta_template.cpython-310.pyc
ADDED
Binary file (751 Bytes). View file
|
|
lagent/lagent/llms/__pycache__/openai.cpython-310.pyc
ADDED
Binary file (17.4 kB). View file
|
|
lagent/lagent/llms/__pycache__/sensenova.cpython-310.pyc
ADDED
Binary file (9.99 kB). View file
|
|
lagent/lagent/llms/__pycache__/vllm_wrapper.cpython-310.pyc
ADDED
Binary file (6.18 kB). View file
|
|
lagent/lagent/memory/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (259 Bytes). View file
|
|
lagent/lagent/memory/__pycache__/base_memory.cpython-310.pyc
ADDED
Binary file (2.24 kB). View file
|
|