API is valid but got invalid response, I am not sure where I got it wrong.
API is valid but got invalid response, I am not sure where I got it wrong.
import os
import logging
import warnings as wa
wa.warn_explicit = wa.warn = lambda *_, **__: None
from datetime import datetime
from configparser import ConfigParser
from lagent.agents import AgentForInternLM
from lagent.actions import WebBrowser
from lagent.prompts import PluginParser
from lagent.llms import GPTAPI
from lagent.agents.stream import get_plugin_prompt
from typing import List
from src.utils.debugger.custom_print import enable_custom_print
from src.agent.prompts import (
searcher_context_template_cn,
searcher_input_template_cn,
searcher_system_prompt_cn,
)
enable_custom_print()
config = ConfigParser()
config.read("config/config.ini")
bing_api_key = config.get("BING", "BING_API_KEY")
subscription_key = config.get("SILICON", "SILICON_API_KEY")
date = datetime.now().strftime("The current date is %Y-%m-%d.")
llm = GPTAPI(
model_type="internlm/internlm2_5-7b-chat",
key=subscription_key,
api_base="https://api.siliconflow.cn/v1/chat/completions",
meta_template=[
dict(role="system", api_role="system"),
dict(role="user", api_role="user"),
dict(role="assistant", api_role="assistant"),
dict(role="environment", api_role="system"),
],
top_k=1,
top_p=0.8,
temperature=0,
max_new_tokens=8192,
repetition_penalty=1.02,
stop_words=["<|im_end|>"],
)
plugins = [
WebBrowser(
searcher_type="BingSearch",
topk=6,
api_key=bing_api_key or os.environ.get("BING_API_KEY"),
)
]
# agent = AgentForInternLM(
# llm=llm, # LLM模型配置
# max_turn=4, # 最大对话轮数
# plugins=plugins, # 可用的插件列表
# template=search_template_cn,
# output_format=PluginParser(
# template=(searcher_system_prompt_cn),
# tool_info=get_plugin_prompt(plugins),
# ),
# )
class SearcherAgent(AgentForInternLM):
def __init__(
self,
user_input_template: str = "{question}",
user_context_template: str = None,
**kwargs,
):
self.user_input_template = user_input_template
self.user_context_template = user_context_template
super().__init__(**kwargs)
def forward(
self,
question: str,
topic: str,
history: List[dict] = None,
session_id=0,
**kwargs,
):
message = [self.user_input_template.format(question=question, topic=topic)]
if history and self.user_context_template:
message = [
self.user_context_template.format_map(item) for item in history
] + message
message = "\n".join(message)
return super().forward(message, session_id=session_id, **kwargs)
searcher_config = dict(
llm=llm,
max_turn=4,
plugins=plugins,
template=date,
output_format=PluginParser(
template=(searcher_system_prompt_cn),
tool_info=get_plugin_prompt(plugins),
),
user_input_template=searcher_input_template_cn,
user_context_template=searcher_context_template_cn,
)
# print(f"searcher_config: {searcher_config}")
searcher_agent = SearcherAgent(**searcher_config)
def main():
question = "请介绍一下ChatGPT的发展历程"
topic = "人工智能"
response = searcher_agent.forward(question=question, topic=topic, history=None)
print("搜索结果:")
print(response)
if __name__ == "__main__":
main()
execution result
root@5583da9ef206:~/MAS-APR# bash run.sh
Script started at: 2025-01-07_06-41-42
--------------------------------
/root/MAS-APR/src/lagent_utils/lagent_internlm.py:124 - 搜索结果:
/root/MAS-APR/src/lagent_utils/lagent_internlm.py:125 - content='invalid API: run' sender='ActionExecutor' formatted=None extra_info=None type=None receiver=None stream_state=<AgentStatusCode.END: 0>
--------------------------------
Script ended at: 2025-01-07_06-41-51
Used time: 9 seconds
I am sure that API is correct cause the following code works
from lagent.llms import GPTAPI
from configparser import ConfigParser
def init_llm():
config = ConfigParser()
config.read("config/config.ini")
subscription_key = config.get("SILICON", "SILICON_API_KEY")
llm = GPTAPI(
model_type="internlm/internlm2_5-7b-chat",
key=subscription_key,
api_base="https://api.siliconflow.cn/v1/chat/completions",
meta_template=[
dict(role="system", api_role="system"),
dict(role="user", api_role="user"),
dict(role="assistant", api_role="assistant"),
dict(role="environment", api_role="system"),
],
top_p=0.8,
top_k=1,
temperature=0,
max_new_tokens=8192,
repetition_penalty=1.02,
stop_words=["<|im_end|>"],
)
return llm
def chat_with_llm(llm, messages):
response = llm.chat(messages, max_new_tokens=100)
return response
if __name__ == "__main__":
# Example usage
llm = init_llm()
messages = [
{"role": "user", "content": "Hello, what is the weather in Beijing?"},
{"role": "assistant", "content": "Hi there!"},
]
response = chat_with_llm(llm, messages)
print(response)
from lagent.actions import WebBrowser
from configparser import ConfigParser
def init_web_browser():
config = ConfigParser()
config.read("config/config.ini")
subscription_key = config.get("BING", "BING_API_KEY")
web_browser = WebBrowser(
searcher_type="BingSearch", topk=6, api_key=subscription_key
)
return web_browser
def search_with_browser(web_browser, query):
return web_browser.search(query)
if __name__ == "__main__":
# Example usage
browser = init_web_browser()
result = search_with_browser(browser, "什么是人工智能")
print(result)
Maybe I am not providing enough plugins to run? I am not sure the internal mechanisms.
It seems that there's an error in tool parsing. Could you provide detailed logs
from lagent.hooks import MessageLogger
searcher_agent.agent.register_hook(MessageLogger())
Here are the hook result
Script started at: 2025-01-07_08-55-23
--------------------------------
2025-01-07 08:57:04,210 INFO lagent - session id: 0, message sender: user
## 主问题
人工智能
## 当前问题
什么是人工智能?
2025-01-07 08:57:05,579 INFO lagent - session id: 0, message sender: Agent
为了准确回答“什么是人工智能?”这个问题,需要找到关于人工智能的定义、历史、应用和未来发展的相关信息。查询应该涵盖这些方面,以确保获取全面的信息。<|action_start|><|plugin|>
{"name": "WebBrowser.search", "parameters": {"query": ["人工智能的定义", "人工智能的历史和发展", "人工智能的应用领域", "人工智能的未来发展"]}}<|action_end|>
2025-01-07 08:57:05,579 INFO lagent - session id: 0, message sender: ActionExecutor
invalid API: run
2025-01-07 08:57:06,854 INFO lagent - session id: 0, message sender: Agent
为了准确回答“什么是人工智能?”这个问题,需要找到权威的定义和解释。可以从多个角度进行搜索,包括学术定义、应用领域以及发展历程等。<|action_start|><|plugin|>
{"name": "WebBrowser.search", "parameters": {"query": ["人工智能的定义", "人工智能的学术解释", "人工智能的应用领域", "人工智能的发展历程"]}}<|action_end|>
2025-01-07 08:57:06,855 INFO lagent - session id: 0, message sender: ActionExecutor
invalid API: run
2025-01-07 08:57:07,916 INFO lagent - session id: 0, message sender: Agent
为了准确回答“什么是人工智能?”这个问题,需要找到权威的定义和解释。可以从学术网站、科技新闻网站以及百科全书等来源获取信息。<|action_start|><|plugin|>
{"name": "WebBrowser.search", "parameters": {"query": ["人工智能定义", "什么是人工智能", "人工智能的解释"]}}<|action_end|>
2025-01-07 08:57:07,916 INFO lagent - session id: 0, message sender: ActionExecutor
invalid API: run
2025-01-07 08:57:09,023 INFO lagent - session id: 0, message sender: Agent
为了准确回答“什么是人工智能?”这个问题,需要找到权威的定义和解释。可以从多个角度进行搜索,包括学术定义、应用实例以及发展历程等。<|action_start|><|plugin|>
{"name": "WebBrowser.search", "parameters": {"query": ["人工智能的定义", "人工智能的学术解释", "人工智能的应用实例", "人工智能的发展历程"]}}<|action_end|>
/root/MAS-APR/src/agent/agent_searcher.py:118 - 搜索结果:
/root/MAS-APR/src/agent/agent_searcher.py:119 - content='invalid API: run' sender='ActionExecutor' formatted=None extra_info=None type=None receiver=None stream_state=<AgentStatusCode.END: 0>
--------------------------------
Script ended at: 2025-01-07_08-57-09
Used time: 6 seconds
It seems that there's an error in tool parsing. Could you provide detailed logs
from lagent.hooks import MessageLogger searcher_agent.agent.register_hook(MessageLogger())
It seems llm are trying to interact with the environment but not found the executor
How about setting stop_words=["<|action_end|>", "<|im_end|>"]
or
output_format=PluginParser(
template=(searcher_system_prompt_cn),
tool_info=get_plugin_prompt(plugins),
begin='<|action_start|><|plugin|>',
end='<|action_end|>'
)
How about setting
stop_words=["<|action_end|>", "<|im_end|>"]or
output_format=PluginParser( template=(searcher_system_prompt_cn), tool_info=get_plugin_prompt(plugins), begin='<|action_start|><|plugin|>', end='<|action_end|>' )
Yeah, that's it. Thx a lot. Anyway, it solved. I am a bit curious about the reason for this bug. Is this specific for me or it is owing to some code reasons? Maybe I should start a pull request to interlm_silicon in models.py in mindsearch project?
The plugin parser splits messages with '<|action_start|><|plugin|>\n' and '<|action_end|>\n' by default, and the action part will be loaded through json.loads. See the code https://github.com/InternLM/lagent/blob/main/lagent/prompts/parsers/tool_parser.py#L44
I think missing newline at the end of messages caused the action to be parsed as '{"name": "WebBrowser.search", "parameters": {"query": ["人工智能定义", "什么是人工智能", "人工智能的解释"]}}<|action_end|>', which does not conform to the JSON protocol.
I'm not sure if it is common for SiliconFlow APIs.
OK, thanks, it seems i am lucky for producing this bug, hh.