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
相关推荐
红豆怪怪几秒前
[LeetCode 热题 100] 32. 最长有效括号
数据结构·python·算法·leetcode·动态规划·代理模式
大嘴带你水论文1 小时前
震惊!仅用10张照片就能随意编辑3D人脸?韩国KAIST最新黑科技FFaceNeRF解析!
论文阅读·人工智能·python·科技·计算机视觉·3d·transformer
CodeCraft Studio1 小时前
国产化PDF处理控件Spire.PDF教程:如何在 Java 中通过模板生成 PDF
java·python·pdf·spire.pdf·java创建pdf·从html创建pdf
即兴小索奇1 小时前
Recharts:React图表库,组件化设计助力高效数据可视化开发
ai·商业·ai商业洞察·即兴小索奇
摆烂z2 小时前
Jupyter Notebook的交互式开发环境方便py开发
ide·python·jupyter
302AI2 小时前
Claude 断供中国之际,Kimi-K2-0905 低调上线:时势造英雄
人工智能·llm·ai编程
一乐小哥3 小时前
一口气同步10年豆瓣记录———豆瓣书影音同步 Notion分享 🚀
后端·python
华研前沿标杆游学3 小时前
华为在国内搞的研发基地有多野?标杆游学带你解锁“研发界顶流”
python
小胖墩有点瘦3 小时前
【基于深度学习的中草药识别系统】
人工智能·python·深度学习·课程设计·计算机毕业设计·中草药识别
正在走向自律3 小时前
Ubuntu系统下Python连接国产KingbaseES数据库实现增删改查
开发语言·数据库·python·ubuntu·kingbasees·ksycopg2