forked from Filimoa/open-parse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsemantic_transforms.py
More file actions
190 lines (156 loc) · 5.89 KB
/
Copy pathsemantic_transforms.py
File metadata and controls
190 lines (156 loc) · 5.89 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
import json
from typing import List, Literal, Dict, Union
from urllib.parse import urlparse
from http.client import HTTPConnection, HTTPSConnection
import numpy as np
from openparse.schemas import Node
from .basic_transforms import ProcessingStep
EmbeddingModel = Literal[
"text-embedding-3-large", "text-embedding-3-small", "text-embedding-ada-002"
]
def cosine_similarity(
a: Union[np.ndarray, List[float]], b: Union[np.ndarray, List[float]]
) -> float:
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
class OpenAIEmbeddings:
def __init__(
self,
model: EmbeddingModel,
api_key: str,
batch_size: int = 256,
):
"""
Used to generate embeddings for Nodes.
Args:
api_key (str): Your OpenAI API key.
model (str): The embedding model to use.
batch_size (int): The number of texts to process in each api call.
"""
self.api_key = api_key
self.model = model
self.batch_size = batch_size
self.client = self._create_client()
def embed_many(self, texts: List[str]) -> List[List[float]]:
"""
Generate embeddings for a list of texts in batches.
Args:
texts (list[str]): The list of texts to embed.
batch_size (int): The number of texts to process in each batch.
Returns:
List[List[float]]: A list of embeddings.
"""
res = []
for i in range(0, len(texts), self.batch_size):
batch_texts = texts[i : i + self.batch_size]
api_resp = self.client.embeddings.create(
input=batch_texts, model=self.model
)
batch_res = [val.embedding for val in api_resp.data]
res.extend(batch_res)
return res
def _create_client(self):
try:
from openai import OpenAI
except ImportError:
raise ImportError(
"You need to install the openai package to use this feature."
)
return OpenAI(api_key=self.api_key)
class OllamaEmbeddings:
"""
Use local models via ollama for calculating embeddings. Uses the REST API
https://github.com/ollama/ollama/blob/main/docs/api.md.
* nomic-embed-text
* mxbai-embed-large
"""
def __init__(
self,
url: str = "http://localhost:11434/",
model: str = "mxbai-embed-large",
batch_size: int = 256,
):
"""
Used to generate embeddings for Nodes.
"""
self.url = url
self.model = model
self.batch_size = batch_size
def embed_many(self, texts: List[str]) -> List[List[float]]:
"""
Generate embeddings for a list of texts. Support for batches coming
soon, cf. https://ollama.com/blog/embedding-models
Args:
texts (list[str]): The list of texts to embed.
batch_size (int): The number of texts to process in each batch.
Returns:
List[List[float]]: A list of embeddings.
"""
conn = self._create_conn()
res = []
for i in range(0, len(texts), self.batch_size):
batch_texts = texts[i : i + self.batch_size]
for text in batch_texts:
params = json.dumps({"model": self.model, "prompt": text})
headers = {"Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"}
conn.request("POST", "/api/embeddings", params, headers)
response = conn.getresponse()
if response.status != 200:
raise RuntimeError(
"embeddings request failed: {} {}".format(
response.status, response.reason
)
)
doc = json.loads(response.read())
res.extend(doc["embedding"])
conn.close()
return res
def _create_conn(self):
parsed = urlparse(self.url)
if parsed.scheme == "https":
return HTTPSConnection(parsed.hostname, parsed.port)
else:
return HTTPConnection(parsed.hostname, parsed.port)
class CombineNodesSemantically(ProcessingStep):
"""
Combines nodes that are semantically related.
"""
def __init__(
self,
embedding_client: OpenAIEmbeddings,
min_similarity: float,
max_tokens: int,
):
self.embedding_client = embedding_client
self.min_similarity = min_similarity
self.max_tokens = max_tokens
def process(self, nodes: List[Node]) -> List[Node]:
modified = True
while modified:
modified = False
nodes = sorted(nodes)
embeddings = self.embedding_client.embed_many([node.text for node in nodes])
i = 0
while i < len(nodes) - 1:
current_embedding = embeddings[i]
next_embedding = embeddings[i + 1]
similarity = cosine_similarity(current_embedding, next_embedding)
is_within_token_limit = (
nodes[i].tokens + nodes[i + 1].tokens <= self.max_tokens
)
if similarity >= self.min_similarity and is_within_token_limit:
nodes[i] = nodes[i] + nodes[i + 1]
del nodes[i + 1]
del embeddings[i + 1]
modified = True
continue
i += 1
return nodes
def _get_node_similarities(self, nodes: List[Node]) -> List[float]:
"""
Get the similarity of each node with the node that precedes it
"""
embeddings = self.embedding_client.embed_many([node.text for node in nodes])
similarities = []
for i in range(1, len(embeddings)):
similarities.append(cosine_similarity(embeddings[i - 1], embeddings[i]))
return [0] + similarities