当前位置: 首页 > news >正文

LLM-201: OpenHands与LLM交互链路分析

一、核心交互链路架构

HTTP请求
SSE/WebSocket
前端UI
API路由
AgentSession
AgentController
LLM
工具执行

二、详细流程分解

  1. 前端交互层
    React组件通过React Query发起API请求:

// OpenHands/frontend/src/components/ChatInput.tsx
const { trigger } = useSWRMutation('/api/chat', sendMessage);async function sendMessage(url: string, { arg }: { arg: string }) {return axios.post(url, {session_id: sessionId,message: arg});
}
  1. API路由层
    FastAPI处理请求并创建会话:
# OpenHands/openhands/server/routes/chat.py
@app.post("/chat")
async def chat_endpoint(request: ChatRequest):session = AgentSessionManager.get_session(request.session_id)await session.start()await session.process_event(MessageAction(content=request.message))return EventStreamResponse(session.event_stream)
  1. Agent控制层
    AgentController主循环处理事件:
# OpenHands/openhands/controller/agent_controller.py
async def _execute_step(self):messages = self.conversation_memory.process_events(...)llm_response = await self.llm.acompletion(messages)actions = self.agent.response_to_actions(llm_response)for action in actions:await self._handle_action(action)
  1. LLM交互层
    通过LiteLLM集成多模型:
# OpenHands/openhands/llm/llm.pyasync def acompletion(self, messages: list[Message]) -> ModelResponse:return await litellm.acompletion(model=self.config.model_name,messages=convert_to_oa_messages(messages),tools=self.tool_schema)
  1. 工具执行层
    文件编辑工具示例:
# OpenHands/openhands/tools/file_edit.py
class FileEditTool(BaseTool):async def execute(self, params: dict) -> FileEditObservation:with open(params['filepath'], 'w') as f:f.write(params['content'])return FileEditObservation(content=f"Updated {params['filepath']}")

三、典型交互示例

  1. 用户请求
    前端发送:POST /chat {"message": "修改README.md第5行"}

  2. 链路处理

API
创建MessageAction
AgentController生成LLM请求
LLM返回函数调用
解析为FileEditAction
执行文件编辑
生成FileEditObservation
通过event_stream返回前端
  1. 结果反馈

前端接收SSE事件:

{"type": "observation","data": {"content": "Successfully updated README.md","type": "file_edit"}
}

四、关键技术特性

  1. 实时事件流:通过Server-Sent Events实现低延迟更新
  2. 上下文管理:ConversationMemory维护500轮对话上下文
  3. 错误恢复:_react_to_exception方法实现异常自动处理
  4. 多模型支持:LLM配置支持30+商业/开源模型接入

五、参考

  1. OpenHands document
  2. OpenHands on Github
http://www.lqws.cn/news/470773.html

相关文章:

  • Linux致命漏洞CVE-2025-6018和CVE-2025-6019
  • 1、自然语言处理任务全流程
  • 什么是redission看门狗机制
  • Redis 分布式锁、红锁分别是什么?红锁有什么问题?
  • Python漂浮的爱心
  • 【Ambari3.0.0 部署】Step2—免密登陆认证-适用于el8
  • 智能机器人后期会如何发展?
  • 【烧脑算法】枚举:有序穷举,分步排查
  • 植物神经小知识
  • 教育培训APP源码核心功能开发详解:直播、考试、组卷系统全拆解
  • 力扣1546. 和为目标值且不重叠的非空子数组的最大数目
  • 1. 常见K线组合
  • 【STM32笔记】F1F4 STM32初识、MDK调试、HAL简介
  • 3.10 坐标导航
  • C++ 函数模板
  • 【基础算法】贪心 (一) :简单贪心
  • JavaWeb后端部分
  • win2003_ddk.3790里面有windbg--6.1.0017.2----备忘
  • 【环境配置】在Ubuntu Server上安装5090 PyTorch环境
  • Python 正确重载运算符(增量赋值运算符)
  • C++重点知识详解(命名空间,缺省参数,函数重载)
  • 【舞蹈】编排:如何对齐拍子并让小节倍数随BPM递减
  • 两个python独立进程通信
  • Kubernetes 节点故障自愈方案:结合 Node Problem Detector 与自动化脚本
  • Java面试题025:一文深入了解数据库Redis(1)
  • 自定义 Hook:在 Vue3 中复用逻辑
  • Vue3 + TypeScript + xlsx 导入excel文件追踪数据流转详细记录(从原文件到目标数据)
  • Vue+spring boot前后端分离项目搭建---小白入门
  • 2025云服务器磁盘空间告急全解析:日志管理策略与智能扩容方案
  • 98. 验证二叉搜索树