Skip to content

OpenAI

Overview

This module adds support for selected Open AI models.

Maven Coordinates

In addition to the Helidon integration with LangChain4j core dependencies, you must add the following:

xml
<dependency>
    <groupId>io.helidon.integrations.langchain4j.providers</groupId>
    <artifactId>helidon-integrations-langchain4j-providers-open-ai</artifactId>
</dependency>

Components

OpenAiChatModel

To automatically create and add OpenAiChatModel to the service registry add the following lines to application.yaml:

yaml
langchain4j:
  providers:
    open-ai:
      api-key: "demo"

  models:
    openai-chat-model:
      provider: open-ai
      model-name: "gpt-4o-mini"

If enabled is set to false, the configuration is ignored, and the component is not created.

Full list of configuration properties:

KeyTypeDescription
api-keystringRequired. The API key used to authenticate requests to the OpenAI API.
base-urlstringThe base URL for the OpenAI API. If not present, the default value supplied from LangChain4j is used.
custom-headersMap<string, string>A map containing custom headers.
enabledbooleanIf set to false, OpenAI model will not be available even if configured.
frequency-penaltydoubleThe frequency penalty, between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model’s likelihood to repeat the same line.
log-requestsbooleanWhether to log API requests.
log-responsesbooleanWhether to log API responses.
logit-biasMap<string, int>LogitBias adjusts the likelihood of specific tokens appearing in a model’s response. A map of token IDs to bias values (-100 to 100). Positive values increase the chance of the token, while negative values reduce it, allowing fine control over token preferences in the output.
max-completion-tokensintThe maximum number of tokens allowed for the model’s response.
max-retriesintegerThe maximum number of retries for failed API requests.
max-tokensintThe maximum number of tokens to generate in the completion.
model-namestringThe model name to use (e.g., "gpt-3.5-turbo").
organization-idstringThe ID of the organization for API requests.
parallel-tool-callsbooleanWhether to allow parallel calls to tools.
presence-penaltydoubleThe presence penalty, between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, encouraging the model to use new words.
response-formatstringThe format in which the model should return the response.
seedintThe seed for the random number generator used by the model.
stopstring[]The list of sequences where the API will stop generating further tokens.
strict-json-schemabooleanWhether to enforce a strict JSON schema for the model’s output.
strict-toolsbooleanWhether to enforce strict validation of tools used by the model.
temperaturedoubleThe sampling temperature to use, between 0 and 2. Higher values make the output more random, while lower values make it more focused and deterministic.
timeoutdurationThe timeout setting for API requests. See here for the format.
top-pdoubleThe nucleus sampling value, where the model considers the results of the tokens with top_p probability mass.
userstringThe user ID associated with the API requests.

Proxy

The framework attempts to resolve a proxy using the following strategy:

  1. Check for a named service: Look in the service registry for a service of type java.net.Proxy with the name open-ai.chat-model.
  2. Fallback to another named service: If the first service is not found, search for a java.net.Proxy service with the name open-ai.
  3. No proxy: If neither service is found, the framework will not use a proxy.

Tokenizer

The framework attempts to resolve a tokenizer using the following strategy:

  1. Check for a named service: Look in the service registry for a service of type dev.langchain4j.model.Tokenizer with the name open-ai.chat-model.
  2. Fallback to another named service: If the first service is not found, search for a dev.langchain4j.model.Tokenizer service with the name open-ai.
  3. Default Tokenizer: If neither service is found, the framework will use the default tokenizer provided by LangChain4j.

OpenAiEmbeddingModel

To automatically create and add OpenAiEmbeddingModel to the service registry add the following lines to application.yaml:

yaml
langchain4j:
  providers:
    open-ai:
      api-key: "demo"

  models:
    openai-embedding-model:
      provider: open-ai
      model-name: "text-embedding-3-small"

If enabled is set to false, the configuration is ignored, and the component is not created.

Full list of configuration properties:

KeyTypeDescription
api-keystringRequired. The API key used to authenticate requests to the OpenAI API.
base-urlstringThe base URL for the OpenAI API. If not present, the default value supplied from LangChain4j is used.
custom-headersMap<string, string>A map containing custom headers.
dimensionsintThe dimensionality of the embeddings generated by the model.
enabledbooleanIf set to false, OpenAI model will not be available even if configured.
log-requestsbooleanWhether to log API requests.
log-responsesbooleanWhether to log API responses.
max-retriesintegerThe maximum number of retries for failed API requests.
model-namestringThe model name to use.
organization-idstringThe ID of the organization for API requests.
timeoutdurationThe timeout setting for API requests. See here for the format.
userstringThe user ID associated with the API requests.

