|
|
|
|
|
|
|
|
def encode_queries(self, text: str): |
|
|
def encode_queries(self, text: str): |
|
|
raise NotImplementedError("Please implement encode method!") |
|
|
raise NotImplementedError("Please implement encode method!") |
|
|
|
|
|
|
|
|
|
|
|
def total_token_count(self, resp): |
|
|
|
|
|
try: |
|
|
|
|
|
return resp.usage.total_tokens |
|
|
|
|
|
except Exception: |
|
|
|
|
|
pass |
|
|
|
|
|
try: |
|
|
|
|
|
return resp["usage"]["total_tokens"] |
|
|
|
|
|
except Exception: |
|
|
|
|
|
pass |
|
|
|
|
|
return 0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DefaultEmbedding(Base): |
|
|
class DefaultEmbedding(Base): |
|
|
_model = None |
|
|
_model = None |
|
|
_model_name = "" |
|
|
_model_name = "" |
|
|
_model_lock = threading.Lock() |
|
|
_model_lock = threading.Lock() |
|
|
|
|
|
|
|
|
def __init__(self, key, model_name, **kwargs): |
|
|
def __init__(self, key, model_name, **kwargs): |
|
|
""" |
|
|
""" |
|
|
If you have trouble downloading HuggingFace models, -_^ this might help!! |
|
|
If you have trouble downloading HuggingFace models, -_^ this might help!! |
|
|
|
|
|
|
|
|
res = self.client.embeddings.create(input=texts[i:i + batch_size], |
|
|
res = self.client.embeddings.create(input=texts[i:i + batch_size], |
|
|
model=self.model_name) |
|
|
model=self.model_name) |
|
|
ress.extend([d.embedding for d in res.data]) |
|
|
ress.extend([d.embedding for d in res.data]) |
|
|
total_tokens += res.usage.total_tokens |
|
|
|
|
|
|
|
|
total_tokens += self.total_token_count(res) |
|
|
return np.array(ress), total_tokens |
|
|
return np.array(ress), total_tokens |
|
|
|
|
|
|
|
|
def encode_queries(self, text): |
|
|
def encode_queries(self, text): |
|
|
res = self.client.embeddings.create(input=[truncate(text, 8191)], |
|
|
res = self.client.embeddings.create(input=[truncate(text, 8191)], |
|
|
model=self.model_name) |
|
|
model=self.model_name) |
|
|
return np.array(res.data[0].embedding), res.usage.total_tokens |
|
|
|
|
|
|
|
|
return np.array(res.data[0].embedding), self.total_token_count(res) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class LocalAIEmbed(Base): |
|
|
class LocalAIEmbed(Base): |
|
|
|
|
|
|
|
|
for e in resp["output"]["embeddings"]: |
|
|
for e in resp["output"]["embeddings"]: |
|
|
embds[e["text_index"]] = e["embedding"] |
|
|
embds[e["text_index"]] = e["embedding"] |
|
|
res.extend(embds) |
|
|
res.extend(embds) |
|
|
token_count += resp["usage"]["total_tokens"] |
|
|
|
|
|
|
|
|
token_count += self.total_token_count(resp) |
|
|
return np.array(res), token_count |
|
|
return np.array(res), token_count |
|
|
except Exception as e: |
|
|
except Exception as e: |
|
|
raise Exception("Account abnormal. Please ensure it's on good standing to use QWen's "+self.model_name) |
|
|
raise Exception("Account abnormal. Please ensure it's on good standing to use QWen's "+self.model_name) |
|
|
|
|
|
|
|
|
text_type="query" |
|
|
text_type="query" |
|
|
) |
|
|
) |
|
|
return np.array(resp["output"]["embeddings"][0] |
|
|
return np.array(resp["output"]["embeddings"][0] |
|
|
["embedding"]), resp["usage"]["total_tokens"] |
|
|
|
|
|
|
|
|
["embedding"]), self.total_token_count(resp) |
|
|
except Exception: |
|
|
except Exception: |
|
|
raise Exception("Account abnormal. Please ensure it's on good standing to use QWen's "+self.model_name) |
|
|
raise Exception("Account abnormal. Please ensure it's on good standing to use QWen's "+self.model_name) |
|
|
return np.array([]), 0 |
|
|
return np.array([]), 0 |
|
|
|
|
|
|
|
|
res = self.client.embeddings.create(input=txt, |
|
|
res = self.client.embeddings.create(input=txt, |
|
|
model=self.model_name) |
|
|
model=self.model_name) |
|
|
arr.append(res.data[0].embedding) |
|
|
arr.append(res.data[0].embedding) |
|
|
tks_num += res.usage.total_tokens |
|
|
|
|
|
|
|
|
tks_num += self.total_token_count(res) |
|
|
return np.array(arr), tks_num |
|
|
return np.array(arr), tks_num |
|
|
|
|
|
|
|
|
def encode_queries(self, text): |
|
|
def encode_queries(self, text): |
|
|
res = self.client.embeddings.create(input=text, |
|
|
res = self.client.embeddings.create(input=text, |
|
|
model=self.model_name) |
|
|
model=self.model_name) |
|
|
return np.array(res.data[0].embedding), res.usage.total_tokens |
|
|
|
|
|
|
|
|
return np.array(res.data[0].embedding), self.total_token_count(res) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class OllamaEmbed(Base): |
|
|
class OllamaEmbed(Base): |
|
|
|
|
|
|
|
|
for i in range(0, len(texts), batch_size): |
|
|
for i in range(0, len(texts), batch_size): |
|
|
res = self.client.embeddings.create(input=texts[i:i + batch_size], model=self.model_name) |
|
|
res = self.client.embeddings.create(input=texts[i:i + batch_size], model=self.model_name) |
|
|
ress.extend([d.embedding for d in res.data]) |
|
|
ress.extend([d.embedding for d in res.data]) |
|
|
total_tokens += res.usage.total_tokens |
|
|
|
|
|
|
|
|
total_tokens += self.total_token_count(res) |
|
|
return np.array(ress), total_tokens |
|
|
return np.array(ress), total_tokens |
|
|
|
|
|
|
|
|
def encode_queries(self, text): |
|
|
def encode_queries(self, text): |
|
|
res = self.client.embeddings.create(input=[text], |
|
|
res = self.client.embeddings.create(input=[text], |
|
|
model=self.model_name) |
|
|
model=self.model_name) |
|
|
return np.array(res.data[0].embedding), res.usage.total_tokens |
|
|
|
|
|
|
|
|
return np.array(res.data[0].embedding), self.total_token_count(res) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class YoudaoEmbed(Base): |
|
|
class YoudaoEmbed(Base): |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
res = requests.post(self.base_url, headers=self.headers, json=data).json() |
|
|
res = requests.post(self.base_url, headers=self.headers, json=data).json() |
|
|
ress.extend([d["embedding"] for d in res["data"]]) |
|
|
ress.extend([d["embedding"] for d in res["data"]]) |
|
|
token_count += res["usage"]["total_tokens"] |
|
|
|
|
|
|
|
|
token_count += self.total_token_count(res) |
|
|
return np.array(ress), token_count |
|
|
return np.array(ress), token_count |
|
|
|
|
|
|
|
|
def encode_queries(self, text): |
|
|
def encode_queries(self, text): |
|
|
|
|
|
|
|
|
res = self.client.embeddings(input=texts[i:i + batch_size], |
|
|
res = self.client.embeddings(input=texts[i:i + batch_size], |
|
|
model=self.model_name) |
|
|
model=self.model_name) |
|
|
ress.extend([d.embedding for d in res.data]) |
|
|
ress.extend([d.embedding for d in res.data]) |
|
|
token_count += res.usage.total_tokens |
|
|
|
|
|
|
|
|
token_count += self.total_token_count(res) |
|
|
return np.array(ress), token_count |
|
|
return np.array(ress), token_count |
|
|
|
|
|
|
|
|
def encode_queries(self, text): |
|
|
def encode_queries(self, text): |
|
|
res = self.client.embeddings(input=[truncate(text, 8196)], |
|
|
res = self.client.embeddings(input=[truncate(text, 8196)], |
|
|
model=self.model_name) |
|
|
model=self.model_name) |
|
|
return np.array(res.data[0].embedding), res.usage.total_tokens |
|
|
|
|
|
|
|
|
return np.array(res.data[0].embedding), self.total_token_count(res) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BedrockEmbed(Base): |
|
|
class BedrockEmbed(Base): |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
res = requests.post(self.base_url, headers=self.headers, json=payload).json() |
|
|
res = requests.post(self.base_url, headers=self.headers, json=payload).json() |
|
|
ress.extend([d["embedding"] for d in res["data"]]) |
|
|
ress.extend([d["embedding"] for d in res["data"]]) |
|
|
token_count += res["usage"]["total_tokens"] |
|
|
|
|
|
|
|
|
token_count += self.total_token_count(res) |
|
|
return np.array(ress), token_count |
|
|
return np.array(ress), token_count |
|
|
|
|
|
|
|
|
def encode_queries(self, text): |
|
|
def encode_queries(self, text): |
|
|
|
|
|
|
|
|
if "data" not in res or not isinstance(res["data"], list) or len(res["data"]) != len(texts_batch): |
|
|
if "data" not in res or not isinstance(res["data"], list) or len(res["data"]) != len(texts_batch): |
|
|
raise ValueError(f"SILICONFLOWEmbed.encode got invalid response from {self.base_url}") |
|
|
raise ValueError(f"SILICONFLOWEmbed.encode got invalid response from {self.base_url}") |
|
|
ress.extend([d["embedding"] for d in res["data"]]) |
|
|
ress.extend([d["embedding"] for d in res["data"]]) |
|
|
token_count += res["usage"]["total_tokens"] |
|
|
|
|
|
|
|
|
token_count += self.total_token_count(res) |
|
|
return np.array(ress), token_count |
|
|
return np.array(ress), token_count |
|
|
|
|
|
|
|
|
def encode_queries(self, text): |
|
|
def encode_queries(self, text): |
|
|
|
|
|
|
|
|
res = requests.post(self.base_url, json=payload, headers=self.headers).json() |
|
|
res = requests.post(self.base_url, json=payload, headers=self.headers).json() |
|
|
if "data" not in res or not isinstance(res["data"], list) or len(res["data"])!= 1: |
|
|
if "data" not in res or not isinstance(res["data"], list) or len(res["data"])!= 1: |
|
|
raise ValueError(f"SILICONFLOWEmbed.encode_queries got invalid response from {self.base_url}") |
|
|
raise ValueError(f"SILICONFLOWEmbed.encode_queries got invalid response from {self.base_url}") |
|
|
return np.array(res["data"][0]["embedding"]), res["usage"]["total_tokens"] |
|
|
|
|
|
|
|
|
return np.array(res["data"][0]["embedding"]), self.total_token_count(res) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ReplicateEmbed(Base): |
|
|
class ReplicateEmbed(Base): |
|
|
|
|
|
|
|
|
res = self.client.do(model=self.model_name, texts=texts).body |
|
|
res = self.client.do(model=self.model_name, texts=texts).body |
|
|
return ( |
|
|
return ( |
|
|
np.array([r["embedding"] for r in res["data"]]), |
|
|
np.array([r["embedding"] for r in res["data"]]), |
|
|
res["usage"]["total_tokens"], |
|
|
|
|
|
|
|
|
self.total_token_count(res), |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
def encode_queries(self, text): |
|
|
def encode_queries(self, text): |
|
|
res = self.client.do(model=self.model_name, texts=[text]).body |
|
|
res = self.client.do(model=self.model_name, texts=[text]).body |
|
|
return ( |
|
|
return ( |
|
|
np.array([r["embedding"] for r in res["data"]]), |
|
|
np.array([r["embedding"] for r in res["data"]]), |
|
|
res["usage"]["total_tokens"], |
|
|
|
|
|
|
|
|
self.total_token_count(res), |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|