Spaces:
Runtime error
Runtime error
eaglelandsonce
commited on
Commit
•
48de81e
1
Parent(s):
ae23195
Update app.py
Browse files
app.py
CHANGED
@@ -6,7 +6,106 @@ import pandas as pd
|
|
6 |
from sentence_transformers import CrossEncoder
|
7 |
import numpy as np
|
8 |
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
model = CrossEncoder('vectara/hallucination_evaluation_model')
|
11 |
|
12 |
# Function to compute HHEM scores
|
@@ -70,9 +169,7 @@ top_k = st.number_input("Top K Results", min_value=1, max_value=50, value=10)
|
|
70 |
|
71 |
if st.button("Query Vectara"):
|
72 |
config = {
|
73 |
-
|
74 |
-
"customer_id": os.environ.get("VECTARA_CUSTOMER_ID", ""),
|
75 |
-
"corpus_id": os.environ.get("VECTARA_CORPUS_ID", ""),
|
76 |
"lambda_val": lambda_val,
|
77 |
"top_k": top_k,
|
78 |
}
|
|
|
6 |
from sentence_transformers import CrossEncoder
|
7 |
import numpy as np
|
8 |
|
9 |
+
|
10 |
+
# Credentials ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
11 |
+
|
12 |
+
corpus_id = os.environ['VECTARA_CORPUS_ID']
|
13 |
+
customer_id = os.environ['VECTARA_CUSTOMER_ID']
|
14 |
+
api_key = os.environ['VECTARA_API_KEY']
|
15 |
+
|
16 |
+
"""
|
17 |
+
"api_key": os.environ.get("VECTARA_API_KEY", ""),
|
18 |
+
"customer_id": os.environ.get("VECTARA_CUSTOMER_ID", ""),
|
19 |
+
"corpus_id": os.environ.get("VECTARA_CORPUS_ID", ""),
|
20 |
+
|
21 |
+
"""
|
22 |
+
|
23 |
+
# Get Data +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
24 |
+
|
25 |
+
|
26 |
+
def get_post_headers() -> dict:
|
27 |
+
"""Returns headers that should be attached to each post request."""
|
28 |
+
return {
|
29 |
+
"x-api-key": api_key,
|
30 |
+
"customer-id": customer_id,
|
31 |
+
"Content-Type": "application/json",
|
32 |
+
}
|
33 |
+
|
34 |
+
def query_vectara(query: str, filter_str="", lambda_val=0.0) -> str:
|
35 |
+
corpus_key = {
|
36 |
+
"customerId": customer_id,
|
37 |
+
"corpusId": corpus_id,
|
38 |
+
"lexicalInterpolationConfig": {"lambda": lambda_val},
|
39 |
+
}
|
40 |
+
if filter_str:
|
41 |
+
corpus_key["metadataFilter"] = filter_str
|
42 |
+
|
43 |
+
data = {
|
44 |
+
"query": [
|
45 |
+
{
|
46 |
+
"query": query,
|
47 |
+
"start": 0,
|
48 |
+
"numResults": 10,
|
49 |
+
"contextConfig": {
|
50 |
+
"sentencesBefore": 2,
|
51 |
+
"sentencesAfter": 2
|
52 |
+
},
|
53 |
+
"corpusKey": [corpus_key],
|
54 |
+
"summary": [
|
55 |
+
{
|
56 |
+
"responseLang": "eng",
|
57 |
+
"maxSummarizedResults": 5,
|
58 |
+
"summarizerPromptName": "vectara-summary-ext-v1.2.0"
|
59 |
+
},
|
60 |
+
]
|
61 |
+
}
|
62 |
+
]
|
63 |
+
}
|
64 |
+
|
65 |
+
response = requests.post(
|
66 |
+
headers=get_post_headers(),
|
67 |
+
url="https://api.vectara.io/v1/query",
|
68 |
+
data=json.dumps(data),
|
69 |
+
timeout=30,
|
70 |
+
)
|
71 |
+
|
72 |
+
if response.status_code != 200:
|
73 |
+
st.error(f"Query failed (code {response.status_code}, reason {response.reason}, details {response.text})")
|
74 |
+
return ""
|
75 |
+
|
76 |
+
result = response.json()
|
77 |
+
|
78 |
+
answer = result["responseSet"][0]["summary"][0]["text"]
|
79 |
+
return re.sub(r'\[\d+(,\d+){0,5}\]', '', answer)
|
80 |
+
|
81 |
+
|
82 |
+
|
83 |
+
# Streamlit UI
|
84 |
+
st.title('Vectara Query Interface')
|
85 |
+
|
86 |
+
# User input for query
|
87 |
+
user_query = st.text_input("Enter your query:", "")
|
88 |
+
|
89 |
+
# Advanced options
|
90 |
+
st.sidebar.header("Advanced Options")
|
91 |
+
filter_str = st.sidebar.text_input("Filter String:", "")
|
92 |
+
lambda_val = st.sidebar.slider("Lambda Value:", min_value=0.0, max_value=1.0, value=0.0)
|
93 |
+
|
94 |
+
if st.button('Search'):
|
95 |
+
if user_query:
|
96 |
+
with st.spinner('Querying Vectara...'):
|
97 |
+
output = query_vectara(user_query, filter_str, lambda_val)
|
98 |
+
st.markdown("## Result")
|
99 |
+
st.write(output)
|
100 |
+
else:
|
101 |
+
st.error("Please enter a query to search.")
|
102 |
+
|
103 |
+
|
104 |
+
|
105 |
+
|
106 |
+
|
107 |
+
|
108 |
+
# Initialize the HHEM model +++++++++++++++++++++++++++++++++++++++++++++++
|
109 |
model = CrossEncoder('vectara/hallucination_evaluation_model')
|
110 |
|
111 |
# Function to compute HHEM scores
|
|
|
169 |
|
170 |
if st.button("Query Vectara"):
|
171 |
config = {
|
172 |
+
|
|
|
|
|
173 |
"lambda_val": lambda_val,
|
174 |
"top_k": top_k,
|
175 |
}
|