?一、軟件介紹
文末提供程序和源碼下載
? ? ? ? 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