Fix:The OpenAI-Compatible Agent API returns an incorrect message (#8177)

### What problem does this PR solve?

https://github.com/infiniflow/ragflow/issues/8175

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
This commit is contained in:
Stephen Hu
2025-06-12 19:17:15 +08:00
committed by GitHub
parent 24ca4cc6b7
commit 1ab0f52832
4 changed files with 28 additions and 5 deletions

View File

@ -294,8 +294,22 @@ def completionOpenAI(tenant_id, agent_id, question, session_id=None, stream=True
"source": "agent",
"dsl": cvs.dsl
}
canvas.messages.append({"role": "user", "content": question, "id": message_id})
canvas.add_user_input(question)
API4ConversationService.save(**conv)
conv = API4Conversation(**conv)
if not conv.message:
conv.message = []
conv.message.append({
"role": "user",
"content": question,
"id": message_id
})
if not conv.reference:
conv.reference = []
conv.reference.append({"chunks": [], "doc_aggs": []})
# Handle existing session
else:
@ -331,7 +345,7 @@ def completionOpenAI(tenant_id, agent_id, question, session_id=None, stream=True
if stream:
try:
completion_tokens = 0
for ans in canvas.run(stream=True):
for ans in canvas.run(stream=True, bypass_begin=True):
if ans.get("running_status"):
completion_tokens += len(tiktokenenc.encode(ans.get("content", "")))
yield "data: " + json.dumps(
@ -394,7 +408,7 @@ def completionOpenAI(tenant_id, agent_id, question, session_id=None, stream=True
else: # Non-streaming mode
try:
all_answer_content = ""
for answer in canvas.run(stream=False):
for answer in canvas.run(stream=False, bypass_begin=True):
if answer.get("running_status"):
continue