wow agent day08 初识zigent

Reference：Datawhale wow agent day08
Zigent 是一个基于 Agentlite 框架改进的智能代理开发框架。Agentlite 最初由 Salesforce AI Research 团队开发，是一个强大的 Agent 开发框架。Zigent 在其基础上进行了定制化改进，使其更适合特定场景的应用。
在本课中，我们将学习如何使用 Zigent 框架创建一个简单但功能完整的搜索代理。这个代理能够通过 DuckDuckGo 搜索引擎查找信息并回答问题。

环境准备

Copy
import os
from dotenv import load_dotenv

# 加载环境变量
load_dotenv()
# 从环境变量中读取api_key
api_key = os.getenv('ZISHU_API_KEY')
base_url = "http://43.200.7.56:8008/v1"
chat_model = "glm-4-flash"

from typing import List
from zigent.agents import ABCAgent, BaseAgent
from zigent.llm.agent_llms import LLM
from zigent.commons import TaskPackage
from zigent.actions.BaseAction import BaseAction
from zigent.logging.multi_agent_log import AgentLogger
from duckduckgo_search import DDGS

配置 LLM

这里使用 zigent 封装的 LLM 加载和配置 LLM 服务：

Copy
llm = LLM(api_key=api_key, base_url=base_url, model_name=chat_model)
response = llm.run("你是谁？")
print(response)

创建搜索动作

Copy
class DuckSearchAction(BaseAction):
def __init__(self) -> None:
action_name = "DuckDuckGo_Search"
action_desc = "Using this action to search online content."
params_doc = {"query": "the search string. be simple."}
self.ddgs = DDGS()
super().__init__(
action_name=action_name,
action_desc=action_desc,
params_doc=params_doc,
)

def __call__(self, query):
results = self.ddgs.chat(query)
return results

通过 call 方法执行实际的搜索操作 使用示例：

Copy
search_action = DuckSearchAction()
results = search_action("什么是 agent")
print(results)

创建搜索代理

我们创建一个继承自 BaseAgent 的搜索代理类，它需要一个大语言模型 (llm)、一组动作（默认是 DuckSearchAction）、代理名称和角色描述：

Copy
class DuckSearchAgent(BaseAgent):
def __init__(
self,
llm: LLM,
actions: List[BaseAction] = [DuckSearchAction()],
manager: ABCAgent = None,
**kwargs
):
name = "duck_search_agent"
role = "You can answer questions by using duck duck go search content."
super().__init__(
name=name,
role=role,
llm=llm,
actions=actions,
manager=manager
)

执行代理

Copy
def do_search_agent():
# 创建代理实例
search_agent = DuckSearchAgent(llm=llm)

# 创建任务
task = "what is the found date of microsoft"
task_pack = TaskPackage(instruction=task)

# 执行任务并获取响应
response = search_agent(task_pack)
print("response:", response)

if __name__ == "__main__":
do_search_agent()

我们得到如下：

Copy
Agent [94mduck_search_agent[0m receives the following [4mTaskPackage[0m:
[96m[
Task ID: e3a62788-ee7b-4495-9822-9530c5fdd799
Instruction: what is the found date of microsoft
][0m
====[94mduck_search_agent[0m starts execution on TaskPackage e3a62788-ee7b-4495-9822-9530c5fdd799====
Agent [94mduck_search_agent[0m takes 0-step [4mAction[0m:
[94m{
name: DuckDuckGo_Search
params: {'query': 'Microsoft founding date'}
}[0m
Observation: [92mMicrosoft was founded on April 4, 1975.[0m
Agent [94mduck_search_agent[0m takes 1-step [4mAction[0m:
[94m{
name: Finish
params: {'response': 'Microsoft was founded on April 4, 1975.'}
}[0m
Observation: [92mTask Completed.[0m
=========[94mduck_search_agent[0m finish execution. TaskPackage[ID:e3a62788-ee7b-4495-9822-9530c5fdd799] status:
[96m[
completion: completed
answer: Microsoft was founded on April 4, 1975.
][0m
==========
response: Microsoft was founded on April 4, 1975.