-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathembeddings.py
More file actions
128 lines (115 loc) · 4.25 KB
/
embeddings.py
File metadata and controls
128 lines (115 loc) · 4.25 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
import os
from typing import Any
from typing import Callable
from typing import Optional
from typing import Union
import httpx
from singlestoredb import manage_workspaces
try:
from langchain_openai import OpenAIEmbeddings
except ImportError:
raise ImportError(
'Could not import langchain_openai python package. '
'Please install it with `pip install langchain_openai`.',
)
try:
from langchain_aws import BedrockEmbeddings
except ImportError:
raise ImportError(
'Could not import langchain-aws python package. '
'Please install it with `pip install langchain-aws`.',
)
import boto3
from botocore import UNSIGNED
from botocore.config import Config
def SingleStoreEmbeddingsFactory(
model_name: str,
api_key: Optional[str] = None,
http_client: Optional[httpx.Client] = None,
obo_token_getter: Optional[Callable[[], Optional[str]]] = None,
**kwargs: Any,
) -> Union[OpenAIEmbeddings, BedrockEmbeddings]:
"""Return an embeddings model instance (OpenAIEmbeddings or BedrockEmbeddings).
"""
inference_api_manager = (
manage_workspaces().organizations.current.inference_apis
)
info = inference_api_manager.get(model_name=model_name)
token_env = os.environ.get('SINGLESTOREDB_USER_TOKEN')
token = api_key if api_key is not None else token_env
if info.hosting_platform == 'Amazon':
# Instantiate Bedrock client
cfg_kwargs = {
'signature_version': UNSIGNED,
'retries': {'max_attempts': 1, 'mode': 'standard'},
}
# Extract timeouts from http_client if provided
t = http_client.timeout if http_client is not None else None
connect_timeout = None
read_timeout = None
if t is not None:
if isinstance(t, httpx.Timeout):
if t.connect is not None:
connect_timeout = float(t.connect)
if t.read is not None:
read_timeout = float(t.read)
if connect_timeout is None and read_timeout is not None:
connect_timeout = read_timeout
if read_timeout is None and connect_timeout is not None:
read_timeout = connect_timeout
elif isinstance(t, (int, float)):
connect_timeout = float(t)
read_timeout = float(t)
if read_timeout is not None:
cfg_kwargs['read_timeout'] = read_timeout
if connect_timeout is not None:
cfg_kwargs['connect_timeout'] = connect_timeout
cfg = Config(**cfg_kwargs)
client = boto3.client(
'bedrock-runtime',
endpoint_url=info.connection_url,
region_name='us-east-1',
aws_access_key_id='placeholder',
aws_secret_access_key='placeholder',
config=cfg,
)
def _inject_headers(request: Any, **_ignored: Any) -> None:
"""Inject dynamic auth/OBO headers prior to Bedrock sending."""
if obo_token_getter is not None:
obo_val = obo_token_getter()
if obo_val:
request.headers['X-S2-OBO'] = obo_val
if token:
request.headers['Authorization'] = f'Bearer {token}'
request.headers.pop('X-Amz-Date', None)
request.headers.pop('X-Amz-Security-Token', None)
emitter = client._endpoint._event_emitter
emitter.register_first(
'before-send.bedrock-runtime.InvokeModel',
_inject_headers,
)
emitter.register_first(
'before-send.bedrock-runtime.InvokeModelWithResponseStream',
_inject_headers,
)
return BedrockEmbeddings(
model_id=model_name,
endpoint_url=info.connection_url,
region_name='us-east-1',
aws_access_key_id='placeholder',
aws_secret_access_key='placeholder',
client=client,
**kwargs,
)
# OpenAI / Azure OpenAI path
openai_kwargs = dict(
base_url=info.connection_url,
api_key=token,
model=model_name,
)
if http_client is not None:
openai_kwargs['http_client'] = http_client
return OpenAIEmbeddings(
**openai_kwargs,
**kwargs,
)