uipath-langchain-client 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- uipath_langchain_client/__init__.py +50 -0
- uipath_langchain_client/__version__.py +3 -0
- uipath_langchain_client/base_client.py +277 -0
- uipath_langchain_client/clients/anthropic/__init__.py +3 -0
- uipath_langchain_client/clients/anthropic/chat_models.py +157 -0
- uipath_langchain_client/clients/azure/__init__.py +4 -0
- uipath_langchain_client/clients/azure/chat_models.py +46 -0
- uipath_langchain_client/clients/azure/embeddings.py +46 -0
- uipath_langchain_client/clients/bedrock/__init__.py +7 -0
- uipath_langchain_client/clients/bedrock/chat_models.py +63 -0
- uipath_langchain_client/clients/bedrock/embeddings.py +33 -0
- uipath_langchain_client/clients/bedrock/utils.py +90 -0
- uipath_langchain_client/clients/google/__init__.py +4 -0
- uipath_langchain_client/clients/google/chat_models.py +203 -0
- uipath_langchain_client/clients/google/embeddings.py +45 -0
- uipath_langchain_client/clients/normalized/__init__.py +4 -0
- uipath_langchain_client/clients/normalized/chat_models.py +419 -0
- uipath_langchain_client/clients/normalized/embeddings.py +31 -0
- uipath_langchain_client/clients/openai/__init__.py +15 -0
- uipath_langchain_client/clients/openai/chat_models.py +102 -0
- uipath_langchain_client/clients/openai/embeddings.py +82 -0
- uipath_langchain_client/clients/vertexai/__init__.py +3 -0
- uipath_langchain_client/clients/vertexai/chat_models.py +48 -0
- uipath_langchain_client/factory.py +217 -0
- uipath_langchain_client/settings.py +32 -0
- uipath_langchain_client-1.0.0.dist-info/METADATA +276 -0
- uipath_langchain_client-1.0.0.dist-info/RECORD +28 -0
- uipath_langchain_client-1.0.0.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
from typing import Self
|
|
2
|
+
|
|
3
|
+
from pydantic import model_validator
|
|
4
|
+
from uipath_langchain_client.base_client import UiPathBaseLLMClient
|
|
5
|
+
from uipath_langchain_client.settings import UiPathAPIConfig
|
|
6
|
+
|
|
7
|
+
try:
|
|
8
|
+
from langchain_aws.embeddings import BedrockEmbeddings
|
|
9
|
+
from uipath_langchain_client.clients.bedrock.utils import WrappedBotoClient
|
|
10
|
+
except ImportError as e:
|
|
11
|
+
raise ImportError(
|
|
12
|
+
"The 'aws' extra is required to use UiPathBedrockEmbeddings. "
|
|
13
|
+
"Install it with: uv add uipath-langchain-client[aws]"
|
|
14
|
+
) from e
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class UiPathBedrockEmbeddings(UiPathBaseLLMClient, BedrockEmbeddings):
|
|
18
|
+
api_config: UiPathAPIConfig = UiPathAPIConfig(
|
|
19
|
+
api_type="embeddings",
|
|
20
|
+
client_type="passthrough",
|
|
21
|
+
vendor_type="awsbedrock",
|
|
22
|
+
freeze_base_url=True,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
# Override fields to avoid errors when instantiating the class
|
|
26
|
+
model_id: str = "PLACEHOLDER"
|
|
27
|
+
region_name: str | None = "PLACEHOLDER"
|
|
28
|
+
|
|
29
|
+
@model_validator(mode="after")
|
|
30
|
+
def setup_uipath_client(self) -> Self:
|
|
31
|
+
self.model_id = self.model_name
|
|
32
|
+
self.client = WrappedBotoClient(self.uipath_sync_client)
|
|
33
|
+
return self
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import base64
|
|
2
|
+
import json
|
|
3
|
+
from typing import Any, Iterator
|
|
4
|
+
|
|
5
|
+
from httpx import Client
|
|
6
|
+
|
|
7
|
+
try:
|
|
8
|
+
from botocore.eventstream import EventStreamBuffer
|
|
9
|
+
except ImportError as e:
|
|
10
|
+
raise ImportError(
|
|
11
|
+
"The 'aws' extra is required to use WrappedBotoClient. "
|
|
12
|
+
"Install it with: uv add uipath-langchain-client[aws]"
|
|
13
|
+
) from e
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class _MockEventHooks:
|
|
17
|
+
"""Mock event hooks that mimics boto3's event registration system."""
|
|
18
|
+
|
|
19
|
+
def register(self, event_name: str, handler: Any) -> None:
|
|
20
|
+
"""No-op register method to satisfy langchain_aws's header registration."""
|
|
21
|
+
pass
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class _MockClientMeta:
|
|
25
|
+
"""Mock client meta that mimics boto3's client.meta structure."""
|
|
26
|
+
|
|
27
|
+
def __init__(self, region_name: str = "PLACEHOLDER"):
|
|
28
|
+
self.region_name = region_name
|
|
29
|
+
self.events = _MockEventHooks()
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class WrappedBotoClient:
|
|
33
|
+
def __init__(self, httpx_client: Client | None = None, region_name: str = "PLACEHOLDER"):
|
|
34
|
+
self.httpx_client = httpx_client
|
|
35
|
+
self.meta = _MockClientMeta(region_name=region_name)
|
|
36
|
+
|
|
37
|
+
def _stream_generator(self, request_body: dict[str, Any]) -> Iterator[dict[str, Any]]:
|
|
38
|
+
if self.httpx_client is None:
|
|
39
|
+
raise ValueError("httpx_client is not set")
|
|
40
|
+
with self.httpx_client.stream("POST", "/", json=request_body) as response:
|
|
41
|
+
buffer = EventStreamBuffer()
|
|
42
|
+
for chunk in response.iter_bytes():
|
|
43
|
+
buffer.add_data(chunk)
|
|
44
|
+
for event in buffer:
|
|
45
|
+
event_as_dict = event.to_response_dict()
|
|
46
|
+
dict_key = event_as_dict["headers"][":event-type"]
|
|
47
|
+
dict_value = json.loads(event_as_dict["body"].decode("utf-8"))
|
|
48
|
+
if "bytes" in dict_value:
|
|
49
|
+
dict_value["bytes"] = base64.b64decode(dict_value["bytes"])
|
|
50
|
+
yield {dict_key: dict_value}
|
|
51
|
+
|
|
52
|
+
def invoke_model(self, **kwargs: Any) -> Any:
|
|
53
|
+
if self.httpx_client is None:
|
|
54
|
+
raise ValueError("httpx_client is not set")
|
|
55
|
+
return {
|
|
56
|
+
"body": self.httpx_client.post(
|
|
57
|
+
"/",
|
|
58
|
+
json=json.loads(kwargs.get("body", {})),
|
|
59
|
+
)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
def invoke_model_with_response_stream(self, **kwargs: Any) -> Any:
|
|
63
|
+
return {"body": self._stream_generator(json.loads(kwargs.get("body", {})))}
|
|
64
|
+
|
|
65
|
+
def converse(
|
|
66
|
+
self, *, messages: list[dict[str, Any]], system: str | None = None, **params: Any
|
|
67
|
+
) -> Any:
|
|
68
|
+
if self.httpx_client is None:
|
|
69
|
+
raise ValueError("httpx_client is not set")
|
|
70
|
+
return self.httpx_client.post(
|
|
71
|
+
"/",
|
|
72
|
+
json={
|
|
73
|
+
"messages": messages,
|
|
74
|
+
"system": system,
|
|
75
|
+
**params,
|
|
76
|
+
},
|
|
77
|
+
).json()
|
|
78
|
+
|
|
79
|
+
def converse_stream(
|
|
80
|
+
self, *, messages: list[dict[str, Any]], system: str | None = None, **params: Any
|
|
81
|
+
) -> Any:
|
|
82
|
+
return {
|
|
83
|
+
"stream": self._stream_generator(
|
|
84
|
+
{
|
|
85
|
+
"messages": messages,
|
|
86
|
+
"system": system,
|
|
87
|
+
**params,
|
|
88
|
+
}
|
|
89
|
+
),
|
|
90
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
from uipath_langchain_client.clients.google.chat_models import UiPathChatGoogleGenerativeAI
|
|
2
|
+
from uipath_langchain_client.clients.google.embeddings import UiPathGoogleGenerativeAIEmbeddings
|
|
3
|
+
|
|
4
|
+
__all__ = ["UiPathChatGoogleGenerativeAI", "UiPathGoogleGenerativeAIEmbeddings"]
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
from collections.abc import AsyncIterator, Iterator
|
|
2
|
+
from typing import Self
|
|
3
|
+
|
|
4
|
+
from httpx import Response
|
|
5
|
+
from pydantic import Field, SecretStr, model_validator
|
|
6
|
+
from uipath_langchain_client.base_client import UiPathBaseLLMClient
|
|
7
|
+
from uipath_langchain_client.settings import UiPathAPIConfig
|
|
8
|
+
|
|
9
|
+
try:
|
|
10
|
+
from langchain_google_genai.chat_models import ChatGoogleGenerativeAI
|
|
11
|
+
|
|
12
|
+
from google.genai.client import Client
|
|
13
|
+
from google.genai.types import HttpOptions
|
|
14
|
+
except ImportError as e:
|
|
15
|
+
raise ImportError(
|
|
16
|
+
"The 'google' extra is required to use UiPathChatGoogleGenerativeAI. "
|
|
17
|
+
"Install it with: uv add uipath-langchain-client[google]"
|
|
18
|
+
) from e
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _wrap_iter_lines(original: Iterator[str]) -> Iterator[str]:
|
|
22
|
+
"""Wrap iter_lines to extract individual JSON objects from streaming responses.
|
|
23
|
+
|
|
24
|
+
The LLM Gateway wraps streaming JSON responses in an array like [{...}, {...}].
|
|
25
|
+
This extracts each complete JSON object and yields them individually.
|
|
26
|
+
Handles multiple JSON objects on a single line (e.g., {...},{...}).
|
|
27
|
+
|
|
28
|
+
We prefix output with 'data: ' so the SDK's _iter_response_stream bypasses its
|
|
29
|
+
broken brace counting (which doesn't handle braces inside strings) and yields
|
|
30
|
+
our JSON objects directly.
|
|
31
|
+
|
|
32
|
+
Temporal Fix until it's fixed in the main package.
|
|
33
|
+
"""
|
|
34
|
+
buffer = ""
|
|
35
|
+
balance = 0
|
|
36
|
+
in_string = False
|
|
37
|
+
escape_next = False
|
|
38
|
+
|
|
39
|
+
for line in original:
|
|
40
|
+
# Handle data: prefix (SSE format)
|
|
41
|
+
if line.startswith("data:"):
|
|
42
|
+
line = line[5:].lstrip()
|
|
43
|
+
|
|
44
|
+
for char in line:
|
|
45
|
+
# Handle escape sequences in strings
|
|
46
|
+
if escape_next:
|
|
47
|
+
buffer += char
|
|
48
|
+
escape_next = False
|
|
49
|
+
continue
|
|
50
|
+
|
|
51
|
+
if char == "\\" and in_string:
|
|
52
|
+
buffer += char
|
|
53
|
+
escape_next = True
|
|
54
|
+
continue
|
|
55
|
+
|
|
56
|
+
if char == '"':
|
|
57
|
+
in_string = not in_string
|
|
58
|
+
buffer += char
|
|
59
|
+
continue
|
|
60
|
+
|
|
61
|
+
# Only track braces outside of strings
|
|
62
|
+
if not in_string:
|
|
63
|
+
if char == "{":
|
|
64
|
+
balance += 1
|
|
65
|
+
buffer += char
|
|
66
|
+
elif char == "}":
|
|
67
|
+
buffer += char
|
|
68
|
+
balance -= 1
|
|
69
|
+
if balance == 0 and buffer:
|
|
70
|
+
# Complete JSON object found - yield with 'data: ' prefix
|
|
71
|
+
# so SDK bypasses its broken brace counting
|
|
72
|
+
yield "data: " + buffer
|
|
73
|
+
buffer = ""
|
|
74
|
+
elif balance == 0:
|
|
75
|
+
# Skip characters outside JSON objects (array brackets, commas, whitespace)
|
|
76
|
+
continue
|
|
77
|
+
else:
|
|
78
|
+
buffer += char
|
|
79
|
+
else:
|
|
80
|
+
buffer += char
|
|
81
|
+
|
|
82
|
+
# Yield any remaining buffer (handles incomplete streams)
|
|
83
|
+
if buffer:
|
|
84
|
+
yield "data: " + buffer
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
async def _wrap_aiter_lines(original: AsyncIterator[str]) -> AsyncIterator[str]:
|
|
88
|
+
"""Async version of _wrap_iter_lines.
|
|
89
|
+
|
|
90
|
+
Extracts individual JSON objects from streaming responses.
|
|
91
|
+
Handles multiple JSON objects on a single line (e.g., {...},{...}).
|
|
92
|
+
|
|
93
|
+
We prefix output with 'data: ' so the SDK's _iter_response_stream bypasses its
|
|
94
|
+
broken brace counting (which doesn't handle braces inside strings) and yields
|
|
95
|
+
our JSON objects directly.
|
|
96
|
+
"""
|
|
97
|
+
buffer = ""
|
|
98
|
+
balance = 0
|
|
99
|
+
in_string = False
|
|
100
|
+
escape_next = False
|
|
101
|
+
|
|
102
|
+
async for line in original:
|
|
103
|
+
# Handle data: prefix (SSE format)
|
|
104
|
+
if line.startswith("data:"):
|
|
105
|
+
line = line[5:].lstrip()
|
|
106
|
+
|
|
107
|
+
for char in line:
|
|
108
|
+
# Handle escape sequences in strings
|
|
109
|
+
if escape_next:
|
|
110
|
+
buffer += char
|
|
111
|
+
escape_next = False
|
|
112
|
+
continue
|
|
113
|
+
|
|
114
|
+
if char == "\\" and in_string:
|
|
115
|
+
buffer += char
|
|
116
|
+
escape_next = True
|
|
117
|
+
continue
|
|
118
|
+
|
|
119
|
+
if char == '"':
|
|
120
|
+
in_string = not in_string
|
|
121
|
+
buffer += char
|
|
122
|
+
continue
|
|
123
|
+
|
|
124
|
+
# Only track braces outside of strings
|
|
125
|
+
if not in_string:
|
|
126
|
+
if char == "{":
|
|
127
|
+
balance += 1
|
|
128
|
+
buffer += char
|
|
129
|
+
elif char == "}":
|
|
130
|
+
buffer += char
|
|
131
|
+
balance -= 1
|
|
132
|
+
if balance == 0 and buffer:
|
|
133
|
+
# Complete JSON object found - yield with 'data: ' prefix
|
|
134
|
+
# so SDK bypasses its broken brace counting
|
|
135
|
+
yield "data: " + buffer
|
|
136
|
+
buffer = ""
|
|
137
|
+
elif balance == 0:
|
|
138
|
+
# Skip characters outside JSON objects (array brackets, commas, whitespace)
|
|
139
|
+
continue
|
|
140
|
+
else:
|
|
141
|
+
buffer += char
|
|
142
|
+
else:
|
|
143
|
+
buffer += char
|
|
144
|
+
|
|
145
|
+
# Yield any remaining buffer (handles incomplete streams)
|
|
146
|
+
if buffer:
|
|
147
|
+
yield "data: " + buffer
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
class UiPathChatGoogleGenerativeAI(UiPathBaseLLMClient, ChatGoogleGenerativeAI):
|
|
151
|
+
api_config: UiPathAPIConfig = UiPathAPIConfig(
|
|
152
|
+
api_type="completions",
|
|
153
|
+
client_type="passthrough",
|
|
154
|
+
vendor_type="vertexai",
|
|
155
|
+
api_flavor="generate-content",
|
|
156
|
+
api_version="v1beta1",
|
|
157
|
+
freeze_base_url=True,
|
|
158
|
+
)
|
|
159
|
+
|
|
160
|
+
# Override fields to avoid errors when instantiating the class
|
|
161
|
+
model: str = Field(default="", alias="model_name")
|
|
162
|
+
google_api_key: SecretStr | None = Field(default=SecretStr("PLACEHOLDER"))
|
|
163
|
+
|
|
164
|
+
@model_validator(mode="after")
|
|
165
|
+
def setup_uipath_client(self) -> Self:
|
|
166
|
+
def fix_streaming_response(response: Response):
|
|
167
|
+
"""Monkey-patch iter_lines to strip JSON array brackets."""
|
|
168
|
+
original_iter_lines = response.iter_lines
|
|
169
|
+
response.iter_lines = lambda: _wrap_iter_lines(original_iter_lines())
|
|
170
|
+
|
|
171
|
+
async def fix_streaming_response_async(response: Response):
|
|
172
|
+
"""Monkey-patch aiter_lines to strip JSON array brackets."""
|
|
173
|
+
original_aiter_lines = response.aiter_lines
|
|
174
|
+
response.aiter_lines = lambda: _wrap_aiter_lines(original_aiter_lines())
|
|
175
|
+
|
|
176
|
+
self.uipath_sync_client.event_hooks["response"].append(fix_streaming_response)
|
|
177
|
+
self.uipath_async_client.event_hooks["response"].append(fix_streaming_response_async)
|
|
178
|
+
|
|
179
|
+
# TODO: in exactly 2 weeks, we need to uncomment this part of the code because it will work, 5 february 2026 is the date.
|
|
180
|
+
# def fix_url_for_streaming(request: Request):
|
|
181
|
+
# if request.headers.get("X-UiPath-Streaming-Enabled") == "true":
|
|
182
|
+
# request.url = URL(request.url).copy_add_param("alt", "sse")
|
|
183
|
+
|
|
184
|
+
# async def fix_url_for_streaming_async(request: Request):
|
|
185
|
+
# if request.headers.get("X-UiPath-Streaming-Enabled") == "true":
|
|
186
|
+
# request.url = URL(request.url).copy_add_param("alt", "sse")
|
|
187
|
+
|
|
188
|
+
# self.uipath_sync_client.event_hooks["request"].append(fix_url_for_streaming)
|
|
189
|
+
# self.uipath_async_client.event_hooks["request"].append(fix_url_for_streaming_async)
|
|
190
|
+
|
|
191
|
+
self.client = Client(
|
|
192
|
+
vertexai=True,
|
|
193
|
+
api_key="PLACEHOLDER",
|
|
194
|
+
http_options=HttpOptions(
|
|
195
|
+
base_url=str(self.uipath_sync_client.base_url),
|
|
196
|
+
headers=dict(self.uipath_sync_client.headers),
|
|
197
|
+
timeout=None, # handled by the UiPath client
|
|
198
|
+
retry_options=None, # handled by the UiPath client
|
|
199
|
+
httpx_client=self.uipath_sync_client,
|
|
200
|
+
httpx_async_client=self.uipath_async_client,
|
|
201
|
+
),
|
|
202
|
+
)
|
|
203
|
+
return self
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
from typing import Self
|
|
2
|
+
|
|
3
|
+
from pydantic import Field, SecretStr, model_validator
|
|
4
|
+
from uipath_langchain_client.base_client import UiPathBaseLLMClient
|
|
5
|
+
from uipath_langchain_client.settings import UiPathAPIConfig
|
|
6
|
+
|
|
7
|
+
try:
|
|
8
|
+
from langchain_google_genai.embeddings import GoogleGenerativeAIEmbeddings
|
|
9
|
+
|
|
10
|
+
from google.genai.client import Client
|
|
11
|
+
from google.genai.types import HttpOptions
|
|
12
|
+
except ImportError as e:
|
|
13
|
+
raise ImportError(
|
|
14
|
+
"The 'google' extra is required to use UiPathGoogleGenerativeAIEmbeddings. "
|
|
15
|
+
"Install it with: uv add uipath-langchain-client[google]"
|
|
16
|
+
) from e
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class UiPathGoogleGenerativeAIEmbeddings(UiPathBaseLLMClient, GoogleGenerativeAIEmbeddings):
|
|
20
|
+
api_config: UiPathAPIConfig = UiPathAPIConfig(
|
|
21
|
+
api_type="embeddings",
|
|
22
|
+
client_type="passthrough",
|
|
23
|
+
vendor_type="vertexai",
|
|
24
|
+
freeze_base_url=True,
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
# Override fields to avoid errors when instantiating the class
|
|
28
|
+
model: str = Field(default="", alias="model_name")
|
|
29
|
+
google_api_key: SecretStr | None = Field(default=SecretStr("PLACEHOLDER"))
|
|
30
|
+
|
|
31
|
+
@model_validator(mode="after")
|
|
32
|
+
def setup_uipath_client(self) -> Self:
|
|
33
|
+
self.client = Client(
|
|
34
|
+
vertexai=True,
|
|
35
|
+
api_key="PLACEHOLDER",
|
|
36
|
+
http_options=HttpOptions(
|
|
37
|
+
timeout=None, # handled by the UiPath client
|
|
38
|
+
retry_options=None, # handled by the UiPath client
|
|
39
|
+
base_url=str(self.uipath_sync_client.base_url),
|
|
40
|
+
headers=dict(self.uipath_sync_client.headers),
|
|
41
|
+
httpx_client=self.uipath_sync_client,
|
|
42
|
+
httpx_async_client=self.uipath_async_client,
|
|
43
|
+
),
|
|
44
|
+
)
|
|
45
|
+
return self
|