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 Client SDK.

from openai import OpenAI

langdb_project_id = "xxxxx"  # LangDB Project ID

client = OpenAI(
    base_url=f"https://api.us-east-1.langdb.ai/{langdb_project_id}/v1",
    api_key="xxxxx" ,           # LangDB token
)
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "system", "content": "You are a helpful assistant"},
        {"role": "user", "content": "What are the earnings of Apple in 2022?"},
    ],
)
print("Assistant:", response.choices[0].message)

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