Package com.sap.ai.sdk.foundationmodels.openai

# Class OpenAiChatCompletionRequest

java.lang.Object
com.sap.ai.sdk.foundationmodels.openai.OpenAiChatCompletionRequest

public final class OpenAiChatCompletionRequest extends Object
Represents a request for OpenAI chat completion, including conversation messages and parameters.

Since:
  1.4.0
See Also:
  - OpenAI API Reference

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `OpenAiChatCompletionRequest(OpenAiMessage message, OpenAiMessage... messages)`
  Creates an OpenAiChatCompletionPrompt with a multiple unpacked messages.
  `OpenAiChatCompletionRequest(String message)`
  Creates an OpenAiChatCompletionPrompt with string as user message.
  `OpenAiChatCompletionRequest(List<OpenAiMessage> messages)`
  Creates an OpenAiChatCompletionPrompt with a list of messages.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `boolean`
  `equals(Object o)`
  `int`
  `hashCode()`
  `String`
  `toString()`
  `OpenAiChatCompletionRequest`
  `withFrequencyPenalty(BigDecimal frequencyPenalty)`
  Encourage new topic by penalising tokens based on their frequency in the completion.
  `OpenAiChatCompletionRequest`
  `withLogitBias(Map<String,Integer> logitBias)`
  A map that adjusts the likelihood of specified tokens by adding a bias value (between -100 and 100) to the logits before sampling.
  `OpenAiChatCompletionRequest`
  `withLogprobs(Boolean logprobs)`
  Sets the log probabilities option.
  `OpenAiChatCompletionRequest`
  `withMaxCompletionTokens(Integer maxCompletionTokens)`
  Maximum number of tokens that can be generated for the completion, including consumed reasoning tokens.
  `OpenAiChatCompletionRequest`
  `withMaxTokens(Integer maxTokens)`
  Maximum number of tokens that can be generated for the completion.
  `OpenAiChatCompletionRequest`
  `withMessages(List<OpenAiMessage> messages)`
  List of messages from the conversation.
  `OpenAiChatCompletionRequest`
  `withN(Integer n)`
  Number of completions to generate.
  `OpenAiChatCompletionRequest`
  `withParallelToolCalls(Boolean parallelToolCalls)`
  Sets the parallel tool calls option.
  `OpenAiChatCompletionRequest`
  `withPresencePenalty(BigDecimal presencePenalty)`
  Encourage new topic by penalising token based on their presence in the completion.
  `OpenAiChatCompletionRequest`
  `withResponseFormat(CreateChatCompletionRequestAllOfResponseFormat responseFormat)`
  Response format for the completion.
  `OpenAiChatCompletionRequest`
  `withSeed(Integer seed)`
  Seed for random number generation.
  `OpenAiChatCompletionRequest`
  `withStop(String sequence, String... sequences)`
  Adds stop sequences to the request.
  `OpenAiChatCompletionRequest`
  `withStop(List<String> stop)`
  Upto 4 Stop sequences to interrupts token generation and returns a response without them.
  `OpenAiChatCompletionRequest`
  `withStreamOptions(ChatCompletionStreamOptions streamOptions)`
  Options for streaming the completion response.
  `OpenAiChatCompletionRequest`
  `withTemperature(BigDecimal temperature)`
  Controls the randomness of the completion.
  `OpenAiChatCompletionRequest`
  `withToolChoice(OpenAiToolChoice choice)`
  Define the model behavior towards calling functions.
  `OpenAiChatCompletionRequest`
  `withTools(List<ChatCompletionTool> tools)`
  Tools the model may invoke during chat completion (metadata only).
  `OpenAiChatCompletionRequest`
  `withToolsExecutable(List<OpenAiTool> toolsExecutable)`
  Tools the model may invoke during chat completion that are also executable at application runtime.
  `OpenAiChatCompletionRequest`
  `withTopLogprobs(Integer topLogprobs)`
  Number of top log probabilities to return for each token.
  `OpenAiChatCompletionRequest`
  `withTopP(BigDecimal topP)`
  Controls the cumulative probability threshold used for nucleus sampling.
  `OpenAiChatCompletionRequest`
  `withUser(String user)`
  Unique identifier for the end-user making the request.

  ### Methods inherited from class java.lang.Object

  `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

- ## Constructor Details

  - ### OpenAiChatCompletionRequest

    public OpenAiChatCompletionRequest(@Nonnull String message)
    Creates an OpenAiChatCompletionPrompt with string as user message.
    Parameters:
      `message` - the message to be added to the prompt

  - ### OpenAiChatCompletionRequest

    public OpenAiChatCompletionRequest(@Nonnull OpenAiMessage message, @Nonnull OpenAiMessage... messages)
    Creates an OpenAiChatCompletionPrompt with a multiple unpacked messages.
    Parameters:
      `message` - the primary message to be added to the prompt
      `messages` - additional messages to be added to the prompt

  - ### OpenAiChatCompletionRequest

    public OpenAiChatCompletionRequest(@Nonnull List<OpenAiMessage> messages)
    Creates an OpenAiChatCompletionPrompt with a list of messages.
    Parameters:
      `messages` - the list of messages to be added to the prompt
    Since:
      1.6.0

- ## Method Details

  - ### withStop

    @Nonnull public OpenAiChatCompletionRequest withStop(@Nonnull String sequence, @Nonnull String... sequences)
    Adds stop sequences to the request.
    Parameters:
      `sequence` - the primary stop sequence
      `sequences` - additional stop sequences
    Returns:
      a new OpenAiChatCompletionRequest instance with the specified stop sequences

  - ### withParallelToolCalls

    @Nonnull public OpenAiChatCompletionRequest withParallelToolCalls(@Nonnull Boolean parallelToolCalls)
    Sets the parallel tool calls option.
    Parameters:
      `parallelToolCalls` - Whether to allow parallel tool calls.
    Returns:
      A new instance with the specified option.

  - ### withLogprobs

    @Nonnull public OpenAiChatCompletionRequest withLogprobs(@Nonnull Boolean logprobs)
    Sets the log probabilities option.
    Parameters:
      `logprobs` - Whether to include log probabilities in the response.
    Returns:
      A new instance with the specified option.

  - ### withToolChoice

    @Nonnull public OpenAiChatCompletionRequest withToolChoice(@Nonnull OpenAiToolChoice choice)
    Define the model behavior towards calling functions.

    Example:

    - `.withToolChoice(OpenAiToolChoice.NONE)`
    - `.withToolChoice(OpenAiToolChoice.OPTIONAL)`
    - `.withToolChoice(OpenAiToolChoice.REQUIRED)`
    - `.withToolChoice(OpenAiToolChoice.function("fibonacci")`
    Parameters:
      `choice` - the generic tool choice.
    Returns:
      the current OpenAiChatCompletionRequest instance.

  - ### equals

    public boolean equals(Object o)
    Overrides:
      `equals` in class `Object`

  - ### hashCode

    public int hashCode()
    Overrides:
      `hashCode` in class `Object`

  - ### toString

    public String toString()
    Overrides:
      `toString` in class `Object`

  - ### withMessages

    public OpenAiChatCompletionRequest withMessages(@Nonnull List<OpenAiMessage> messages)
    List of messages from the conversation.
    Returns:
      a clone of this object, except with this updated property (returns `this` if an identical value is passed).

  - ### withStop

    public OpenAiChatCompletionRequest withStop(@Nullable List<String> stop)
    Upto 4 Stop sequences to interrupts token generation and returns a response without them.
    Returns:
      a clone of this object, except with this updated property (returns `this` if an identical value is passed).

  - ### withTemperature

    public OpenAiChatCompletionRequest withTemperature(@Nullable BigDecimal temperature)
    Controls the randomness of the completion.

    Lower values (e.g. 0.0) make the model more deterministic and repetitive, while higher values (e.g. 1.0) make the model more random and creative.

    Returns:
      a clone of this object, except with this updated property (returns `this` if an identical value is passed).

  - ### withTopP

    public OpenAiChatCompletionRequest withTopP(@Nullable BigDecimal topP)
    Controls the cumulative probability threshold used for nucleus sampling. Alternative to `temperature`.

    Lower values (e.g. 0.1) limit the model to consider only the smallest set of tokens whose combined probabilities add up to at least 10% of the total.

    Returns:
      a clone of this object, except with this updated property (returns `this` if an identical value is passed).

  - ### withMaxTokens

    public OpenAiChatCompletionRequest withMaxTokens(@Nullable Integer maxTokens)
    Maximum number of tokens that can be generated for the completion.
    Returns:
      a clone of this object, except with this updated property (returns `this` if an identical value is passed).

  - ### withMaxCompletionTokens

    public OpenAiChatCompletionRequest withMaxCompletionTokens(@Nullable Integer maxCompletionTokens)
    Maximum number of tokens that can be generated for the completion, including consumed reasoning tokens. This field supersedes `maxTokens` and should be used with newer models.
    Returns:
      a clone of this object, except with this updated property (returns `this` if an identical value is passed).

  - ### withPresencePenalty

    public OpenAiChatCompletionRequest withPresencePenalty(@Nullable BigDecimal presencePenalty)
    Encourage new topic by penalising token based on their presence in the completion.

    Value should be in range [-2, 2].

    Returns:
      a clone of this object, except with this updated property (returns `this` if an identical value is passed).

  - ### withFrequencyPenalty

    public OpenAiChatCompletionRequest withFrequencyPenalty(@Nullable BigDecimal frequencyPenalty)
    Encourage new topic by penalising tokens based on their frequency in the completion.

    Value should be in range [-2, 2].

    Returns:
      a clone of this object, except with this updated property (returns `this` if an identical value is passed).

  - ### withLogitBias

    public OpenAiChatCompletionRequest withLogitBias(@Nullable Map<String,Integer> logitBias)
    A map that adjusts the likelihood of specified tokens by adding a bias value (between -100 and 100) to the logits before sampling. Extreme values can effectively ban or enforce the selection of tokens.
    Returns:
      a clone of this object, except with this updated property (returns `this` if an identical value is passed).

  - ### withUser

    public OpenAiChatCompletionRequest withUser(@Nullable String user)
    Unique identifier for the end-user making the request. This can help with monitoring and abuse detection.
    Returns:
      a clone of this object, except with this updated property (returns `this` if an identical value is passed).

  - ### withTopLogprobs

    public OpenAiChatCompletionRequest withTopLogprobs(@Nullable Integer topLogprobs)
    Number of top log probabilities to return for each token. An integer between 0 and 20. This is only relevant if `logprobs` is enabled.
    Returns:
      a clone of this object, except with this updated property (returns `this` if an identical value is passed).

  - ### withN

    public OpenAiChatCompletionRequest withN(@Nullable Integer n)
    Number of completions to generate.
    Returns:
      a clone of this object, except with this updated property (returns `this` if an identical value is passed).

  - ### withSeed

    public OpenAiChatCompletionRequest withSeed(@Nullable Integer seed)
    Seed for random number generation.
    Returns:
      a clone of this object, except with this updated property (returns `this` if an identical value is passed).

  - ### withStreamOptions

    public OpenAiChatCompletionRequest withStreamOptions(@Nullable ChatCompletionStreamOptions streamOptions)
    Options for streaming the completion response.
    Returns:
      a clone of this object, except with this updated property (returns `this` if an identical value is passed).

  - ### withResponseFormat

    public OpenAiChatCompletionRequest withResponseFormat(@Nullable CreateChatCompletionRequestAllOfResponseFormat responseFormat)
    Response format for the completion.
    Returns:
      a clone of this object, except with this updated property (returns `this` if an identical value is passed).

  - ### withTools

    public OpenAiChatCompletionRequest withTools(@Nullable List<ChatCompletionTool> tools)
    Tools the model may invoke during chat completion (metadata only).

    Use `withToolsExecutable(java.util.List<com.sap.ai.sdk.foundationmodels.openai.OpenAiTool>)` for registering executable tools.

    Returns:
      a clone of this object, except with this updated property (returns `this` if an identical value is passed).

  - ### withToolsExecutable

    public OpenAiChatCompletionRequest withToolsExecutable(@Nullable List<OpenAiTool> toolsExecutable)
    Tools the model may invoke during chat completion that are also executable at application runtime.
    Returns:
      a clone of this object, except with this updated property (returns `this` if an identical value is passed).
    Since:
      1.8.0