-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.py
229 lines (163 loc) · 7.33 KB
/
chat.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import os
from openai import OpenAI
import json
import re
from time import time,sleep
from uuid import uuid4
import datetime
import yaml
from pinecone import Pinecone, ServerlessSpec
with open('config2.yaml', 'r') as yaml_file:
config = yaml.safe_load(yaml_file)
mi_model = config["config"]["model"]["modelname"]
mi_model_emb = config["config"]["embedder"]["old"]
index_name = config["config"]["credentials"]["pinecone"]["indexname"]
def open_file(filepath):
with open(filepath, 'r', encoding='utf-8') as infile:
return infile.read()
def save_file(filepath, content):
#s.makedirs(os.path.dirname(filepath), exist_ok=True) no cambies esto, revisa el directorio antes
with open(filepath, 'w', encoding='utf-8') as outfile:
outfile.write(content)
def load_json(filepath):
with open(filepath, 'r', encoding='utf-8') as infile:
return json.load(infile)
def save_json(filepath, payload):
with open(filepath, 'w', encoding='utf-8') as outfile:
json.dump(payload, outfile, ensure_ascii=False, sort_keys=True, indent=2)
def timestamp_to_datetime(unix_time):
return datetime.datetime.fromtimestamp(unix_time).strftime("%A, %B %d, %Y at %I:%M%p %Z")
# Revisar esto https://huggingface.co/nomic-ai/nomic-embed-text-v1.5
# para RAG
def get_embedding(text, rol):
text = text.replace("\n", " ")
# las de sistema search_document:
# las de usuario search_query:
if rol == "USER":
return client.embeddings.create(input = ["search_query: "+text], model=mi_model_emb).data[0].embedding
elif rol == "SYSTEM":
return client.embeddings.create(input = ["search_document: "+text], model=mi_model_emb).data[0].embedding
else: #ASSISTANT
return client.embeddings.create(input = [text], model=mi_model_emb).data[0].embedding
client = OpenAI(
base_url=config["config"]["model"]["host"],
api_key=config["config"]["model"]["api_key"]
)
def text_completion(prompt, engine=mi_model):
max_retry = 5
retry = 0
#p#rompt = prompt.encode(encoding='ASCII',errors='ignore').decode()
#print("\n soy la prompt ey" + prompt +"\n")
while True:
try:
response = client.chat.completions.create(
messages=[
{
"role": "user",
"content": prompt,
}
],
model=engine,
)
text = response.choices[0].message.content
#text = response['choices'][0]['text'].strip()
#print("\n"+text+"\n")
text = re.sub('[\r\n]+', '\n', text)
text = re.sub('[\t ]+', ' ', text)
#filename = '%s_log.txt' % time()
#if not os.path.exists('./textos/logs'):
# os.makedirs('./textos/logs')
#save_file('./textos/logs/%s' % filename, prompt + '\n\n==========\n\n' + text)
return text
except Exception as oops:
retry += 1
if retry >= max_retry:
return "Model error: %s" % oops
print('Error communicating with model:', oops)
sleep(1)
def check_nexo(results):
result = list()
for m in results['matches']:
try:
info = load_json('./nexo/%s.json' % m['id']) #TODO cambie por nexo2
result.append(info)
except:
print("Parece que " + './nexo/%s.json' % m['id'] + " no esta" )
continue
#ordered = sorted(result, key=lambda d: d['time'], reverse=False) # sort them all chronologically
messages = [i['message'] for i in result]
return '\n'.join(messages).strip()
#TODO https://delicias.dia.fi.upm.es/wiki/index.php/Charlas
if __name__ == '__main__':
convo_length = 2 # se puede cambiar
#openai.api_key = open_file('./openaiapikey.txt')
pc = Pinecone(
api_key=config["config"]["credentials"]["pinecone"]["key"]
)
# index_name = "mispruebas-index"
# Now do stuff
if index_name not in pc.list_indexes().names():
pc.create_index(
name=index_name,
dimension=768,
metric="euclidean",
spec=ServerlessSpec(
cloud='aws',
region='us-east-1'
)
)
#pinecone.init(api_key=, environment='us-east1-gcp')
index = pc.Index(index_name)
unique_conv_id = str(uuid4())
prev_conv = ""
filename = unique_conv_id+'_log.txt'
#rint("Voy a guardar")
save_file('./textos/logs/%s' % filename, prev_conv)
#rint("He fgardar")
# index.upsert -> echarle un ojo
while True:
#### get user input, save it, vectorize it, save to pinecone
payload = list()
a = input('\n\nUSER: ')
if (a == "q"):
#save_json('./nexo/%s.json' % unique_id, metadata)
break
timestamp = time()
timestring = timestamp_to_datetime(timestamp)
message = '%s: %s - %s' % ('USER', timestring, a)
vector = get_embedding(message, "USER")
#unique_id = str(uuid4())
#metadata = {'speaker': 'USER', 'time': timestamp, 'message': message, 'timestring': timestring, 'uuid': unique_id}
#save_json('./nexo2/%s.json' % unique_id, metadata)
#payload.append((unique_id, vector))
#### search for relevant messages, and generate a response
results = index.query(vector=vector, top_k=convo_length)
wiki_data = check_nexo(results) # results should be a DICT with 'matches' which is a LIST of DICTS, with 'id'
#filename = unique_conv_id+'_log.txt'
if not os.path.exists('./textos/logs'):
os.makedirs('./textos/logs')
#prev_conv = open_file('./textos/logs/%s' % filename)
#save_file('./textos/logs/%s' % filename, prev_conv+"\n"+message)
prev_conv = open_file('./textos/logs/%s' % filename)
#print("\n\nSoy las conversacion previa, a ver que hay aquí \n \n")
#print(prev_conv)
#print("\n \n")
#print("\n\nSoy los datos, a ver que hay aquí \n \n")
#print(wiki_data)
#print("\n \n")
prompt = open_file('./textos/contexto.txt').replace('<<DATOS>>', wiki_data).replace('<<CONVERSACIÓN>>', prev_conv).replace('<<MENSAJE>>', a) #puede que sea demasiado largo, mirar
#print(prompt)
#### generate response, vectorize, save, etc
output = text_completion(prompt) #prompt
timestamp = time()
timestring = timestamp_to_datetime(timestamp)
messageBot = '%s: %s - %s' % ('ABIGAIL', timestring, output)
print('\n\nABIGAIL: %s' % output)
save_file('./textos/logs/%s' % filename, prev_conv+"\n"+message+"\n"+messageBot)
#message = output
#vector = get_embedding(message, "ASSISTANT")
#unique_id = str(uuid4())
#metadata = {'speaker': 'ABIGAIL', 'time': timestamp, 'message': message, 'timestring': timestring, 'uuid': unique_id}
#save_json('./nexo2/%s.json' % unique_id, metadata)
#payload.append((unique_id, vector))
#index.upsert(payload)