1818COMPARTMENT_ID = ""
1919CONVERSATION_STORE_ID = ""
2020OPENAI_PROJECT = ""
21- OVERRIDE_URL = ""
2221PROFILE_NAME = "DEFAULT"
23- REGION = "us-chicago-1"
2422GEMINI_API_KEY = ""
25- GEMINI_BASE_URL = ""
23+
24+ # OpenAI-compatible base URLs.
25+ OPENAI_BASE_URL_PT = "https://inference.generativeai.us-chicago-1.oci.oraclecloud.com/v1"
26+ OPENAI_BASE_URL_NP = "https://inference.generativeai.us-chicago-1.oci.oraclecloud.com/openai/v1"
27+ # Switch to "NP" for examples that store data on the server.
28+ RESPONSE_API_MODE = "PT" # "PT" (pass-through) or "NP" (non-pass-through)
29+
30+ # Other provider base URLs.
31+ ANTHROPIC_BASE_URL = "https://inference.generativeai.us-chicago-1.oci.oraclecloud.com/anthropic"
32+ GOOGLE_BASE_URL = "https://inference.generativeai.us-chicago-1.oci.oraclecloud.com/google"
2633
2734
2835def _build_headers (include_conversation_store_id : bool = False ) -> dict [str , str ]:
29- headers : dict [str , str ] = {}
30- if COMPARTMENT_ID :
31- headers ["CompartmentId" ] = COMPARTMENT_ID
32- headers ["opc-compartment-id" ] = COMPARTMENT_ID
33- if OPENAI_PROJECT :
34- headers ["OpenAI-Project" ] = OPENAI_PROJECT
35- if include_conversation_store_id and CONVERSATION_STORE_ID :
36+ headers : dict [str , str ] = {
37+ "CompartmentId" : COMPARTMENT_ID ,
38+ "opc-compartment-id" : COMPARTMENT_ID ,
39+ "OpenAI-Project" : OPENAI_PROJECT ,
40+ }
41+ if include_conversation_store_id :
3642 headers ["opc-conversation-store-id" ] = CONVERSATION_STORE_ID
37- return headers
43+ return { key : value for key , value in headers . items () if value }
3844
3945
4046def _resolve_openai_base_url () -> str :
41- service_endpoint = OVERRIDE_URL or (
42- f"https://inference.generativeai.{ REGION } .oci.oraclecloud.com" if REGION else ""
43- )
44- if not service_endpoint :
45- raise ValueError ("REGION or OVERRIDE_URL must be set." )
46- return f"{ service_endpoint .rstrip (' /' )} /openai/v1"
47-
48-
49- def _resolve_anthropic_base_url () -> str :
50- if not REGION :
51- raise ValueError ("REGION or ANTHROPIC_BASE_URL must be set." )
52- return f"https://inference.generativeai.{ REGION } .oci.oraclecloud.com/anthropic"
53-
54-
55- def _resolve_google_base_url () -> str :
56- if not REGION :
57- raise ValueError ("REGION or GOOGLE_BASE_URL must be set." )
58- return f"https://inference.generativeai.{ REGION } .oci.oraclecloud.com/google"
47+ return OPENAI_BASE_URL_NP if RESPONSE_API_MODE == "NP" else OPENAI_BASE_URL_PT
5948
6049
6150def build_openai_client () -> "OpenAI" :
@@ -95,7 +84,7 @@ def build_anthropic_client() -> "Anthropic":
9584
9685 return Anthropic (
9786 api_key = "not-used" ,
98- base_url = _resolve_anthropic_base_url () ,
87+ base_url = ANTHROPIC_BASE_URL ,
9988 http_client = httpx .Client (
10089 auth = OciSessionAuth (profile_name = PROFILE_NAME ),
10190 headers = _build_headers (),
@@ -108,7 +97,7 @@ def build_anthropic_async_client() -> "AsyncAnthropic":
10897
10998 return AsyncAnthropic (
11099 api_key = "not-used" ,
111- base_url = _resolve_anthropic_base_url () ,
100+ base_url = ANTHROPIC_BASE_URL ,
112101 http_client = httpx .AsyncClient (
113102 auth = OciSessionAuth (profile_name = PROFILE_NAME ),
114103 headers = _build_headers (),
@@ -127,7 +116,7 @@ def build_google_client() -> "genai.Client":
127116 return genai .Client (
128117 api_key = "not-used" ,
129118 http_options = {
130- "base_url" : _resolve_google_base_url () ,
119+ "base_url" : GOOGLE_BASE_URL ,
131120 "headers" : headers ,
132121 "httpx_client" : http_client ,
133122 },
@@ -145,7 +134,7 @@ def build_google_async_client() -> tuple["genai.Client", httpx.AsyncClient]:
145134 client = genai .Client (
146135 api_key = "not-used" ,
147136 http_options = {
148- "base_url" : _resolve_google_base_url () ,
137+ "base_url" : GOOGLE_BASE_URL ,
149138 "headers" : headers ,
150139 "httpx_async_client" : http_client ,
151140 },
0 commit comments