Proxy

The framework attempts to resolve a proxy using the following strategy:

  1. Check for a named service: Look in the service registry for a service of type java.net.Proxy with the name open-ai.embedding-model.
  2. Fallback to another named service: If the first service is not found, search for a java.net.Proxy service with the name open-ai.
  3. No proxy: If neither service is found, the framework will not use a proxy.

Tokenizer

The framework attempts to resolve a tokenizer using the following strategy:

  1. Check for a named service: Look in the service registry for a service of type dev.langchain4j.model.Tokenizer with the name open-ai.embedding-model.
  2. Fallback to another named service: If the first service is not found, search for a dev.langchain4j.model.Tokenizer service with the name open-ai.
  3. Default Tokenizer: If neither service is found, the framework will use the default tokenizer provided by LangChain4j.

OpenAiImageModel

To automatically create and add OpenAiImageModel to the service registry add the following lines to application.yaml:

yaml
langchain4j:
  providers:
    open-ai:
      api-key: "demo"

  models:
    openai-image-model:
      provider: open-ai
      model-name: "gpt-image-1"

If enabled is set to false, the configuration is ignored, and the component is not created.

Full list of configuration properties:

KeyTypeDescription
api-keystringRequired. The API key used to authenticate requests to the OpenAI API.
base-urlstringThe base URL for the OpenAI API. If not present, the default value supplied from LangChain4j is used.
custom-headersMap<string, string>A map containing custom headers.
enabledbooleanIf set to false, the OpenAI model will not be available even if configured.
log-requestsbooleanWhether to log API requests.
log-responsesbooleanWhether to log API responses.
max-retriesintegerThe maximum number of retries for failed API requests.
model-namestringThe model name to use.
organization-idstringThe ID of the organization for API requests.
persist-toPathThe path or location where the generated images should be persisted.
qualitystringThe quality of the generated images.
response-formatstringThe format of the response.
sizestringThe desired size of the generated images.
stylestringThe style of the generated images.
timeoutdurationThe timeout setting for API requests. See here for the format.
userstringThe unique identifier for the user making the request.
with-persistingbooleanThe flag to indicate whether to persist the generated images.

Proxy

The framework attempts to resolve a proxy using the following strategy:

  1. Check for a named service: Look in the service registry for a service of type java.net.Proxy with the name open-ai.image-model.
  2. Fallback to another named service: If the first service is not found, search for a java.net.Proxy service with the name open-ai.
  3. No proxy: If neither service is found, the framework will not use a proxy.

OpenAiLanguageModel

To automatically create and add OpenAiLanguageModel to the service registry add the following lines to application.yaml:

yaml
langchain4j:
  providers:
    open-ai:
      api-key: "demo"

  models:
    openai-language-model:
      provider: open-ai
      model-name: "gpt-4o-mini"

If enabled is set to false, the configuration is ignored, and the component is not created.

Full list of configuration properties:

KeyTypeDescription
api-keystringRequired. The API key used to authenticate requests to the OpenAI API.
base-urlstringThe base URL for the OpenAI API. If not present, the default value supplied from LangChain4j is used.
custom-headersMap<string, string>A map containing custom headers.
enabledbooleanIf set to false, the OpenAI model will not be available even if configured.
log-requestsbooleanWhether to log API requests.
log-responsesbooleanWhether to log API responses.
max-retriesintegerThe maximum number of retries for failed API requests.
model-namestringThe model name to use.
organization-idstringThe ID of the organization for API requests.
temperaturedoubleThe sampling temperature to use, between 0 and 2. Higher values make the output more random, while lower values make it more focused and deterministic.
timeoutdurationThe timeout setting for API requests. See here for the format.

Proxy

The framework attempts to resolve a proxy using the following strategy:

  1. Check for a named service: Look in the service registry for a service of type java.net.Proxy with the name open-ai.language-model.
  2. Fallback to another named service: If the first service is not found, search for a java.net.Proxy service with the name open-ai.
  3. No proxy: If neither service is found, the framework will not use a proxy.

Tokenizer

The framework attempts to resolve a tokenizer using the following strategy:

  1. Check for a named service: Look in the service registry for a service of type dev.langchain4j.model.Tokenizer with the name open-ai.language-model.
  2. Fallback to another named service: If the first service is not found, search for a dev.langchain4j.model.Tokenizer service with the name open-ai.
  3. Default Tokenizer: If neither service is found, the framework will use the default tokenizer provided by LangChain4j.

