Spaces:
Runtime error
Runtime error
1.1
Browse files
app.py
CHANGED
|
@@ -22,47 +22,41 @@ with st.sidebar:
|
|
| 22 |
corpus_id = st.text_input("Vectara Corpus ID", value=str(os.getenv("CORPUS_ID", "")))
|
| 23 |
openai_api_key = st.text_input("OpenAI API Key", value=os.getenv("OPENAI_API_KEY", ""))
|
| 24 |
submit_button = st.button("Submit")
|
| 25 |
-
|
| 26 |
keys_provided = all([customer_id, api_key, corpus_id, openai_api_key])
|
| 27 |
|
| 28 |
-
|
| 29 |
-
CUSTOMER_ID = customer_id
|
| 30 |
-
API_KEY = api_key
|
| 31 |
-
CORPUS_ID = int(corpus_id)
|
| 32 |
-
OPENAI_API_KEY = openai_api_key
|
| 33 |
|
| 34 |
-
|
| 35 |
-
def initialize_vectara():
|
| 36 |
-
vectara = Vectara(
|
| 37 |
vectara_customer_id=CUSTOMER_ID,
|
| 38 |
vectara_corpus_id=CORPUS_ID,
|
| 39 |
vectara_api_key=API_KEY
|
| 40 |
)
|
| 41 |
-
return vectara
|
| 42 |
-
|
| 43 |
-
vectara_client = initialize_vectara()
|
| 44 |
|
| 45 |
-
# Function to get knowledge content from Vectara
|
| 46 |
-
def get_knowledge_content(vectara, query, threshold=0.5):
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
)
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
return knowledge_content
|
| 55 |
-
|
| 56 |
-
# Prompt and response setup
|
| 57 |
-
prompt = PromptTemplate.from_template(
|
| 58 |
-
"""You are a professional and friendly Legal Consultant and you are helping a client with a legal issue. The client is asking you for advice on a legal issue. Just explain him in detail the answer and nothing else. This is the issue: {issue}
|
| 59 |
-
To assist him with his issue, you need to know the following information: {knowledge}
|
| 60 |
-
"""
|
| 61 |
-
)
|
| 62 |
-
runnable = prompt | ChatOpenAI(streaming=True, callbacks=[StreamingStdOutCallbackHandler()], openai_api_key=OPENAI_API_KEY) | StrOutputParser()
|
| 63 |
-
|
| 64 |
-
# Main Streamlit App
|
| 65 |
-
if keys_provided:
|
| 66 |
st.title("Legal Consultation Chat")
|
| 67 |
|
| 68 |
# Initialize chat history
|
|
@@ -81,8 +75,6 @@ if keys_provided:
|
|
| 81 |
st.markdown(user_input)
|
| 82 |
|
| 83 |
knowledge_content = get_knowledge_content(vectara_client, user_input)
|
| 84 |
-
print("__________________ Start of knowledge content __________________")
|
| 85 |
-
print(knowledge_content)
|
| 86 |
response = runnable.invoke({"knowledge": knowledge_content, "issue": user_input})
|
| 87 |
|
| 88 |
response_words = response.split()
|
|
|
|
| 22 |
corpus_id = st.text_input("Vectara Corpus ID", value=str(os.getenv("CORPUS_ID", "")))
|
| 23 |
openai_api_key = st.text_input("OpenAI API Key", value=os.getenv("OPENAI_API_KEY", ""))
|
| 24 |
submit_button = st.button("Submit")
|
| 25 |
+
|
| 26 |
keys_provided = all([customer_id, api_key, corpus_id, openai_api_key])
|
| 27 |
|
| 28 |
+
if keys_provided:
|
| 29 |
+
CUSTOMER_ID = customer_id
|
| 30 |
+
API_KEY = api_key
|
| 31 |
+
CORPUS_ID = int(corpus_id)
|
| 32 |
+
OPENAI_API_KEY = openai_api_key
|
| 33 |
|
| 34 |
+
vectara_client = Vectara(
|
|
|
|
|
|
|
| 35 |
vectara_customer_id=CUSTOMER_ID,
|
| 36 |
vectara_corpus_id=CORPUS_ID,
|
| 37 |
vectara_api_key=API_KEY
|
| 38 |
)
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
# Function to get knowledge content from Vectara
|
| 41 |
+
def get_knowledge_content(vectara, query, threshold=0.5):
|
| 42 |
+
found_docs = vectara.similarity_search_with_score(
|
| 43 |
+
query,
|
| 44 |
+
score_threshold=threshold,
|
| 45 |
+
)
|
| 46 |
+
knowledge_content = ""
|
| 47 |
+
for number, (score, doc) in enumerate(found_docs):
|
| 48 |
+
knowledge_content += f"Document {number}: {found_docs[number][0].page_content}\n"
|
| 49 |
+
return knowledge_content
|
| 50 |
+
|
| 51 |
+
# Prompt and response setup
|
| 52 |
+
prompt = PromptTemplate.from_template(
|
| 53 |
+
"""You are a professional and friendly Legal Consultant and you are helping a client with a legal issue. The client is asking you for advice on a legal issue. Just explain him in detail the answer and nothing else. This is the issue: {issue}
|
| 54 |
+
To assist him with his issue, you need to know the following information: {knowledge}
|
| 55 |
+
"""
|
| 56 |
)
|
| 57 |
+
runnable = prompt | ChatOpenAI(streaming=True, callbacks=[StreamingStdOutCallbackHandler()], openai_api_key=OPENAI_API_KEY) | StrOutputParser()
|
| 58 |
+
|
| 59 |
+
# Main Streamlit App
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
st.title("Legal Consultation Chat")
|
| 61 |
|
| 62 |
# Initialize chat history
|
|
|
|
| 75 |
st.markdown(user_input)
|
| 76 |
|
| 77 |
knowledge_content = get_knowledge_content(vectara_client, user_input)
|
|
|
|
|
|
|
| 78 |
response = runnable.invoke({"knowledge": knowledge_content, "issue": user_input})
|
| 79 |
|
| 80 |
response_words = response.split()
|