Reference:Datawhale wow agent day08
Zigent is an intelligent agent development framework improved based on the Agentlite framework. Agentlite was originally developed by the Salesforce AI Research team and is a powerful agent development framework. Zigent has been customized and improved on this basis to make it more suitable for specific application scenarios.
In this lesson, we will learn how to create a simple yet fully functional search agent using the Zigent framework. This agent can find information and answer questions through the DuckDuckGo search engine.
Environment Preparation#
import os
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Read api_key from environment variables
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
Configure LLM#
Here we use the LLM wrapped by zigent to load and configure the LLM service:
llm = LLM(api_key=api_key, base_url=base_url, model_name=chat_model)
response = llm.run("Who are you?")
print(response)
Create Search Action#
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
Execute the actual search operation through the call method with an example:
search_action = DuckSearchAction()
results = search_action("What is an agent")
print(results)
Create Search Agent#
We create a search agent class that inherits from BaseAgent, which requires a large language model (llm), a set of actions (default is DuckSearchAction), the agent name, and role description:
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
)
Execute Agent#
def do_search_agent():
# Create agent instance
search_agent = DuckSearchAgent(llm=llm)
# Create task
task = "what is the found date of microsoft"
task_pack = TaskPackage(instruction=task)
# Execute task and get response
response = search_agent(task_pack)
print("response:", response)
if __name__ == "__main__":
do_search_agent()
We get the following:
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.