File size: 5,582 Bytes
1871536
 
 
f5ce1a8
1871536
e84a43c
 
 
48de81e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e84a43c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48de81e
e84a43c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import streamlit as st
import requests
import json
import os
import pandas as pd
from sentence_transformers import CrossEncoder
import numpy as np


# Credentials ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

corpus_id = os.environ['VECTARA_CORPUS_ID']
customer_id = os.environ['VECTARA_CUSTOMER_ID']
api_key = os.environ['VECTARA_API_KEY']

"""
        "api_key": os.environ.get("VECTARA_API_KEY", ""),
        "customer_id": os.environ.get("VECTARA_CUSTOMER_ID", ""),
        "corpus_id": os.environ.get("VECTARA_CORPUS_ID", ""),      

"""

# Get Data +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


def get_post_headers() -> dict:
    """Returns headers that should be attached to each post request."""
    return {
        "x-api-key": api_key,
        "customer-id": customer_id,
        "Content-Type": "application/json",
    }

def query_vectara(query: str, filter_str="", lambda_val=0.0) -> str:
    corpus_key = {
        "customerId": customer_id,
        "corpusId": corpus_id,
        "lexicalInterpolationConfig": {"lambda": lambda_val},
    }
    if filter_str:
        corpus_key["metadataFilter"] = filter_str

    data = {
        "query": [
            {
                "query": query,
                "start": 0,
                "numResults": 10,
                "contextConfig": {
                    "sentencesBefore": 2,
                    "sentencesAfter": 2
                },
                "corpusKey": [corpus_key],
                "summary": [
                    {
                        "responseLang": "eng",
                        "maxSummarizedResults": 5,
                        "summarizerPromptName": "vectara-summary-ext-v1.2.0"
                    },
                ]                    
            }
        ]
    }

    response = requests.post(
        headers=get_post_headers(),
        url="https://api.vectara.io/v1/query",
        data=json.dumps(data),
        timeout=30,
    )

    if response.status_code != 200:
        st.error(f"Query failed (code {response.status_code}, reason {response.reason}, details {response.text})")
        return ""

    result = response.json()

    answer = result["responseSet"][0]["summary"][0]["text"]
    return re.sub(r'\[\d+(,\d+){0,5}\]', '', answer)

    

# Streamlit UI
st.title('Vectara Query Interface')

# User input for query
user_query = st.text_input("Enter your query:", "")

# Advanced options
st.sidebar.header("Advanced Options")
filter_str = st.sidebar.text_input("Filter String:", "")
lambda_val = st.sidebar.slider("Lambda Value:", min_value=0.0, max_value=1.0, value=0.0)

if st.button('Search'):
    if user_query:
        with st.spinner('Querying Vectara...'):
            output = query_vectara(user_query, filter_str, lambda_val)
            st.markdown("## Result")
            st.write(output)
    else:
        st.error("Please enter a query to search.")






# Initialize the HHEM model +++++++++++++++++++++++++++++++++++++++++++++++
model = CrossEncoder('vectara/hallucination_evaluation_model')

# Function to compute HHEM scores
def compute_hhem_scores(texts, summary):
    pairs = [[text, summary] for text in texts]
    scores = model.predict(pairs)
    return scores

# Define the Vectara query function
def vectara_query(query: str, config: dict):
    corpus_key = [{
        "customerId": config["customer_id"],
        "corpusId": config["corpus_id"],
        "lexicalInterpolationConfig": {"lambda": config.get("lambda_val", 0.5)},
    }]
    data = {
        "query": [{
            "query": query,
            "start": 0,
            "numResults": config.get("top_k", 10),
            "contextConfig": {
                "sentencesBefore": 2,
                "sentencesAfter": 2,
            },
            "corpusKey": corpus_key,
            "summary": [{
                "responseLang": "eng",
                "maxSummarizedResults": 5,
            }]
        }]
    }

    headers = {
        "x-api-key": config["api_key"],
        "customer-id": config["customer_id"],
        "Content-Type": "application/json",
    }
    response = requests.post(
        headers=headers,
        url="https://api.vectara.io/v1/query",
        data=json.dumps(data),
    )
    if response.status_code != 200:
        st.error(f"Query failed (code {response.status_code}, reason {response.reason}, details {response.text})")
        return [], ""

    result = response.json()
    responses = result["responseSet"][0]["response"]
    summary = result["responseSet"][0]["summary"][0]["text"]

    res = [[r['text'], r['score']] for r in responses]
    return res, summary

# Streamlit UI setup
st.title("Vectara Content Query Interface")

# User inputs
query = st.text_input("Enter your query here", "")
lambda_val = st.slider("Lambda Value", min_value=0.0, max_value=1.0, value=0.5)
top_k = st.number_input("Top K Results", min_value=1, max_value=50, value=10)

if st.button("Query Vectara"):
    config = {

        "lambda_val": lambda_val,
        "top_k": top_k,
    }

    results, summary = vectara_query(query, config)

    if results:
        st.subheader("Summary")
        st.write(summary)
        
        st.subheader("Top Results")
        
        # Extract texts from results
        texts = [r[0] for r in results[:5]]
        
        # Compute HHEM scores
        scores = compute_hhem_scores(texts, summary)
        
        # Prepare and display the dataframe
        df = pd.DataFrame({'Fact': texts, 'HHEM Score': scores})
        st.dataframe(df)
    else:
        st.write("No results found.")