Spaces:
Sleeping
Sleeping
Tomas Larsson commited on
Commit ·
59f8385
1
Parent(s): 7c35fc6
vs
Browse files
app.py
CHANGED
|
@@ -105,13 +105,9 @@ if submit_button:
|
|
| 105 |
if question: # Check if there is a question typed
|
| 106 |
# Process the question here (a placeholder answer is used in this example)
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
answer = "This is a placeholder answer to your question."
|
| 115 |
-
answer_placeholder.text(answer) # Display the answer
|
| 116 |
else:
|
| 117 |
answer_placeholder.warning("Please type a question.")
|
|
|
|
| 105 |
if question: # Check if there is a question typed
|
| 106 |
# Process the question here (a placeholder answer is used in this example)
|
| 107 |
|
| 108 |
+
Awnser = rag_chain.invoke(question)
|
| 109 |
+
contexts = retriever.get_relevant_documents(question)
|
| 110 |
+
|
| 111 |
+
answer_placeholder.text(Awnser) # Display the answer
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
else:
|
| 113 |
answer_placeholder.warning("Please type a question.")
|
start.py
CHANGED
|
@@ -8,94 +8,96 @@ try:
|
|
| 8 |
import requests
|
| 9 |
#import fitz # PyMuPDF
|
| 10 |
|
|
|
|
|
|
|
| 11 |
|
|
|
|
|
|
|
| 12 |
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
|
| 25 |
-
from langchain.embeddings import OpenAIEmbeddings
|
| 26 |
-
from langchain.vectorstores import Weaviate
|
| 27 |
-
import weaviate
|
| 28 |
-
from weaviate.embedded import EmbeddedOptions
|
| 29 |
-
#from dotenv import load_dotenv,find_dotenv
|
| 30 |
-
|
| 31 |
-
# Load OpenAI API key from .env file
|
| 32 |
-
#load_dotenv(find_dotenv())
|
| 33 |
-
|
| 34 |
-
# Setup vector database|
|
| 35 |
-
client = weaviate.Client(
|
| 36 |
-
embedded_options = EmbeddedOptions()
|
| 37 |
-
)
|
| 38 |
############################################################################
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
pickle_file_path = 'vectorstore.pkl'
|
| 42 |
-
import pickle
|
| 43 |
-
|
| 44 |
-
with open(pickle_file_path, 'rb') as file:
|
| 45 |
-
docs = pickle.load(file)
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
vectorstore = Weaviate.from_documents(
|
| 50 |
-
client = client,
|
| 51 |
-
documents = docs,
|
| 52 |
-
embedding = OpenAIEmbeddings(),
|
| 53 |
-
by_text = False
|
| 54 |
-
)
|
| 55 |
-
|
| 56 |
-
# Define vectorstore as retriever to enable semantic search
|
| 57 |
-
retriever = vectorstore.as_retriever()
|
| 58 |
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
# Create RAG chain
|
| 64 |
-
|
| 65 |
-
#from langchain.chat_models import ChatOpenAI
|
| 66 |
-
|
| 67 |
-
from langchain_openai import ChatOpenAI
|
| 68 |
-
|
| 69 |
-
from langchain.prompts import ChatPromptTemplate
|
| 70 |
-
from langchain.schema.runnable import RunnablePassthrough
|
| 71 |
-
from langchain.schema.output_parser import StrOutputParser
|
| 72 |
-
|
| 73 |
-
# Define LLM
|
| 74 |
-
llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0)
|
| 75 |
-
|
| 76 |
-
#llm = ChatOpenAI(model_name="gpt-4", temperature=0.15)
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
# Define prompt template / context
|
| 80 |
-
template = """You are a lawyer responding to creditors questions.
|
| 81 |
-
Use the following pieces of retrieved context to answer the question.
|
| 82 |
-
If you don't know the answer, just say that you don't know and add a funny joke.
|
| 83 |
-
Keep the answer concise.
|
| 84 |
-
Question: {question}
|
| 85 |
-
Context: {context}
|
| 86 |
-
Answer:
|
| 87 |
-
"""
|
| 88 |
-
|
| 89 |
-
prompt = ChatPromptTemplate.from_template(template)
|
| 90 |
-
|
| 91 |
-
# Setup RAG pipeline
|
| 92 |
-
rag_chain = (
|
| 93 |
-
{"context": retriever, "question": RunnablePassthrough()}
|
| 94 |
-
| prompt
|
| 95 |
-
| llm
|
| 96 |
-
| StrOutputParser()
|
| 97 |
-
)
|
| 98 |
|
| 99 |
-
|
| 100 |
except Exception as e:
|
| 101 |
em = "bbb" + str(e)
|
|
|
|
| 8 |
import requests
|
| 9 |
#import fitz # PyMuPDF
|
| 10 |
|
| 11 |
+
if 'retriever' not in st.session_state:
|
| 12 |
+
|
| 13 |
|
| 14 |
+
os.environ["OPENAI_API_KEY"] = os.getenv('openkey')
|
| 15 |
+
org = os.getenv('openorg')
|
| 16 |
|
| 17 |
|
| 18 |
+
from langchain.embeddings import OpenAIEmbeddings
|
| 19 |
+
from langchain.vectorstores import Weaviate
|
| 20 |
+
import weaviate
|
| 21 |
+
from weaviate.embedded import EmbeddedOptions
|
| 22 |
+
#from dotenv import load_dotenv,find_dotenv
|
| 23 |
+
|
| 24 |
+
# Load OpenAI API key from .env file
|
| 25 |
+
#load_dotenv(find_dotenv())
|
| 26 |
+
|
| 27 |
+
# Setup vector database|
|
| 28 |
+
client = weaviate.Client(
|
| 29 |
+
embedded_options = EmbeddedOptions()
|
| 30 |
+
)
|
| 31 |
+
############################################################################
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
pickle_file_path = 'vectorstore.pkl'
|
| 35 |
+
import pickle
|
| 36 |
+
|
| 37 |
+
with open(pickle_file_path, 'rb') as file:
|
| 38 |
+
docs = pickle.load(file)
|
| 39 |
|
| 40 |
|
| 41 |
+
|
| 42 |
+
vectorstore = Weaviate.from_documents(
|
| 43 |
+
client = client,
|
| 44 |
+
documents = docs,
|
| 45 |
+
embedding = OpenAIEmbeddings(),
|
| 46 |
+
by_text = False
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
# Define vectorstore as retriever to enable semantic search
|
| 50 |
+
retriever = vectorstore.as_retriever()
|
| 51 |
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
############################################################################
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
|
| 56 |
+
# Create RAG chain
|
| 57 |
+
|
| 58 |
+
#from langchain.chat_models import ChatOpenAI
|
| 59 |
+
|
| 60 |
+
from langchain_openai import ChatOpenAI
|
| 61 |
+
|
| 62 |
+
from langchain.prompts import ChatPromptTemplate
|
| 63 |
+
from langchain.schema.runnable import RunnablePassthrough
|
| 64 |
+
from langchain.schema.output_parser import StrOutputParser
|
| 65 |
+
|
| 66 |
+
# Define LLM
|
| 67 |
+
llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0)
|
| 68 |
+
|
| 69 |
+
#llm = ChatOpenAI(model_name="gpt-4", temperature=0.15)
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
# Define prompt template / context
|
| 73 |
+
template = """You are a lawyer responding to creditors questions.
|
| 74 |
+
Use the following pieces of retrieved context to answer the question.
|
| 75 |
+
If you don't know the answer, just say that you don't know and add a funny joke.
|
| 76 |
+
Keep the answer concise.
|
| 77 |
+
Question: {question}
|
| 78 |
+
Context: {context}
|
| 79 |
+
Answer:
|
| 80 |
+
"""
|
| 81 |
+
|
| 82 |
+
prompt = ChatPromptTemplate.from_template(template)
|
| 83 |
+
|
| 84 |
+
# Setup RAG pipeline
|
| 85 |
+
rag_chain = (
|
| 86 |
+
{"context": retriever, "question": RunnablePassthrough()}
|
| 87 |
+
| prompt
|
| 88 |
+
| llm
|
| 89 |
+
| StrOutputParser()
|
| 90 |
+
)
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
+
st.session_state.retriever = retriever
|
| 94 |
+
st.session_state.retriever = rag_chain
|
| 95 |
|
| 96 |
+
else:
|
| 97 |
+
retriever = st.session_state.retriever
|
| 98 |
+
rag_chain = st.session_state.retriever
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
+
em = "aaa"
|
| 102 |
except Exception as e:
|
| 103 |
em = "bbb" + str(e)
|