Skip to main content

OpenAI-Compatible Chat Completions

Build conversational AI applications using Eden AI’s OpenAI-compatible chat completions endpoint.

Overview

Eden AI V3 provides full OpenAI API compatibility with multi-provider support. The endpoint follows OpenAI’s exact format, making it a drop-in replacement. Endpoint:
POST /v3/llm/chat/completions
Important: V3 uses mandatory streaming via Server-Sent Events (SSE). All responses are streamed.

Model Format

Use the simplified model string format for LLM:
provider/model
Examples:
  • openai/gpt-4
  • anthropic/claude-3-5-sonnet-20241022
  • google/gemini-pro
  • cohere/command-r-plus

Basic Chat Completion

import requests

url = "https://api.edenai.run/v3/llm/chat/completions"
headers = {
    "Authorization": "Bearer YOUR_API_KEY",
    "Content-Type": "application/json"
}

payload = {
    "model": "openai/gpt-4",
    "messages": [
        {"role": "user", "content": "Hello! How are you?"}
    ],
    "stream": True
}

# Streaming response
response = requests.post(url, headers=headers, json=payload, stream=True)

for line in response.iter_lines():
    if line:
        line_str = line.decode('utf-8')
        if line_str.startswith('data: '):
            data = line_str[6:]  # Remove 'data: ' prefix
            if data != '[DONE]':
                print(data)

Multi-Turn Conversations

Build conversations with message history:
import requests
payload = {
    "model": "anthropic/claude-3-5-sonnet-20241022",
    "messages": [
        {"role": "user", "content": "What is the capital of France?"},
        {"role": "assistant", "content": "The capital of France is Paris."},
        {"role": "user", "content": "What's the population?"}
    ],
    "stream": True
}

response = requests.post(url, headers=headers, json=payload, stream=True)

for line in response.iter_lines():
    if line:
        line_str = line.decode('utf-8')
        if line_str.startswith('data: '):
            data = line_str[6:]
            if data != '[DONE]':
                print(data)

System Messages

Guide the model’s behavior with system messages:
import requests
payload = {
    "model": "openai/gpt-4",
    "messages": [
        {
            "role": "system",
            "content": "You are a helpful assistant that speaks like a pirate."
        },
        {
            "role": "user",
            "content": "Tell me about artificial intelligence."
        }
    ],
    "stream": True
}

response = requests.post(url, headers=headers, json=payload, stream=True)

Temperature and Parameters

Control response creativity and behavior:
import requests
payload = {
    "model": "openai/gpt-4",
    "messages": [
        {"role": "user", "content": "Write a creative story about a robot."}
    ],
    "temperature": 0.9,  # Higher = more creative (0-2)
    "max_tokens": 500,   # Limit response length
    "top_p": 1.0,        # Nucleus sampling
    "stream": True
}

response = requests.post(url, headers=headers, json=payload, stream=True)

Available Parameters

ParameterTypeDefaultDescription
modelstringRequiredModel string (e.g., openai/gpt-4)
messagesarrayRequiredConversation messages
streambooleantrueEnable streaming (mandatory in V3)
temperaturefloat1.0Randomness (0-2)
max_tokensinteger-Maximum response tokens
top_pfloat1.0Nucleus sampling threshold
frequency_penaltyfloat0.0Penalize repeated tokens (-2 to 2)
presence_penaltyfloat0.0Penalize topic repetition (-2 to 2)

Response Format (Streaming)

Server-Sent Events format:
data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1677652288,"model":"gpt-4","choices":[{"index":0,"delta":{"role":"assistant"},"finish_reason":null}]}

data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1677652288,"model":"gpt-4","choices":[{"index":0,"delta":{"content":"Hello"},"finish_reason":null}]}

data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1677652288,"model":"gpt-4","choices":[{"index":0,"delta":{"content":"!"},"finish_reason":null}]}

data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1677652288,"model":"gpt-4","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}

data: [DONE]

Available Models

OpenAI

  • openai/gpt-4
  • openai/gpt-4-turbo
  • openai/gpt-3.5-turbo

Anthropic

  • anthropic/claude-3-5-sonnet-20241022
  • anthropic/claude-3-opus-20240229
  • anthropic/claude-3-sonnet-20240229

Google

  • google/gemini-pro
  • google/gemini-1.5-pro

Cohere

  • cohere/command-r-plus
  • cohere/command-r

Meta

  • meta/llama-3-70b
  • meta/llama-3-8b

OpenAI Python SDK Integration

Use Eden AI with the OpenAI SDK:
from openai import OpenAI

# Point to Eden AI endpoint
client = OpenAI(
    api_key="YOUR_EDEN_AI_API_KEY",
    base_url="https://api.edenai.run/v3/llm"
)

# Use any provider through OpenAI SDK
stream = client.chat.completions.create(
    model="anthropic/claude-3-5-sonnet-20241022",
    messages=[{"role": "user", "content": "Hello!"}],
    stream=True
)

for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end='')

Next Steps