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 |
|---|---|---|
request | ChatCompletionsRequest | 请求(类型 `ChatCompletionsRequest`)。The original request |
reply | Msg | 智能体's 回复消息。The agent's reply message |
requestId | String | 请求标识(类型 `String`)。The request ID for tracking |
ChatCompletionsResponse返回ChatCompletionsResponse。A `ChatCompletionsResponse` containing the agent's reply
源码Source
io/agentscope/core/chat/completions/builder/ChatCompletionsResponseBuilder.java:88–117buildErrorResponse
public ChatCompletionsResponse buildErrorResponse(ChatCompletionsRequest request, Throwable error, String requestId)
构建错误响应。
Build an error response.
参数Parameters
| 名称Name | 类型Type | 说明Description |
|---|---|---|
request | ChatCompletionsRequest | 请求(类型 `ChatCompletionsRequest`)。The original request |
error | Throwable | 错误(类型 `Throwable`)。The error that occurred |
requestId | String | 请求标识(类型 `String`)。The request ID for tracking |
ChatCompletionsResponse返回ChatCompletionsResponse。A `ChatCompletionsResponse` containing the error message with finish reason "error"
源码Source
io/agentscope/core/chat/completions/builder/ChatCompletionsResponseBuilder.java:128–146convertMsgToChatMessage
public ChatMessage convertMsgToChatMessage(Msg msg)
转换内部消息到外部 ChatMessage。
Convert an internal Msg to an external ChatMessage.
参数Parameters
| 名称Name | 类型Type | 说明Description |
|---|---|---|
msg | Msg | 内部消息到转换。The internal message to convert |
ChatMessage返回ChatMessage。The OpenAI-compatible ChatMessage
源码Source
io/agentscope/core/chat/completions/builder/ChatCompletionsResponseBuilder.java:156–195extractTextContent
public String extractTextContent(Msg msg)
设置该配置项。
Extract text content from a Msg safely.
参数Parameters
| 名称Name | 类型Type | 说明Description |
|---|---|---|
msg | Msg | 消息(类型 `Msg`)。The message to extract text from |
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