Skip to content

Commit d697d6a

Browse files
authored
Merge pull request #46 from jctarla/langgraph_agent_with_genai
Langgraph agent with genai
2 parents 34c9487 + f5cb9d1 commit d697d6a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+2532
-0
lines changed
Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
# Chat With Your Personal Files: A Real-Life Example of Using LangGraph and Oracle Generative AI to Summarize, Index, and Find Files Instantly.
2+
3+
4+
We all know the frustration of searching endlessly for a document you know exists somewhere in your personal files. Traditional search often relies on exact file names or rigid folder structures, making it difficult to quickly find what you need. But what if you could simply chat with your documents and instantly locate the right file—summarized, indexed, and ready to use?
5+
6+
In this article, I’ll share a real-life example of how I built a solution that does exactly that. By combining **LangGraph** with **Oracle Generative AI** and **Oracle Database 23ai**, I created an intelligent system that automatically extracts metadata, generates concise summaries, and indexes file contents for easy retrieval. The result is a conversational agent that transforms how we interact with our personal data: instead of searching through folders, you ask questions in natural language and the system finds the right document for you.
7+
8+
>**Note**: This agent is intended for file search only. It does not have the ability to answer in-depth questions regarding the content of your files, as it can only access the summary of each document.
9+
10+
# File Indexing: Discovery what your files are about
11+
12+
This blog demonstrates how to index PDF, Images, DOCx or TXT files using Python code, Generative AI models and Oracle 23ai database
13+
14+
![T1_1](images/FileProcessingFlow.png "T1_1")
15+
16+
17+
>**IMPORTANT**: This blog is designed solely for educational and study purposes. It provides an environment for learners to experiment and gain practical experience in a controlled setting. It is crucial to note that the security configurations and practices employed in this lab might not be suitable for real-world scenarios.
18+
>
19+
> Security considerations for real-world applications are often far more complex and dynamic. Therefore, before implementing any of the techniques or configurations demonstrated here in a production environment, it is essential to conduct a comprehensive security assessment and review. This review should encompass all aspects of security, including access control, encryption, monitoring, and compliance, to ensure that the system aligns with the organization's security policies and standards.
20+
>
21+
> Security should always be a top priority when transitioning from a lab environment to a real-world deployment.
22+
23+
24+
## Technologies powering this solution
25+
- [Oracle Autonomous Database 23ai](https://docs.oracle.com/en-us/iaas/autonomous-database-serverless/doc/autonomous-intro-adb.html#GUID-8EAA5AE6-397D-4E9A-9BD0-3E37A0345E24) : Oracle 23ai is a converged database that seamlessly combines advanced vector search with proven relational capabilities, enabling AI-driven and traditional workloads in a single platform.
26+
- [LangGraph](https://github.com/langchain-ai/langgraph) : LangGraph is a low-level, stateful orchestration framework for building and managing long-running AI agents and complex multi-agent workflows using graph-based architecture
27+
- [OCI Generative AI Services](https://docs.oracle.com/en-us/iaas/Content/generative-ai/getting-started.htm): OCI Generative AI is a managed service offering customizable LLMs for chat, text generation and summarization.
28+
- [Sentence Transformers](https://pypi.org/project/sentence-transformers/): The sentence-transformers package is a Python framework designed for creating and working with embeddings—dense vector representations of text that capture semantic meaning. Built on top of PyTorch and Hugging Face Transformers, it simplifies the process of encoding sentences, paragraphs, or documents into fixed-size vectors suitable for a wide range of Natural Language Processing (NLP) tasks.
29+
30+
31+
32+
## Prerequisites - Oracle Cloud Infrastructure
33+
34+
- Oracle account with admin level access permissions, if not admin level then apply the needed policies [Getting Access to Generative AI](https://docs.oracle.com/en-us/iaas/Content/generative-ai/iam-policies.htm)
35+
- Oracle CLI installed on your local machine, see details here: [Installing the CLI](https://docs.oracle.com/en-us/iaas/Content/API/SDKDocs/cliinstall.htm)
36+
- Oracle Autonomous Database 23ai, see details here: [Provision an Autonomous Database Instance](https://docs.oracle.com/en/cloud/paas/autonomous-database/serverless/adbsb/autonomous-provision.html)
37+
38+
- Python 3.12.10 installed, you can find more details here [Simple Python Version Management: pyenv](https://github.com/pyenv/pyenv)
39+
- OpenGL libs installed on your machine, if you're using Oracle Linux, you can install by running the command:
40+
```
41+
# Install OpenGL
42+
sudo dnf install -y mesa-libGL
43+
```
44+
>**Note**: If you're using MacOS you don't need to install OpenGL.
45+
46+
## Quick Start
47+
48+
1. Collect all database needed information:
49+
50+
1.1 - Go to your autonomous Oracle 23ai database, click on **Database Connection** and then click on **Download Wallet**.
51+
>**Note** Take note of your database wallet password!
52+
53+
1.2 - Click on **Storage**, under **Object Storage & Archive Storage** click on **Buckets**, then create a new bucket to store your database wallet, then upload the downloaded wallet zip file.
54+
>**Note** Take note of your namespace, bucket name and wallet zip file name.
55+
56+
57+
1.3 - Collect all that information and make a note of it — we’ll need it later.
58+
59+
| Variable | Value | Description |
60+
|----------|--------|-------------------------------|
61+
| DB_USER | "admin"| The administrative database username |
62+
| DB_PASSWORD | "xxxxxx" | Your database password |
63+
| DB_DSN | "xxxxxxx" | Get the database service name from your tnsnames.ora, ie. yourdatabase_low |
64+
| WALLET_PASSWORD | "xxxxxxxx" | Get your wallet password |
65+
| OCI_BUCKET_NAMESPACE | "your bucket namespace" | The namespace of your created bucket |
66+
| OCI_BUCKET_NAME_WALLET | "your bucket name" | Your bucket name |
67+
| OCI_WALLET_OBJECT_NAME | "your wallet zip file name" | The name of your database wallet zip file |
68+
| OCI_COMPARTMENT_ID | "your compartment OCID" | Get your compartment OCID |
69+
70+
> **Note** The tnsnames.ora file can be found inside the Wallet zip file.
71+
72+
73+
74+
2. Let's start indexing the files
75+
2.1 - On your shell machine, get the code and create your .env file
76+
````
77+
git clone https://github.com/oracle-devrel/devrel-labs.git
78+
cd langgraph_agent_with_genai/src
79+
## Create your .env file from the template
80+
cp .env_template .env
81+
````
82+
83+
2.2 - Setting your values for the environment variables on **.env**
84+
> **Note** Edit the .env file using your preferred text editor and fill the information with your collected values from **step 1**
85+
86+
```
87+
cat .env
88+
89+
OCI_CLI_PROFILE=DEFAULT
90+
DB_USER="admin"
91+
DB_PASSWORD="xxxxxxx"
92+
DB_DSN="myrdbms_tp"
93+
WALLET_PASSWORD="xxxxxxx"
94+
OCI_BUCKET_NAMESPACE="xxxxxxxx"
95+
OCI_BUCKET_NAME_WALLET="my-bucket"
96+
OCI_WALLET_OBJECT_NAME="Wallet_MyRDBMS.zip"
97+
98+
OCI_COMPARTMENT_ID="ocid1.compartment.oc1..xxxxx"
99+
100+
#meta.llama-3.2-90b-vision-instruct
101+
OCI_GENAI_IMAGE_MODEL_OCID="ocid1.generativeaimodel.oc1.sa-saopaulo-1.amaaaaaask7dceyalwceqwzlywqqxfzz3grpzjr42fej5qlybhu2d666oz4q"
102+
OCI_IMAGE_MODEL_ENDPOINT="https://inference.generativeai.sa-saopaulo-1.oci.oraclecloud.com"
103+
104+
#meta.llama-3.3-70b-instruct
105+
OCI_GENAI_ENDPOINT="https://inference.generativeai.sa-saopaulo-1.oci.oraclecloud.com"
106+
OCI_GENAI_REASONING_MODEL_OCID="ocid1.generativeaimodel.oc1.sa-saopaulo-1.amaaaaaask7dceyarsn4m6k3aqvvgatida3omyprlcs3alrwcuusblru4jaa"
107+
108+
```
109+
> **IMPORTANT**: This sample code was validated using models available in the **sa-saopaulo-1** region. Model availability may vary across regions; please refer to the documentation for the latest details.
110+
You can find more information here [Pretrained Foundational Models in Generative AI](https://docs.oracle.com/en-us/iaas/Content/generative-ai/pretrained-models.htm#pretrained-models).
111+
**If you use different models, you will need to adjust the distance threshold on the search agent.**
112+
113+
114+
We use different models for each requirement:
115+
- OCI_GENAI_IMAGE_MODEL_OCID: This is used to extract text from images (OCR), you must choose a **Multimodal** model.
116+
- OCI_GENAI_REASONING_MODEL_OCID: This is used as LLM generation and understanding of the extracted text.
117+
118+
119+
120+
2.3 - Initialize the database table on Oracle 23ai
121+
Run the **init_database.py** script that will create the table.
122+
123+
```
124+
ls -lrt
125+
# Make sure you install all needed python dependencies.
126+
pip install -r requirements.txt
127+
python --version
128+
python init_database.py
129+
```
130+
131+
![T02_03](images/T02_03_InitDatabase.png "T02_03")
132+
133+
134+
2.4 - Run the IA file indexing.
135+
136+
> **Note**: For demonstration purposes, sample files have been created to support tool validation. These files simulate real documents, including lab test requisitions and results, a Brazilian driver’s license, and Brazilian invoices and receipts.
137+
The files are located on ./samples directory.
138+
139+
```
140+
python batch_process_samples.py
141+
142+
```
143+
![T02_04](images/T02_04_IndexResultPart1.png "T02_04")
144+
![T02_04](images/T02_04_IndexResultPart2.png "T02_04")
145+
146+
The sample files from **samples** directory are now successfully indexed and stored your Oracle 23ai database.
147+
148+
2.5 - Let's validate our indexed data (this is not our Search Agent).
149+
To validate the data, run the **validation.py** script. This script verifies the loaded data and checks the distances returned by the model when querying specific columns. The results can be used to confirm and refine the distance thresholds applied in the Search Agent.
150+
151+
````
152+
python validation.py
153+
````
154+
![T02_05](images/T02_05_Validation_1.png "T02_05")
155+
156+
````
157+
python validation.py PERSON_NAME_EMBEDDING "heloisa pires"
158+
````
159+
![T02_05](images/T02_05_Validation_2.png "T02_05")
160+
161+
> **Note**: As we can see, a simple query for person name using vector search return a very close distance, which means our model is running as expected.
162+
163+
<br>
164+
<br>
165+
166+
# AI Agent Search: Chat with your files
167+
168+
The agent is designed to perform file retrieval based on both document summaries and associated metadata. Users can submit natural language queries about their files, and the agent will intelligently process the request to identify and return the most relevant documents.
169+
170+
171+
![T03_01](images/LangGraphPipelineSimplified.png "T03_01")
172+
173+
![T03_01](images/ToolPipeline_Simplified.png "T03_01")
174+
![T03_01](images/ToolPipeline_2_Simplified.png "T03_01")
175+
176+
177+
<br>
178+
179+
180+
### Key Code Snippet: Leveraging OCI Generative AI in the Agent
181+
182+
⚠️ **Warning**
183+
When working with LangChain on OCI, make sure you use the official Python package **langchain_oci**. Many users get confused because there is also a community ChatOCIGenAI class, which is depreacated and should not be used, for more information, check [langchain_oci.](https://docs.public.content.oci.oraclecloud.com/en-us/iaas/Content/generative-ai/langchain.htm)
184+
185+
The following snippet demonstrates the code currently used in this blog.
186+
187+
```
188+
# This is the correct package used in this agent
189+
cat requirements.txt |grep langchain-oci==0.1.3
190+
```
191+
192+
193+
```
194+
.
195+
.
196+
.
197+
from langchain_oci.chat_models import ChatOCIGenAI
198+
199+
llm = ChatOCIGenAI(
200+
model_id=model_id,
201+
service_endpoint=endpoint,
202+
compartment_id=compartment_id,
203+
auth_type="API_KEY",
204+
model_kwargs={"temperature": 0.2, "max_tokens": 1000}
205+
)
206+
207+
```
208+
209+
210+
211+
212+
### Run the agent, by running **AgentChat.py** script and start asking questions...
213+
214+
```
215+
python AgentChat.py
216+
```
217+
![T03_01](images/AgentStart.png "T03_01")
218+
219+
Let's try with a initial question: "**I need a document that is about receipt of physiotherapy**"
220+
221+
![T03_01](images/AgentQuestion1_01.png "T03_01")
222+
![T03_01](images/AgentQuestion1_02.png "T03_01")
223+
224+
### Playing with questions:
225+
226+
- **"Which documents talk about brazilian beaches?"**
227+
![T03_01](images/Question2_result.png "T03_01")
228+
229+
- **"List test results from August 2025"**
230+
![T03_01](images/Question3_result.png "T03_01")
231+
232+
- **"List receipts that are from physiotherapy"**
233+
![T03_01](images/Question4_result.png "T03_01")
234+
235+
<br>
236+
237+
## Conclusion
238+
239+
240+
This project demonstrates how combining LangGraph, Oracle Generative AI, and Oracle Database 23ai can transform the way we interact with our files—making search faster, smarter, and more intuitive. While the implementation here focuses on document indexing and retrieval, the same principles can be extended to many other real-world scenarios. By experimenting with additional tools, refining thresholds, and expanding metadata extraction, you can evolve this agent into a powerful foundation for building AI-driven knowledge management systems. The journey does not end here—this is just the starting point for reimagining how we connect with our data.
241+
242+
## Contributing
243+
244+
This project is open source. Please submit your contributions by forking this repository and submitting a pull request! Oracle appreciates any contributions that are made by the open source community.
245+
246+
## License
247+
248+
Copyright (c) 2025 Oracle and/or its affiliates.
249+
250+
Licensed under the Universal Permissive License (UPL), Version 1.0.
251+
252+
See [LICENSE](../LICENSE) for more details.
253+
254+
ORACLE 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.
101 KB
Loading
178 KB
Loading
153 KB
Loading
71.3 KB
Loading
766 KB
Loading
197 KB
Loading
41.4 KB
Loading
65.2 KB
Loading
27.6 KB
Loading

0 commit comments

Comments
 (0)