Skip to main content

Python OpenAI API

Integrations

pip install openai

from openai import OpenAI
client = OpenAI(api_key="YOUR_KEY",
base_url="https://inference.samaira.ai/openai")

Chat Completations (Streaming)

from openai import OpenAI
client = OpenAI(api_key="YOUR_KEY",
base_url="https://inference.samaira.ai/openai")

stream = client.chat.completions.create(
model='qwq-32b',
messages=[{'role': 'user', 'content': 'Why is the sky blue?'}],
stream=True,
)

for chunk in stream:
print(chunk.choices[0].delta.content, end="")

Generate (Non Streaming)

from openai import OpenAI
client = OpenAI(api_key="YOUR_KEY",
base_url="https://inference.samaira.ai/openai")

stream = client.completions.create(
model='qwq-32b',
prompt='Why is the sky blue?',
stream=True,
)

for chunk in stream:
print(chunk.choices[0].text, end="")

Generate Image (Non Streaming)

response = client.images.generate(
model="flux1-schnell",
prompt="A futuristic cityscape with flying cars and neon lights",
n=1, # Number of images to generate
size="1024x1024" # Image size (can also be 256x256 or 512x512)
)

print(response.data[0].b64_json)
Tools Call Support

We are still testing Tools calling in supported models, we do support function call but its on beta and not recommeded at the moment.