OpenAI Language Model

OpenAI model for use with AI Agent

Overview

This node integrates with an OpenAI language model to generate text completions or structured JSON responses based on user prompts. It is useful for scenarios where you want to leverage AI-generated content, such as drafting text, answering questions, or producing data that conforms to a specific JSON schema. For example, you could use it to generate creative writing, summarize information, or produce structured data outputs like configuration objects or form data validated against a schema.

Properties

Name Meaning
Model The OpenAI model used to generate the completion (e.g., "gpt-4-turbo").
Temperature Controls randomness of the output; ranges from 0 (deterministic) to 2 (more random).
Response Format Type Output format type: either plain text or a JSON object conforming to a specified JSON schema.
Schema Name The name assigned to the JSON schema when using the JSON Schema response format.
JSON Schema The actual JSON schema definition used to validate and structure the AI's JSON response.

Output

The node outputs a JSON object containing the AI-generated content in the json field. The content depends on the selected response format:

  • Text: A plain text string generated by the AI model.
  • JSON Schema: A JSON object structured according to the provided JSON schema, ensuring the output adheres to a predefined format.

If the JSON Schema option is selected, the node instructs the AI to respond strictly following the schema, enabling structured data extraction directly from the AI output.

Dependencies

  • Requires an API key credential for authenticating requests to the OpenAI API.
  • Makes HTTP POST requests to the OpenAI chat completions endpoint (https://api.openai.com/v1/chat/completions).
  • No additional external dependencies beyond the OpenAI API and n8n's HTTP request helper.

Troubleshooting

  • Common Issues:
    • Invalid or missing API key will cause authentication errors.
    • Incorrect JSON schema definitions may lead to unexpected or invalid AI responses.
    • Setting temperature too high might produce less coherent or inconsistent outputs.
  • Error Messages:
    • Authentication failures typically indicate issues with the API key; verify the key and permissions.
    • HTTP request failures may indicate network issues or API rate limits.
    • Schema validation errors suggest problems with the JSON schema syntax or structure; ensure the schema is valid JSON and correctly formatted.

Links and References

Discussion