Logo

dev-resources.site

for different kinds of informations.

IRIS-RAG-Gen: Personalizing ChatGPT RAG Application Powered by IRIS Vector Search

Published at
12/26/2024
Categories
sql
chatgpt
python
gpt3
Author
intersystemsdev
Categories
4 categories in total
sql
open
chatgpt
open
python
open
gpt3
open
Author
15 person written this
intersystemsdev
open
IRIS-RAG-Gen: Personalizing ChatGPT RAG Application Powered by IRIS Vector Search

image

Hi Community,

In this article, I will introduce my applicationĀ iris-RAG-GenĀ .

Iris-RAG-Gen is a generative AI Retrieval-Augmented Generation (RAG) application that leverages the functionality of IRIS Vector Search to personalize ChatGPT with the help of the Streamlit web framework, LangChain, and OpenAI. The application uses IRIS as a vector store.
image

Application Features

  • Ingest Documents (PDF or TXT) into IRIS
  • Chat with the selected Ingested document
  • Delete Ingested Documents
  • OpenAI ChatGPT

Ingest Documents (PDF or TXT) into IRIS

Follow the Below Steps to Ingest the document:

  • Enter OpenAI Key
  • Select Document (PDF or TXT)
  • Enter Document Description
  • Click on the Ingest Document Button

image
Ā 

Ingest Document functionality inserts document details into rag_documents table and creates 'rag_document + id'Ā (id of the rag_documents) table to save vector data.

image

The Python code below will save the selected documentĀ into vectors:

from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.document_loaders import PyPDFLoader, TextLoader
from langchain_iris import IRISVector
from langchain_openai import OpenAIEmbeddings
from sqlalchemy import create_engine,text

class RagOpr:
    #Ingest document. Parametres contains file path, description and file type  
    def ingestDoc(self,filePath,fileDesc,fileType):
        embeddings = OpenAIEmbeddings() 
        #Load the document based on the file type
        if fileType == "text/plain":
            loader = TextLoader(filePath)       
        elif fileType == "application/pdf":
            loader = PyPDFLoader(filePath)       
        
        #load data into documents
        documents = loader.load()        
        
        text_splitter = RecursiveCharacterTextSplitter(chunk_size=400, chunk_overlap=0)
        #Split text into chunks
        texts = text_splitter.split_documents(documents)
        
        #Get collection Name from rag_doucments table. 
        COLLECTION_NAME = self.get_collection_name(fileDesc,fileType)
               
        # function to create collection_name table and store vector data in it.
        db = IRISVector.from_documents(
            embedding=embeddings,
            documents=texts,
            collection_name = COLLECTION_NAME,
            connection_string=self.CONNECTION_STRING,
        )

    #Get collection name
    def get_collection_name(self,fileDesc,fileType):
        # check if rag_documents table exists, if not then create it 
        with self.engine.connect() as conn:
            with conn.begin():     
                sql = text("""
                    SELECT *
                    FROM INFORMATION_SCHEMA.TABLES
                    WHERE TABLE_SCHEMA = 'SQLUser'
                    AND TABLE_NAME = 'rag_documents';
                    """)
                result = []
                try:
                    result = conn.execute(sql).fetchall()
                except Exception as err:
                    print("An exception occurred:", err)               
                    return ''
                #if table is not created, then create rag_documents table first
                if len(result) == 0:
                    sql = text("""
                        CREATE TABLE rag_documents (
                        description VARCHAR(255),
                        docType VARCHAR(50) )
                        """)
                    try:    
                        result = conn.execute(sql) 
                    except Exception as err:
                        print("An exception occurred:", err)                
                        return ''
        #Insert description value 
        with self.engine.connect() as conn:
            with conn.begin():     
                sql = text("""
                    INSERT INTO rag_documents 
                    (description,docType) 
                    VALUES (:desc,:ftype)
                    """)
                try:    
                    result = conn.execute(sql, {'desc':fileDesc,'ftype':fileType})
                except Exception as err:
                    print("An exception occurred:", err)                
                    return ''
                #select ID of last inserted record
                sql = text("""
                    SELECT LAST_IDENTITY()
                """)
                try:
                    result = conn.execute(sql).fetchall()
                except Exception as err:
                    print("An exception occurred:", err)
                    return ''
        return "rag_document"+str(result[0][0])

