类 OpenAIResponse

java.lang.Object
io.agentscope.core.formatter.openai.dto.OpenAIResponse

public class OpenAIResponse extends Object
OpenAI Chat Completion API response DTO.

This class represents the response structure from OpenAI's Chat Completion API. It supports both non-streaming and streaming (chunk) responses.

Example non-streaming response:


 {
   "id": "chatcmpl-123",
   "object": "chat.completion",
   "created": 1677652280,
   "model": "gpt-4o",
   "choices": [{
     "index": 0,
     "message": {
       "role": "assistant",
       "content": "Hello!"
     },
     "finish_reason": "stop"
   }],
   "usage": {
     "prompt_tokens": 10,
     "completion_tokens": 20,
     "total_tokens": 30
   }
 }
 

Example streaming chunk:


 {
   "id": "chatcmpl-123",
   "object": "chat.completion.chunk",
   "created": 1677652280,
   "model": "gpt-4o",
   "choices": [{
     "index": 0,
     "delta": {
       "content": "Hello"
     },
     "finish_reason": null
   }]
 }
 
  • 构造器详细资料

    • OpenAIResponse

      public OpenAIResponse()
  • 方法详细资料

    • 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<OpenAIChoice> getChoices()
    • setChoices

      public void setChoices(List<OpenAIChoice> choices)
    • getUsage

      public OpenAIUsage getUsage()
    • setUsage

      public void setUsage(OpenAIUsage usage)
    • getSystemFingerprint

      public String getSystemFingerprint()
    • setSystemFingerprint

      public void setSystemFingerprint(String systemFingerprint)
    • getError

      public OpenAIError getError()
    • setError

      public void setError(OpenAIError error)
    • isError

      public boolean isError()
      Check if this response represents an error.
      返回:
      true if the response contains an error
    • isChunk

      public boolean isChunk()
      Check if this is a streaming chunk response.

      Detects streaming chunks by:

      1. Object type not equals "chat.completion" (Not Streaming)
      2. Object type equals "chat.completion.chunk" (OpenAI standard)
      3. OR presence of delta field without message field (GLM and other OpenAI-compatible APIs)
      返回:
      true if this is a streaming chunk response
    • getFirstChoice

      public OpenAIChoice getFirstChoice()
      Get the first choice if available.
      返回:
      the first choice, or null if no choices