langchaint 0.3.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.
- langchaint/__init__.py +98 -0
- langchaint/anthropic/__init__.py +230 -0
- langchaint/anthropic/messages_provider.py +856 -0
- langchaint/exceptions.py +328 -0
- langchaint/inference_params.py +30 -0
- langchaint/llm.py +563 -0
- langchaint/messages.py +218 -0
- langchaint/openai/__init__.py +133 -0
- langchaint/openai/responses_provider.py +792 -0
- langchaint/provider.py +284 -0
- langchaint/py.typed +0 -0
- langchaint/rate_limiter.py +219 -0
- langchaint/response.py +124 -0
- langchaint/streaming.py +319 -0
- langchaint/tools.py +302 -0
- langchaint/tracing/__init__.py +668 -0
- langchaint/usage.py +62 -0
- langchaint-0.3.0.dist-info/METADATA +167 -0
- langchaint-0.3.0.dist-info/RECORD +21 -0
- langchaint-0.3.0.dist-info/WHEEL +4 -0
- langchaint-0.3.0.dist-info/licenses/LICENSE +21 -0
langchaint/__init__.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"""langchaint: a provider-neutral LLM client.
|
|
2
|
+
|
|
3
|
+
Adapters wrap the official anthropic/openai SDK clients; generation happens only through LLM.bind(...) -> BoundLLM.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from langchaint.exceptions import (
|
|
7
|
+
AbortBatchError,
|
|
8
|
+
AttemptRecord,
|
|
9
|
+
ExceededMaxCompletionTokensError,
|
|
10
|
+
GenerationError,
|
|
11
|
+
InvalidToolArgsError,
|
|
12
|
+
RefusalError,
|
|
13
|
+
RetriesExhaustedError,
|
|
14
|
+
StreamProtocolError,
|
|
15
|
+
TransientError,
|
|
16
|
+
)
|
|
17
|
+
from langchaint.inference_params import InferenceParams, ReasoningEffort
|
|
18
|
+
from langchaint.llm import LLM, BoundLLM
|
|
19
|
+
from langchaint.messages import (
|
|
20
|
+
AssistantMessage,
|
|
21
|
+
ImagePart,
|
|
22
|
+
Message,
|
|
23
|
+
MessageContent,
|
|
24
|
+
Part,
|
|
25
|
+
ReasoningTrace,
|
|
26
|
+
StopReason,
|
|
27
|
+
TextPart,
|
|
28
|
+
ToolCall,
|
|
29
|
+
ToolMessage,
|
|
30
|
+
TurnElement,
|
|
31
|
+
UserMessage,
|
|
32
|
+
)
|
|
33
|
+
from langchaint.provider import (
|
|
34
|
+
PricingTable,
|
|
35
|
+
SpecificTool,
|
|
36
|
+
StreamItem,
|
|
37
|
+
ToolChoice,
|
|
38
|
+
)
|
|
39
|
+
from langchaint.rate_limiter import RateLimiter
|
|
40
|
+
from langchaint.response import Response, RowValue, to_row
|
|
41
|
+
from langchaint.streaming import StreamHandle
|
|
42
|
+
from langchaint.tools import (
|
|
43
|
+
DispatchHandled,
|
|
44
|
+
DispatchInvalidToolArgs,
|
|
45
|
+
DispatchOutcome,
|
|
46
|
+
DispatchUnknownTool,
|
|
47
|
+
Tool,
|
|
48
|
+
ToolManager,
|
|
49
|
+
ToolOutput,
|
|
50
|
+
ToolOutputExplicit,
|
|
51
|
+
)
|
|
52
|
+
from langchaint.usage import Usage
|
|
53
|
+
|
|
54
|
+
__all__ = [
|
|
55
|
+
"LLM",
|
|
56
|
+
"AbortBatchError",
|
|
57
|
+
"AssistantMessage",
|
|
58
|
+
"AttemptRecord",
|
|
59
|
+
"BoundLLM",
|
|
60
|
+
"DispatchHandled",
|
|
61
|
+
"DispatchInvalidToolArgs",
|
|
62
|
+
"DispatchOutcome",
|
|
63
|
+
"DispatchUnknownTool",
|
|
64
|
+
"ExceededMaxCompletionTokensError",
|
|
65
|
+
"GenerationError",
|
|
66
|
+
"ImagePart",
|
|
67
|
+
"InferenceParams",
|
|
68
|
+
"InvalidToolArgsError",
|
|
69
|
+
"Message",
|
|
70
|
+
"MessageContent",
|
|
71
|
+
"Part",
|
|
72
|
+
"PricingTable",
|
|
73
|
+
"RateLimiter",
|
|
74
|
+
"ReasoningEffort",
|
|
75
|
+
"ReasoningTrace",
|
|
76
|
+
"RefusalError",
|
|
77
|
+
"Response",
|
|
78
|
+
"RetriesExhaustedError",
|
|
79
|
+
"RowValue",
|
|
80
|
+
"SpecificTool",
|
|
81
|
+
"StopReason",
|
|
82
|
+
"StreamHandle",
|
|
83
|
+
"StreamItem",
|
|
84
|
+
"StreamProtocolError",
|
|
85
|
+
"TextPart",
|
|
86
|
+
"Tool",
|
|
87
|
+
"ToolCall",
|
|
88
|
+
"ToolChoice",
|
|
89
|
+
"ToolManager",
|
|
90
|
+
"ToolMessage",
|
|
91
|
+
"ToolOutput",
|
|
92
|
+
"ToolOutputExplicit",
|
|
93
|
+
"TransientError",
|
|
94
|
+
"TurnElement",
|
|
95
|
+
"Usage",
|
|
96
|
+
"UserMessage",
|
|
97
|
+
"to_row",
|
|
98
|
+
]
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
"""The anthropic backend: the Messages adapter, its model catalog, and pricing.
|
|
2
|
+
|
|
3
|
+
Importing this subpackage requires the anthropic package (install langchaint[anthropic]);
|
|
4
|
+
the import below raises a ModuleNotFoundError naming the extra to install.
|
|
5
|
+
|
|
6
|
+
anthropic_model takes the provider's own model identifier, the same string the wire accepts,
|
|
7
|
+
so switching models never changes an import; it constructs the Messages adapter and wraps it in an LLM.
|
|
8
|
+
client None constructs the native first-party SDK client, which reads credentials from the environment.
|
|
9
|
+
anthropic_bedrock_model is the Bedrock sibling: it names the same catalog model and reads the model's
|
|
10
|
+
Bedrock surface (which of two client classes) and its wire model id from ANTHROPIC_BEDROCK,
|
|
11
|
+
so the application names neither the client class nor the Bedrock id.
|
|
12
|
+
pricing None selects the model's public prices from ANTHROPIC_PRICING, shared by both constructors.
|
|
13
|
+
Pass your own PricingTable to override, for example when your account bills at a custom rate.
|
|
14
|
+
|
|
15
|
+
Prices are USD per one million tokens,
|
|
16
|
+
taken from the provider's official pricing page: https://platform.claude.com/docs/en/about-claude/pricing.
|
|
17
|
+
Prices are the one provider fact this package cannot verify by SDK introspection;
|
|
18
|
+
re-check the page before relying on a table for billing.
|
|
19
|
+
Rates derive from the base input price: cache read 0.1x, 5-minute cache write 1.25x, 1-hour cache write 2x.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
from dataclasses import dataclass
|
|
23
|
+
from typing import Literal
|
|
24
|
+
|
|
25
|
+
try:
|
|
26
|
+
from anthropic import AsyncAnthropic, AsyncAnthropicBedrock, AsyncAnthropicBedrockMantle
|
|
27
|
+
except ModuleNotFoundError as exc:
|
|
28
|
+
if exc.name != "anthropic":
|
|
29
|
+
raise
|
|
30
|
+
raise ModuleNotFoundError(
|
|
31
|
+
"langchaint's anthropic backend requires the anthropic package; install langchaint[anthropic]."
|
|
32
|
+
) from exc
|
|
33
|
+
|
|
34
|
+
from langchaint.anthropic.messages_provider import AnthropicMessagesProvider, CacheTtl
|
|
35
|
+
from langchaint.llm import LLM
|
|
36
|
+
from langchaint.provider import PricingTable
|
|
37
|
+
from langchaint.rate_limiter import RateLimiter
|
|
38
|
+
|
|
39
|
+
type AnthropicModelName = Literal[
|
|
40
|
+
"claude-fable-5",
|
|
41
|
+
"claude-sonnet-4-6",
|
|
42
|
+
"claude-sonnet-5",
|
|
43
|
+
"claude-opus-4-6",
|
|
44
|
+
"claude-opus-4-7",
|
|
45
|
+
"claude-opus-4-8",
|
|
46
|
+
"claude-haiku-4-5-20251001",
|
|
47
|
+
]
|
|
48
|
+
"""Model identifiers with public prices in ANTHROPIC_PRICING."""
|
|
49
|
+
|
|
50
|
+
ANTHROPIC_PRICING: dict[AnthropicModelName, PricingTable] = {
|
|
51
|
+
"claude-fable-5": PricingTable(
|
|
52
|
+
input_cache_none_usd_per_million_tokens=10.00,
|
|
53
|
+
output_usd_per_million_tokens=50.00,
|
|
54
|
+
cache_read_usd_per_million_tokens=1.00,
|
|
55
|
+
cache_write_usd_per_million_tokens=12.50,
|
|
56
|
+
cache_write_1h_usd_per_million_tokens=20.00,
|
|
57
|
+
),
|
|
58
|
+
"claude-sonnet-4-6": PricingTable(
|
|
59
|
+
input_cache_none_usd_per_million_tokens=3.00,
|
|
60
|
+
output_usd_per_million_tokens=15.00,
|
|
61
|
+
cache_read_usd_per_million_tokens=0.30,
|
|
62
|
+
cache_write_usd_per_million_tokens=3.75,
|
|
63
|
+
cache_write_1h_usd_per_million_tokens=6.00,
|
|
64
|
+
),
|
|
65
|
+
# introductory pricing, through 2026-08-31; standard 3.00/15.00 from 2026-09-01
|
|
66
|
+
"claude-sonnet-5": PricingTable(
|
|
67
|
+
input_cache_none_usd_per_million_tokens=2.00,
|
|
68
|
+
output_usd_per_million_tokens=10.00,
|
|
69
|
+
cache_read_usd_per_million_tokens=0.20,
|
|
70
|
+
cache_write_usd_per_million_tokens=2.50,
|
|
71
|
+
cache_write_1h_usd_per_million_tokens=4.00,
|
|
72
|
+
),
|
|
73
|
+
"claude-opus-4-6": PricingTable(
|
|
74
|
+
input_cache_none_usd_per_million_tokens=5.00,
|
|
75
|
+
output_usd_per_million_tokens=25.00,
|
|
76
|
+
cache_read_usd_per_million_tokens=0.50,
|
|
77
|
+
cache_write_usd_per_million_tokens=6.25,
|
|
78
|
+
cache_write_1h_usd_per_million_tokens=10.00,
|
|
79
|
+
),
|
|
80
|
+
"claude-opus-4-7": PricingTable(
|
|
81
|
+
input_cache_none_usd_per_million_tokens=5.00,
|
|
82
|
+
output_usd_per_million_tokens=25.00,
|
|
83
|
+
cache_read_usd_per_million_tokens=0.50,
|
|
84
|
+
cache_write_usd_per_million_tokens=6.25,
|
|
85
|
+
cache_write_1h_usd_per_million_tokens=10.00,
|
|
86
|
+
),
|
|
87
|
+
"claude-opus-4-8": PricingTable(
|
|
88
|
+
input_cache_none_usd_per_million_tokens=5.00,
|
|
89
|
+
output_usd_per_million_tokens=25.00,
|
|
90
|
+
cache_read_usd_per_million_tokens=0.50,
|
|
91
|
+
cache_write_usd_per_million_tokens=6.25,
|
|
92
|
+
cache_write_1h_usd_per_million_tokens=10.00,
|
|
93
|
+
),
|
|
94
|
+
"claude-haiku-4-5-20251001": PricingTable(
|
|
95
|
+
input_cache_none_usd_per_million_tokens=1.00,
|
|
96
|
+
output_usd_per_million_tokens=5.00,
|
|
97
|
+
cache_read_usd_per_million_tokens=0.10,
|
|
98
|
+
cache_write_usd_per_million_tokens=1.25,
|
|
99
|
+
cache_write_1h_usd_per_million_tokens=2.00,
|
|
100
|
+
),
|
|
101
|
+
}
|
|
102
|
+
"""Public prices per anthropic model; the default pricing lookup, shared by both constructors."""
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
@dataclass(frozen=True, kw_only=True)
|
|
106
|
+
class BedrockRouting:
|
|
107
|
+
"""How one catalog model reaches Bedrock: which surface, and the ready-to-send wire model id.
|
|
108
|
+
|
|
109
|
+
surface selects the client class in _BEDROCK_CLIENT_CLASS.
|
|
110
|
+
"mantle" is the "Claude in Amazon Bedrock" Messages-API surface (AsyncAnthropicBedrockMantle),
|
|
111
|
+
"legacy" the InvokeModel surface (AsyncAnthropicBedrock).
|
|
112
|
+
wire_model is the id Bedrock accepts, with any inference-profile prefix already applied;
|
|
113
|
+
it is not derivable from the native name by a rule, so it is stored per model.
|
|
114
|
+
"""
|
|
115
|
+
|
|
116
|
+
surface: Literal["mantle", "legacy"]
|
|
117
|
+
wire_model: str
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
ANTHROPIC_BEDROCK: dict[AnthropicModelName, BedrockRouting] = {
|
|
121
|
+
"claude-fable-5": BedrockRouting(surface="mantle", wire_model="anthropic.claude-fable-5"),
|
|
122
|
+
"claude-opus-4-8": BedrockRouting(surface="mantle", wire_model="anthropic.claude-opus-4-8"),
|
|
123
|
+
"claude-opus-4-7": BedrockRouting(surface="mantle", wire_model="anthropic.claude-opus-4-7"),
|
|
124
|
+
"claude-sonnet-5": BedrockRouting(surface="mantle", wire_model="anthropic.claude-sonnet-5"),
|
|
125
|
+
"claude-haiku-4-5-20251001": BedrockRouting(surface="mantle", wire_model="anthropic.claude-haiku-4-5"),
|
|
126
|
+
"claude-opus-4-6": BedrockRouting(surface="legacy", wire_model="us.anthropic.claude-opus-4-6-v1"),
|
|
127
|
+
"claude-sonnet-4-6": BedrockRouting(surface="legacy", wire_model="us.anthropic.claude-sonnet-4-6"),
|
|
128
|
+
}
|
|
129
|
+
"""Per-model Bedrock routing; total over AnthropicModelName so a new catalog model must add an entry."""
|
|
130
|
+
|
|
131
|
+
_BEDROCK_CLIENT_CLASS: dict[
|
|
132
|
+
Literal["mantle", "legacy"], type[AsyncAnthropicBedrockMantle | AsyncAnthropicBedrock]
|
|
133
|
+
] = {
|
|
134
|
+
"mantle": AsyncAnthropicBedrockMantle,
|
|
135
|
+
"legacy": AsyncAnthropicBedrock,
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def anthropic_model(
|
|
140
|
+
model: AnthropicModelName,
|
|
141
|
+
*,
|
|
142
|
+
client: AsyncAnthropic | AsyncAnthropicBedrock | None = None,
|
|
143
|
+
pricing: PricingTable | None = None,
|
|
144
|
+
default_max_completion_tokens: int = 4096,
|
|
145
|
+
cache_ttl: CacheTtl = "5m",
|
|
146
|
+
rate_limiter: RateLimiter | None = None,
|
|
147
|
+
) -> LLM:
|
|
148
|
+
"""Build a ready LLM for one cataloged model on the Messages API.
|
|
149
|
+
|
|
150
|
+
client None constructs AsyncAnthropic(), which reads ANTHROPIC_API_KEY from the environment.
|
|
151
|
+
pricing None selects ANTHROPIC_PRICING[model].
|
|
152
|
+
cache_ttl applies uniformly to every cache marker the adapter writes:
|
|
153
|
+
"5m" is the API default; "1h" holds entries across longer gaps and bills writes at 2x instead of 1.25x,
|
|
154
|
+
paying off when requests reusing the prefix arrive more than five minutes apart.
|
|
155
|
+
rate_limiter None means the RateLimiter defaults;
|
|
156
|
+
pass one shared instance across models on the same account to share its budget,
|
|
157
|
+
built in the same event loop as the LLMs, since one instance serves one loop.
|
|
158
|
+
"""
|
|
159
|
+
return LLM(
|
|
160
|
+
AnthropicMessagesProvider(
|
|
161
|
+
client=client if client is not None else AsyncAnthropic(),
|
|
162
|
+
model=model,
|
|
163
|
+
pricing=pricing if pricing is not None else ANTHROPIC_PRICING[model],
|
|
164
|
+
default_max_completion_tokens=default_max_completion_tokens,
|
|
165
|
+
cache_ttl=cache_ttl,
|
|
166
|
+
),
|
|
167
|
+
rate_limiter=rate_limiter,
|
|
168
|
+
)
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def anthropic_bedrock_model(
|
|
172
|
+
model: AnthropicModelName,
|
|
173
|
+
*,
|
|
174
|
+
aws_region: str | None = None,
|
|
175
|
+
client: AsyncAnthropicBedrock | AsyncAnthropicBedrockMantle | None = None,
|
|
176
|
+
pricing: PricingTable | None = None,
|
|
177
|
+
default_max_completion_tokens: int = 4096,
|
|
178
|
+
cache_ttl: CacheTtl = "5m",
|
|
179
|
+
rate_limiter: RateLimiter | None = None,
|
|
180
|
+
) -> LLM:
|
|
181
|
+
"""Build a ready LLM for one cataloged model on Bedrock.
|
|
182
|
+
|
|
183
|
+
The model's Bedrock surface and wire model id come from ANTHROPIC_BEDROCK[model],
|
|
184
|
+
so the application names neither the client class nor the Bedrock id, only the native model.
|
|
185
|
+
client None constructs the surface's client class with aws_region
|
|
186
|
+
(None resolves the region from the AWS credential chain).
|
|
187
|
+
Pass client to supply your own; it must match the model's surface.
|
|
188
|
+
pricing None selects ANTHROPIC_PRICING[model], the same table anthropic_model uses:
|
|
189
|
+
the default is Anthropic's first-party list price, an estimate on Bedrock (AWS sets the real rate),
|
|
190
|
+
corrected by passing pricing.
|
|
191
|
+
cache_ttl has the anthropic_model meaning; Bedrock supports both tiers.
|
|
192
|
+
rate_limiter None means the RateLimiter defaults;
|
|
193
|
+
pass one shared instance across models on the same account to share its budget,
|
|
194
|
+
built in the same event loop as the LLMs, since one instance serves one loop.
|
|
195
|
+
|
|
196
|
+
Raises:
|
|
197
|
+
ValueError: client is provided but its class does not serve model's Bedrock surface.
|
|
198
|
+
"""
|
|
199
|
+
routing = ANTHROPIC_BEDROCK[model]
|
|
200
|
+
if client is None:
|
|
201
|
+
client = _BEDROCK_CLIENT_CLASS[routing.surface](aws_region=aws_region)
|
|
202
|
+
else:
|
|
203
|
+
required_class = _BEDROCK_CLIENT_CLASS[routing.surface]
|
|
204
|
+
if not isinstance(client, required_class):
|
|
205
|
+
raise ValueError(
|
|
206
|
+
f"{model!r} is served on the {routing.surface!r} Bedrock surface, which requires a "
|
|
207
|
+
f"{required_class.__name__} client, but a {type(client).__name__} was passed."
|
|
208
|
+
)
|
|
209
|
+
return LLM(
|
|
210
|
+
AnthropicMessagesProvider(
|
|
211
|
+
client=client,
|
|
212
|
+
model=routing.wire_model,
|
|
213
|
+
pricing=pricing if pricing is not None else ANTHROPIC_PRICING[model],
|
|
214
|
+
default_max_completion_tokens=default_max_completion_tokens,
|
|
215
|
+
cache_ttl=cache_ttl,
|
|
216
|
+
),
|
|
217
|
+
rate_limiter=rate_limiter,
|
|
218
|
+
)
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
__all__ = [
|
|
222
|
+
"ANTHROPIC_BEDROCK",
|
|
223
|
+
"ANTHROPIC_PRICING",
|
|
224
|
+
"AnthropicMessagesProvider",
|
|
225
|
+
"AnthropicModelName",
|
|
226
|
+
"BedrockRouting",
|
|
227
|
+
"CacheTtl",
|
|
228
|
+
"anthropic_bedrock_model",
|
|
229
|
+
"anthropic_model",
|
|
230
|
+
]
|