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
相关推荐
蓝婷儿44 分钟前
Python 机器学习核心入门与实战进阶 Day 1 - 分类 vs 回归
python·机器学习·分类
Devil枫2 小时前
Kotlin扩展函数与属性
开发语言·python·kotlin
程序员阿超的博客3 小时前
Python 数据分析与机器学习入门 (八):用 Scikit-Learn 跑通第一个机器学习模型
python·机器学习·数据分析·scikit-learn·入门教程·python教程
xingshanchang4 小时前
PyTorch 不支持旧GPU的异常状态与解决方案:CUDNN_STATUS_NOT_SUPPORTED_ARCH_MISMATCH
人工智能·pytorch·python
Deepoch6 小时前
Deepoc 大模型在无人机行业应用效果的方法
人工智能·科技·ai·语言模型·无人机
Deepoch6 小时前
Deepoc 大模型:无人机行业的智能变革引擎
人工智能·科技·算法·ai·动态规划·无人机
费弗里7 小时前
Python全栈应用开发利器Dash 3.x新版本介绍(1)
python·dash
李少兄9 天前
解决OSS存储桶未创建导致的XML错误
xml·开发语言·python
就叫飞六吧9 天前
基于keepalived、vip实现高可用nginx (centos)
python·nginx·centos
Vertira9 天前
PyTorch中的permute, transpose, view, reshape和flatten函数详解(已解决)
人工智能·pytorch·python