Spaces:
Sleeping
Sleeping
function file
Browse files
functions/__pycache__/gptResponse.cpython-310.pyc
ADDED
|
Binary file (1.37 kB). View file
|
|
|
functions/__pycache__/sidebar.cpython-310.pyc
ADDED
|
Binary file (415 Bytes). View file
|
|
|
functions/__pycache__/web_chain.cpython-310.pyc
ADDED
|
Binary file (1.59 kB). View file
|
|
|
functions/gptResponse.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_openai import ChatOpenAI
|
| 2 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 3 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
load_dotenv()
|
| 8 |
+
openai_key = os.getenv('OPENAI_API_KEY')
|
| 9 |
+
|
| 10 |
+
def get_response(user_query, chat_history, context):
|
| 11 |
+
template = """
|
| 12 |
+
You are a helpful assistant. Answer the following questions considering the background information of the conversation:
|
| 13 |
+
|
| 14 |
+
Chat History: {chat_history}
|
| 15 |
+
|
| 16 |
+
Background Information: {context}
|
| 17 |
+
|
| 18 |
+
User question: {user_question}
|
| 19 |
+
"""
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
llm = ChatOpenAI(api_key=openai_key)
|
| 23 |
+
try:
|
| 24 |
+
prompt = ChatPromptTemplate.from_template(template)
|
| 25 |
+
|
| 26 |
+
llm = ChatOpenAI(api_key=openai_key)
|
| 27 |
+
|
| 28 |
+
chain = prompt | llm | StrOutputParser()
|
| 29 |
+
|
| 30 |
+
value = chain.stream({
|
| 31 |
+
"chat_history": chat_history,
|
| 32 |
+
"context": context,
|
| 33 |
+
"user_question": user_query,
|
| 34 |
+
})
|
| 35 |
+
if value:
|
| 36 |
+
response = " ".join([part for part in value])
|
| 37 |
+
return response
|
| 38 |
+
else:
|
| 39 |
+
return "No response received from model."
|
| 40 |
+
except Exception as e:
|
| 41 |
+
return f"Error in generating response: {str(e)}"
|
functions/sidebar.py
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
|
| 3 |
+
def sidebar():
|
| 4 |
+
|
| 5 |
+
st.sidebar.page_link("app.py", label="Home")
|
| 6 |
+
st.sidebar.page_link("pages/chat_rag.py", label="RAG CHAT")
|
| 7 |
+
st.sidebar.page_link("pages/test.py", label="TESTING")
|
| 8 |
+
|
functions/web_chain.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_community.document_loaders import WebBaseLoader
|
| 2 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 3 |
+
from langchain_chroma import Chroma
|
| 4 |
+
from langchain_openai import OpenAIEmbeddings
|
| 5 |
+
from PyPDF2 import PdfReader
|
| 6 |
+
|
| 7 |
+
def get_pdf_text(pdf_docs):
|
| 8 |
+
text = ""
|
| 9 |
+
for pdf in pdf_docs:
|
| 10 |
+
pdf_reader = PdfReader(pdf)
|
| 11 |
+
for page in pdf_reader.pages:
|
| 12 |
+
text += page.extract_text()
|
| 13 |
+
return text
|
| 14 |
+
|
| 15 |
+
def loadUrlData(url):
|
| 16 |
+
loader = WebBaseLoader(url)
|
| 17 |
+
loader.requests_kwargs = {'verify':False}
|
| 18 |
+
html = loader.load()
|
| 19 |
+
return html
|
| 20 |
+
|
| 21 |
+
def splitDoc(data):
|
| 22 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
| 23 |
+
chunk_size=1000, chunk_overlap=200, add_start_index=True)
|
| 24 |
+
return text_splitter.split_documents(data)
|
| 25 |
+
|
| 26 |
+
def splitText(data):
|
| 27 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
| 28 |
+
chunk_size=400,
|
| 29 |
+
chunk_overlap=50,
|
| 30 |
+
length_function=len,
|
| 31 |
+
is_separator_regex=False,
|
| 32 |
+
)
|
| 33 |
+
return text_splitter.split_text(data)
|
| 34 |
+
|
| 35 |
+
def vectorize(data, type):
|
| 36 |
+
if type == "document":
|
| 37 |
+
docs = splitDoc(data)
|
| 38 |
+
return Chroma.from_documents(documents=docs, embedding=OpenAIEmbeddings())
|
| 39 |
+
elif type == "text":
|
| 40 |
+
texts = splitText(data)
|
| 41 |
+
return Chroma.from_texts(texts=texts, embedding=OpenAIEmbeddings())
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
|