Agent Builder API - Agent Smith 扩展的后端服务(开源代码)
一、软件介绍
文末提供程序和源码下载
Agent Builder API - Agent Smith 扩展的后端服务(开源代码)手动设置:在本地计算机中克隆此存储库并启动 python FAST API 服务器。(可选)安装并设置 Mongo DB。Dev Container 设置:Dev Containers 允许您自动执行环境设置。使用此设置可在预安装了扩展的隔离环境中安装和运行容器服务。您还可以使用密钥在远程环境/浏览器中打开 GitHub Codespaces 以传递模型 API 密钥。
二、Manual Setup 手动设置
-
Set up a Python virtualenv and install dependencies
设置 Python virtualenv 并安装依赖项python -m venv --prompt agent-builder-api venv source venv/Scripts/activate # venv/Scripts/activate (Windows PS) pip install -r requirements.txt
-
Set the model name and API key in .env file
在 .env 文件中设置模型名称和 API 密钥OPENAI_API_KEY="sk----" MODEL_NAME="openai"
-
Start the server in the new terminal
在新终端中启动服务器python -m agentbuilder.main
[Optional] Start using Poetry
[可选]开始使用 Poetry
For fine-grained dependency management, use Poetry to pick and choose dependency packs based on your LLM model provider and tool features.
对于精细的依赖项管理,请使用 Poetry 根据您的LLM模型提供程序和工具功能来挑选依赖项包。
-
Follow the Offical Instruction Guide to install Poetry.
按照 官方说明指南安装 Poetry。 -
Pick and choose dependency packs to install.
选择要安装的依赖项包。poetry install --extras "mcp openai gemini cohere anthropic mongodb vectordb langgraph guardrails ui"
-
Set the model name and API key in .env file
在 .env 文件中设置模型名称和 API 密钥OPENAI_API_KEY="sk----" MODEL_NAME="openai"
-
Start the server in the new terminal
在新终端中启动服务器poetry run start-server
Note 注意
Poetry will create a virtual environment for us.
诗歌将为我们创造一个虚拟环境。
MCP servers MCP 服务器
-
Local MCP servers can be added in MCP server folder
可以在 MCP 服务器文件夹中添加本地 MCP 服务器 -
Multiple MCP clients can be configured in MPC client file
可以在 MPC 客户端文件中配置多个 MCP 客户端mcp_servers={"math": {"command": sys.executable,"args": [f"{current_dir}\\servers\\math.py"],"transport": "stdio",}}
[Optional] Enable MongoDB
[可选]启用 MongoDB
By default, data is stored as JSON files. Enable storage in Mongo DB by setting url using the environment variable.
默认情况下,数据存储为 JSON 文件。通过使用环境变量设置 url 在 Mongo DB 中启用存储。
MONGODB_URL="mongodb://localhost:27017/llmdb"
三、Dev Containers Setup 开发容器设置
-
Enable Dev Containers in vscode by following the steps in official documentation.
按照官方文档中的步骤在 vscode 中启用 Dev Containers。 -
Click on the badge below to run the services in an isolated container environment in a local machine.
单击下面的徽章,在本地计算机的隔离容器环境中运行服务。This will clone the repo and start the API and Mongo DB container services.
这将克隆存储库并启动 API 和 Mongo DB 容器服务。
Tip 提示
Use URL mongodb://mongodb:27017/llmdb in Mongo DB vscode extension to view storage data.
使用 Mongo DB vscode 扩展中的 URL mongodb://mongodb:27017/llmdb 查看存储数据。
-
Execute F1 > Dev Containers: Attach to Running Container.. and select agent-builder-container.
执行 F1 > Dev Containers:附加到正在运行的容器..,然后选择 agent-builder-container。 -
Set the model name and API key in .env file
在 .env 文件中设置模型名称和 API 密钥OPENAI_API_KEY="sk----" MODEL_NAME="openai"
四、Customization 定制
Dependency packs and environment configuration
依赖项包和环境配置
Dependency packs allow fine-grained package installations based on your requirements. Use the environment variable EXTRA_DEPS in the docker-compose file to update.
依赖项包允许根据您的要求进行精细的软件包安装。使用 docker-compose 文件中的环境变量 EXTRA_DEPS 进行更新。
install-extra-deps.sh script can be used in dev container mode if docker-compose is not available.
如果 docker-compose 不可用,则可以 install-extra-deps.sh Dev Container 模式下使用该脚本。
For example, the below environment configuration will install dependencies for the Gemini model, Mongo DB, Langchain Graph, and VectorDB
例如,以下环境配置将安装 Gemini 模型、Mongo DB、Langchain Graph 和 VectorDB 的依赖项
EXTRA_DEPS: "gemini,mongodb,langgraph,vectordb"
Tip 提示
Start with a basic dependency pack to support your model and add other features incrementally
从基本依赖项包开始以支持您的模型,并逐步添加其他功能
The following models are supported by its dependency pack
其依赖项包支持以下模型
Model 型 | Dependency pack 依赖项包 | ENV key name ENV 密钥名称 |
---|---|---|
OPEN AI 开放人工智能 | openai OpenAI 公司 | OPENAI_API_KEY |
GEMINI 双子座 | gemini 双子座 | GOOGLE_API_KEY |
COHERE | cohere 凝聚 | COHERE_API_KEY |
ANTHROPIC ANTHROPIC 类 | anthropic 人为的 | ANTHROPIC_API_KEY |
Some pre-configured tools require extra dependencies or API keys to get enabled.
某些预配置工具需要额外的依赖项或 API 密钥才能启用。
Tool 工具 | Dependency pack 依赖项包 | ENV key name ENV 密钥名称 |
---|---|---|
internet_search | - | TAVILY_API_KEY |
vectorstore_search | vectordb 矢量数据库 | EMBED_MODEL_NAME |
Adding Tools 添加工具
Add custom Tools or Toolkits using tool factory module (agentbuilder/factory/tool_factory).
使用工具工厂模块 (agentbuilder/factory/tool_factory) 添加自定义工具或工具包。
-
Create your tool 创建您的工具
agentbuilder/tools/my_custom_tool.py
代理构建器/工具/my_custom_tool.pyfrom pathlib import Path from langchain_core.tools import tool from pydantic import BaseModel, Field@tool def my_custom_tool(a: int, b: int): """Custom Tool Description""" return a + bmy_custom_tool.name="custom_tool_name" my_custom_tool.description="Custom Tool Description"class field_inputs(BaseModel): a: int = Field(description="First input") b: int = Field(description="Second input")my_custom_tool.args_schema = sum_inputs my_custom_tool.metadata= {"file_path": str(Path(__file__).absolute())}
-
Add your tool in get_all_tools method in tool_factory module.
在 get_all_tools 模块tool_factory方法中添加您的工具。agentbuilder/factory/tool_factory.py
代理生成器/工厂/tool_factory.pydef get_all_tools()->Sequence[BaseTool]:return get_vectordb_tools()+ get_websearch_tools() + json_tools + [directly_answer_tool,weather_clothing_tool,temperature_tool,temperature_sensor_tool,sum_tool,greeting_tool,git_diff_tool,repl_tool, + my_custom_tool]
Adding Agents 添加代理
Agents can be created using Extension UI or declared in code. Add your agents using the Agent Factory Module (agentbuilder/factory/agent_factory).
代理可以使用扩展 UI 创建,也可以在代码中声明。使用 Agent Factory 模块 (agentbuilder/factory/agent_factory) 添加代理。
-
Create your agent 创建您的代理
def my_agent():return AgentParams(name="my_agent",preamble= "You are a powerful agent that uses tools to answer Human questions",tools= ["my_custom_tool"],agent_type= 'tool_calling')
-
Add your agent in get_all_agents method.
在 get_all_agents 方法中添加代理。def get_all_agents(): return [default_agent(),weather_agent(),python_agent(),git_agent(), + my_agent() ]
Custom Agent Builder and Graphs
自定义代理生成器和图表
Customize your Agent Workflow using custom prompts and graphs. Filter the agent using the agent name to apply customizations.
使用自定义提示和图表自定义您的代理工作流程。使用代理名称筛选代理以应用自定义。
For example, the following code applies graph builder workflow for the Agent named "graph_agent"
例如,以下代码将图形生成器工作流应用于名为“graph_agent”的代理
def get_agent_builder(params:AgentBuilderParams):agent_name= params.namematch agent_name:case "graph_agent":from agentbuilder.agents.BaseGraphAgentBuilder import BaseGraphAgentBuilderreturn BaseGraphAgentBuilder(params)case _:return BaseAgentBuilder(params)
Important 重要
Dependency pack "langgraph" needs to be installed for BaseGraphAgentBuilder.
需要为 BaseGraphAgentBuilder 安装依赖项包 “langgraph”。
Configuring models 配置模型
Update model configuration using environment variables.
使用环境变量更新模型配置。
Supports {Provider}/{ModelName} format
支持 {Provider}/{ModelName} 格式
-
Gemini 双子座
Create API keys https://aistudio.google.com/app/apikey
创建 API 密钥 https://aistudio.google.com/app/apikeyMODEL_NAME="gemini/gemini-pro"EMBED_MODEL_NAME="gemini/embedding-001"GOOGLE_API_KEY=<GOOGLE_API_KEY>
-
Cohere 凝聚
Create API keys Login | Cohere
创建 API 密钥 https://dashboard.cohere.com/api-keysMODEL_NAME="cohere/command"EMBED_MODEL_NAME="cohere/embed-english-v3.0"COHERE_API_KEY=<COHERE_API_KEY>
-
Open AI 打开 AI
Create API keys https://platform.openai.com/docs/quickstart/account-setup
创建 API 密钥 https://platform.openai.com/docs/quickstart/account-setupMODEL_NAME="openai/gpt-4o"EMBED_MODEL_NAME="openai/text-embedding-3-large"OPENAI_API_KEY=<OPENAI_API_KEY>
-
Anthropic 人
Create API keys Home \ Anthropic and Voyage AI | Home
创建 API 密钥 https://www.anthropic.com/ 和 https://www.voyageai.com/MODEL_NAME="anthropic/claude-3-opus-20240229"EMBED_MODEL_NAME="voyageai/voyage-2"ANTHROPIC_API_KEY=<ANTHROPIC_API_KEY>VOYAGE_API_KEY=<VOYAGE_API_KEY>
-
Ollama 奥拉马
Use local models for function calls.
使用本地模型进行函数调用。[!TIP] Use JSON chat agent type for better compatibility with local models.
[!提示] 使用 JSON 聊天代理类型以更好地与本地模型兼容。Install Ollama and pull the model.
安装 Ollama 并拉取模型。ollama pull mistral:v0.3
Set environment variable.
设置环境变量。MODEL_NAME="ollama/mistral:v0.3"
五、软件下载
夸克网盘分享
本文信息来源于GitHub作者地址:https://github.com/nagaraj-real/agent-builder-api