-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathusing_spacy_embed.py
67 lines (53 loc) · 1.88 KB
/
using_spacy_embed.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
import spacy
import chromadb
import numpy as np
import ollama
import chromadb
client = chromadb.Client()
nlp = spacy.load('en_core_web_lg')
def read_text_from_file(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
text = file.read()
return text
def create_vector_db_from_text(file_path):
text = read_text_from_file(file_path)
serialized_convo = f"document_text: {text}"
vector_db_name = "text_conversations"
try:
client.delete_collection(name=vector_db_name)
except ValueError:
pass
vector_db = client.create_collection(name=vector_db_name)
doc = nlp(serialized_convo)
embedding = doc.vector
vector_db.add(
ids=['1'],
embeddings=[embedding.tolist()],
documents=[serialized_convo]
)
def stream_response(prompt):
convo.append({'role': 'user', 'content': prompt})
response = ''
stream = ollama.chat(model='tinyllama:latest ', messages=convo, stream=True)
print('\nASSISTANT:\n')
for chunk in stream:
content = chunk['message']['content']
response += content
print(content, end='', flush=True)
print('\n')
convo.append({'role': 'assistant', 'content': response})
def retrieve_embeddings(prompt):
doc = nlp(prompt)
prompt_embedding = doc.vector
vector_db = client.get_collection(name='text_conversations')
results = vector_db.query(query_embeddings=[prompt_embedding.tolist()], n_results=1)
best_embedding = results['documents'][0][0]
return best_embedding
file_path = r"C:\Users\Yatharth\Desktop\desktop1\AI\isro_hack\hypothetical_flood_Data.txt"
create_vector_db_from_text(file_path)
convo = []
while True:
user_prompt = input('USER:\n')
context = retrieve_embeddings(prompt=user_prompt)
enhanced_prompt = f'USER PROMPT: {user_prompt} \nCONTEXT FROM EMBEDDINGS: {context}'
stream_response(enhanced_prompt)