Ā 

Type the below SQL command in the management portal to retrieve vector data

SELECT top 5
id, embedding, document, metadata
FROM SQLUser.rag_document2

image

Ā 

Chat with the selected Ingested document

Select the Document from select chat option section and type question.Ā The application will read the vector data and return the relevant answer
image

The Python code below will save the selected documentĀ into vectors:

from langchain_iris import IRISVector
from langchain_openai import OpenAIEmbeddings,ChatOpenAI
from langchain.chains import ConversationChain
from langchain.chains.conversation.memory import ConversationSummaryMemory
from langchain.chat_models import ChatOpenAI


class RagOpr:
    def ragSearch(self,prompt,id):
        #Concat document id with rag_doucment to get the collection name
        COLLECTION_NAME = "rag_document"+str(id)
        embeddings = OpenAIEmbeddings() 
        #Get vector store reference
        db2 = IRISVector (
            embedding_function=embeddings,    
            collection_name=COLLECTION_NAME,
            connection_string=self.CONNECTION_STRING,
        )
        #Similarity search
        docs_with_score = db2.similarity_search_with_score(prompt)
        #Prepair the retrieved documents to pass to LLM
        relevant_docs = ["".join(str(doc.page_content)) + " " for doc, _ in docs_with_score]
        #init LLM
        llm = ChatOpenAI(
            temperature=0,    
            model_name="gpt-3.5-turbo"
        )
        #manage and handle LangChain multi-turn conversations
        conversation_sum = ConversationChain(
            llm=llm,
            memory= ConversationSummaryMemory(llm=llm),
            verbose=False
        )
        #Create prompt
        template = f"""
        Prompt: {prompt}
        Relevant Docuemnts: {relevant_docs}
        """
        #Return the answer
        resp = conversation_sum(template)
        return resp['response']

    


For more details, please visitĀ iris-RAG-GenĀ open exchange application page.

Thanks

gpt3 Article's
30 articles in total
Favicon
The Technology behind GPT that defined todayā€™s world
Favicon
šŸ¤– DevOps-GPT: Automating SRE Resolutions with AI-Powered Agents and InsightsĀ šŸ¤–
Favicon
Evolution of language models
Favicon
NVIDIA CES 2025 Keynote: AI Revolution and the $3000 Personal Supercomputer
Favicon
Rust and Generative AI: Creating High-Performance Applications
Favicon
The Rise of AI Agent Agencies: Transforming Business Operations for the Digital Age
Favicon
The Economics of Training Frontier Models
Favicon
IRIS-RAG-Gen: Personalizing ChatGPT RAG Application Powered by IRIS Vector Search
Favicon
A Sneak Peek into Video Generation: Webinar Recap
Favicon
šŸ§ Generative AI - 3
Favicon
šŸ§ Generative AI - 2
Favicon
Harnessing OpenAI Assistant 2.0 for Named Entity Recognition in PHP/Symfony 7
Favicon
ChatGPT Prompts That Will Change Your Life in 2025
Favicon
Amazon Bedrock and its benefits in a RAG project
Favicon
A Belief introduction of generative AI
Favicon
Top 5 AI Tools for Coding in 2025
Favicon
Integrating Generative AI with MERN Applications
Favicon
Generative AI for Developers: The Game-Changing Tools You Should Be Using in 2025
Favicon
DeepSeek V3
Favicon
Gen AI Solving Software Engineering Problems
Favicon
GPT-3 PHP Integration: 5 Steps to Master for PHP with OpenAIā€™s GPT-3 API
Favicon
Why Businesses Need Generative AI Services Today
Favicon
Empowering Rookie Nigerian Developers: Trends, Tools, and Best Practices for 2024
Favicon
Generative AI System Design
Favicon
textGrad: Automatic ā€œDifferentiationā€ via Text
Favicon
AI and All Data Weekly for 16 December 2024
Favicon
How ChatGPT Integration Can Transform Your Website
Favicon
Day 32 - Switch Transformers: Efficient Large-Scale Models
Favicon
Large Language Models (LLMs)
Favicon
The Future of Database Management with Text to SQL AI

Featured ones: