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

一、核心交互链路架构

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

二、详细流程分解

  1. 前端交互层
    React组件通过React Query发起API请求:
typescript 复制代码
// 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处理请求并创建会话:
python 复制代码
# 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主循环处理事件:
python 复制代码
# 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集成多模型:
python 复制代码
# OpenHands/openhands/llm/llm.py

async 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. 工具执行层
    文件编辑工具示例:
python 复制代码
# 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事件:

json 复制代码
{
  "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
相关推荐
go546315846526 分钟前
Python点阵字生成与优化:从基础实现到高级渲染技术
开发语言·人工智能·python·深度学习·分类·数据挖掘
猫头虎31 分钟前
2025年02月11日 Go生态洞察:Go 1.24 发布亮点全面剖析
开发语言·后端·python·golang·go·beego·go1.19
仰望天空—永强42 分钟前
PS 2025【七月最新v26.5】PS铺软件安装|最新版|附带安装文件|详细安装说明|附PS插件
开发语言·图像处理·python·图形渲染·photoshop
守城小轩1 小时前
从零开始学习Dify-数据库数据可视化(五)
ai·ai客服·ai浏览器
MediaTea1 小时前
Python 库手册:xmlrpc.client 与 xmlrpc.server 模块
开发语言·python
悦悦子a啊1 小时前
Python之--字典
开发语言·python·学习
水军总督1 小时前
OpenCV+Python
python·opencv·计算机视觉
qyhua2 小时前
Windows 平台源码部署 Dify教程(不依赖 Docker)
人工智能·windows·python
一车小面包2 小时前
Python高级入门Day6
开发语言·python
青Cheng序员石头2 小时前
【转译】Agentic AI 与 AI Agent:五大差异及其重要性
llm·aigc·agent