Class ChatCompletionsChunk

java.lang.Object
io.agentscope.core.chat.completions.model.ChatCompletionsChunk

public class ChatCompletionsChunk extends Object
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 Details

    • ChatCompletionsChunk

      public ChatCompletionsChunk()
      Default constructor.
    • ChatCompletionsChunk

      public ChatCompletionsChunk(String id, String model)
      Create a chunk with basic info.
      Parameters:
      id - Request ID
      model - Model name
  • Method Details

    • textChunk

      public static ChatCompletionsChunk textChunk(String id, String model, String content)
      Create a text content chunk.
      Parameters:
      id - Request ID
      model - Model name
      content - Text content delta
      Returns:
      ChatCompletionsChunk with text delta
    • toolCallChunk

      public static ChatCompletionsChunk toolCallChunk(String id, String model, List<ToolCall> toolCalls)
      Create a tool call chunk.
      Parameters:
      id - Request ID
      model - Model name
      toolCalls - 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 ID
      model - Model name
      toolCallId - The ID of the tool call this result corresponds to (currently unused in streaming)
      toolName - The name of the tool that was executed
      content - The tool execution result content
      Returns:
      ChatCompletionsChunk with tool result formatted as assistant content
    • finishChunk

      public static ChatCompletionsChunk finishChunk(String id, String model, String finishReason)
      Create a finish chunk.
      Parameters:
      id - Request ID
      model - Model name
      finishReason - The finish reason (stop, tool_calls, etc.)
      Returns:
      ChatCompletionsChunk with finish_reason
    • getId

      public String getId()
    • setId

      public void setId(String id)
    • getObject

      public String getObject()
    • setObject

      public void setObject(String object)
    • getCreated

      public long getCreated()
    • setCreated

      public void setCreated(long created)
    • getModel

      public String getModel()
    • setModel

      public void setModel(String model)
    • getChoices

      public List<ChatChoice> getChoices()
    • setChoices

      public void setChoices(List<ChatChoice> choices)