0% found this document useful (0 votes)
10 views6 pages

Updated Chatbot Code for AI Libraries

The document provides an updated version of a chatbot code that is compatible with the latest library versions, including changes in imports, AI models, and QA chain implementation. It details the steps for setting up a virtual environment and installing required packages, along with the modernized code for processing PDF files and generating responses. Key improvements include upgraded models, prompt engineering, and enhanced text chunking methods.

Uploaded by

abhimanyu kumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

Updated Chatbot Code for AI Libraries

The document provides an updated version of a chatbot code that is compatible with the latest library versions, including changes in imports, AI models, and QA chain implementation. It details the steps for setting up a virtual environment and installing required packages, along with the modernized code for processing PDF files and generating responses. Key improvements include upgraded models, prompt engineering, and enhanced text chunking methods.

Uploaded by

abhimanyu kumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Below is an updated version of the code that works with the latest library versions.

Feel
free to use this in place of the one shown in the course — the core functionality and
logic remains the same, but it's been modernized for compatibility with latest libraries.
We will shortly update the course videos also.

Summary of Changes:
1: Updated imports: Related to langchain, embeddings and chat models
2: Latest AI models: Upgraded from GPT-3.5-turbo to GPT-4o, and switched to text-embedding-
3-small for embedding generation
3: Modern QA chain: Replaced the deprecated load_qa_chain with LangChain Expression
Language (LCEL) using RunnablePassthrough, StrOutputParser, and a retrieval chain pattern
4: Prompt Engineering: Introduced using prompts to better control the output response,
including in case where the question is unrelated to supplied document
5: Updated the separators parameter to accept a list for better text chunking with multiple
separator options

Note - You may encounter issues with compatibility of different packages, hence it is better to
stick with fixed version# by using virtual env and a requirement text file. Please follow the
steps below to run your final code:

1: Create a folder on your desktop (or any location of choice)


2: Save the chatbot code (given below) to a python file ([Link]) in the folder
3: Create another file named "[Link]" inside this folder
4: Put the following details in this [Link] and save it:
streamlit==1.50.0
PyPDF2==3.0.1
langchain==1.0.2
langchain-openai==1.0.1
langchain-community==0.4
langchain-text-splitters==1.0.0
langchain-core==1.0.0
faiss-cpu==1.12.0
openai==2.6.0
numpy==2.2.6

5: Next, open Command prompt, by pressing Windows + R buttons. Type "cmd" and hit enter
6: On the command prompt that opens, go to the folder where Python code and
[Link] file is, by writing:
"cd C:\Users\YourName\Desktop\ChatbotProject" (Without quotes, and replace with your actual
folder path)

7: Next, copy and paste this in the command prompt (without quotes) and hit Enter:
"python -m venv venv"
- Wait for it to complete (will take ~10-20 seconds). You'll see a new folder called `venv` created
in your step 1 folder

8: Activate the virtual environment by typing following command and hit Enter:
venv\Scripts\activate

Success indicator - You should see "(venv)" appear at the beginning of your command line, like
this:
(venv) C:\Users\YourName\Desktop\ChatbotProject>

9: Finally, copy and paste below command to install the required packages. This will take some
time (2-5 minutes) as you see the list of packages getting installed
pip install -r [Link]

10: Now you can run the code, so type - streamlit run [Link] - on the command line and hit
enter
11: This will give you the Streamlit URL and launch the application

UPDATED CODE BELOW - COMPATIBLE WITH UPDATED THIRD PARTY PACKAGES

import streamlit as st
from PyPDF2 import PdfReader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
from langchain_community.vectorstores import FAISS
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough

OPENAI_API_KEY = "sk-7gyX4fvfs9kihnEA" # Pass your key here

# Upload PDF files


[Link]("My first Chatbot")

with [Link]:
[Link]("Your Documents")
file = st.file_uploader(" Upload a PDF file and start asking questions", type="pdf")

# Extract the text


if file is not None:
pdf_reader = PdfReader(file)
text = ""
for page in pdf_reader.pages:
text += page.extract_text()

# Break it into chunks


text_splitter = RecursiveCharacterTextSplitter(
separators=["\n\n", "\n", " ", ""],
chunk_size=1000,
chunk_overlap=150,
length_function=len
)
chunks = text_splitter.split_text(text)

# generating embedding
embeddings = OpenAIEmbeddings(
model="text-embedding-3-small",
openai_api_key=OPENAI_API_KEY
)

# creating vector store - FAISS


vector_store = FAISS.from_texts(chunks, embeddings)

# define the LLM


llm = ChatOpenAI(
model="gpt-4o",
temperature=0,
max_tokens=1000,
openai_api_key=OPENAI_API_KEY
)

# define a basic prompt


prompt = ChatPromptTemplate.from_messages([
("system",
"You are a helpful assistant that answers questions strictly based on the provided context
from the PDF document. "
"Only answer questions using information from the context below. "
"If the question cannot be answered using the context, respond with: 'I can only answer
questions related to the uploaded PDF document.'\n\n"
"Context:\n{context}"),
("human", "{question}")
])

# Helper function to format documents


def format_docs(docs):
return "\n\n".join([doc.page_content for doc in docs])

#output results
#chain -> take the question, get relevant document, pass it to the LLM, generate the output
retriever = vector_store.as_retriever()

chain = (
{"context": retriever | format_docs, "question": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)

# get user questions


user_question = st.text_input("Type Your question here")

if user_question:
# Get response using the modern LCEL chain
response = [Link](user_question)

# Display answer
[Link](response)
[CODE SHOWN IN COURSE - will still work but some library versions have changed]

import streamlit as st
from PyPDF2 import PdfReader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.embeddings import OpenAIEmbeddings
from langchain_community.vectorstores import FAISS
from [Link].question_answering import load_qa_chain
from langchain_community.chat_models import ChatOpenAI

OPENAI_API_KEY = "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" #Pass


your key here

#Upload PDF files


[Link]("My first Chatbot")

with [Link]:
[Link]("Your Documents")
file = st.file_uploader(" Upload a PDf file and start asking questions", type="pdf")

#Extract the text


if file is not None:
pdf_reader = PdfReader(file)
text = ""
for page in pdf_reader.pages:
text += page.extract_text()
#[Link](text)

#Break it into chunks


text_splitter = RecursiveCharacterTextSplitter(
separators="\n",
chunk_size=1000,
chunk_overlap=150,
length_function=len
)
chunks = text_splitter.split_text(text)
#[Link](chunks)

# generating embedding
embeddings = OpenAIEmbeddings(openai_api_key=OPENAI_API_KEY)

# creating vector store - FAISS


vector_store = FAISS.from_texts(chunks, embeddings)

# get user question


user_question = st.text_input("Type Your question here")

# do similarity search
if user_question:
match = vector_store.similarity_search(user_question)
#[Link](match)

#define the LLM


llm = ChatOpenAI(
openai_api_key = OPENAI_API_KEY,
temperature = 0,
max_tokens = 1000,
model_name = "gpt-3.5-turbo"
)

#output results
#chain -> take the question, get relevant document, pass it to the LLM, generate the output
chain = load_qa_chain(llm, chain_type="stuff")
response = [Link](input_documents = match, question = user_question)
[Link](response)

You might also like