-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
113 lines (90 loc) · 3.54 KB
/
main.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
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
# Importing required modules
import json
import typing
import openai
import header
from chat import chat_with_openai, grab_ai_message
from embedding import gen_embedding
import qdrant
# Initialize conversation history
conversation_history = []
def search():
print("[*] Starting up the search module...")
while True:
search_text = input("[?] Enter your query: ")
embedding = gen_embedding(search_text)
print("[*] Searching the database...")
print(f"[+] Search Results Found: ")
search_results = qdrant.search_similar_vectors(embedding)
for scored_point in search_results:
print("Score:", scored_point.score)
print("Message: ")
print(scored_point.payload['message'])
r = input("[?] Search again? (yes/no): ")
if r.lower() == "no":
break
def conversation():
print("[*] Starting conversation module...")
counter = 0
while True:
try:
user_input = input("👤: ")
if user_input.lower() == "quit":
break
embedding = gen_embedding(user_input)
qdrant.create_point(embedding, )
print("[*] Communicating with OpenAI...")
response = chat_with_openai(user_input, conversation_history)
print(response)
ai_message = grab_ai_message(response, conversation_history)
print(f"💻: {ai_message}")
print("[*] Creating embedding...")
print("[*] Storing conversation point...")
qdrant.create_point(embedding, response)
counter += 1
except Exception as e:
print("OpenAI has raised an Error: ", e)
def load_chat():
print("[*] Loading chat module...")
questions = [
"Can you explain the theory of general relativity in simple terms?",
"Could you simplify the concept of quantum mechanics for me?",
"How would you describe the process of evolution to a child?",
"Can you break down how a computer works for someone who doesn't know about technology?",
"What's the easiest way to understand the stock market?"
]
print("[*] Initializing system...")
conversation_history.append({"role": "system", "content": "You are a helpful assistant."})
counter = 0
print("[*] Communicating with OpenAI...")
try:
for question in questions:
response = chat_with_openai(question, conversation_history)
ai_message = grab_ai_message(response, conversation_history)
embedding = gen_embedding(ai_message)
qdrant.temp_create_point(embedding, counter, response.openai_id, ai_message)
counter += 1
print(f"[+] Question {counter} | {len(questions)}: Completed")
print("[+] Loading Completed!")
except openai.error.OpenAIError as e:
print(f"OpenAI Loading Error: {e}")
def load_chat_from_file():
file_name = "testfile.json"
conversation: typing.List[typing.Dict[str, str]] = []
with open(file_name, 'r') as f:
conversation = json.load(open(file_name))
test = qdrant.create_points_batch(conversation)
def main():
qdrant.create_collection()
header.header_print()
user_choice = input("[?] Enter your choice:\n\n[1] Load pre-existing questions\n[2] Input your own "
"questions\n\nChoice: ")
if user_choice == "1":
load_chat()
elif user_choice == "2":
load_chat_from_file()
else:
print("[-] Invalid choice...")
search()
if __name__ == '__main__':
main()