Feat: add primitive support for function calls (#6840)

### What problem does this PR solve?

This PR introduces ​**​primitive support for function calls​**​,
enabling the system to handle basic function call capabilities.
However, this feature is currently experimental and ​**​not yet enabled
for general use​**​, as it is only supported by a subset of models,
namely, Qwen and OpenAI models.

### Type of change

- [x] New Feature (non-breaking change which adds functionality)
This commit is contained in:
Yongteng Lei
2025-04-08 16:09:03 +08:00
committed by GitHub
parent a20439bf81
commit dc2c74b249
5 changed files with 574 additions and 130 deletions

View File

@ -116,7 +116,7 @@ def create_agent_session(tenant_id, agent_id):
for ans in canvas.run(stream=False):
pass
cvs.dsl = json.loads(str(canvas))
conv = {"id": get_uuid(), "dialog_id": cvs.id, "user_id": user_id, "message": [{"role": "assistant", "content": canvas.get_prologue()}], "source": "agent", "dsl": cvs.dsl}
API4ConversationService.save(**conv)
@ -243,6 +243,11 @@ def chat_completion_openai_like(tenant_id, chat_id):
msg = None
msg = [m for m in messages if m["role"] != "system" and (m["role"] != "assistant" or msg)]
# tools = get_tools()
# toolcall_session = SimpleFunctionCallServer()
tools = None
toolcall_session = None
if req.get("stream", True):
# The value for the usage field on all chunks except for the last one will be null.
# The usage field on the last chunk contains token usage statistics for the entire request.
@ -262,7 +267,7 @@ def chat_completion_openai_like(tenant_id, chat_id):
}
try:
for ans in chat(dia, msg, True):
for ans in chat(dia, msg, True, toolcall_session=toolcall_session, tools=tools):
answer = ans["answer"]
reasoning_match = re.search(r"<think>(.*?)</think>", answer, flags=re.DOTALL)
@ -325,7 +330,7 @@ def chat_completion_openai_like(tenant_id, chat_id):
return resp
else:
answer = None
for ans in chat(dia, msg, False):
for ans in chat(dia, msg, False, toolcall_session=toolcall_session, tools=tools):
# focus answer content only
answer = ans
break