Working with API

LangDB allows you to use your favourite Agentic libraries using OpenAI Completions API. With the AI Gateway, you can use LangDB as a drop-in replacement for OpenAI APIs, making it easy to integrate into existing workflows and libraries such as OpenAI’s Python client.

Example: Using OpenAI Client

# Import OpenAI library
import openai
from openai import OpenAI
# Set up the LangDB API base URL and bearer token
api_base = "https://api.us-east-1.langdb.ai"  # LangDB API base URL
api_key = "xxxxx"  # Replace with your LangDB token
default_headers = {"x-project-id": "xxxxx"} # LangDB Project ID
# Replace with your Project
client = OpenAI(
    base_url=api_base,
    api_key=api_key,
)
# Define the conversation messages
messages = [
    {
        "role": "system",
        "content": "You are a financial assistant. Help the user with their financial queries regarding companies.", 
    },
    {
        "role": "user",
        "content": "What are the earnings of Apple in 2022?",
    },
]
# Make the API call to LangDB's Completions API
response = client.chat.completions.create(
    model="gpt-4o",  # Use the model
    messages=messages,  # Define the interaction
    temperature=0.7,  # Control the creativity of the response
    max_tokens=300,  # Limit the length of the response
    top_p=1.0,  # Use nucleus sampling
    extra_headers=default_headers
)
# Extract the assistant's reply
assistant_reply = response.choices[0].message

# Print the assistant's response
print("Assistant:", assistant_reply)

Example: cURL

curl "https://api.us-east-1.langdb.ai/chat/completions" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer $LANGDB_API_KEY" \
    -X "X-Project-Id: $Project_ID" \
    -d '{
        "model": "gpt-4o",
        "messages": [
            {
                "role": "system",
                "content": "You are a helpful assistant."
            },
            {
                "role": "user",
                "content": "Write a haiku about recursion in programming."
            }
        ],
        "temperature": 0.8
    }'

By using LangDB, you gain the benefits of cost optimization, dynamic routing, and centralized management while retaining the flexibility of OpenAI APIs. Whether you're building with Python, cURL, or other libraries, LangDB ensures a smooth and scalable experience.

Check out the API reference here.
Updated on