#first step -
#we need to create a folder where we will store the files related to chatbot
#then we need to access our bash/commandprompt to access the folder by specifying the path using the cd, changedirectory command
#suppose our folder path is c\user\chatbot, then we need to copy this path and in our cmd, commandprompt type 'cd c\user\chatbot'
#when the cmd is showing path as c\user\chatbot, type 'code .'
#this 'code .' opens the vscode app directly from the commandprompt
#it will open the vscode, as shown in the below screengrab - (this shows my completed project)
#step2- click on the 3 dots as visible on the very top of vscode application and create a new terminal
#step4 creating virtual environment for the project with python 3.10
#step 5-
#creating the text file about our business/service
##Mr A's caffe is a small family restaurant in the outskirts of the city. It serves the patrons with the loal delights and desi cuisines which are finger licking good
#step7, this will take some time, so please be patient, now we will need to create the tokenization/embedding of the text that we created earlier
from langchain_community.document_loaders import TextLoader from langchain_text_splitters import RecursiveCharacterTextSplitter from langchain_community.embeddings import OllamaEmbeddings from langchain_chroma import Chroma
loader = TextLoader("text.txt") text_docs = loader.load()
splitter = RecursiveCharacterTextSplitter(chunk_size=100, chunk_overlap=30) final_docs = splitter.split_documents(text_docs)
myembeddings = OllamaEmbeddings(model="gemma2:2b")
vectordb = Chroma.from_documents( documents=final_docs, embedding=myembeddings, persist_directory="./chroma_db" )
#final step-
#creating app.py file integrating the vectorized text embeddings and api key model from groq.com in the streamlit app interface
import os from dotenv import load_dotenv from langchain_community.embeddings import OllamaEmbeddings from langchain_chroma import Chroma from langchain_core.prompts import ChatPromptTemplate from langchain_groq import ChatGroq from langchain.chains import create_retrieval_chain from langchain.chains.combine_documents import create_stuff_documents_chain import streamlit as st
load_dotenv()
groq_api_key ="insert your key here" model = ChatGroq(model="gemma2-9b-it", groq_api_key=groq_api_key) embeddings = OllamaEmbeddings(model = 'gemma2:2b') mydb = Chroma(persist_directory='./chroma_db',embedding_function=embeddings) retriever = mydb.as_retriever(search_type='similarity', search_kwargs={"k":6})
st.title("Welcome to A's Caffe") query = st.chat_input("Ask me anything- ")
system_prompt= ( "You are an assitant for question answering task for a restaurant called A's caffe" "Use the following pieces of retrieved context to answer the question." "Make sure you talk very polite with the customer and don't write anything bad about the restaurant" "Your tone of reply should always be exciting and luring to the customers" "\n\n" "{context}" )
prompt = ChatPromptTemplate.from_messages([ ('system', system_prompt),('human',"{input}") ])
if query: question_answer_chain= create_stuff_documents_chain(model,prompt) rag_chain = create_retrieval_chain(retriever, question_answer_chain)
response = rag_chain.invoke({'input':query})
st.write(response['answer'])
#in the code I have reproduced in the above cell, we can see that we have integrated the system and human prompt, using both the models
#final words - after you have saved every file, go to the command prompt and type 'streamlit run app.py' it will open the app
# As fastai's founder, Jeremy Howard keeps saying and as is the nike's logo - 'just do it', will be explaining both code snippet in other post