-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathchat.py
More file actions
139 lines (126 loc) · 4.57 KB
/
Copy pathchat.py
File metadata and controls
139 lines (126 loc) · 4.57 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
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 ChatOpenAI
except ImportError:
raise ImportError(
'Could not import langchain_openai python package. '
'Please install it with `pip install langchain_openai`.',
)
try:
from langchain_aws import ChatBedrockConverse
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 SingleStoreChatFactory(
model_name: str,
api_key: Optional[str] = None,
streaming: bool = True,
http_client: Optional[httpx.Client] = None,
obo_token_getter: Optional[Callable[[], Optional[str]]] = None,
**kwargs: Any,
) -> Union[ChatOpenAI, ChatBedrockConverse]:
"""Return a chat model instance (ChatOpenAI or ChatBedrockConverse).
"""
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.Converse',
_inject_headers,
)
emitter.register_first(
'before-send.bedrock-runtime.ConverseStream',
_inject_headers,
)
emitter.register_first(
'before-send.bedrock-runtime.InvokeModel',
_inject_headers,
)
emitter.register_first(
'before-send.bedrock-runtime.InvokeModelWithResponseStream',
_inject_headers,
)
return ChatBedrockConverse(
model_id=model_name,
endpoint_url=info.connection_url,
region_name='us-east-1',
aws_access_key_id='placeholder',
aws_secret_access_key='placeholder',
disable_streaming=not streaming,
client=client,
**kwargs,
)
# OpenAI / Azure OpenAI path
openai_kwargs = dict(
base_url=info.connection_url,
api_key=token,
model=model_name,
streaming=streaming,
)
if http_client is not None:
openai_kwargs['http_client'] = http_client
return ChatOpenAI(
**openai_kwargs,
**kwargs,
)