Using a Project

Using projects is required to call the OCI OpenAI-compatible API.

  1. In the Projects list page, select project that you want to use. If you need help finding the list page, see Listing a project.
  2. Select the How to use tab.
  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="sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", # replace with your Generative AI API Key
        project="ocid1.generativeaiproject.oc1.us-chicago-1.xxx"
    
    response = client.responses.create(
        model="xai.grok-4-1-fast-reasoning",
        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_openai import OciSessionAuth
    import httpx
    
    client = OpenAI(
        base_url="https://inference.generativeai.us-chicago-1.oci.oraclecloud.com/openai/v1",
        api_key="not-used",
        project="ocid1.generativeaiproject.oc1.us-chicago-1.xxx",
        http_client=httpx.Client(auth=OciSessionAuth(profile_name="DEFAULT")), # change "DEFAULT" to your profile name
    )
    
    response = client.responses.create(
        model="xai.grok-4-1-fast-reasoning",
        input="What is 2x2?"
    )
    
    print(response.output_text)