实时音视频 如何实现上下文管理

大模型的上下面管理非常重要,它可以让大模型根据聊天历史进行更精准的内容回答。TRTC AI 提供了基本的上下面管理能力,同时也支持开发者实现自己的丰富上下面管理方案。

相关产品
实时音视频 产品页,含版本规格、价格与购买入口。

一、基本的上下文管理

TRTC AI 提供了基本的上下文管理功能。在 LLMConfig 参数中,我们引入了 History 参数来控制上下文管理:

History:

·设置 LLM 的上下文轮次默认值0(不提供上下文管理)。

·最大值:50(提供最近50轮的上下文管理)。

相关配置示例如下:

"LLMConfig": { "LLMType": "openai", "Model":"gpt-4o", "APIKey":"api-key", "APIUrl":"https://api.openai.com/chat/completions", "Streaming": true, "SystemPrompt": "你是一个个人助手", "Timeout": 3.0, "History": 5 // 最大支持 50 轮对话, 默认为 0 }

二、自定义上下文管理

TRTC AI 对话服务支持标准的 OpenAI 规范,这使得开发者能够在自己的业务中实现定制化的上下文管理。实现流程如下:

这个流程图展示了自定义上下文管理的基本步骤。开发者可以根据自己的具体需求对这个流程进行调整和优化。

三、实现示例

开发者可以在自己的业务后台实现与 OpenAI API 兼容的大模型接口,并将封装了上下文逻辑的大模型请求发送给第三方大模型。以下是一个简化的示例代码:

import timefrom fastapi import FastAPI, HTTPExceptionfrom fastapi.middleware.cors import CORSMiddlewarefrom pydantic import BaseModelfrom typing import List, Optionalfrom langchain_core.messages import HumanMessage, SystemMessagefrom langchain_openai import ChatOpenAIapp = FastAPI(debug=True)# 添加 CORS 中间件app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"],)class Message(BaseModel): role: str content: strclass ChatRequest(BaseModel): model: str messages: List[Message] temperature: Optional[float] = 0.7class ChatResponse(BaseModel): id: str object: str created: int model: str choices: List[dict] usage: dict@app.post("/v1/chat/completions")async def chat_completions(request: ChatRequest): try: # 将请求消息转换为 LangChain 消息格式 langchain_messages = [] for msg in request.messages: if msg.role == "system": langchain_messages.append(SystemMessage(content=msg.content)) elif msg.role == "user": langchain_messages.append(HumanMessage(content=msg.content)) # add more historys  # 使用 LangChain 的 ChatOpenAI 模型 chat = ChatOpenAI(temperature=request.temperature, model_name=request.model) response = chat(langchain_messages) print(response) # 构造符合 OpenAI API 格式的响应 return ChatResponse( id="chatcmpl-" + "".join([str(ord(c)) for c in response.content[:8]]), object="chat.completion", created=int(time.time()), model=request.model, choices=[{ "index": 0, "message": { "role": "assistant", "content": response.content }, "finish_reason": "stop" }], usage={ "prompt_tokens": -1, # LangChain 不提供这些信息,所以我们使用占位值 "completion_tokens": -1, "total_tokens": -1 } ) except Exception as e: raise HTTPException(status_code=500, detail=str(e))if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=8000)

详情请点击 查看示例。

赞 (0)
上一篇 4天前
下一篇 4天前