Compatible endpoint examples
Use these examples with a server that implements one of the adapter’scompatible protocols.
Connection settings
Install Core and set the API root, exact model ID, and API key for your server. Keep any required version or proxy prefix in the root. The adapter appends /chat/completions,/responses, or /messages for the selected mode.
export COMPATIBLE_BASE_URL="http://localhost:8000/v1"
export COMPATIBLE_MODEL="your-server-model"
export COMPATIBLE_API_KEY="your-api-key"Set these variables in the terminal where you run Python. For a local server with authentication disabled, use a nonempty placeholder. Set real credentials in COMPATIBLE_API_KEY and use HTTPS for remote servers.
Chat Completions
Chat Completions is the default for openai_compatible. The adapter sends the key as a bearer credential and maps max_tokens to max_completion_tokens.
import os
from llm_api_adapter.models.messages.chat_message import UserMessage
from llm_api_adapter.universal_adapter import UniversalLLMAPIAdapter
adapter = UniversalLLMAPIAdapter(
organization="openai_compatible",
base_url=os.environ["COMPATIBLE_BASE_URL"],
model=os.environ["COMPATIBLE_MODEL"],
api_key=os.environ["COMPATIBLE_API_KEY"],
)
response = adapter.chat(
messages=[UserMessage("Reply with OK.")],
max_tokens=128,
)
print(response.content)Responses
For a server that implements Responses, set api_variant="responses". The adapter sendsmax_output_tokens and sets "store": false in the request. Include the full conversation history in each request.
import os
from llm_api_adapter.models.messages.chat_message import UserMessage
from llm_api_adapter.universal_adapter import UniversalLLMAPIAdapter
adapter = UniversalLLMAPIAdapter(
organization="openai_compatible",
api_variant="responses",
base_url=os.environ["COMPATIBLE_BASE_URL"],
model=os.environ["COMPATIBLE_MODEL"],
api_key=os.environ["COMPATIBLE_API_KEY"],
)
response = adapter.chat(
messages=[UserMessage("Reply with OK.")],
max_tokens=128,
)
print(response.content)Messages
Use anthropic_compatible with the default api_variant and include a positivemax_tokens on every request. This mode sends x-api-key andanthropic-version: 2023-06-01.
import os
from llm_api_adapter.models.messages.chat_message import UserMessage
from llm_api_adapter.universal_adapter import UniversalLLMAPIAdapter
adapter = UniversalLLMAPIAdapter(
organization="anthropic_compatible",
base_url=os.environ["COMPATIBLE_BASE_URL"],
model=os.environ["COMPATIBLE_MODEL"],
api_key=os.environ["COMPATIBLE_API_KEY"],
)
response = adapter.chat(
messages=[UserMessage("Reply with OK.")],
max_tokens=128,
)
print(response.content)Async connections
Install the async extra to use achat() and astream_chat() with HTTPX.
python -m pip install "llm-api-adapter[async]==0.11.0"The example uses Responses. For Chat Completions orMessages, use the adapter configuration from that example with the same async methods. Use aclosing() to close a stream if iteration ends early.
import asyncio
import os
from contextlib import aclosing
from llm_api_adapter.models.messages.chat_message import UserMessage
from llm_api_adapter.universal_adapter import UniversalLLMAPIAdapter
adapter = UniversalLLMAPIAdapter(
organization="openai_compatible",
api_variant="responses",
base_url=os.environ["COMPATIBLE_BASE_URL"],
model=os.environ["COMPATIBLE_MODEL"],
api_key=os.environ["COMPATIBLE_API_KEY"],
)
async def main():
messages = [UserMessage("Reply with OK.")]
response = await adapter.achat(messages=messages, max_tokens=128)
print(response.content)
async with aclosing(adapter.astream_chat(messages=messages, max_tokens=128)) as stream:
async for text in stream:
print(text, end="", flush=True)
print()
if __name__ == "__main__":
asyncio.run(main())