OpenAI API¶
Use the openai provider to call OpenAI with an API key. It supports chat, embeddings, and images. Chat requests default to Responses; Chat Completions is also available.
Get started¶
Install Republic using the quickstart, set REPUBLIC_OPENAI_API_KEY, and replace MODEL_ID with a model available to your account:
import asyncio
import republic
async def main():
model = republic.get_model("openai:MODEL_ID")
response = await model.chat("Say hello in one sentence.")
print(response.text)
asyncio.run(main())
Authentication¶
Republic reads REPUBLIC_OPENAI_API_KEY when the provider is created. You can pass a key loaded by your application as api_key=; see API-key authentication.
To use a ChatGPT account login, select the Codex provider.
Model support¶
To use Chat Completions, replace the model construction above with:
For an embedding model, use get_embedding_model("openai:MODEL_ID"), then await embed(text) or embed_many(texts) inside an async function. Choose a model that supports the selected operation.
For an image model, use get_image_model("openai:MODEL_ID"), then await generate(prompt) or edit(prompt, images, mask=None). They use the images format, which calls /images/generations and /images/edits. Edits send input images and the mask as image_url references, so local images and remote URLs both work. See image models.
See configuration for endpoint overrides and request options.