-
Notifications
You must be signed in to change notification settings - Fork 3
/
vectara.py
81 lines (63 loc) · 2.06 KB
/
vectara.py
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
import requests
import json
import streamlit as st
customerID = "566695243"
api_key = st.secrets["VECTARA_API_KEY"]
# Step 1 Reset Corpus
def ResetCorpus():
url = "https://api.vectara.io/v1/reset-corpus"
payload = json.dumps({"corpusId": 15})
headers = {
"Content-Type": "application/json",
"Accept": "application/json",
"x-api-key": api_key,
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
# Step 2 Add Transcription Txt to Corpus
def AddVideoTranscription():
url = "https://api.vectara.io/v1/upload?c=566695243&o=15"
payload = {}
files = [
("file", ("video_transcription", open("video_transcription.txt", "rb"), "application/txt"))
]
headers = {
"customer-id": "566695243",
"Accept": "application/json",
"x-api-key": api_key,
}
response = requests.request("POST", url, headers=headers, data=payload, files=files)
print(response.text)
# Step 3 Ask Question
def askQuestion(prompt):
url = "https://api.vectara.io/v1/query"
payload = json.dumps(
{
"query": [
{
"query": prompt,
"start": 0,
"numResults": 3,
"contextConfig": {
"sentences_before": 3,
"sentences_after": 3,
"start_tag": "<b>",
"end_tag": "</b>",
},
"corpusKey": [{"corpus_id": 15}],
"summary": [{"max_summarized_results": 10, "response_lang": "en"}],
}
]
}
)
headers = {
"Content-Type": "application/json",
"Accept": "application/json",
"customer-id": "566695243",
"x-api-key": api_key,
}
response = requests.request("POST", url, headers=headers, data=payload)
response_data = response.json()
result = response_data["responseSet"][0]["summary"][0]
RawAnswer = result["text"]
return RawAnswer