Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[oci_generative_ai] Option to pass auth_file_location #29481

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/docs/integrations/llms/oci_generative_ai.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
" compartment_id=\"MY_OCID\",\n",
" auth_type=\"SECURITY_TOKEN\",\n",
" auth_profile=\"MY_PROFILE\", # replace with your profile name\n",
" auth_file_location=\"MY_CONFIG_FILE_LOCATION\", # replace with file location where profile name configs present\n",
")"
]
},
Expand All @@ -159,6 +160,7 @@
" service_endpoint=\"https://inference.generativeai.us-chicago-1.oci.oraclecloud.com\",\n",
" compartment_id=\"DEDICATED_COMPARTMENT_OCID\",\n",
" auth_profile=\"MY_PROFILE\", # replace with your profile name,\n",
" auth_file_location=\"MY_CONFIG_FILE_LOCATION\", # replace with file location where profile name configs present\n",
" provider=\"MODEL_PROVIDER\", # e.g., \"cohere\" or \"meta\"\n",
" context_size=\"MODEL_CONTEXT_SIZE\", # e.g., 128000\n",
")"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
" compartment_id=\"MY_OCID\",\n",
" auth_type=\"SECURITY_TOKEN\",\n",
" auth_profile=\"MY_PROFILE\", # replace with your profile name\n",
" auth_file_location=\"MY_CONFIG_FILE_LOCATION\", # replace with file location where profile name configs present\n",
")\n",
"\n",
"\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,8 @@ class ChatOCIGenAI(BaseChatModel, OCIGenAIBase):
The authentication type to use, e.g., API_KEY (default), SECURITY_TOKEN, INSTANCE_PRINCIPAL, RESOURCE_PRINCIPAL.
auth_profile: Optional[str]
The name of the profile in ~/.oci/config, if not specified , DEFAULT will be used.
auth_file_location: Optional[str]
Path to the config file, If not specified, ~/.oci/config will be used.
provider: str
Provider name of the model. Default to None, will try to be derived from the model_id otherwise, requires user input.
See full list of supported init args and their descriptions in the params section.
Expand Down
24 changes: 17 additions & 7 deletions libs/community/langchain_community/embeddings/oci_generative_ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class OCIGenAIEmbeddings(BaseModel, Embeddings):
Make sure you have the required policies (profile/roles) to
access the OCI Generative AI service. If a specific config profile is used,
you must pass the name of the profile (~/.oci/config) through auth_profile.
If a specific config file location is used, you must pass
the file location where profile name configs present
through auth_file_location

To use, you must provide the compartment id
along with the endpoint url, and model id
Expand Down Expand Up @@ -66,6 +69,11 @@ class OCIGenAIEmbeddings(BaseModel, Embeddings):
If not specified , DEFAULT will be used
"""

auth_file_location: Optional[str] = "~/.oci/config"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is Optional[str], what is the difference between this default and setting a default of None?

Copy link
Author

@ashutoshSce ashutoshSce Feb 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ccurme for response.

This is already there in this file
auth_profile: Optional[str] = "DEFAULT"

We have added this new one, so that we can give option to overwrite default config file location as well, same as profile name.
auth_file_location: Optional[str] = "~/.oci/config"

Later on, in this same file we have used auth_profile and auth_file_location to load configs. We can't pass their values as None, because of the definition of function. It will throw exception.

def from_file(file_location=DEFAULT_LOCATION, profile_name=DEFAULT_PROFILE):

https://docs.oracle.com/en-us/iaas/tools/python/2.143.1/configuration.html

"""Path to the config file.
If not specified, ~/.oci/config will be used
"""

model_id: Optional[str] = None
"""Id of the model to call, e.g., cohere.embed-english-light-v2.0"""

Expand Down Expand Up @@ -108,7 +116,8 @@ def validate_environment(cls, values: Dict) -> Dict: # pylint: disable=no-self-

if values["auth_type"] == OCIAuthType(1).name:
client_kwargs["config"] = oci.config.from_file(
profile_name=values["auth_profile"]
file_location=values["auth_file_location"],
profile_name=values["auth_profile"],
)
client_kwargs.pop("signer", None)
elif values["auth_type"] == OCIAuthType(2).name:
Expand All @@ -124,7 +133,8 @@ def make_security_token_signer(oci_config): # type: ignore[no-untyped-def]
return oci.auth.signers.SecurityTokenSigner(st_string, pk)

client_kwargs["config"] = oci.config.from_file(
profile_name=values["auth_profile"]
file_location=values["auth_file_location"],
profile_name=values["auth_profile"],
)
client_kwargs["signer"] = make_security_token_signer(
oci_config=client_kwargs["config"]
Expand All @@ -151,11 +161,11 @@ def make_security_token_signer(oci_config): # type: ignore[no-untyped-def]
) from ex
except Exception as e:
raise ValueError(
"Could not authenticate with OCI client. "
"Please check if ~/.oci/config exists. "
"If INSTANCE_PRINCIPLE or RESOURCE_PRINCIPLE is used, "
"Please check the specified "
"auth_profile and auth_type are valid."
"""Could not authenticate with OCI client.
If INSTANCE_PRINCIPAL or RESOURCE_PRINCIPAL is used,
please check the specified
auth_profile, auth_file_location and auth_type are valid.""",
e,
) from e

return values
Expand Down
19 changes: 14 additions & 5 deletions libs/community/langchain_community/llms/oci_generative_ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ class OCIGenAIBase(BaseModel, ABC):
If not specified , DEFAULT will be used
"""

auth_file_location: Optional[str] = "~/.oci/config"
"""Path to the config file.
If not specified, ~/.oci/config will be used
"""

model_id: Optional[str] = None
"""Id of the model to call, e.g., cohere.command"""

Expand Down Expand Up @@ -125,7 +130,8 @@ def validate_environment(cls, values: Dict) -> Dict:

if values["auth_type"] == OCIAuthType(1).name:
client_kwargs["config"] = oci.config.from_file(
profile_name=values["auth_profile"]
file_location=values["auth_file_location"],
profile_name=values["auth_profile"],
)
client_kwargs.pop("signer", None)
elif values["auth_type"] == OCIAuthType(2).name:
Expand All @@ -141,7 +147,8 @@ def make_security_token_signer(oci_config): # type: ignore[no-untyped-def]
return oci.auth.signers.SecurityTokenSigner(st_string, pk)

client_kwargs["config"] = oci.config.from_file(
profile_name=values["auth_profile"]
file_location=values["auth_file_location"],
profile_name=values["auth_profile"],
)
client_kwargs["signer"] = make_security_token_signer(
oci_config=client_kwargs["config"]
Expand Down Expand Up @@ -171,11 +178,10 @@ def make_security_token_signer(oci_config): # type: ignore[no-untyped-def]
) from ex
except Exception as e:
raise ValueError(
"""Could not authenticate with OCI client.
Please check if ~/.oci/config exists.
"""Could not authenticate with OCI client.
If INSTANCE_PRINCIPAL or RESOURCE_PRINCIPAL is used,
please check the specified
auth_profile and auth_type are valid.""",
auth_profile, auth_file_location and auth_type are valid.""",
e,
) from e

Expand Down Expand Up @@ -223,6 +229,9 @@ class OCIGenAI(LLM, OCIGenAIBase):
access the OCI Generative AI service.
If a specific config profile is used, you must pass
the name of the profile (from ~/.oci/config) through auth_profile.
If a specific config file location is used, you must pass
the file location where profile name configs present
through auth_file_location

To use, you must provide the compartment id
along with the endpoint url, and model id
Expand Down
Loading