Oracle Cloud Generative AI Service API #17798
-
Hello, what do I need for calling the Oracle Generative AI Service from a python notebook? Thanks, Federico |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
To call the Oracle Generative AI Service API from a Python notebook, you need to set up an OCI Gen AI client using the import oci
from oci.generative_ai_inference import GenerativeAiInferenceClient
def create_client(auth_type, auth_profile, auth_file_location, service_endpoint):
client_kwargs = {
"config": {},
"signer": None,
"service_endpoint": service_endpoint,
"retry_strategy": oci.retry.DEFAULT_RETRY_STRATEGY,
"timeout": (10, 240), # default timeout config for OCI Gen AI service
}
if auth_type == "API_KEY":
client_kwargs["config"] = oci.config.from_file(
file_location=auth_file_location, profile_name=auth_profile
)
client_kwargs.pop("signer", None)
elif auth_type == "SECURITY_TOKEN":
def make_security_token_signer(oci_config):
pk = oci.signer.load_private_key_from_file(
oci_config.get("key_file"), None
)
with open(oci_config.get("security_token_file"), encoding="utf-8") as f:
st_string = f.read()
return oci.auth.signers.SecurityTokenSigner(st_string, pk)
client_kwargs["config"] = oci.config.from_file(
file_location=auth_file_location, profile_name=auth_profile
)
client_kwargs["signer"] = make_security_token_signer(
oci_config=client_kwargs["config"]
)
elif auth_type == "INSTANCE_PRINCIPAL":
client_kwargs["signer"] = oci.auth.signers.InstancePrincipalsSecurityTokenSigner()
elif auth_type == "RESOURCE_PRINCIPAL":
client_kwargs["signer"] = oci.auth.signers.get_resource_principals_signer()
else:
raise ValueError(f"Please provide valid value to auth_type, {auth_type} is not valid.")
return GenerativeAiInferenceClient(**client_kwargs)
# Example usage
auth_type = "API_KEY" # or "SECURITY_TOKEN", "INSTANCE_PRINCIPAL", "RESOURCE_PRINCIPAL"
auth_profile = "DEFAULT"
auth_file_location = "~/.oci/config"
service_endpoint = "https://inference.generativeai.us-chicago-1.oci.oraclecloud.com"
client = create_client(auth_type, auth_profile, auth_file_location, service_endpoint) Ensure you have the !pip install -U oci If you are using LlamaIndex with OCI Generative AI, you can also initialize the To continue talking to Dosu, mention @dosu. Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Other | Bug Report |
Beta Was this translation helpful? Give feedback.
To call the Oracle Generative AI Service API via LlamaIndex, you can use the
OCIGenAI
class from thellama_index.llms.oci_genai
module. Here's how you can do it:Install Required Packages:
Make sure you have the necessary packages installed:
Initialize the OCIGenAI Interface:
Set up the
OCIGenAI
interface with your OCI endpoint, model ID, OCID, and authentication method.Call the API:
Use the
complete
method to send a pro…