Display actions in frontend (needs to add manifest files to LibreChat)
This commit is contained in:
@@ -170,19 +170,19 @@ async def chat_completions(chat_request: ChatCompletionRequest):
|
||||
f"Chat request - stream={chat_request.stream}, input_length={len(user_input)}"
|
||||
)
|
||||
|
||||
try:
|
||||
answer = agent.step(user_input)
|
||||
except LLMAPIError as e:
|
||||
logger.error(f"LLM API error: {e}")
|
||||
raise HTTPException(status_code=502, detail=f"LLM API error: {e}")
|
||||
except Exception as e:
|
||||
logger.error(f"Agent error: {e}", exc_info=True)
|
||||
raise HTTPException(status_code=500, detail="Internal agent error")
|
||||
|
||||
created_ts = int(time.time())
|
||||
completion_id = f"chatcmpl-{uuid.uuid4().hex}"
|
||||
|
||||
if not chat_request.stream:
|
||||
try:
|
||||
answer = agent.step(user_input)
|
||||
except LLMAPIError as e:
|
||||
logger.error(f"LLM API error: {e}")
|
||||
raise HTTPException(status_code=502, detail=f"LLM API error: {e}")
|
||||
except Exception as e:
|
||||
logger.error(f"Agent error: {e}", exc_info=True)
|
||||
raise HTTPException(status_code=500, detail="Internal agent error")
|
||||
|
||||
return JSONResponse(
|
||||
{
|
||||
"id": completion_id,
|
||||
@@ -205,20 +205,44 @@ async def chat_completions(chat_request: ChatCompletionRequest):
|
||||
)
|
||||
|
||||
async def event_generator():
|
||||
chunk = {
|
||||
"id": completion_id,
|
||||
"object": "chat.completion.chunk",
|
||||
"created": created_ts,
|
||||
"model": chat_request.model,
|
||||
"choices": [
|
||||
{
|
||||
"index": 0,
|
||||
"delta": {"role": "assistant", "content": answer or ""},
|
||||
"finish_reason": "stop",
|
||||
}
|
||||
],
|
||||
}
|
||||
yield f"data: {json.dumps(chunk, ensure_ascii=False)}\n\n"
|
||||
yield "data: [DONE]\n\n"
|
||||
try:
|
||||
# Stream the agent execution
|
||||
async for chunk in agent.step_streaming(user_input, completion_id, created_ts, chat_request.model):
|
||||
yield f"data: {json.dumps(chunk, ensure_ascii=False)}\n\n"
|
||||
yield "data: [DONE]\n\n"
|
||||
except LLMAPIError as e:
|
||||
logger.error(f"LLM API error: {e}")
|
||||
error_chunk = {
|
||||
"id": completion_id,
|
||||
"object": "chat.completion.chunk",
|
||||
"created": created_ts,
|
||||
"model": chat_request.model,
|
||||
"choices": [
|
||||
{
|
||||
"index": 0,
|
||||
"delta": {"role": "assistant", "content": f"Error: {e}"},
|
||||
"finish_reason": "stop",
|
||||
}
|
||||
],
|
||||
}
|
||||
yield f"data: {json.dumps(error_chunk, ensure_ascii=False)}\n\n"
|
||||
yield "data: [DONE]\n\n"
|
||||
except Exception as e:
|
||||
logger.error(f"Agent error: {e}", exc_info=True)
|
||||
error_chunk = {
|
||||
"id": completion_id,
|
||||
"object": "chat.completion.chunk",
|
||||
"created": created_ts,
|
||||
"model": chat_request.model,
|
||||
"choices": [
|
||||
{
|
||||
"index": 0,
|
||||
"delta": {"role": "assistant", "content": "Internal agent error"},
|
||||
"finish_reason": "stop",
|
||||
}
|
||||
],
|
||||
}
|
||||
yield f"data: {json.dumps(error_chunk, ensure_ascii=False)}\n\n"
|
||||
yield "data: [DONE]\n\n"
|
||||
|
||||
return StreamingResponse(event_generator(), media_type="text/event-stream")
|
||||
|
||||
Reference in New Issue
Block a user