mirror of
https://github.com/csunny/DB-GPT.git
synced 2025-09-08 12:30:14 +00:00
refactor(agent): Refactor resource of agents (#1518)
This commit is contained in:
@@ -21,7 +21,6 @@ from dbgpt.agent import (
|
||||
AgentMemory,
|
||||
AutoPlanChatManager,
|
||||
LLMConfig,
|
||||
ResourceLoader,
|
||||
UserProxyAgent,
|
||||
)
|
||||
from dbgpt.agent.expand.code_assistant_agent import CodeAssistantAgent
|
||||
@@ -37,18 +36,15 @@ async def main():
|
||||
|
||||
agent_memory = AgentMemory()
|
||||
|
||||
llm_client = OpenAILLMClient(model_alias="gpt-4")
|
||||
llm_client = OpenAILLMClient(model_alias="gpt-4o")
|
||||
context: AgentContext = AgentContext(
|
||||
conv_id="test456", gpts_app_name="代码分析助手", max_new_tokens=2048
|
||||
)
|
||||
|
||||
resource_loader = ResourceLoader()
|
||||
|
||||
coder = (
|
||||
await CodeAssistantAgent()
|
||||
.bind(context)
|
||||
.bind(LLMConfig(llm_client=llm_client))
|
||||
.bind(resource_loader)
|
||||
.bind(agent_memory)
|
||||
.build()
|
||||
)
|
||||
|
@@ -15,26 +15,20 @@
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import os
|
||||
|
||||
from dbgpt.agent import (
|
||||
AgentContext,
|
||||
AgentMemory,
|
||||
AgentResource,
|
||||
LLMConfig,
|
||||
ResourceLoader,
|
||||
ResourceType,
|
||||
UserProxyAgent,
|
||||
WrappedAWELLayoutManager,
|
||||
)
|
||||
from dbgpt.agent.expand.plugin_assistant_agent import PluginAssistantAgent
|
||||
from dbgpt.agent.expand.resources.search_tool import baidu_search
|
||||
from dbgpt.agent.expand.summary_assistant_agent import SummaryAssistantAgent
|
||||
from dbgpt.agent.resource import PluginFileLoadClient
|
||||
from dbgpt.configs.model_config import ROOT_PATH
|
||||
from dbgpt.agent.expand.tool_assistant_agent import ToolAssistantAgent
|
||||
from dbgpt.agent.resource import ToolPack
|
||||
from dbgpt.util.tracer import initialize_tracer
|
||||
|
||||
test_plugin_dir = os.path.join(ROOT_PATH, "examples/test_files/plugins")
|
||||
|
||||
initialize_tracer("/tmp/agent_trace.jsonl", create_system_app=True)
|
||||
|
||||
|
||||
@@ -45,23 +39,14 @@ async def main():
|
||||
context: AgentContext = AgentContext(conv_id="test456", gpts_app_name="信息析助手")
|
||||
|
||||
agent_memory = AgentMemory()
|
||||
resource_loader = ResourceLoader()
|
||||
plugin_file_loader = PluginFileLoadClient()
|
||||
resource_loader.register_resource_api(plugin_file_loader)
|
||||
|
||||
plugin_resource = AgentResource(
|
||||
type=ResourceType.Plugin,
|
||||
name="test",
|
||||
value=test_plugin_dir,
|
||||
)
|
||||
|
||||
tools = ToolPack([baidu_search])
|
||||
tool_engineer = (
|
||||
await PluginAssistantAgent()
|
||||
await ToolAssistantAgent()
|
||||
.bind(context)
|
||||
.bind(LLMConfig(llm_client=llm_client))
|
||||
.bind(agent_memory)
|
||||
.bind([plugin_resource])
|
||||
.bind(resource_loader)
|
||||
.bind(tools)
|
||||
.build()
|
||||
)
|
||||
summarizer = (
|
||||
@@ -86,7 +71,7 @@ async def main():
|
||||
await user_proxy.initiate_chat(
|
||||
recipient=manager,
|
||||
reviewer=user_proxy,
|
||||
message="查询成都今天天气",
|
||||
message="查询北京今天天气",
|
||||
# message="查询今天的最新热点财经新闻",
|
||||
# message="Find papers on gpt-4 in the past three weeks on arxiv, and organize their titles, authors, and links into a markdown table",
|
||||
# message="find papers on LLM applications from arxiv in the last month, create a markdown table of different domains.",
|
||||
|
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())
|
@@ -17,17 +17,9 @@
|
||||
import asyncio
|
||||
import os
|
||||
|
||||
from dbgpt.agent import (
|
||||
AgentContext,
|
||||
AgentMemory,
|
||||
AgentResource,
|
||||
LLMConfig,
|
||||
ResourceLoader,
|
||||
ResourceType,
|
||||
UserProxyAgent,
|
||||
)
|
||||
from dbgpt.agent.expand.plugin_assistant_agent import PluginAssistantAgent
|
||||
from dbgpt.agent.resource import PluginFileLoadClient
|
||||
from dbgpt.agent import AgentContext, AgentMemory, LLMConfig, UserProxyAgent
|
||||
from dbgpt.agent.expand.tool_assistant_agent import ToolAssistantAgent
|
||||
from dbgpt.agent.resource import AutoGPTPluginToolPack
|
||||
|
||||
current_dir = os.getcwd()
|
||||
parent_dir = os.path.dirname(current_dir)
|
||||
@@ -42,25 +34,16 @@ async def main():
|
||||
|
||||
agent_memory = AgentMemory()
|
||||
|
||||
plugin_resource = AgentResource(
|
||||
type=ResourceType.Plugin,
|
||||
name="test",
|
||||
value=test_plugin_dir,
|
||||
)
|
||||
|
||||
resource_loader = ResourceLoader()
|
||||
plugin_file_loader = PluginFileLoadClient()
|
||||
resource_loader.register_resource_api(plugin_file_loader)
|
||||
tools = AutoGPTPluginToolPack(test_plugin_dir)
|
||||
|
||||
user_proxy = await UserProxyAgent().bind(agent_memory).bind(context).build()
|
||||
|
||||
tool_engineer = (
|
||||
await PluginAssistantAgent()
|
||||
await ToolAssistantAgent()
|
||||
.bind(context)
|
||||
.bind(LLMConfig(llm_client=llm_client))
|
||||
.bind(agent_memory)
|
||||
.bind([plugin_resource])
|
||||
.bind(resource_loader)
|
||||
.bind(tools)
|
||||
.build()
|
||||
)
|
||||
|
||||
|
@@ -17,17 +17,9 @@
|
||||
import asyncio
|
||||
import os
|
||||
|
||||
from dbgpt.agent import (
|
||||
AgentContext,
|
||||
AgentMemory,
|
||||
AgentResource,
|
||||
LLMConfig,
|
||||
ResourceLoader,
|
||||
ResourceType,
|
||||
UserProxyAgent,
|
||||
)
|
||||
from dbgpt.agent import AgentContext, AgentMemory, LLMConfig, UserProxyAgent
|
||||
from dbgpt.agent.expand.data_scientist_agent import DataScientistAgent
|
||||
from dbgpt.agent.resource import SqliteLoadClient
|
||||
from dbgpt.agent.resource import SQLiteDBResource
|
||||
from dbgpt.util.tracer import initialize_tracer
|
||||
|
||||
current_dir = os.getcwd()
|
||||
@@ -38,22 +30,14 @@ initialize_tracer("/tmp/agent_trace.jsonl", create_system_app=True)
|
||||
|
||||
|
||||
async def main():
|
||||
from dbgpt.model.proxy.llms.chatgpt import OpenAILLMClient
|
||||
|
||||
agent_memory = AgentMemory()
|
||||
from dbgpt.model.proxy import OpenAILLMClient
|
||||
|
||||
llm_client = OpenAILLMClient(model_alias="gpt-3.5-turbo")
|
||||
context: AgentContext = AgentContext(conv_id="test456")
|
||||
agent_memory = AgentMemory()
|
||||
|
||||
db_resource = AgentResource(
|
||||
type=ResourceType.DB,
|
||||
name="TestData",
|
||||
value=f"{test_plugin_dir}/dbgpt.db",
|
||||
)
|
||||
|
||||
resource_loader = ResourceLoader()
|
||||
sqlite_file_loader = SqliteLoadClient()
|
||||
resource_loader.register_resource_api(sqlite_file_loader)
|
||||
sqlite_resource = SQLiteDBResource("SQLite Database", f"{test_plugin_dir}/dbgpt.db")
|
||||
|
||||
user_proxy = await UserProxyAgent().bind(agent_memory).bind(context).build()
|
||||
|
||||
@@ -61,8 +45,7 @@ async def main():
|
||||
await DataScientistAgent()
|
||||
.bind(context)
|
||||
.bind(LLMConfig(llm_client=llm_client))
|
||||
.bind([db_resource])
|
||||
.bind(resource_loader)
|
||||
.bind(sqlite_resource)
|
||||
.bind(agent_memory)
|
||||
.build()
|
||||
)
|
||||
|
Reference in New Issue
Block a user