Skip to main content

OpenAI Provider

Use OpenAI’s GPT models including GPT-4o, GPT-4, and GPT-3.5 with PraisonAI TypeScript.

Environment Variables

export OPENAI_API_KEY=sk-...
VariableRequiredDescription
OPENAI_API_KEYYesYour OpenAI API key
OPENAI_ORG_IDNoOrganization ID (optional)
OPENAI_BASE_URLNoCustom base URL (for proxies)

Supported Modalities

ModalitySupported
Text/Chat
Embeddings
Image
Audio
Speech
Tools

Quick Start

import { Agent } from 'praisonai';

const agent = new Agent({
  name: 'Assistant',
  instructions: 'You are a helpful assistant.',
  llm: 'openai/gpt-4o-mini'
});

const response = await agent.chat('Hello!');
console.log(response);

Available Models

ModelDescriptionContext
gpt-4oLatest multimodal model128K
gpt-4o-miniFast, affordable128K
gpt-4-turboHigh capability128K
gpt-4Original GPT-48K
gpt-3.5-turboFast, cost-effective16K
o1Reasoning model128K
o1-miniFast reasoning128K

Advanced Configuration

import { Agent } from 'praisonai';

const agent = new Agent({
  name: 'CustomAgent',
  instructions: 'You are an expert analyst.',
  llm: 'openai/gpt-4o',
  llmConfig: {
    temperature: 0.7,
    maxTokens: 4096,
    topP: 0.9,
  }
});

With Tools

import { Agent } from 'praisonai';

const searchTool = {
  name: 'search',
  description: 'Search the web',
  parameters: {
    type: 'object',
    properties: {
      query: { type: 'string', description: 'Search query' }
    },
    required: ['query']
  },
  execute: async ({ query }) => {
    return `Results for: ${query}`;
  }
};

const agent = new Agent({
  name: 'SearchAgent',
  instructions: 'Search for information when asked.',
  llm: 'openai/gpt-4o-mini',
  tools: [searchTool]
});

Streaming

import { Agent } from 'praisonai';

const agent = new Agent({
  name: 'StreamAgent',
  instructions: 'You are helpful.',
  llm: 'openai/gpt-4o-mini'
});

for await (const chunk of agent.stream('Tell me a story')) {
  process.stdout.write(chunk);
}

Multi-Agent Workflow

import { Agent, Agents } from 'praisonai';

const researcher = new Agent({
  name: 'Researcher',
  instructions: 'Research topics thoroughly.',
  llm: 'openai/gpt-4o'
});

const writer = new Agent({
  name: 'Writer',
  instructions: 'Write clear summaries.',
  llm: 'openai/gpt-4o-mini'
});

const agents = new Agents({
  agents: [researcher, writer],
  tasks: [
    { agent: researcher, description: 'Research {topic}' },
    { agent: writer, description: 'Summarize the research' }
  ]
});

const result = await agents.start({ topic: 'AI trends' });

Troubleshooting

Invalid API Key

Error: Authentication failed
Solution: Verify your OPENAI_API_KEY is set correctly and has not expired.

Rate Limits

Error: Rate limit exceeded
Solution: Implement retry logic or upgrade your OpenAI plan.

Model Not Found

Error: Model not found
Solution: Check the model name is correct. Use gpt-4o-mini instead of gpt-4-mini.