-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathembedding.py
More file actions
49 lines (41 loc) · 1.56 KB
/
embedding.py
File metadata and controls
49 lines (41 loc) · 1.56 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
# coding=utf-8
"""
@project: MaxKB
@Author:虎
@file: embedding.py
@date:2024/7/12 17:44
@desc:
"""
from typing import Dict, List
import openai
from models_provider.base_model_provider import MaxKBBaseModel
class OpenAIEmbeddingModel(MaxKBBaseModel):
model_name: str
optional_params: dict
def __init__(self, api_key, base_url, model_name: str, optional_params: dict):
self.client = openai.OpenAI(api_key=api_key, base_url=base_url).embeddings
self.model_name = model_name
self.optional_params = optional_params
@staticmethod
def new_instance(model_type, model_name, model_credential: Dict[str, object], **model_kwargs):
optional_params = MaxKBBaseModel.filter_optional_params(model_kwargs)
return OpenAIEmbeddingModel(
api_key=model_credential.get('api_key'),
model_name=model_name,
base_url=model_credential.get('api_base'),
optional_params=optional_params
)
def embed_query(self, text: str):
res = self.embed_documents([text])
return res[0]
def embed_documents(
self, texts: List[str], chunk_size: int | None = None
) -> List[List[float]]:
if len(self.optional_params) > 0:
res = self.client.create(
input=texts, model=self.model_name, encoding_format="float",
**self.optional_params
)
else:
res = self.client.create(input=texts, model=self.model_name, encoding_format="float")
return [e.embedding for e in res.data]