-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
115 lines (74 loc) · 2.08 KB
/
Copy pathcli.py
File metadata and controls
115 lines (74 loc) · 2.08 KB
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
import uuid
import requests
import questionary
BASE_URL = "http://localhost:5000"
def ask_question(question):
try:
response = requests.post(
f"{BASE_URL}/question",
json={"question": question},
timeout=120,
)
response.raise_for_status()
return response.json()
except requests.RequestException as e:
print(f"\nRequest failed: {e}")
return None
def send_feedback(conversation_id, feedback):
response = requests.post(
f"{BASE_URL}/feedback",
json={
"conversation_id": conversation_id,
"feedback": feedback,
},
)
return response.status_code
def main():
print("===================================")
print(" Harvard CS249R Study Assistant")
print("===================================")
while True:
question = questionary.text(
"Ask your question:"
).ask()
if not question:
break
result = ask_question(question)
if not result:
continue
print("\nAnswer:\n")
print(result.get("answer"))
sources = result.get("sources", [])
if sources:
print("\nSources:")
for source in sources:
print(f"- {source}")
conversation_id = result.get(
"conversation_id",
str(uuid.uuid4())
)
feedback = questionary.select(
"Rate this answer:",
choices=[
"👍 Good",
"👎 Bad",
"Skip",
],
).ask()
if feedback != "Skip":
value = 1 if feedback == "👍 Good" else -1
status = send_feedback(
conversation_id,
value,
)
print(
f"Feedback saved (HTTP {status})"
)
again = questionary.confirm(
"Ask another question?"
).ask()
if not again:
break
print("Goodbye!")
if __name__ == "__main__":
main()