0% found this document useful (0 votes)
27 views3 pages

AIlab 10

Uploaded by

nskushal13
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)
27 views3 pages

AIlab 10

Uploaded by

nskushal13
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
You are on page 1/ 3

Build a chatbot for the Indian Penal Code.

We'll start by
downloading the official Indian Penal Code document, and
then we'll create a chatbot that can interact with it. Users will
be able to ask questions about the Indian Penal Code and
have a conversation with it.
!pip install --upgrade cohere
!pip install pdfplumber cohere faiss-cpu langchain
!pip install -U langchain-community
!pip install -U pdfplumber cohere faiss-cpu langchain langchain-core
langchain-community langchain-text-splitters
from google.colab import drive
drive.mount('/content/drive')
import pdfplumber
import cohere
import faiss
import numpy as np
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.vectorstores import FAISS
from langchain.embeddings.base import Embeddings

# Replace with your Cohere API key


COHERE_API_KEY = "8n4xkb0PwycO5Rha5w1scujuGGBCCmyY6p8VPG5c"
co = cohere.Client(COHERE_API_KEY)

# Step 1: Load PDF and extract text


def load_pdf_text(file_path):
text = ""
with pdfplumber.open(file_path) as pdf:
for page in pdf.pages:
extracted = page.extract_text()
if extracted:
text += extracted + "\n"
return text

# Step 2: Split text into chunks


def split_text(text):
splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=100
)
return splitter.split_text(text)

# Custom LangChain-compatible Cohere Embeddings class


class CohereEmbeddings(Embeddings):
def embed_documents(self, texts):
response = co.embed(
texts=texts,
model="embed-english-v3.0",
input_type="search_document"
)
return response.embeddings

def embed_query(self, query):


response = co.embed(
texts=[query],
model="embed-english-v3.0",
input_type="search_query"
)
return response.embeddings[0]

# Step 3: Create FAISS vector store using Cohere embeddings


def create_vector_store(chunks):
embeddings = CohereEmbeddings()
return FAISS.from_texts(chunks, embeddings)

# Step 4: Short answer retrieval with Cohere generate


def ask_question(vectorstore, query):
retriever = vectorstore.as_retriever()
docs = retriever.get_relevant_documents(query)
if not docs:
return "Sorry, I couldn't find any relevant section."

# Try a different model that is likely available for your API key
response = co.generate(
model="command-xlarge", # Replace with a model available for
your API key
prompt=docs[0].page_content + "\n\n" + query,
max_tokens=100 # Limit the response length
)

return response.generations[0].text.strip()

# Main chatbot loop


def main():
print("Loading IPC document...")
raw_text = load_pdf_text("/content/drive/MyDrive/6sem/AIML.pdf")

print("Splitting and indexing text...")


chunks = split_text(raw_text)
vectorstore = create_vector_store(chunks)

print("\nChatbot is ready. Ask IPC-related questions! (type 'exit'


to quit)\n")
while True:
query = input("You: ")
if query.lower() in ['exit', 'quit']:
print("Exiting.")
break
response = ask_question(vectorstore, query)
print("Bot:", response)

if __name__ == "__main__":
main()

You might also like