Spaces:
Sleeping
Sleeping
File size: 3,362 Bytes
b8685e9 b386bd9 65a8672 474d141 a6fcae6 fe51435 a6fcae6 474d141 a6fcae6 474d141 65a8672 a0c5b6d 474d141 a0c5b6d 474d141 b8685e9 474d141 a6fcae6 474d141 a6fcae6 474d141 c471578 474d141 a6fcae6 65a8672 b0b3ffc 0b23cfd 65a8672 b386bd9 474d141 0b23cfd b386bd9 | 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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
try:
st.session_state.retriever = ""
st.session_state.rag_chain = ""
global rag_chain, retriever, em
import requests
from langchain.document_loaders import TextLoader
from langchain.text_splitter import CharacterTextSplitter
import re
import requests
#import fitz # PyMuPDF
import inspect
def get_LN():
"""
Returns the line number in the caller's source code.
"""
# Get the current frame, then the caller's frame, and finally the line number
frame = inspect.currentframe()
caller_frame = frame.f_back
line_number = caller_frame.f_lineno
return str(line_number)
st.session_state.ln = get_LN()
os.environ["OPENAI_API_KEY"] = os.getenv('openkey')
org = os.getenv('openorg')
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import Weaviate
import weaviate
from weaviate.embedded import EmbeddedOptions
#from dotenv import load_dotenv,find_dotenv
# Load OpenAI API key from .env file
#load_dotenv(find_dotenv())
st.session_state.ln = get_LN()
# Setup vector database|
client = weaviate.Client(
embedded_options = EmbeddedOptions()
)
############################################################################
st.session_state.ln = get_LN()
pickle_file_path = 'vectorstore.pkl'
import pickle
with open(pickle_file_path, 'rb') as file:
docs = pickle.load(file)
st.session_state.ln = get_LN()
vectorstore = Weaviate.from_documents(
client = client,
documents = docs,
embedding = OpenAIEmbeddings(),
by_text = False
)
st.session_state.ln = get_LN()
# Define vectorstore as retriever to enable semantic search
retriever = vectorstore.as_retriever()
st.session_state.ln = get_LN()
############################################################################
# Create RAG chain
#from langchain.chat_models import ChatOpenAI
from langchain_openai import ChatOpenAI
from langchain.prompts import ChatPromptTemplate
from langchain.schema.runnable import RunnablePassthrough
from langchain.schema.output_parser import StrOutputParser
# Define LLM
llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0)
#llm = ChatOpenAI(model_name="gpt-4", temperature=0.15)
st.session_state.ln = get_LN()
# Define prompt template / context
template = """You are a lawyer responding to creditors questions.
Use the following pieces of retrieved context to answer the question.
If you don't know the answer, just say that you don't know and add a funny joke.
Keep the answer concise.
Question: {question}
Context: {context}
Answer:
"""
prompt = ChatPromptTemplate.from_template(template)
# Setup RAG pipeline
rag_chain = (
{"context": retriever, "question": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)
st.session_state.retriever = retriever
st.session_state.rag_chain = rag_chain
st.session_state.ln = get_LN()
em = "aaa"
st.session_state.started = True
except Exception as e:
em = "bbb " + str(e)
st.session_state.em = em
|