mirror of
https://github.com/csunny/DB-GPT.git
synced 2025-09-30 05:49:25 +00:00
refactor(agent): Refactor resource of agents (#1518)
This commit is contained in:
80
examples/agents/custom_tool_agent_example.py
Normal file
80
examples/agents/custom_tool_agent_example.py
Normal file
@@ -0,0 +1,80 @@
|
||||
import asyncio
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
|
||||
from typing_extensions import Annotated, Doc
|
||||
|
||||
from dbgpt.agent import AgentContext, AgentMemory, LLMConfig, UserProxyAgent
|
||||
from dbgpt.agent.expand.tool_assistant_agent import ToolAssistantAgent
|
||||
from dbgpt.agent.resource import ToolPack, tool
|
||||
|
||||
logging.basicConfig(
|
||||
stream=sys.stdout,
|
||||
level=logging.INFO,
|
||||
format="%(asctime)s | %(levelname)s | %(name)s | %(message)s",
|
||||
)
|
||||
|
||||
|
||||
@tool
|
||||
def simple_calculator(first_number: int, second_number: int, operator: str) -> float:
|
||||
"""Simple calculator tool. Just support +, -, *, /."""
|
||||
if operator == "+":
|
||||
return first_number + second_number
|
||||
elif operator == "-":
|
||||
return first_number - second_number
|
||||
elif operator == "*":
|
||||
return first_number * second_number
|
||||
elif operator == "/":
|
||||
return first_number / second_number
|
||||
else:
|
||||
raise ValueError(f"Invalid operator: {operator}")
|
||||
|
||||
|
||||
@tool
|
||||
def count_directory_files(path: Annotated[str, Doc("The directory path")]) -> int:
|
||||
"""Count the number of files in a directory."""
|
||||
if not os.path.isdir(path):
|
||||
raise ValueError(f"Invalid directory path: {path}")
|
||||
return len(os.listdir(path))
|
||||
|
||||
|
||||
async def main():
|
||||
from dbgpt.model.proxy import OpenAILLMClient
|
||||
|
||||
llm_client = OpenAILLMClient(model_alias="gpt-3.5-turbo")
|
||||
context: AgentContext = AgentContext(conv_id="test456")
|
||||
|
||||
agent_memory = AgentMemory()
|
||||
|
||||
tools = ToolPack([simple_calculator, count_directory_files])
|
||||
|
||||
user_proxy = await UserProxyAgent().bind(agent_memory).bind(context).build()
|
||||
|
||||
tool_engineer = (
|
||||
await ToolAssistantAgent()
|
||||
.bind(context)
|
||||
.bind(LLMConfig(llm_client=llm_client))
|
||||
.bind(agent_memory)
|
||||
.bind(tools)
|
||||
.build()
|
||||
)
|
||||
|
||||
await user_proxy.initiate_chat(
|
||||
recipient=tool_engineer,
|
||||
reviewer=user_proxy,
|
||||
message="Calculate the product of 10 and 99",
|
||||
)
|
||||
|
||||
await user_proxy.initiate_chat(
|
||||
recipient=tool_engineer,
|
||||
reviewer=user_proxy,
|
||||
message="Count the number of files in /tmp",
|
||||
)
|
||||
|
||||
# dbgpt-vis message infos
|
||||
print(await agent_memory.gpts_memory.one_chat_completions("test456"))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
Reference in New Issue
Block a user