-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRAGwithChatGROQ.py
73 lines (62 loc) · 2.46 KB
/
RAGwithChatGROQ.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import streamlit as st
import os
from langchain_groq import ChatGroq
from langchain_community.document_loaders import WebBaseLoader
from langchain_community.embeddings import OllamaEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain_core.prompts import ChatPromptTemplate
from langchain.chains import create_retrieval_chain
from langchain_community.vectorstores import FAISS
import time
from dotenv import load_dotenv
# ENV VARIABLES
load_dotenv()
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
os.environ["LANGCHAIN_API_KEY"] = os.getenv("LANGCHAIN_API_KEY")
os.environ["LANGCHAIN_TRACING_V2"] = "true"
os.environ["GROQ_API_KEY"] = os.getenv("GROQ_API_KEY")
# load_
groq_api_key = os.getenv("GROQ_API_KEY")
# I'll be using streamlit session_state
if "vector" not in st.session_state:
st.session_state.embeddings = OllamaEmbeddings()
st.session_state.loader = WebBaseLoader("https://docs.smith.langchain.com/")
st.session_state.docs = st.session_state.loader.load()
st.session_state.text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000, chunk_overlap=200
)
st.session_state.final_documents = st.session_state.text_splitter.split_documents(
st.session_state.docs[:10]
)
st.session_state.vectors = FAISS.from_documents(
st.session_state.final_documents, st.session_state.embeddings
)
st.title("ChatGropq Demo")
llm = ChatGroq(model_name="Gemma-7b-It")
# print(llm)
prompt = ChatPromptTemplate.from_template(
"""Answer the questions based on the provided context only.
Please provide the most accurate response based on the question.
<context>
{context}
</context>
Questions:{input}
"""
)
# create_doc_chain
document_chain = create_stuff_documents_chain(llm, prompt)
retriever = st.session_state.vectors.as_retriever()
retriever_chain = create_retrieval_chain(retriever, document_chain)
prompt = st.text_input("Input your prompt")
if prompt:
start = time.process_time()
response = retriever_chain.invoke({"input": prompt})
print("Response time:", time.process_time() - start)
st.write(response["answer"])
# with streamlit expander
with st.expander("Document Similarity Search"):
# find relevanr chunks
for i, doc in enumerate(response["context"]):
st.write(doc.page_content)
st.write("--------------------------------")