Spaces:
Runtime error
Runtime error
ajaypandey1585
commited on
Commit
•
c877b01
1
Parent(s):
dcce908
main
Browse files
app.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#OpenAI(api_key='sk-proj-PxCAkOqCTsVhVWTJSMKJT3BlbkFJz0J48QsSGmrt9Qjud2Sl')
|
2 |
+
#embedding_function = OpenAIEmbeddingFunction(api_key='sk-proj-PxCAkOqCTsVhVWTJSMKJT3BlbkFJz0J48QsSGmrt9Qjud2Sl', model_name='text-embedding-3-small')
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
import os
|
5 |
+
from openai import OpenAI
|
6 |
+
|
7 |
+
client = OpenAI(api_key='XX')
|
8 |
+
import pprint
|
9 |
+
from halo import Halo
|
10 |
+
import chromadb
|
11 |
+
from chromadb.utils.embedding_functions import OpenAIEmbeddingFunction
|
12 |
+
|
13 |
+
|
14 |
+
# Load environment variables
|
15 |
+
load_dotenv()
|
16 |
+
pp = pprint.PrettyPrinter(indent=4)
|
17 |
+
|
18 |
+
def generate_response(messages):
|
19 |
+
spinner = Halo(text='Loading...', spinner='dots')
|
20 |
+
spinner.start()
|
21 |
+
model_name = 'gpt-3.5-turbo-0301'
|
22 |
+
response = client.chat.completions.create(model=model_name,
|
23 |
+
messages=messages,
|
24 |
+
temperature=0.5,
|
25 |
+
max_tokens=250)
|
26 |
+
|
27 |
+
spinner.stop()
|
28 |
+
print("Request:")
|
29 |
+
pp.pprint(messages)
|
30 |
+
|
31 |
+
print(f"Completion tokens: {response.usage.completion_tokens}, Prompt tokens: {response.usage.prompt_tokens}, Total tokens: {response.usage.total_tokens}")
|
32 |
+
return response.choices[0].message
|
33 |
+
|
34 |
+
def main():
|
35 |
+
chroma_client = chromadb.Client()
|
36 |
+
embedding_function = OpenAIEmbeddingFunction(api_key='XX', model_name='text-embedding-3-small')
|
37 |
+
collection = chroma_client.create_collection(name="conversations", embedding_function=embedding_function)
|
38 |
+
current_id = 0
|
39 |
+
while True:
|
40 |
+
chat_history = []
|
41 |
+
chat_metadata = []
|
42 |
+
history_ids = []
|
43 |
+
|
44 |
+
messages=[
|
45 |
+
{"role": "system", "content": "You are a kind and wise wizard"}
|
46 |
+
]
|
47 |
+
input_text = input("You: ")
|
48 |
+
if input_text.lower() == "quit":
|
49 |
+
break
|
50 |
+
|
51 |
+
results = collection.query(
|
52 |
+
query_texts=[input_text],
|
53 |
+
where={"role": "assistant"},
|
54 |
+
n_results=2
|
55 |
+
)
|
56 |
+
|
57 |
+
# append the query result into the messages
|
58 |
+
for res in results['documents'][0]:
|
59 |
+
messages.append({"role": "user", "content": f"previous chat: {res}"})
|
60 |
+
|
61 |
+
# append user input at the end of conversation chain
|
62 |
+
messages.append({"role": "user", "content": input_text})
|
63 |
+
response = generate_response(messages)
|
64 |
+
|
65 |
+
chat_metadata.append({"role":"user"})
|
66 |
+
chat_history.append(input_text)
|
67 |
+
chat_metadata.append({"role":"assistant"})
|
68 |
+
chat_history.append(response.content)
|
69 |
+
current_id += 1
|
70 |
+
history_ids.append(f"id_{current_id}")
|
71 |
+
current_id += 1
|
72 |
+
history_ids.append(f"id_{current_id}")
|
73 |
+
collection.add(
|
74 |
+
documents=chat_history,
|
75 |
+
metadatas=chat_metadata,
|
76 |
+
ids=history_ids
|
77 |
+
)
|
78 |
+
print(f"Wizard: {response.content}")
|
79 |
+
|
80 |
+
if __name__ == "__main__":
|
81 |
+
main()
|