OpenAI and OpenAI-Compatible APIs
OpenAIBackend connects Mellea to the OpenAI API and to any server that implements
the OpenAI HTTP API — including LM Studio, Ollama's OpenAI endpoint, vLLM, and
OpenAI-compatible providers.
Prerequisites: pip install mellea, a valid API key for the OpenAI API or a
local OpenAI-compatible server running.
OpenAI API
Set your API key as an environment variable (recommended):
export OPENAI_API_KEY=sk-...
Then create a session:
# Requires: mellea
# Returns: ModelOutputThunk
from mellea import MelleaSession
from mellea.backends.openai import OpenAIBackend
from mellea.stdlib.context import ChatContext
m = MelleaSession(
OpenAIBackend(model_id="gpt-4o"),
ctx=ChatContext(),
)
reply = m.chat("What is the capital of France?")
print(str(reply))
# Output will vary — LLM responses depend on model and temperature.
Pass the key directly if you prefer not to use an environment variable:
# Requires: mellea
# Returns: MelleaSession
from mellea import MelleaSession
from mellea.backends.openai import OpenAIBackend
m = MelleaSession(
OpenAIBackend(model_id="gpt-4o", api_key="sk-..."),
)
Note: Never commit API keys to source control. Use environment variables or a secrets manager in production.
OpenAI-compatible local servers
OpenAIBackend works with any server that implements the OpenAI HTTP API. No real
API key is needed for local servers — pass any non-empty string:
LM Studio
# Requires: mellea
# Returns: MelleaSession
from mellea import MelleaSession
from mellea.backends.openai import OpenAIBackend
m = MelleaSession(
OpenAIBackend(
model_id="qwen/qwen2.5-vl-7b",
base_url="http://127.0.0.1:1234/v1",
)
)