AS AgentScopeAPI 手册 / API Reference

ChatCompletionsResponseBuilder

public class ChatCompletionsResponseBuilder
源码Source io/agentscope/core/chat/completions/builder/ChatCompletionsResponseBuilder.java:76–257

聊天补全响应构建器,用于构建并配置目标实例。位于 io.agentscope.core.chat.completions.builder 包。核心成员包括 buildResponse、buildErrorResponse、convertMsgToChatMessage、extractTextContent。

Service for building Chat Completions API responses.

This builder handles the construction of response objects that are 100% compatible with OpenAI's Chat Completions API format.

Response Format:

`{
"id": "chatcmpl-xxx",
"object": "chat.completion",
"created": 1234567890,
"model": "gpt-4",
"choices": [{
"index": 0,
"message": {"role": "assistant", "content": "Hello!"},
"finish_reason": "stop"
}],
"usage": {"prompt_tokens": 10, "completion_tokens": 5, "total_tokens": 15}
}
`

Tool Calls: When the agent uses tools, the response includes tool_calls:

`{
"message": {
"role": "assistant",
"content": null,
"tool_calls": [{
"id": "call_abc",
"type": "function",
"function": {"name": "get_weather", "arguments": "{\"city\":\"Hangzhou\"}"}
}]
},
"finish_reason": "tool_calls"
}
`

方法摘要Method Summary

方法Method说明Description
public ChatCompletionsResponse buildResponse(ChatCompletionsRequest request, Msg reply, String requestId)
构建响应。
Build a successful chat completion response.
public ChatCompletionsResponse buildErrorResponse(ChatCompletionsRequest request, Throwable error, String requestId)
构建错误响应。
Build an error response.
public ChatMessage convertMsgToChatMessage(Msg msg)
转换内部消息到外部 ChatMessage。
Convert an internal Msg to an external ChatMessage.
public String extractTextContent(Msg msg)
设置该配置项。
Extract text content from a Msg safely.

方法详情Method Detail

buildResponse

public ChatCompletionsResponse buildResponse(ChatCompletionsRequest request, Msg reply, String requestId)

构建响应。

Build a successful chat completion response.

参数Parameters
名称Name类型Type说明Description
requestChatCompletionsRequest请求(类型 `ChatCompletionsRequest`)。The original request
replyMsg智能体's 回复消息。The agent's reply message
requestIdString请求标识(类型 `String`)。The request ID for tracking
返回Returns ChatCompletionsResponse
返回ChatCompletionsResponse。A `ChatCompletionsResponse` containing the agent's reply
源码Source io/agentscope/core/chat/completions/builder/ChatCompletionsResponseBuilder.java:88–117

buildErrorResponse

public ChatCompletionsResponse buildErrorResponse(ChatCompletionsRequest request, Throwable error, String requestId)

构建错误响应。

Build an error response.

参数Parameters
名称Name类型Type说明Description
requestChatCompletionsRequest请求(类型 `ChatCompletionsRequest`)。The original request
errorThrowable错误(类型 `Throwable`)。The error that occurred
requestIdString请求标识(类型 `String`)。The request ID for tracking
返回Returns ChatCompletionsResponse
返回ChatCompletionsResponse。A `ChatCompletionsResponse` containing the error message with finish reason "error"
源码Source io/agentscope/core/chat/completions/builder/ChatCompletionsResponseBuilder.java:128–146

convertMsgToChatMessage

public ChatMessage convertMsgToChatMessage(Msg msg)

转换内部消息到外部 ChatMessage。

Convert an internal Msg to an external ChatMessage.

参数Parameters
名称Name类型Type说明Description
msgMsg内部消息到转换。The internal message to convert
返回Returns ChatMessage
返回ChatMessage。The OpenAI-compatible ChatMessage
源码Source io/agentscope/core/chat/completions/builder/ChatCompletionsResponseBuilder.java:156–195

extractTextContent

public String extractTextContent(Msg msg)

设置该配置项。

Extract text content from a Msg safely.

参数Parameters
名称Name类型Type说明Description
msgMsg消息(类型 `Msg`)。The message to extract text from
返回Returns String
返回字符串结果。The text content as a `String`, or an empty string if the message is null or contains no text
源码Source io/agentscope/core/chat/completions/builder/ChatCompletionsResponseBuilder.java:204–210
基于官方固定版本源码静态生成Generated from pinned official sources · Python 2.0.11 · Java 2.0.4
签名与文档为静态抽取;如有疑问请以源码为准。Signatures and docs are extracted statically; verify against the source when in doubt.