OpenAI Compatible icon

OpenAI Compatible

Interact with OpenAI-compatible models

Text Completion

AI-generated

Overview

This node enables interaction with OpenAI-compatible language models to generate text completions. It supports two main operations: Chat Completion and Text Completion. Users can provide prompts or chat messages, configure model parameters, and receive generated text outputs.

Common scenarios include:

  • Generating conversational responses in chatbot applications.
  • Creating content based on a given prompt, such as article snippets, summaries, or code generation.
  • Experimenting with different model parameters to control creativity, length, and style of the output.

For example, you might use this node to generate a customer support reply by providing chat messages or to create a product description by supplying a text prompt.

Properties

Name Meaning
Operation Choose between "Chat Completion" (generate chat-based responses) or "Text Completion" (generate text from a prompt).
Model The specific OpenAI-compatible model to use for generating completions (e.g., gpt-3.5-turbo).
Prompt The input text prompt for generating a text completion (used only when Operation is "Text Completion").
Messages A collection of messages with roles (system, user, assistant) and content for chat completion (used only when Operation is "Chat Completion").
Additional Fields Optional parameters to customize the completion behavior:
- Temperature Controls randomness; lower values produce more deterministic results (range 0 to 2).
- Max Tokens Maximum number of tokens to generate in the completion (minimum 1).
- Top P Controls diversity via nucleus sampling (range 0 to 1).
- Frequency Penalty Penalizes new tokens based on their existing frequency in the text (range -2 to 2).
- Presence Penalty Penalizes new tokens based on whether they appear in the text (range -2 to 2).
- Stop Up to 4 sequences where the API will stop generating further tokens (string).

Output

The node outputs an array with one item per input. Each output item contains a json field holding the response from the OpenAI-compatible API. This JSON includes the generated completions according to the selected operation:

  • For Chat Completion, the output JSON contains the chat completion response including generated messages.
  • For Text Completion, the output JSON contains the generated text completion.

No binary data output is produced by this node.

Dependencies

  • Requires an OpenAI-compatible API key credential configured in n8n.
  • The API base URL and API key must be provided via credentials.
  • The node makes HTTP POST requests to the API endpoints /v1/chat/completions or /v1/completions depending on the operation.

Troubleshooting

  • Missing Base URL or API Key: The node throws an error if either the base URL or API key is not set in credentials. Ensure these are correctly configured.
  • Empty Prompt or Messages: For text completion, a prompt must be provided; for chat completion, at least one message is required. Errors will be thrown if these inputs are missing.
  • API Request Failures: Network issues or invalid API keys may cause request failures. Check your API credentials and network connectivity.
  • Parameter Validation: Ensure numeric parameters like temperature, max tokens, top_p, frequency penalty, and presence penalty are within allowed ranges to avoid API errors.

If the node is set to continue on failure, errors will be returned in the output JSON under an error property instead of stopping execution.

Links and References

Discussion