当前位置: 首页 > 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.xdnf.cn/news/1063495.html

相关文章:

  • 【JS-4.3-鼠标常用事件】深入理解DOM鼠标事件:全面指南与最佳实践
  • Rabbitmq的五种消息类型介绍,以及集成springboot的使用
  • React JSX语法
  • OCCT基础类库介绍:Modeling Algorithm - Features
  • 软件工程期末试卷简答题版带答案(共21道)
  • 【DCS开源项目】—— Lua 如何调用 DLL、DLL 与 DCS World 的交互
  • Vue3 + TypeScript + xlsx 导入excel文件追踪数据流转详细记录(从原文件到目标数据)
  • 领域驱动设计(DDD)【3】之事件风暴
  • EasyExcel导出极致封装 含枚举转换 分页导出
  • GitHub Copilot快捷键
  • 缓存与加速技术实践-Kafka消息队列
  • 腾讯云IM即时通讯:开启实时通信新时代
  • Python中字符串常用的操作方法
  • Linux TCP/IP协议栈中的TCP输入处理:net/ipv4/tcp_input.c解析
  • 学习C++、QT---03(C++的输入输出、C++的基本数据类型介绍)
  • AI与SEO关键词协同进化
  • IEC61850 通信协议测试验证方法详解
  • 解锁K-近邻算法:数据挖掘的秘密武器
  • 华为云Flexus+DeepSeek征文 | 基于Flexus X实例的金融AI Agent开发:智能风控与交易决策系统
  • 【AI论文】扩散二元性
  • 面试题-定义一个函数入参数是any类型,返回值是string类型,如何写出这个函数,代码示例
  • ncu学习笔记01——合并访存
  • 系统化的Node.js服务器搭建攻略
  • 将Python的JSON字符串转换为JSON
  • UE5 游戏模板 —— FirstShootGame
  • Docker简单介绍与使用以及下载对应镜像(项目前置)
  • 【软考高级系统架构论文】论湖仓一体架构及其应用
  • RNN工作原理和架构
  • Python的6万张图像数据集CIFAR-10和CIFAR-100说明
  • Redis哨兵模式的学习(三)