forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcurrent.py
More file actions
191 lines (156 loc) · 5.87 KB
/
Copy pathcurrent.py
File metadata and controls
191 lines (156 loc) · 5.87 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
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
import os
import uuid
from datetime import datetime, timezone
from typing import Any, List, cast
import numpy as np
import umap
from plotly.subplots import make_subplots # type: ignore[reportUnknownVariableType] # plotly partially typed
from _shared import *
from models.chat import Message, MessageSender, MessageType
from models.conversation import Conversation
def _get_mesage(text: str, sender: str) -> Message:
return Message(
id=str(uuid.uuid4()),
text=text,
created_at=datetime.now(timezone.utc),
sender=MessageSender(sender),
type=MessageType.text,
)
conversation = [
_get_mesage('Hi', 'human'),
_get_mesage('Hi, how can I help you today?', 'ai'),
_get_mesage('Have I learned about business, and entreprenurship?', 'human'),
]
def get_data(topics: List[str], top_k: int = 1000) -> dict[str, List[Any]]:
memories = get_memories()
memories = {memory['id']: memory for memory in memories}
all_vectors = query_vectors('', uid, k=top_k)
all_vectors_dict: dict[str, dict[str, Any]] = {mid: {'vector': vector, 'topics': []} for mid, vector in all_vectors}
for topic in topics:
vectors = query_vectors(topic, uid, k=5)
for mid, _vector in vectors:
if mid in all_vectors_dict:
all_vectors_dict[mid]['topics'].append(topic)
result: dict[str, List[Any]] = {}
for mid, data in all_vectors_dict.items():
memory = memories.get(mid)
if not memory:
continue
result[mid] = [
memory['structured']['title'],
data['vector'],
data['topics'],
]
return result
def get_markers(
data: dict[str, List[Any]],
data_points: Any,
color: str,
name: str,
show_top: int | None = None,
) -> Any:
if show_top:
data_items = list(data.items())[:show_top]
data_points = data_points[:show_top]
else:
data_items = list(data.items())
return go.Scatter(
x=data_points[:, 0],
y=data_points[:, 1],
mode='markers',
marker=dict(size=8, opacity=0.7, color=color),
text=[f"Title: {item[1][0]}<br>Topics: {', '.join(item[1][2])}" for item in data_items],
hoverinfo='text',
name=name,
)
def get_data2(topics: List[str], retrieved_memories: List[Conversation]) -> dict[str, List[Any]]:
# print('get_data2', len(topics), topics)
# print('retrieved_memories', len(retrieved_memories))
memories = get_memories()
memories = {memory['id']: memory for memory in memories}
all_vectors = query_vectors('', uid, k=1000)
all_vectors_dict: dict[str, dict[str, Any]] = {mid: {'vector': vector, 'topics': []} for mid, vector in all_vectors}
result: dict[str, List[Any]] = {}
retrieved_memories_id = {memory.id for memory in retrieved_memories}
for mid, data in all_vectors_dict.items():
memory = memories.get(mid)
if not memory:
continue
result[mid] = [
memory['structured']['title'],
data['vector'],
[] if memory['id'] not in retrieved_memories_id else topics,
]
return result
def generate_visualization(
topics: List[str],
memories: List[Conversation] | None = None,
file_path: str = 'embedding_visualization_multi_topic.html',
) -> None:
# context: Tuple = determine_requires_context(conversation)
# if not context or not context[0]:
# print('No context is needed')
# return
# topics = context[0]
# topics = ['Business', 'Entrepreneurship', 'Failures']
print('topics', topics)
os.makedirs('visualizations/', exist_ok=True)
file_path = os.path.join('visualizations/', file_path)
if memories is not None:
data = get_data2(topics, memories)
else:
data = get_data(topics)
# print('data', len(data))
embedding_values = [item[1] for item in data.values()]
if not embedding_values:
return
all_embeddings = cast(Any, np.array(embedding_values))
if all_embeddings.ndim != 2:
return
topic_points: Any = []
if topics:
topic_embeddings = [openai_embeddings.embed_query(topic) for topic in topics]
all_embeddings = cast(Any, np.vstack([all_embeddings] + topic_embeddings))
if all_embeddings.shape[0] < 3:
return
umap_transform = cast(
Any,
umap.UMAP(
init='random' if all_embeddings.shape[0] == 3 else 'spectral',
n_neighbors=min(15, all_embeddings.shape[0] - 1),
n_components=2,
random_state=0,
transform_seed=0,
),
)
umap_embeddings = umap_transform.fit_transform(all_embeddings)
if topics:
data_points = umap_embeddings[: -len(topics)]
topic_points = umap_embeddings[-len(topics) :]
else:
data_points = umap_embeddings
fig: Any = make_subplots(rows=1, cols=1)
colors = ['blue', 'green', 'orange', 'purple', 'cyan', 'magenta']
# Add all vectors
fig.add_trace(get_markers(data, data_points, 'gray', 'All Vectors'))
# Add vectors for each topic
for i, topic in enumerate(topics):
color = colors[i % len(colors)]
topic_data = {mid: item for mid, item in data.items() if topic in item[2]}
topic_data_points = cast(
Any, np.array([data_points[list(data.keys()).index(mid)] for mid in topic_data.keys()])
)
fig.add_trace(get_markers(topic_data, topic_data_points, color, f'Top 5 - {topic}'))
fig.add_trace(get_query_marker(topic_points[i], topic))
fig.update_layout(
title='Embedding Visualization for Multiple Topics',
xaxis_title='UMAP Dimension 1',
yaxis_title='UMAP Dimension 2',
width=800,
height=600,
showlegend=True,
hovermode='closest',
)
generate_html_visualization(fig, file_name=file_path)
if __name__ == '__main__':
generate_visualization([])