Using a Project

Use the project's code examples to call the OCI OpenAI-compatible API or create a sandbox.

  1. In the Projects list page, select the project that you want to use. If you need help finding the list page, see Listing Projects.
  2. Select the How to use tab.
    For model inference, choose one of the two authentication options. Replace the placeholders in the examples with your project's OCID and credentials, and use an endpoint and model available in your region. Sandbox API calls use OCI IAM authentication separately.
  3. If you're going to access the models using Generative AI API Key auth, then copy the code for option one.

    Example code:

    from openai import OpenAI
    
    client = OpenAI(
        base_url="https://inference.generativeai.us-chicago-1.oci.oraclecloud.com/openai/v1",
        api_key="<your-api-key>", # replace with your Generative AI API Key
        project="<project-ocid>",
    )
    
    response = client.responses.create(
        model="openai.gpt-oss-120b",
        input="What is 2x2?"
    )
    
    print(response.output_text) # should output a string like "2 x 2 = **4**."
    
  4. If you're going to access the models using IAM auth, then copy the code for option two.

    Example code:

    from openai import OpenAI
    from oci_genai_auth import OciSessionAuth
    import httpx
    import os
    
    profile = os.environ.get("OCI_PROFILE", "DEFAULT")
    
    client = OpenAI(
        base_url="https://inference.generativeai.us-chicago-1.oci.oraclecloud.com/openai/v1",
        api_key="not-used",
        project="<project-ocid>",
        http_client=httpx.Client(auth=OciSessionAuth(profile_name=profile)),
    )
    
    response = client.responses.create(
        model="openai.gpt-oss-120b",
        input="What is 2x2?"
    )
    
    print(response.output_text)
    
  5. (Optional) To create a sandbox, copy the command under Create Sandbox from CLI or SDK.

    Enable sandboxes for the project and wait for the project to become Active before running the command. Set profile_name, region, and project_id to your values:

    oci --profile "$profile_name" --auth security_token --region "$region" \
      generative-ai-sandbox sandbox create \
      --project-id "$project_id" \
      --display-name my-sandbox \
      --runtime python-3.11 \
      --shape SMALL \
      --expiration-duration PT5M

    This example requests a five-minute sandbox. See Creating and Using a Sandbox for prerequisites and the complete workflow.