diff --git a/.gitignore b/.gitignore index 4488c9f4..de94a3d9 100644 --- a/.gitignore +++ b/.gitignore @@ -44,3 +44,4 @@ temp/tools.ipynb temp/tools.py temp/json-dual.sql env.sh +temp/oci_genai.py diff --git a/app/src/content/oci_config.py b/app/src/content/oci_config.py index 037e0d02..8d232814 100644 --- a/app/src/content/oci_config.py +++ b/app/src/content/oci_config.py @@ -89,6 +89,7 @@ def main(): key="text_input_region", ) key_file = st.text_input("Key File:", value=state.oci_config["key_file"], key="text_input_key_file") + compartment_id = st.text_input("Compartment ID:", value=state.oci_config["compartment_id"], key="text_input_compartment_id") if st.form_submit_button(label="Save"): print("I'm Here!") diff --git a/app/src/modules/metadata.py b/app/src/modules/metadata.py index f99ebd73..994ba64e 100644 --- a/app/src/modules/metadata.py +++ b/app/src/modules/metadata.py @@ -17,6 +17,8 @@ from langchain_openai import OpenAIEmbeddings from langchain_ollama import OllamaEmbeddings from langchain_cohere import CohereEmbeddings +from langchain_community.embeddings.oci_generative_ai import OCIGenAIEmbeddings + from langchain.retrievers.document_compressors import CohereRerank logger = logging_config.logging.getLogger("modules.metadata") @@ -87,6 +89,21 @@ def ll_models(): """Define example Language Model Support""" # Lists are in [user, default, min, max] format ll_models_dict = { + + "cohere.command-r-plus-08-2024": { + "enabled": os.getenv("OCI_PROFILE") is not None, + "api": "CohereOCI", + "url": "https://inference.generativeai.us-chicago-1.oci.oraclecloud.com", + "api_key": os.environ.get("OCI_PROFILE", default=""), + "openai_compat": False, + "context_length": 131072 , + "temperature": [0.3, 0.3, 0.0, 1.0], + "top_p": [1.0, 1.0, 0.0, 1.0], + "max_tokens": [100, 100, 1, 4096], + "frequency_penalty": [0.0, 0.0, -1.0, 1.0], + "presence_penalty": [0.0, 0.0, -2.0, 2.0], + }, + "command-r": { "enabled": os.getenv("COHERE_API_KEY") is not None, "api": "Cohere", @@ -279,6 +296,15 @@ def embedding_models(): "chunk_max": 8191, "dimensions": 1536, }, + "cohere.embed-multilingual-v3.0":{ + "enabled": os.getenv("OCI_PROFILE") is not None, + "api": OCIGenAIEmbeddings, + "url": "https://inference.generativeai.us-chicago-1.oci.oraclecloud.com", + "api_key": os.environ.get("OCI_PROFILE", default=""), + "openai_compat": False, + "chunk_max": 512, + "dimensions": 1024, + }, "embed-english-v3.0": { "enabled": os.getenv("COHERE_API_KEY") is not None, "api": CohereEmbeddings, diff --git a/app/src/modules/utilities.py b/app/src/modules/utilities.py index 451d19d1..31ed9877 100644 --- a/app/src/modules/utilities.py +++ b/app/src/modules/utilities.py @@ -32,6 +32,9 @@ from langchain_ollama import ChatOllama from langchain_openai import ChatOpenAI +from langchain_community.chat_models.oci_generative_ai import ChatOCIGenAI + + from llama_index.core import Document from llama_index.core.node_parser import SentenceSplitter @@ -102,7 +105,8 @@ def get_ll_model(model, ll_models_config=None, giskarded=False): "frequency_penalty": lm_params["frequency_penalty"][0], "presence_penalty": lm_params["presence_penalty"][0], } - + logger.info("LLM_API:"+llm_api) + ## Start - Add Additional Model Authentication Here client = None if giskarded: @@ -117,6 +121,17 @@ def get_ll_model(model, ll_models_config=None, giskarded=False): client = ChatPerplexity(pplx_api_key=lm_params["api_key"], model_kwargs=common_params) elif llm_api == "ChatOllama": client = ChatOllama(model=model,base_url=lm_params["url"], model_kwargs=common_params) + elif llm_api == "CohereOCI": + #state.oci_config["tenancy_ocid"] + client = ChatOCIGenAI( + model_id=model, + service_endpoint=lm_params["url"], + compartment_id=os.environ.get("OCI_COMPARTMENT_ID", default=""), + auth_profile=lm_params["api_key"], + model_kwargs={"temperature": common_params["temperature"], "max_tokens": common_params["max_tokens"],"top_p": common_params["top_p"], + "frequency_penalty": common_params["frequency_penalty"], "presence_penalty": common_params["presence_penalty"]} + ) + ## End - Add Additional Model Authentication Here api_accessible, err_msg = is_url_accessible(llm_url) @@ -134,6 +149,7 @@ def get_embedding_model(model, embed_model_config=None, giskarded=False): embed_key = embed_model_config[model]["api_key"] logger.debug("Matching Embedding API: %s", embed_api) + if giskarded: giskard_key = embed_key or "giskard" _client = OpenAI(api_key=giskard_key, base_url=f"{embed_url}/v1/") @@ -148,6 +164,9 @@ def get_embedding_model(model, embed_model_config=None, giskarded=False): client = embed_api(model=model, base_url=embed_url) elif embed_api.__name__ == "CohereEmbeddings": client = embed_api(model=model, cohere_api_key=embed_key) + elif embed_api.__name__ == "OCIGenAIEmbeddings": + client = embed_api(model_id=model, service_endpoint=embed_url, compartment_id= os.environ.get("OCI_COMPARTMENT_ID", default=""), auth_profile=embed_key) + else: client = embed_api(model=embed_url) @@ -397,6 +416,10 @@ def json_to_doc(file: str): execute_sql(db_conn, mergesql) db_conn.commit() + #NOTE: In this release, index is automatically created without user control. This part of code helps the future release + #to re-create an index or leave without an existing vectorstore. + #for this reason the index_exists is set to True to recreate in any case the index. + index_exists=True if (index_exists): # Build the Index @@ -498,6 +521,7 @@ def oci_initialize( region=None, key_file=None, security_token_file=None, + compartment_id=None, ): """Initialize the configuration for OCI AuthN""" config = { @@ -510,10 +534,12 @@ def oci_initialize( "additional_user_agent": "", "log_requests": False, "pass_phrase": None, + "compartment_id":compartment_id, } config_file = os.environ.get("OCI_CLI_CONFIG_FILE", default=oci.config.DEFAULT_LOCATION) config_profile = os.environ.get("OCI_CLI_PROFILE", default=oci.config.DEFAULT_PROFILE) + compartment_id = os.environ.get("OCI_COMPARTMENT_ID", default="") # Ingest config file when parameter are missing if not (fingerprint and tenancy and region and key_file and (user or security_token_file)): @@ -529,6 +555,7 @@ def oci_initialize( config["tenancy"] = os.environ.get("OCI_CLI_TENANCY", config.get("tenancy")) config["region"] = os.environ.get("OCI_CLI_REGION", config.get("region")) config["key_file"] = os.environ.get("OCI_CLI_KEY_FILE", config.get("key_file")) + config["compartment_id"] = os.environ.get("OCI_COMPARTMENT_ID", config.get("compartment_id")) return config