当前位置: 首页 > 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

相关文章:

  • 一,python语法教程.内置API
  • 软件工程概述知识点总结
  • 软件工程期末试卷简答题版带答案(共21道)
  • 每天一个前端小知识 Day 8 - 网络通信与前端性能优化
  • Linux--迷宫探秘:从路径解析到存储哲学
  • FastGPT:开启大模型应用新时代(4/6)
  • 分享两个可以一键生成sql server数据库 html格式巡检报告的脚本
  • AI大模型提示词工程研究报告:长度与效果的辩证分析
  • Thrift作为客户端流程(多路复用)
  • 设计模式在上位机项目的实战
  • 基于SpringBoot+Uniapp的活动中心预约小程序(协同过滤算法、腾讯地图、二维码识别)
  • 微服务拆分——nacos/Feign
  • python中学物理实验模拟:凸透镜成像和凹透镜成像
  • 力扣1477. 找两个和为目标值且不重叠的子数组
  • IEEE5节点系统潮流仿真模型(simulink+matlab全功能模型)
  • MySQL误删数据急救指南:基于Binlog日志的实战恢复详解
  • Vue3 + TypeScript + xlsx 导入excel文件追踪数据流转详细记录(从原文件到目标数据)
  • 编程基础:调用访问
  • pyqt事件过滤器eventFilter
  • 计算机网络学习笔记:应用层概述、动态主机配置协议DHCP
  • 有哪些网站可以兼职做笔译/百度云登陆首页
  • 请人做网站合同/沈阳seo按天计费
  • 赣州网站建设专家/西安排名seo公司
  • wordpress 切换中文/优化服务
  • 无锡建设网站的公司哪家好/国际新闻最新消息今天军事新闻
  • 网站建设的创新之处/关键词免费