{"id":51116624,"url":"https://github.com/oracle-devrel/langchain-oci-genai","last_synced_at":"2026-06-24T22:30:43.802Z","repository":{"id":240289862,"uuid":"802225841","full_name":"oracle-devrel/langchain-oci-genai","owner":"oracle-devrel","description":"langchain-oci-genai","archived":false,"fork":false,"pushed_at":"2024-07-23T21:09:38.000Z","size":8365,"stargazers_count":13,"open_issues_count":0,"forks_count":7,"subscribers_count":5,"default_branch":"main","last_synced_at":"2026-04-02T08:37:04.616Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"upl-1.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/oracle-devrel.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-05-17T19:14:25.000Z","updated_at":"2025-11-06T10:45:12.000Z","dependencies_parsed_at":null,"dependency_job_id":"a80795bf-0316-412f-8c7a-9ac0d15f8a9f","html_url":"https://github.com/oracle-devrel/langchain-oci-genai","commit_stats":null,"previous_names":["oracle-devrel/langchain-oci-genai"],"tags_count":0,"template":false,"template_full_name":"oracle-devrel/repo-template","purl":"pkg:github/oracle-devrel/langchain-oci-genai","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oracle-devrel%2Flangchain-oci-genai","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oracle-devrel%2Flangchain-oci-genai/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oracle-devrel%2Flangchain-oci-genai/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oracle-devrel%2Flangchain-oci-genai/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/oracle-devrel","download_url":"https://codeload.github.com/oracle-devrel/langchain-oci-genai/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oracle-devrel%2Flangchain-oci-genai/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34752465,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-24T02:00:07.484Z","response_time":106,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-06-24T22:30:43.727Z","updated_at":"2026-06-24T22:30:43.787Z","avatar_url":"https://github.com/oracle-devrel.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# How to work with LangChain and Oracle Generative AI\n\n[![License: UPL](https://img.shields.io/badge/license-UPL-green)](https://img.shields.io/badge/license-UPL-green)\u003c!--[![Quality gate](https://sonarcloud.io/api/project_badges/quality_gate?project=oracle-devrel_langchain-oci-genai)](https://sonarcloud.io/dashboard?id=oracle-devrel_langchain-oci-genai)--\u003e\n\n\nIn this demo, we will learn how to work with **#LangChain**'s open-source building blocks, components \u0026 **#LLMs **integrations, **#Streamlit**, an open-source **#Python** framework for **#DataScientists** and AI/ML engineers and **#OracleGenerativeAI** to build the next generation of Intelligent Applications. \n\nWe will lay the groundwork by setting up the OCI Command line interface, Creating a simple LongChain app, writing an interactive **#Healthcare** application, and creating a search application that can scan through a training **#PDF** document. \n\n**What is LangChain?**\n\n[**LangChain**][6] is a framework for developing applications powered by large language models (LLMs). \n\n\n[6]: https://www.langchain.com/\n\nLangChain simplifies every stage of the LLM application lifecycle: \n\n- **Development**: Build your applications using LangChain's open-source building blocks and components. Hit the ground running using third-party integrations and Templates.\n- **Productionization**: Use [LangSmith][7] to inspect, monitor and evaluate your chains, so that you can continuously optimize and deploy with confidence.\n\n[7]: https://python.langchain.com/docs/langsmith/\n\nWith LangChain we can build **context-aware** reasoning applications with LangChain's flexible framework that leverages your company's data and APIs. Future-proof your application by making vendor optionality part of your LLM infrastructure design. \n\n**What is Streamlit?**\n\nA faster way to build and share data apps. [Streamlit][8] turns data scripts into shareable web apps in minutes. All in pure Python. No front‑end experience is required. \n\n[8]: https://streamlit.io/\n\n\n### How does LangChain work with Oracle Generative AI?\n\nThis is a 3-step process. \n\n1. Set up the OCI Command Line Interface on your laptop or use a Cloud Compute Instance.\n2. Install required Python libraries.\n3. Write and Run the Python code.\n\n**Assumption:**\n\n1. You already have an Oracle cloud account and access to Oracle Generative AI in the Chicago region. \n2. You have a default compartment created.\n3. You have administrative rights in the tenancy or your Administrator has set up required policies to access [Generative AI Services][9].\n4. [Download Python Code][10] and [Budget PDF.][11] used in this article.\n\n\n[9]: https://docs.oracle.com/en-us/iaas/Content/generative-ai/iam-policies.htm\n[10]: https://github.com/oracle-devrel/langchain-oci-genai/tree/main/assets/GenAI\n[11]: https://github.com/oracle-devrel/langchain-oci-genai/tree/main/assets/budget_speech.pdf\n\n## Step 1: Installing OCI CLI and Configuring\n\n### Get User's OCID\n\n1. After logging into the cloud console, click on User Settings under the top right navigation; this will take you to **the User Details** page. Copy the OCID into a text file. We will need this later:\n\n    ![][12]\n\n2. On the same page click on **Add API Key** button:\n\n    ![][13]\n\n[12]: images/1712837918418.jpg\n[13]: images/1712837944195.jpg\n\n### Generate and Download RSA Key Pair\n\n1. Choose the option to Generate a new key pair; if you already have keys generated, you can upload them here. The most important thing is to download both of them when you create a new key pair. Click **the Add** button:\n\n    ![][14]\n\n    You should now be able to see the Configuration file. Copy and paste this into a file we will need later:\n\n    ![][15]\n\n    Click on the **Close** button.\n\n2. We can now see our newly created fingerprint:\n\n    ![][16]\n\n\n[14]: images/1712837990031.jpg\n[15]: images/1712838022822.jpg\n[16]: images/1712838294532.jpg\n\n### Get Compartment OCID\n\nUnder Identity \u003e Compartments, note your compartment OCID; we will need this OCID later:\n\n![][17]\n\n\n[17]: images/1712895249226.jpg\n\n### Install OCI Command Line Interface (CLI)\n\n1. **Install OCI CLI on MAC OS**\n2. If you have not installed Brew on your MacOS, please refer to their official guide, [Install Brew on Mac][18]:\n\n        /bin/bash -c \"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\"\n    \n        brew update \u0026\u0026 brew install oci-cli\n\n    For Other Operating systems and more details on OCI CLI [please check this link][19]. \n\n[18]: https://docs.brew.sh/Installation\n[19]: https://docs.oracle.com/en-us/iaas/Content/API/SDKDocs/cliinstall.htm#\n\n### Update OCI Configuration file\n\nUpdate DEFAULT values as per your OCI parameters:\n\n    $ vi ~/.oci/config\n\nThe parameters will look something like this:\n\n    [DEFAULT]\n    user=ocid1.user.oc1..aaaaaaXXXX63smy6knndy5q\n    fingerprint=d5:84:a7:0e:XXXX:ed:11:1a:50\n    tenancy=ocid1.tenancy.oc1..aaaaaaaaj4XXXymyo4xwxyv3gfa\n    region=us-phoenix-1 #(replace-with-your-oci-region)\n    key_file=/your-folder/your-oci-key.pem\n\nSee if we can list all buckets in a compartment to check if all configurations are correct. Provide your compartment ocid where the OCI buckets have been created:\n\n```console\n# set chmod on .pem file\n$ chmod 600 /your-folder/your-oci-key.pem\n\n# get tenancy namespace\n$ oci os ns get\n\n{\n  \"data\": \"yournamespace\"\n}\n\n# run oci cli to list buckets in a OCI Object storage \n$ oci os bucket list --compartment-id ocid1.compartment.oc1..aXXXn4hgg\n\n# expect a similar JSON output without errors\n\n  {\n      \"compartment-id\": \"ocid1.compartment.oc1..aXXX32q\", \n      \"defined-tags\": null,\n      \"etag\": \"25973f28-5125-4eae-a37c-73327f5c2644\",\n      \"freeform-tags\": null,\n      \"name\": \"your-bucket-name\",\n      \"namespace\": \"your-tenancy-namespace\",\n      \"time-created\": \"2023-03-26T16:18:17.991000+00:00\"\n    }\n```\n\nIf this lists objects in an OCI bucket or the name of your tenancy namespace, we are good to move forward; you can create a bucket in OCI Object storage and test it. If there are issues, check the troubleshooting section in this article. \n\n## Step 2: Install the required Python libraries\n\nAdd any additional libraries as required to run the Python code: \n\n```console\n$ python3 --version\nPython 3.10.4\n\n$ pip install -U langchain oci\nlangchain-core\nlangchain-cli\nlangchain_cohere \ncohere\nlanggraph \nlangsmith\nstreamlit \n```\n\n## Step 3: Write and Run the Python code.\n\nThe basic command to get the Oracle LLM handle is shown below:\n\n```python\nfrom langchain_community.llms import OCIGenAI \n\n...\n\nllm = OCIGenAI(\n    model_id=\"cohere.command\",\n    service_endpoint=\"https://inference.generativeai.us-chicago-1.oci.oraclecloud.com\",\n    compartment_id=\"\u003cYour-Compartment-Id\u003e\",\n    model_kwargs={\"temperature\": 0.7, \"top_p\": 0.75, \"max_tokens\": 1000}\n)\n```\n\nTo get OCI Generative AI Embeddings:\n\n```python\nfrom langchain_community.llms import OCIGenAI \nfrom langchain_community.embeddings import OCIGenAIEmbeddings\n\n...\n\nembeddings = OCIGenAIEmbeddings(\n    model_id=\"cohere.embed-english-light-v3.0\",\n    service_endpoint=\"https://inference.generativeai.us-chicago-1.oci.oraclecloud.com\", \n    compartment_id=\"\u003cYour-Compartment-Id\u003e\",\n)\n```\n\n## Examples\n\n### Example 1: Simple Generative AI Console App\n\nLet's start with a basic Python code in the console to check the connectivity, authentication and AI response:\n\n![][20]\n\n\n[20]: images/1712895679841.jpg\n\nDownload [Basic.py][21] run the Python code, and view output:\n\n[21]: http://Basic.py\n\n```console\n$ python3 basic.py\n\nThe Egyptians built the pyramids. The Egyptian pyramids are ancient pyramid-shaped masonry structures located in Egypt.\n```\n\n### Example 2: Simple Generative AI Web Application with Input\n\nIn this simple Generative AI Application, we will just take input prompt and display AI result:\n\n![][22]\n\n\n[22]: images/1712895931279.jpg\n\nDownload [quickstart.py][23] and run the code:\n\n\n[23]: https://github.com/oracle-devrel/langchain-oci-genai/tree/main/assets/GenAI\n\nRun the code from your laptop or desktop command prompt:\n\n```console\n$ streamlit run quickstart.py\n\nYou can now view your Streamlit app in your browser.\n\nLocal URL: http://localhost:8501\nNetwork URL: http://192.168.X.Y:8501\n```\n\n![][24]\n\n\n[24]: images/1712826035917.jpg\n\n![][25]\n\n[25]: images/1712826094620.jpg\n\n### Example 3: Simple Healthcare AI Application\n\nBuild your first LangChain and Oracle Generative AI-based Healthcare Application. In this Application, we will take disease as input, and based on this, we will create a LangChain that will list the disease, symptoms and first aid. \n\nGet the required imports and initialise Oracle Generative AI LLM:\n\n![][26]\n\n[26]: images/1712896566863.jpg\n\nBuild the Streamlit framework and input prompts:\n\n![][27]\n\n[27]: images/1712896636612.jpg\n\nInitialise Memory and Chains:\n\n![][28]\n\n[28]: images/1712896739610.jpg\n\nSet SequentialChain and Print output on the browser:\n\n![][29]\n\n[29]: images/1712896835859.jpg\n\nDownload [symptoms.py][30] and run the code. \n\n[30]: https://github.com/oracle-devrel/langchain-oci-genai/tree/main/assets/GenAI\n\nRun the Application:\n\n```console\n$ streamlit run symptoms.py\n\nYou can now view your Streamlit app in your browser.\n\nLocal URL: http://localhost:8501\nNetwork URL: http://192.168.X.Y:8501\n```\n\n![][31]\n\n\n[31]: images/1712826990273.jpg\n\n![][32]\n\n\n[32]: images/1712827016788.jpg\n\n### Example 4: PDF Search AI Application\n\nA smart application that can read and search PDFs and provide AI output for a given prompt. Download and put the [Budget PDF][33] in the same directory as the Python code. \n    \n    \n[33]: https://github.com/oracle-devrel/langchain-oci-genai/tree/main/assets/budget_speech.pdf\n\nPrompt: What is Amrit Kaal?  lets check in the PDF document\n\n```python\nfrom langchain_community.document_loaders import PyPDFLoader\nfrom langchain_community.llms import OCIGenAI \nfrom langchain_core.prompts import PromptTemplate\nfrom langchain.schema.output_parser import StrOutputParser\nfrom langchain.schema.runnable import RunnablePassthrough\nfrom langchain_community.embeddings import OCIGenAIEmbeddings\nfrom langchain_community.vectorstores import Chroma\n  \nloader = PyPDFLoader(\"budget_speech.pdf\")\npages = loader.load_and_split()\n\nllm = OCIGenAI(\n    model_id=\"cohere.command\",\n    service_endpoint=\"https://inference.generativeai.us-chicago-1.oci.oraclecloud.com\",\n    compartment_id=\"\u003cYour-Compartment-Id\u003e\",\n    model_kwargs={\"temperature\": 0.7, \"top_p\": 0.75, \"max_tokens\": 1000}\n)\n\nembeddings = OCIGenAIEmbeddings(\n    model_id=\"cohere.embed-english-light-v3.0\",\n    service_endpoint=\"https://inference.generativeai.us-chicago-1.oci.oraclecloud.com\",\n    compartment_id=\"\u003cYour-Compartment-Id\u003e\",\n)\n\nvectorstore = Chroma.from_documents(\n    pages,\n    embedding=embeddings    \n)\nretriever = vectorstore.as_retriever()\n\ntemplate = \"\"\" \n{context}\nIndian Budget Speech : {input} \n\"\"\"\nprompt = PromptTemplate.from_template(template)\n \n\nchain = (\n    {\"context\": retriever, \n     \"input\": RunnablePassthrough()}\n    | prompt\n    | llm\n    | StrOutputParser()\n)\n\nprint(chain.invoke(\"What is Amrit Kaal\"))\n```\n\nRun the code in the terminal:\n\n![][34]\n\n[34]: images/1712837027530.jpg\n\n```console\n$ python3 searchpdf.py\n\n-- Output from Generative AI after searching the PDF --\n\nThe Indian Budget Speech outlines the government's plans and priorities for the fiscal year 2024-25. One of the key focus areas highlighted in the speech is the concept of \"Amrit Kaal,\" which translates to \"Era of Immortality\" or \"Golden Era.\" \n\nThe government aims to foster inclusive and sustainable development to improve productivity, create opportunities for all, and contribute to the generation of resources to power investments and fulfill aspirations. \n\nTo achieve this, the government intends to adopt economic policies that facilitate growth and transform the country. This includes ensuring timely and adequate finances, relevant technologies, and appropriate training for Micro, Small and Medium Enterprises (MSME) to compete globally.  \n\n....\n```\n\nChange the prompt and re-run the same code:\n\n1. Update the code\n\n    ```python\n    print(chain.invoke(\"What is India's fiscal deficit\"))\n    ```\n\n2. Run the code:\n\n    ```console\n    $ python3 searchpdf.py\n    ```\n\nAI Output after searching through the Budget PDF is: \n\n    India's fiscal deficit for the year 2024-25 is estimated to be 5.1% of GDP, according to a budget speech by the country's Finance Minister Nirmala Sitharaman. This is in line with the government's goal to reduce the fiscal deficit to below 4.5% by 2025-26. \n    \n    Would you like me to tell you more about India's budgetary process or fiscal policies?\n\n### Example 5: PDF Search AI Application Streamlit version\n\n![][35]\n\n\n[35]: images/1712976897287.jpg\n\n![][36]\n\n[36]: images/1712976912969.jpg\n\n```python\nimport streamlit as st\nfrom langchain_community.llms import OCIGenAI \nfrom langchain_community.document_loaders import PyPDFLoader\nfrom langchain_core.prompts import PromptTemplate\nfrom langchain_community.embeddings import OCIGenAIEmbeddings\nfrom langchain_community.vectorstores import Chroma\n\nst.title('🦜🔗 PDF AI Search Application')\n \ndef generate_response(input_text): \n  loader = PyPDFLoader(\"budget_speech.pdf\")\n  pages = loader.load_and_split()\n  llm = OCIGenAI(\n    model_id=\"cohere.command\",\n    service_endpoint=\"https://inference.generativeai.us-chicago-1.oci.oraclecloud.com\",\n    compartment_id=\"\u003cYour-Compartment-Id\u003e\",\n    model_kwargs={\"temperature\": 0.7, \"top_p\": 0.75, \"max_tokens\": 300}\n  )  \n  embeddings = OCIGenAIEmbeddings(\n    model_id=\"cohere.embed-english-light-v3.0\",\n    service_endpoint=\"https://inference.generativeai.us-chicago-1.oci.oraclecloud.com\",\n    compartment_id=\"\u003cYour-Compartment-Id\u003e\",\n  ) \n  vectorstore = Chroma.from_documents(\n        pages,\n        embedding=embeddings    \n    )\n  retriever = vectorstore.as_retriever()\n  template = \"\"\" \n  {context}\n  Indian Budget Speech : {input} \n  \"\"\"\n  prompt = PromptTemplate.from_template(template) \n  st.info(llm(input_text))\n\nwith st.form('my_form'):\n  text = st.text_area('Enter Search:','What is Amrit Kaal?')\n  submitted = st.form_submit_button('Submit') \ngenerate_response(text)\n\n#streamlit run pdfsearch.py\n```\n\n[Download Python Code][37] and [Budget PDF][38] used \n\n\n[37]: https://github.com/oracle-devrel/langchain-oci-genai/tree/main/assets/GenAI\n[38]: https://github.com/oracle-devrel/langchain-oci-genai/tree/main/assets/budget_speech.pdf\n\n## Troubleshooting:\n\nError Message:\n\n```console\nTraceback (most recent call last):\n  File \"/some-folder/basic.py\", line 3, in \u003cmodule\u003e\n    llm = OCIGenAI(\n  File \"/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/langchain_core/load/serializable.py\", line 120, in __init__\n    super().__init__(**kwargs)\n  File \"/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pydantic/v1/main.py\", line 341, in __init__\n    raise validation_error\npydantic.v1.error_wrappers.ValidationError: 1 validation error for OCIGenAI\n__root__\n  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. (type=value_error)\n```\n\n**Solution**: This is an Authentication issue, Verify the config file settings:\n\n```console\n$ vi ~/.oci/config\n\n[DEFAULT]\nuser=ocid1.user.oc1..aaaaaaaaompuufgfXXXXndy5q\nfingerprint=d5:84:a7:0e:bf:43:XXXXX:11:1a:50\ntenancy=ocid1.tenancy.oc1..aaaaaaaaXXXXXgfa\nregion=us-phoenix-1\nkey_file=/Users/somefolder/oci_api_key_xxx.pem\n```\n\n## References\n\n- [Oracle Cloud Infrastructure Generative AI][39]\n- [OCI CLI reference][40]\n\n\n[39]: https://python.langchain.com/docs/integrations/llms/oci_generative_ai/\n[40]: https://docs.oracle.com/en-us/iaas/tools/oci-cli/3.39.0/oci_cli_docs/cmdref/os/bucket/list.html\n\n## Contributing\n\nThis project welcomes contributions from the community. Before submitting a pull\nrequest, please [review our contribution guide](./CONTRIBUTING.md).\n\n## Security\n\nPlease consult the [security guide](./SECURITY.md) for our responsible security\nvulnerability disclosure process.\n\n## License\n\nCopyright (c) 2024 Oracle and/or its affiliates.\n\nLicensed under the Universal Permissive License (UPL), Version 1.0.\n\nSee [LICENSE](LICENSE.txt) for more details.\n\nORACLE AND ITS AFFILIATES DO NOT PROVIDE ANY WARRANTY WHATSOEVER, EXPRESS OR IMPLIED, FOR ANY SOFTWARE, MATERIAL OR CONTENT OF ANY KIND CONTAINED OR PRODUCED WITHIN THIS REPOSITORY, AND IN PARTICULAR SPECIFICALLY DISCLAIM ANY AND ALL IMPLIED WARRANTIES OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE.  FURTHERMORE, ORACLE AND ITS AFFILIATES DO NOT REPRESENT THAT ANY CUSTOMARY SECURITY REVIEW HAS BEEN PERFORMED WITH RESPECT TO ANY SOFTWARE, MATERIAL OR CONTENT CONTAINED OR PRODUCED WITHIN THIS REPOSITORY. IN ADDITION, AND WITHOUT LIMITING THE FOREGOING, THIRD PARTIES MAY HAVE POSTED SOFTWARE, MATERIAL OR CONTENT TO THIS REPOSITORY WITHOUT ANY REVIEW. USE AT YOUR OWN RISK.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foracle-devrel%2Flangchain-oci-genai","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foracle-devrel%2Flangchain-oci-genai","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foracle-devrel%2Flangchain-oci-genai/lists"}