File size: 2,600 Bytes
2ddcbaa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import requests
import json
from langchain.vectorstores import Vectara
from sentence_transformers import CrossEncoder

# Input your API keys
vectara_customer_id = "3939498282"
vectara_corpus_id = 2
vectara_api_key = 'zqt_6s_5KqwCCxK5tosYGbpSie8n2-hO7LdlxBWUBA'

# Initialize Vectara
vectara_instance = Vectara(
    vectara_customer_id='3939498282',
    vectara_corpus_id=2,
    vectara_api_key='zqt_Y3kD9bueJq3QO5t_FISVQLmgTWMDhzgMgK9Isw',
)

# Model initialization
model = CrossEncoder('vectara/hallucination_evaluation_model')

# Streamlit app
st.title('RAG-Based App')

# Input message from the user
message = st.text_input('Enter your message')

# Button to trigger the processing
if st.button('Process'):
    # Processing logic
    corpus_key = [
        {
            "customerId": vectara_customer_id,
            "corpusId": vectara_corpus_id,
            "lexicalInterpolationConfig": {"lambda": 0.025},
        }
    ]
    data = {
        "query": [
            {
                "query": message,
                "start": 0,
                "numResults": 10,
                "contextConfig": {
                    "sentencesBefore": 2,
                    "sentencesAfter": 2,
                },
                "corpusKey": corpus_key,
                "summary": [
                    {
                        "responseLang": "eng",
                        "maxSummarizedResults": 5,
                    }
                ]
            }
        ]
    }

    headers = {
        "x-api-key": vectara_api_key,
        "customer-id": vectara_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("Query failed")
    else:
        result = response.json()
        responses = result["responseSet"][0]["response"]
        summary = result["responseSet"][0]["summary"][0]["text"]

        res = [[r['text'], r['score']] for r in responses]
        texts = [r[0] for r in res[:5]]
        scores = [model.predict([text, summary]) for text in texts]

        text_elements = []
        docs = vectara_instance.similarity_search(message)
        for source_idx, source_doc in enumerate(docs[:5]):
            source_name = f"Source {source_idx + 1}"
            text_elements.append(source_doc.page_content)

        ans = f"{summary}\n HHEM Scores: {scores}"
        st.text(ans)
        st.text("Sources:")
        for text in text_elements:
            st.text(text)