Call with the OpenAI SDK

This page is for applications that already use the OpenAI Python SDK. After completing it, the application receives a text response through Genesis.

1. Configure the service address and credential

Create a personal access token in Access credentials, or create a service-account API key with Genesis access, then set the environment variables:

export GENESIS_BASE_URL='https://token.moi.matrixorigin.cn/v1'
export GENESIS_ACCESS_TOKEN='<your-access-credential>'

2. Get a model ID

Open a model in the Genesis model marketplace and copy its model ID. You can also call List models to list the models available to the current credential.

Save the model ID as an environment variable:

export GENESIS_MODEL_ID='<model-id>'

3. Call example

This example sends one message through Chat Completions:

import os

from openai import OpenAI

client = OpenAI(
    base_url=os.environ["GENESIS_BASE_URL"],
    api_key=os.environ["GENESIS_ACCESS_TOKEN"],
)

response = client.chat.completions.create(
    model=os.environ["GENESIS_MODEL_ID"],
    messages=[
        {"role": "user", "content": "Explain data lineage in one sentence."},
    ],
)

print(response.choices[0].message.content)

On success, the program prints the text generated by the model.

Next steps

  • See Chat Completions for complete request parameters and response fields.

  • To use separate input and output objects, use Responses.

Last updated on