OpenAiModerationModel

To automatically create and add OpenAiModerationModel to the service registry add the following lines to application.yaml:

yaml
langchain4j:
  providers:
    open-ai:
      api-key: "demo"

  models:
    openai-moderation-model:
      provider: open-ai
      model-name: "omni-moderation-latest"

If enabled is set to false, the configuration is ignored, and the component is not created.

Full list of configuration properties:

KeyTypeDescription
api-keystringRequired. The API key used to authenticate requests to the OpenAI API.
base-urlstringThe base URL for the OpenAI API. If not present, the default value supplied from LangChain4j is used.
custom-headersMap<string, string>A map containing custom headers.
enabledbooleanIf set to false, the OpenAI model will not be available even if configured.
log-requestsbooleanWhether to log API requests.
log-responsesbooleanWhether to log API responses.
max-retriesintegerThe maximum number of retries for failed API requests.
model-namestringThe model name to use.
organization-idstringThe ID of the organization for API requests.
timeoutdurationThe timeout setting for API requests. See here for the format.

Proxy

The framework attempts to resolve a proxy using the following strategy:

  1. Check for a named service: Look in the service registry for a service of type java.net.Proxy with the name open-ai.moderation-model.
  2. Fallback to another named service: If the first service is not found, search for a java.net.Proxy service with the name open-ai.
  3. No proxy: If neither service is found, the framework will not use a proxy.

OpenAiStreamingChatModel

To automatically create and add OpenAiStreamingChatModel to the service registry add the following lines to application.yaml:

yaml
langchain4j:
  providers:
    open-ai:
      api-key: "demo"

  models:
    openai-streaming-chat-model:
      provider: open-ai
      model-name: "gpt-4o-mini"

If enabled is set to false, the configuration is ignored, and the component is not created.

Full list of configuration properties:

KeyTypeDescription
api-keystringRequired. The API key used to authenticate requests to the OpenAI API.
base-urlstringThe base URL for the OpenAI API. If not present, the default value supplied from LangChain4j is used.
custom-headersMap<string, string>A map containing custom headers.
enabledbooleanIf set to false, OpenAI model will not be available even if configured.
frequency-penaltydoubleThe frequency penalty, between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model’s likelihood to repeat the same line.
log-requestsbooleanWhether to log API requests.
log-responsesbooleanWhether to log API responses.
logit-biasMap<string, int>LogitBias adjusts the likelihood of specific tokens appearing in a model’s response. A map of token IDs to bias values (-100 to 100). Positive values increase the chance of the token, while negative values reduce it, allowing fine control over token preferences in the output.
max-completion-tokensintThe maximum number of tokens allowed for the model’s response.
max-tokensintThe maximum number of tokens to generate in the completion.
model-namestringThe model name to use (e.g., "gpt-3.5-turbo").
organization-idstringThe ID of the organization for API requests.
parallel-tool-callsbooleanWhether to allow parallel calls to tools.
presence-penaltydoubleThe presence penalty, between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, encouraging the model to use new words.
response-formatstringThe format in which the model should return the response.
seedintThe seed for the random number generator used by the model.
stopstring[]The list of sequences where the API will stop generating further tokens.
strict-toolsbooleanWhether to enforce strict validation of tools used by the model.
temperaturedoubleThe sampling temperature to use, between 0 and 2. Higher values make the output more random, while lower values make it more focused and deterministic.
timeoutdurationThe timeout setting for API requests. See here for the format.
top-pdoubleThe nucleus sampling value, where the model considers the results of the tokens with top_p probability mass.
userstringThe user ID associated with the API requests.

Proxy

The framework attempts to resolve a proxy using the following strategy:

  1. Check for a named service: Look in the service registry for a service of type java.net.Proxy with the name open-ai.streaming-chat-model.
  2. Fallback to another named service: If the first service is not found, search for a java.net.Proxy service with the name open-ai.
  3. No proxy: If neither service is found, the framework will not use a proxy.

Tokenizer

The framework attempts to resolve a tokenizer using the following strategy:

  1. Check for a named service: Look in the service registry for a service of type dev.langchain4j.model.Tokenizer with the name open-ai.streaming-chat-model.
  2. Fallback to another named service: If the first service is not found, search for a dev.langchain4j.model.Tokenizer service with the name open-ai.
  3. Default Tokenizer: If neither service is found, the framework will use the default tokenizer provided by LangChain4j.

Additional Information