OpenAI
Installation
You can setup the apiKey on the environment variables, like:
You can optionally set a custom base URL, like:
or
Using OpenAI Responses API
The OpenAI Responses API provides enhanced functionality for handling complex interactions, including built-in tools, annotations, and streaming responses. Here's how to use it:
Basic Setup
Message Content Types
The API supports different types of message content, including text and images:
Advanced Features
Built-in Tools
Response Tracking and Storage
Streaming Responses
Configuration Options
The OpenAI Responses API supports various configuration options:
Response Structure
The API returns responses with rich metadata and optional annotations:
Best Practices
- Use
trackPreviousResponses
when you need conversation continuity - Enable
strict
mode when using tools to ensure accurate function calls - Set appropriate
maxOutputTokens
to control response length - Use
annotations
to track citations and references in responses - Implement error handling for potential API failures and retries
Using JSON Response Format
You can configure OpenAI to return responses in JSON format:
Response Formats
The OpenAI LLM supports different response formats to structure the output in specific ways. There are two main approaches to formatting responses:
1. JSON Object Format
The simplest way to get structured JSON responses is using the json_object
response format:
2. Schema Validation with Zod
For more robust type safety and validation, you can use Zod schemas to define the expected response structure:
Response Format Options
The response format can be configured in two ways:
- At LLM initialization:
- Per request:
The response format options are:
{ type: "json_object" }
- Returns responses as JSON objectszodSchema
- A Zod schema that defines and validates the response structure
Best Practices
- Use JSON object format for simple structured responses
- Use Zod schemas when you need:
- Type safety
- Response validation
- Complex nested structures
- Specific field constraints
- Set a low temperature (e.g. 0) when using structured outputs for more reliable formatting
- Include clear instructions in system or user messages about the expected response format
- Handle potential parsing errors when working with JSON responses
Load and index documents
For this example, we will use a single document. In a real-world scenario, you would have multiple documents to index.