-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathclient.py
More file actions
77 lines (63 loc) · 1.89 KB
/
Copy pathclient.py
File metadata and controls
77 lines (63 loc) · 1.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
import json
import functools
from typing import Any, Dict, Iterable, List, Optional, Union
import voyageai
from voyageai.util import default_api_key
from voyageai.embeddings_object import EmbeddingsObject
class Client:
"""Voyage AI Client
Args:
api_key (str): Your API key.
"""
def __init__(
self,
api_key: Optional[str] = None,
) -> None:
self.api_key = api_key or default_api_key()
# self.max_retries = max_retries
# self.timeout = timeout
# self.batch_size = voyageai.VOYAGE_EMBED_BATCH_SIZE
self._params = {
"api_key": self.api_key
}
def embed(
self,
texts: List[str],
model: str = voyageai.VOYAGE_EMBED_DEFAULT_MODEL,
input_type: Optional[str] = None,
truncation: Optional[bool] = None,
) -> EmbeddingsObject:
result = EmbeddingsObject()
response = voyageai.Embedding.create(
input=texts,
model=model,
input_type=input_type,
truncation=truncation,
**self._params,
)
result.update(response)
return result
@property
@functools.lru_cache()
def tokenizer(self):
try:
from tokenizers import Tokenizer
except ImportError:
raise ImportError(
"tokenizers package not found. Please run `pip install tokenizers` "
"to install the dependency."
)
tokenizer = Tokenizer.from_pretrained('voyageai/voyage')
tokenizer.no_truncation()
return tokenizer
def tokenize(
self,
texts: List[str],
) -> List[Any]:
return self.tokenizer.encode_batch(texts)
def count_tokens(
self,
texts: List[str],
) -> int:
tokenized = self.tokenize(texts)
return sum([len(t) for t in tokenized])