OpenAI, Ollama, and Faker services
Call OpenAI or local Ollama models asynchronously and generate deterministic fake data for development.
Work through each step in order. The examples use the generated LenAPI project structure and are designed to be adapted to your app.
Install optional integration dependencies
Install the AI extra for the HTTP client. OpenAI reads its key from OPENAI_API_KEY by default. Ollama must be running at its base URL, and the requested model must already be pulled there.
python -m pip install 'lenapi[ai]'
export OPENAI_API_KEY="your-openai-api-key"
ollama pull qwen3:0.6bCall OpenAI asynchronously
Use the Responses API through OpenAI.responses(). The client uses OPENAI_API_KEY from the environment unless you pass api_key directly. It returns the provider's JSON response; output_text contains the generated text when available.
import os
from lenapi import OpenAI
async def ask_openai():
async with OpenAI(
model="gpt-4.1-mini",
api_key=os.environ["OPENAI_API_KEY"],
) as openai:
response = await openai.responses(
"Explain what an API is in one sentence."
)
return response.get("output_text", response)You can omit api_key=... to let OpenAI read OPENAI_API_KEY itself. Keep the key in your environment or secret manager, never in source code or a public repository.
Call Ollama asynchronously
The Ollama wrapper defaults to the local server at http://127.0.0.1:11434. Set base_url to the URL of your own Ollama server when it is hosted elsewhere. The call is non-streaming and returns Ollama's JSON response.
from lenapi import Ollama
async def ask_ollama():
async with Ollama(
model="qwen3:0.6b",
base_url="http://127.0.0.1:11434",
) as ollama:
response = await ollama.chat([
{
"role": "user",
"content": "Explain what an API is in one sentence.",
}
])
return response["message"]["content"]Use the Ollama host URL only, such as http://127.0.0.1:11434. Do not add /api/chat; the LenAPI client appends that endpoint itself.
Generate repeatable test data
The Faker service delegates provider methods such as name, email, phone_number, and password. A seed makes an instance's generation sequence repeatable.
from lenapi.service import Faker
fake = Faker("en_NG", seed=42)
name = fake.name()
email = fake.email()
phone = fake.phone_number()
password = fake.password()Faker is an optional integration. Install it with python -m pip install 'lenapi[faker]'.