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
相关推荐
java1234_小锋2 分钟前
一周学会Matplotlib3 Python 数据可视化-绘制自相关图
开发语言·python·信息可视化·matplotlib·matplotlib3
GitLqr1 小时前
AI洞察 | 智元、阿里在机器人领域的重磅开源
meta·机器人·llm
CoderJia程序员甲1 小时前
GitHub 热榜项目 - 日榜(2025-08-16)
人工智能·ai·开源·github
用户5449750302161 小时前
我不想再学新框架了,直到我遇见了 ZipAgent
agent
用户5449750302162 小时前
ZipAgent 核心技术深度解析(一):函数装饰器与类型系统的极简设计哲学
agent
Juchecar2 小时前
分析:将现代开源浏览器的JavaScript引擎更换为Python的可行性与操作
前端·javascript·python
科大饭桶2 小时前
昇腾AI自学Day2-- 深度学习基础工具与数学
人工智能·pytorch·python·深度学习·numpy
聚客AI3 小时前
🔷告别天价算力!2025性价比最高的LLM私有化训练路径
人工智能·llm·掘金·日新计划
用户84913717547164 小时前
joyagent智能体学习(第1期):项目概览与架构解析
人工智能·llm·agent
逍岚子4 小时前
以官网计算器为例:手把手教你用 TypeScript SDK 开发 MCP Server
llm·agent·mcp