OpenAI-compatible provider base for Metorial
Project description
metorial-openai-compatible
OpenAI-compatible provider integration for Metorial. Works with any API that follows the OpenAI chat completions format.
Installation
pip install metorial openai
Quick Start
import asyncio
from metorial import Metorial, MetorialOpenAICompatible
from openai import AsyncOpenAI
metorial = Metorial(api_key="your-metorial-api-key")
# Works with any OpenAI-compatible API
client = AsyncOpenAI(
api_key="your-api-key",
base_url="https://your-api-endpoint.com/v1"
)
async def main():
async def session_handler(session):
messages = [{"role": "user", "content": "What's the latest news?"}]
for _ in range(10):
response = await client.chat.completions.create(
model="your-model",
messages=messages,
tools=session["tools"]
)
choice = response.choices[0]
tool_calls = choice.message.tool_calls
if not tool_calls:
print(choice.message.content)
break
tool_responses = await session["callTools"](tool_calls)
messages.append({"role": "assistant", "tool_calls": tool_calls})
messages.extend(tool_responses)
await session["closeSession"]()
await metorial.with_provider_session(
MetorialOpenAICompatible.chat_completions,
{"serverDeployments": [{"serverDeploymentId": "your-server-deployment-id"}]},
session_handler
)
asyncio.run(main())
Streaming
import asyncio
from metorial import Metorial, MetorialOpenAICompatible
from openai import AsyncOpenAI
metorial = Metorial(api_key="your-metorial-api-key")
client = AsyncOpenAI(
api_key="your-api-key",
base_url="https://your-api-endpoint.com/v1"
)
async def main():
async def session_handler(session):
messages = [{"role": "user", "content": "What's the latest news?"}]
stream = await client.chat.completions.create(
model="your-model",
messages=messages,
tools=session["tools"],
stream=True
)
async for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
await session["closeSession"]()
await metorial.with_provider_session(
MetorialOpenAICompatible.chat_completions,
{
"serverDeployments": [{"serverDeploymentId": "your-server-deployment-id"}],
"streaming": True, # Required for streaming with tool calls
},
session_handler
)
asyncio.run(main())
Compatible Providers
Any provider with an OpenAI-compatible API:
- DeepSeek (
https://api.deepseek.com) - Together AI (
https://api.together.xyz/v1) - XAI (
https://api.x.ai/v1) - Groq (
https://api.groq.com/openai/v1) - And many more...
Session Object
async def session_handler(session):
tools = session["tools"] # Tool definitions in OpenAI format
call_tools = session["callTools"] # Execute tools and get responses
close_session = session["closeSession"] # Close the session when done
Error Handling
from metorial import MetorialAPIError
try:
await metorial.with_provider_session(...)
except MetorialAPIError as e:
print(f"API Error: {e.message} (Status: {e.status})")
except Exception as e:
print(f"Unexpected error: {e}")
License
MIT License - see LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file metorial_openai_compatible-1.0.5.tar.gz.
File metadata
- Download URL: metorial_openai_compatible-1.0.5.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c9301e7ebfde8213baf5c167eb96bbfc46dc1bf0c46e0d8e433572404a043fdf
|
|
| MD5 |
9c6af5ff8e1195d9548f26bcc1e8b4b8
|
|
| BLAKE2b-256 |
4ae3182f8ddf052438a29d2c45021b8c26f57471a4a3598296bfdd6bc1231338
|
Provenance
The following attestation bundles were made for metorial_openai_compatible-1.0.5.tar.gz:
Publisher:
release.yml on metorial/metorial-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
metorial_openai_compatible-1.0.5.tar.gz -
Subject digest:
c9301e7ebfde8213baf5c167eb96bbfc46dc1bf0c46e0d8e433572404a043fdf - Sigstore transparency entry: 776931349
- Sigstore integration time:
-
Permalink:
metorial/metorial-python@ed129f7a01110a9ea3729266af09ac6326785b5e -
Branch / Tag:
refs/heads/main - Owner: https://github.com/metorial
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@ed129f7a01110a9ea3729266af09ac6326785b5e -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file metorial_openai_compatible-1.0.5-py3-none-any.whl.
File metadata
- Download URL: metorial_openai_compatible-1.0.5-py3-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2e09deb1222685bb1107054c4d96db1c96260f7c368d3209e5cb4b2e08f71e7c
|
|
| MD5 |
19f6b0993de6fcbbd813742b1a19adfd
|
|
| BLAKE2b-256 |
4de6496023bc0affe30ce20e9d38401c00e5dab31ee6bbaa3b9d0757f6c33272
|
Provenance
The following attestation bundles were made for metorial_openai_compatible-1.0.5-py3-none-any.whl:
Publisher:
release.yml on metorial/metorial-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
metorial_openai_compatible-1.0.5-py3-none-any.whl -
Subject digest:
2e09deb1222685bb1107054c4d96db1c96260f7c368d3209e5cb4b2e08f71e7c - Sigstore transparency entry: 776931359
- Sigstore integration time:
-
Permalink:
metorial/metorial-python@ed129f7a01110a9ea3729266af09ac6326785b5e -
Branch / Tag:
refs/heads/main - Owner: https://github.com/metorial
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@ed129f7a01110a9ea3729266af09ac6326785b5e -
Trigger Event:
workflow_dispatch
-
Statement type: