# Define the Streamlit app code as a string
streamlit_code = """
import streamlit as st
import os
from langchain_nvidia_ai_endpoints import NVIDIAEmbeddings, ChatNVIDIA
from langchain_core.prompts import ChatPromptTemplate
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain.chains import create_retrieval_chain
from langchain_community.vectorstores import FAISS
from langchain_community.document_loaders import PyPDFDirectoryLoader
# from langchain_community.document_loaders import TextLoader
# from langchain_community.document_loaders import Docx2txtLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
llm = ChatNVIDIA(model = "meta/llama-3.1-8b-instruct") # This model gets called from NVIDIA NIM Inferencing
# Define the folder path and index name for saving/loading the FAISS index
FAISS_FOLDER_PATH = "faiss_index"
FAISS_INDEX_NAME = "index"
# Create vector embeddings for all pdf files in your knowledge base, and store them in a vector database
def vector_embedding():
if "vectors" not in st.session_state:
# Check if the FAISS index for the vector embeddings already exists. If so, load it.
if os.path.exists(os.path.join(FAISS_FOLDER_PATH, f"{FAISS_INDEX_NAME}.faiss")):
st.session_state.embeddings = NVIDIAEmbeddings()
# Load the saved FAISS index
st.session_state.vectors = FAISS.load_local(
FAISS_FOLDER_PATH, st.session_state.embeddings, index_name=FAISS_INDEX_NAME, allow_dangerous_deserialization=True
)
# Note: setting allow_dangerous_deserialization=True can be done if you are certain the files you are searching for
# your context do not contain malicious code (e.g., internal company files are usually safe). But if searching
# through pdf files found on the internet, e.g., be careful.
else:
# If the index doesn't exist, create the embeddings and vector store
st.session_state.embeddings = NVIDIAEmbeddings() # Will create the embeddings used to convert the text into vectors
st.session_state.loader = PyPDFDirectoryLoader("C:/Users/rsb84/Documents/GitHub/portfolio/LLMs/RAG/pdf/")
st.session_state.docs = st.session_state.loader.load()
st.session_state.text_splitter = RecursiveCharacterTextSplitter(chunk_size=700, chunk_overlap=50)
st.session_state.final_documents = st.session_state.text_splitter.split_documents(st.session_state.docs)
st.session_state.vectors = FAISS.from_documents(st.session_state.final_documents, st.session_state.embeddings)
# Save the FAISS index for future use
st.session_state.vectors.save_local(FAISS_FOLDER_PATH, index_name=FAISS_INDEX_NAME)
st.title("NVIDIA NIM RAG Demo")
# Initialize the document processing on app load
if 'initialized' not in st.session_state:
vector_embedding()
prompt = ChatPromptTemplate.from_template(
\"""
Answer the question based on the provided context only.
Please provide the most accurate response based on the question.
<context>
{context}
<context>
Questions: {input}
\"""
)
prompt1 = st.text_area("Enter Your Question related to Your Documents:", height=150)
# Create an optional button to use to perform the search
search_button = st.button("Search")
# If you enter the prompt, this next code chunk creates a document chain. This first line ensures the vector embeddings have
# already been created before user can enter their question
if (prompt1 and 'vectors' in st.session_state) or search_button:
document_chain = create_stuff_documents_chain(llm, prompt)
retriever = st.session_state.vectors.as_retriever() # When we use the vectors database as_retriever(), this becomes an
# interface to retrieve all the data from the database
retrieval_chain = create_retrieval_chain(retriever, document_chain)
response = retrieval_chain.invoke({'input': prompt1})
st.write(response['answer'])
# This will display all of the context used, using streamlit expander()
with st.expander("Document Similarity Search"):
# Identify relevant chunks
for i, doc in enumerate(response["context"]):
st.write(doc.page_content)
st.write("----------------------------------")
from openai import OpenAI
client = OpenAI(
base_url = "https://integrate.api.nvidia.com/v1",
api_key = nvidia_api_key
)
completion = client.chat.completions.create(
model="meta/llama-3.1-70b-instruct",
messages=[{"role":"user","content":"List the 5 least wealthy countries, and the main reasons each struggles with poverty."}],
temperature=0.2,
top_p=0.7,
max_tokens=1024,
stream=True
)
for chunk in completion:
if chunk.choices[0].delta.content is not None:
print(chunk.choices[0].delta.content, end="")
"""
# Specify the filename
filename = 'app.py'
# Write the code to a Python script file
with open(filename, 'w') as file:
file.write(streamlit_code)
# Verify the file has been created
if os.path.exists(filename):
# Get the full path of the file
full_path = os.path.abspath(filename)
print(f"File '{filename}' has been created successfully.")
print(f"Full path of the file: {full_path}")
# Verify the file content
with open(filename, 'r') as file:
content = file.read()
# Check if the content matches the expected code
if content == streamlit_code:
print("File content verified successfully.")
else:
print("Warning: File content does not match expected content.")
else:
print(f"Error: File '{filename}' could not be created.")