Class ChatCompletionsChunk
java.lang.Object
io.agentscope.core.chat.completions.model.ChatCompletionsChunk
Streaming chunk for Chat Completions API.
This follows OpenAI's streaming response format for chat completions. Each chunk contains a delta with partial content.
Example streaming chunk:
{
"id": "chatcmpl-xxx",
"object": "chat.completion.chunk",
"created": 1234567890,
"model": "gpt-4",
"choices": [{
"index": 0,
"delta": {"content": "Hello"},
"finish_reason": null
}]
}
Tool call streaming chunk:
{
"id": "chatcmpl-xxx",
"object": "chat.completion.chunk",
"created": 1234567890,
"model": "gpt-4",
"choices": [{
"index": 0,
"delta": {
"tool_calls": [{
"index": 0,
"id": "call_abc",
"type": "function",
"function": {"name": "get_weather", "arguments": "{...}"}
}]
},
"finish_reason": null
}]
}
-
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor.ChatCompletionsChunk(String id, String model) Create a chunk with basic info. -
Method Summary
Modifier and TypeMethodDescriptionstatic ChatCompletionsChunkfinishChunk(String id, String model, String finishReason) Create a finish chunk.longgetId()getModel()voidsetChoices(List<ChatChoice> choices) voidsetCreated(long created) voidvoidvoidstatic ChatCompletionsChunkCreate a text content chunk.static ChatCompletionsChunktoolCallChunk(String id, String model, List<ToolCall> toolCalls) Create a tool call chunk.static ChatCompletionsChunkCreate a tool result chunk.
-
Constructor Details
-
ChatCompletionsChunk
public ChatCompletionsChunk()Default constructor. -
ChatCompletionsChunk
Create a chunk with basic info.- Parameters:
id- Request IDmodel- Model name
-
-
Method Details
-
textChunk
Create a text content chunk.- Parameters:
id- Request IDmodel- Model namecontent- Text content delta- Returns:
- ChatCompletionsChunk with text delta
-
toolCallChunk
Create a tool call chunk.- Parameters:
id- Request IDmodel- Model nametoolCalls- Tool calls to include- Returns:
- ChatCompletionsChunk with tool calls
-
toolResultChunk
public static ChatCompletionsChunk toolResultChunk(String id, String model, String toolCallId, String toolName, String content) Create a tool result chunk.This represents the result of a tool execution in streaming format. While not part of the standard OpenAI streaming response (since OpenAI doesn't execute tools), AgentScope's ReActAgent executes tools internally, so we expose the results in the stream.
Important: In OpenAI's streaming API specification, delta.role can only be "assistant" or "user". The role "tool" is not supported in streaming responses. Therefore, tool results are formatted as assistant content with a prefix indicating the tool name.
Example output:
{ "id": "chatcmpl-xxx", "object": "chat.completion.chunk", "created": 1234567890, "model": "gpt-4", "choices": [{ "index": 0, "delta": { "role": "assistant", "content": "[Tool: get_weather] The weather is sunny..." } }] }- Parameters:
id- Request IDmodel- Model nametoolCallId- The ID of the tool call this result corresponds to (currently unused in streaming)toolName- The name of the tool that was executedcontent- The tool execution result content- Returns:
- ChatCompletionsChunk with tool result formatted as assistant content
-
finishChunk
Create a finish chunk.- Parameters:
id- Request IDmodel- Model namefinishReason- The finish reason (stop, tool_calls, etc.)- Returns:
- ChatCompletionsChunk with finish_reason
-
getId
-
setId
-
getObject
-
setObject
-
getCreated
public long getCreated() -
setCreated
public void setCreated(long created) -
getModel
-
setModel
-
getChoices
-
setChoices
-