Hi 大家好
在本文中,我讲介绍我的应用 iris-AgenticAI .
代理式人工智能的兴起标志着人工智能与世界互动方式的变革性飞跃--从静态响应转变为动态、目标驱动的问题解决方式。参看 OpenAI’s Agentic SDK , OpenAI Agents SDK使您能够在一个轻量级、易用且抽象程度极低的软件包中构建代理人工智能应用程序。它是我们之前的代理实验 Swarm 的生产就绪升级版。
该应用展示了下一代自主人工智能系统,这些系统能够进行推理、协作,并以类似人类的适应能力执行复杂任务。
应用功能
- Agent Loop 🔄 一个内置循环,可自主管理工具的执行,将结果发回 LLM,并迭代直至任务完成。
- Python-First 🐍 利用本地 Python 语法(装饰器、生成器等)来协调和连锁代理,而无需外部 DSL。
- Handoffs 🤝 通过在专业代理之间委派任务,无缝协调多代理工作流程。
- Function Tools ⚒️ 用 @tool 修饰任何 Python 函数,可立即将其集成到代理的工具包中。
- Vector Search (RAG) 🧠 原生集成向量存储(IRIS),用于 RAG 检索。
- Tracing 🔍 内置跟踪功能,可实时可视化、调试和监控代理工作流(想想 LangSmith 的替代方案)。
- MCP Servers 🌐 通过 stdio 和 HTTP 支持模型上下文协议(MCP),实现跨进程代理通信。
- Chainlit UI 🖥️ 集成 Chainlit 框架,可使用最少的代码构建交互式聊天界面。
- Stateful Memory 🧠 跨会话保存聊天历史、上下文和代理状态,以实现连续性和长期任务。
代理
代理是应用程序的核心构件。代理是一个大型语言模型(LLM),配置有指令和工具。基本配置您需要配置的代理最常见的属性,包括:
Instructions (说明):也称为开发人员信息或系统提示。
model:要使用的 LLM,以及可选的 model_settings,用于配置温度、top_p 等模型调整参数。
tools工具: 代理用来完成任务的工具。
from agents import Agent, ModelSettings, function_tool
@function_tool
def get_weather(city: str) -> str:
return f"The weather in {city} is sunny"
agent = Agent(
name="Haiku agent",
instructions="Always respond in haiku form",
model="o3-mini",
tools=[get_weather],
)
运行代理
您可以通过 Runner 类运行代理。您有 3 个选项:
1.Runner.run():异步运行并返回 RunResult。
2.Runner.run_sync(),这是一种同步方法,只是在引擎盖下运行 .run()。
3.Runner.run_streamed():异步运行并返回 RunResultStreaming。它以流式模式调用 LLM,并在接收到事件时将其流式传输给您。
from agents import Agent, Runner
async def main():
agent = Agent(name="Assistant", instructions="You are a helpful assistant")
result = await Runner.run(agent, "Write a haiku about recursion in programming.")
print(result.final_output)
# Code within the code,
# Functions calling themselves,
# Infinite loop's dance.
代理架构
该应用程序由 7 个专业代理组成:
1. 分诊代理 🤖
- 角色: 主要路由器,接收用户输入,并通过切换分配任务
- 示例: 路由 “显示生产错误” → IRIS 生产代理
2.矢量搜索代理 🤖
- 作用: 提供 IRIS 2025.1 版本说明详情(RAG 功能)
- 示例: 路由 “向我提供发行说明摘要”→矢量搜索代理
3. IRIS 仪表板代理 🤖
功能: 提供实时管理门户指标:明文副本。
-
-
ApplicationErrors, CSPSessions, CacheEfficiency, DatabaseSpace, DiskReads, DiskWrites, ECPAppServer, ECPDataServer, GloRefs, JournalStatus, LicenseCurrent, LockTable, Processes, SystemUpTime, WriteDaemon, [...]
-
4. IRIS Running Process Agent 🤖
- 功能: 监控活动进程的详细信息:
Process ID
|Namespace
|Routine
|State
|PidExternal
5. IRIS Production Agent 🤖
- 角色: 提供生产详情以及启动和停止生产的功能。
6. WebSearch Agent 🤖
- 功能: 通过 API 集成执行上下文网络搜索
7.Order Agent 🤖
- 功能: 使用订单 ID 检索订单状态
交接
交接允许代理将任务委托给另一个代理。这在不同代理擅长不同领域的情况下尤其有用。例如,客户支持应用程序可能会有专门处理订单状态、退款、常见问题等任务的代理。
分流代理是我们的主代理,它会根据用户输入将任务分配给另一个代理
#TRIAGE AGENT, Main agent receives user input and delegates to other agent by using handoffs
triage_agent = Agent(
name="Triage agent",
instructions=(
"Handoff to appropriate agent based on user query."
"if they ask about Release Notes, handoff to the vector_search_agent."
"If they ask about production, handoff to the production agent."
"If they ask about dashboard, handoff to the dashboard agent."
"If they ask about process, handoff to the processes agent."
"use the WebSearchAgent tool to find information related to the user's query and do not use this agent is query is about Release Notes."
"If they ask about order, handoff to the order_agent."
),
handoffs=[vector_search_agent,production_agent,dashboard_agent,processes_agent,order_agent,web_search_agent]
)
跟踪
Agents SDK 包括内置跟踪功能,可收集代理运行期间事件的全面记录: LLM 生成、工具调用、切换、防护栏,甚至发生的自定义事件。使用跟踪仪表板,您可以在开发和生产过程中调试、可视化和监控工作流。
https://platform.openai.com/logs
应用界面
.png)
应用工作流程
矢量搜索代理
矢量搜索代理自动获取 New in InterSystems IRIS 2025.1 如果数据还不存在,只需将文本信息输入 IRIS 矢量存储区一次。
使用下面的查询来获取数据
SELECT
id, embedding, document, metadata
FROM SQLUser.AgenticAIRAG
分流代理接收用户输入,将问题转给矢量搜索代理。
IRIS 仪表盘代理
分流代理接收用户输入,将问题路由到 IRIS 仪表板代理。
.png)
IRIS 流程代理
分流代理接收用户输入,将问题路由到 IRIS 流程代理。
.png)
IRIS 生产代理
使用生产代理启动和停止生产。
使用生产代理获取生产详情。
本地代理
分流代理接收用户输入,将问题转给本地订单代理。
.png)
WebSearch 代理
在这里,分流代理接收到两个问题,并将两个问题都路由到 WebSearcg 代理。
MCP Server 应用
MCP Server在这里运行 https://localhost:8000/sse
下面是启动 MCP 服务器的代码:
import os
import shutil
import subprocess
import time
from typing import Any
from dotenv import load_dotenv
load_dotenv()
#Get OPENAI Key, if not fond in .env then get the GEIMINI API KEY
#IF Both defined then take OPENAI Key
openai_api_key = os.getenv("OPENAI_API_KEY")
if not openai_api_key:
raise ValueError("OPENAI_API_KEY is not set. Please ensure to defined in .env file.")
if __name__ == "__main__":
# Let's make sure the user has uv installed
if not shutil.which("uv"):
raise RuntimeError(
"uv is not installed. Please install it: https://docs.astral.sh/uv/getting-started/installation/"
)
# We'll run the SSE server in a subprocess. Usually this would be a remote server, but for this
# demo, we'll run it locally at http://localhost:8000/sse
process: subprocess.Popen[Any] | None = None
try:
this_dir = os.path.dirname(os.path.abspath(__file__))
server_file = os.path.join(this_dir, "MCPserver.py")
print("Starting SSE server at http://localhost:8000/sse ...")
# Run `uv run server.py` to start the SSE server
process = subprocess.Popen(["uv", "run", server_file])
# Give it 3 seconds to start
time.sleep(3)
print("SSE server started. Running example...\n\n")
except Exception as e:
print(f"Error starting SSE server: {e}")
exit(1)
MCP 服务器配备了以下工具:
- 提供 IRIS 2025.1 发行说明详情(矢量搜索)
- IRIS 信息工具
- 检查天气工具
- 查找暗语工具(本地功能)
- 加法工具(本地功能)
MCP 应用在这里运行 http://localhost:8001
MCP 服务器矢量搜索(RAG)功能
MCP 服务器配备 InterSystems IRIS 向量搜索摄取功能和检索增强生成 (RAG) 功能。
MCP Server other functionality
The MCP Server dynamically delegates tasks to the appropriate tool based on user input.
更多详情,请访问 iris-AgenticAI open exchange 界面。
谢谢!