langchain-core 0.4.0.dev0__py3-none-any.whl → 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.
Potentially problematic release.
This version of langchain-core might be problematic. Click here for more details.
- langchain_core/__init__.py +1 -1
- langchain_core/_api/__init__.py +3 -4
- langchain_core/_api/beta_decorator.py +45 -70
- langchain_core/_api/deprecation.py +80 -80
- langchain_core/_api/path.py +22 -8
- langchain_core/_import_utils.py +10 -4
- langchain_core/agents.py +25 -21
- langchain_core/caches.py +53 -63
- langchain_core/callbacks/__init__.py +1 -8
- langchain_core/callbacks/base.py +341 -348
- langchain_core/callbacks/file.py +55 -44
- langchain_core/callbacks/manager.py +546 -683
- langchain_core/callbacks/stdout.py +29 -30
- langchain_core/callbacks/streaming_stdout.py +35 -36
- langchain_core/callbacks/usage.py +65 -70
- langchain_core/chat_history.py +48 -55
- langchain_core/document_loaders/base.py +46 -21
- langchain_core/document_loaders/langsmith.py +39 -36
- langchain_core/documents/__init__.py +0 -1
- langchain_core/documents/base.py +96 -74
- langchain_core/documents/compressor.py +12 -9
- langchain_core/documents/transformers.py +29 -28
- langchain_core/embeddings/fake.py +56 -57
- langchain_core/env.py +2 -3
- langchain_core/example_selectors/base.py +12 -0
- langchain_core/example_selectors/length_based.py +1 -1
- langchain_core/example_selectors/semantic_similarity.py +21 -25
- langchain_core/exceptions.py +15 -9
- langchain_core/globals.py +4 -163
- langchain_core/indexing/api.py +132 -125
- langchain_core/indexing/base.py +64 -67
- langchain_core/indexing/in_memory.py +26 -6
- langchain_core/language_models/__init__.py +15 -27
- langchain_core/language_models/_utils.py +267 -117
- langchain_core/language_models/base.py +92 -177
- langchain_core/language_models/chat_models.py +547 -407
- langchain_core/language_models/fake.py +11 -11
- langchain_core/language_models/fake_chat_models.py +72 -118
- langchain_core/language_models/llms.py +168 -242
- langchain_core/load/dump.py +8 -11
- langchain_core/load/load.py +32 -28
- langchain_core/load/mapping.py +2 -4
- langchain_core/load/serializable.py +50 -56
- langchain_core/messages/__init__.py +36 -51
- langchain_core/messages/ai.py +377 -150
- langchain_core/messages/base.py +239 -47
- langchain_core/messages/block_translators/__init__.py +111 -0
- langchain_core/messages/block_translators/anthropic.py +470 -0
- langchain_core/messages/block_translators/bedrock.py +94 -0
- langchain_core/messages/block_translators/bedrock_converse.py +297 -0
- langchain_core/messages/block_translators/google_genai.py +530 -0
- langchain_core/messages/block_translators/google_vertexai.py +21 -0
- langchain_core/messages/block_translators/groq.py +143 -0
- langchain_core/messages/block_translators/langchain_v0.py +301 -0
- langchain_core/messages/block_translators/openai.py +1010 -0
- langchain_core/messages/chat.py +2 -3
- langchain_core/messages/content.py +1423 -0
- langchain_core/messages/function.py +7 -7
- langchain_core/messages/human.py +44 -38
- langchain_core/messages/modifier.py +3 -2
- langchain_core/messages/system.py +40 -27
- langchain_core/messages/tool.py +160 -58
- langchain_core/messages/utils.py +527 -638
- langchain_core/output_parsers/__init__.py +1 -14
- langchain_core/output_parsers/base.py +68 -104
- langchain_core/output_parsers/json.py +13 -17
- langchain_core/output_parsers/list.py +11 -33
- langchain_core/output_parsers/openai_functions.py +56 -74
- langchain_core/output_parsers/openai_tools.py +68 -109
- langchain_core/output_parsers/pydantic.py +15 -13
- langchain_core/output_parsers/string.py +6 -2
- langchain_core/output_parsers/transform.py +17 -60
- langchain_core/output_parsers/xml.py +34 -44
- langchain_core/outputs/__init__.py +1 -1
- langchain_core/outputs/chat_generation.py +26 -11
- langchain_core/outputs/chat_result.py +1 -3
- langchain_core/outputs/generation.py +17 -6
- langchain_core/outputs/llm_result.py +15 -8
- langchain_core/prompt_values.py +29 -123
- langchain_core/prompts/__init__.py +3 -27
- langchain_core/prompts/base.py +48 -63
- langchain_core/prompts/chat.py +259 -288
- langchain_core/prompts/dict.py +19 -11
- langchain_core/prompts/few_shot.py +84 -90
- langchain_core/prompts/few_shot_with_templates.py +14 -12
- langchain_core/prompts/image.py +19 -14
- langchain_core/prompts/loading.py +6 -8
- langchain_core/prompts/message.py +7 -8
- langchain_core/prompts/prompt.py +42 -43
- langchain_core/prompts/string.py +37 -16
- langchain_core/prompts/structured.py +43 -46
- langchain_core/rate_limiters.py +51 -60
- langchain_core/retrievers.py +52 -192
- langchain_core/runnables/base.py +1727 -1683
- langchain_core/runnables/branch.py +52 -73
- langchain_core/runnables/config.py +89 -103
- langchain_core/runnables/configurable.py +128 -130
- langchain_core/runnables/fallbacks.py +93 -82
- langchain_core/runnables/graph.py +127 -127
- langchain_core/runnables/graph_ascii.py +63 -41
- langchain_core/runnables/graph_mermaid.py +87 -70
- langchain_core/runnables/graph_png.py +31 -36
- langchain_core/runnables/history.py +145 -161
- langchain_core/runnables/passthrough.py +141 -144
- langchain_core/runnables/retry.py +84 -68
- langchain_core/runnables/router.py +33 -37
- langchain_core/runnables/schema.py +79 -72
- langchain_core/runnables/utils.py +95 -139
- langchain_core/stores.py +85 -131
- langchain_core/structured_query.py +11 -15
- langchain_core/sys_info.py +31 -32
- langchain_core/tools/__init__.py +1 -14
- langchain_core/tools/base.py +221 -247
- langchain_core/tools/convert.py +144 -161
- langchain_core/tools/render.py +10 -10
- langchain_core/tools/retriever.py +12 -19
- langchain_core/tools/simple.py +52 -29
- langchain_core/tools/structured.py +56 -60
- langchain_core/tracers/__init__.py +1 -9
- langchain_core/tracers/_streaming.py +6 -7
- langchain_core/tracers/base.py +103 -112
- langchain_core/tracers/context.py +29 -48
- langchain_core/tracers/core.py +142 -105
- langchain_core/tracers/evaluation.py +30 -34
- langchain_core/tracers/event_stream.py +162 -117
- langchain_core/tracers/langchain.py +34 -36
- langchain_core/tracers/log_stream.py +87 -49
- langchain_core/tracers/memory_stream.py +3 -3
- langchain_core/tracers/root_listeners.py +18 -34
- langchain_core/tracers/run_collector.py +8 -20
- langchain_core/tracers/schemas.py +0 -125
- langchain_core/tracers/stdout.py +3 -3
- langchain_core/utils/__init__.py +1 -4
- langchain_core/utils/_merge.py +47 -9
- langchain_core/utils/aiter.py +70 -66
- langchain_core/utils/env.py +12 -9
- langchain_core/utils/function_calling.py +139 -206
- langchain_core/utils/html.py +7 -8
- langchain_core/utils/input.py +6 -6
- langchain_core/utils/interactive_env.py +6 -2
- langchain_core/utils/iter.py +48 -45
- langchain_core/utils/json.py +14 -4
- langchain_core/utils/json_schema.py +159 -43
- langchain_core/utils/mustache.py +32 -25
- langchain_core/utils/pydantic.py +67 -40
- langchain_core/utils/strings.py +5 -5
- langchain_core/utils/usage.py +1 -1
- langchain_core/utils/utils.py +104 -62
- langchain_core/vectorstores/base.py +131 -179
- langchain_core/vectorstores/in_memory.py +113 -182
- langchain_core/vectorstores/utils.py +23 -17
- langchain_core/version.py +1 -1
- langchain_core-1.0.0.dist-info/METADATA +68 -0
- langchain_core-1.0.0.dist-info/RECORD +172 -0
- {langchain_core-0.4.0.dev0.dist-info → langchain_core-1.0.0.dist-info}/WHEEL +1 -1
- langchain_core/beta/__init__.py +0 -1
- langchain_core/beta/runnables/__init__.py +0 -1
- langchain_core/beta/runnables/context.py +0 -448
- langchain_core/memory.py +0 -116
- langchain_core/messages/content_blocks.py +0 -1435
- langchain_core/prompts/pipeline.py +0 -133
- langchain_core/pydantic_v1/__init__.py +0 -30
- langchain_core/pydantic_v1/dataclasses.py +0 -23
- langchain_core/pydantic_v1/main.py +0 -23
- langchain_core/tracers/langchain_v1.py +0 -23
- langchain_core/utils/loading.py +0 -31
- langchain_core/v1/__init__.py +0 -1
- langchain_core/v1/chat_models.py +0 -1047
- langchain_core/v1/messages.py +0 -755
- langchain_core-0.4.0.dev0.dist-info/METADATA +0 -108
- langchain_core-0.4.0.dev0.dist-info/RECORD +0 -177
- langchain_core-0.4.0.dev0.dist-info/entry_points.txt +0 -4
|
@@ -7,21 +7,17 @@ import functools
|
|
|
7
7
|
import inspect
|
|
8
8
|
import json
|
|
9
9
|
import logging
|
|
10
|
-
import warnings
|
|
11
10
|
from abc import ABC, abstractmethod
|
|
12
|
-
from collections.abc import AsyncIterator, Iterator, Sequence
|
|
11
|
+
from collections.abc import AsyncIterator, Callable, Iterator, Sequence
|
|
13
12
|
from pathlib import Path
|
|
14
13
|
from typing import (
|
|
15
14
|
TYPE_CHECKING,
|
|
16
15
|
Any,
|
|
17
|
-
Callable,
|
|
18
|
-
Optional,
|
|
19
|
-
Union,
|
|
20
16
|
cast,
|
|
21
17
|
)
|
|
22
18
|
|
|
23
19
|
import yaml
|
|
24
|
-
from pydantic import ConfigDict
|
|
20
|
+
from pydantic import ConfigDict
|
|
25
21
|
from tenacity import (
|
|
26
22
|
RetryCallState,
|
|
27
23
|
before_sleep_log,
|
|
@@ -33,7 +29,6 @@ from tenacity import (
|
|
|
33
29
|
)
|
|
34
30
|
from typing_extensions import override
|
|
35
31
|
|
|
36
|
-
from langchain_core._api import deprecated
|
|
37
32
|
from langchain_core.caches import BaseCache
|
|
38
33
|
from langchain_core.callbacks import (
|
|
39
34
|
AsyncCallbackManager,
|
|
@@ -51,10 +46,7 @@ from langchain_core.language_models.base import (
|
|
|
51
46
|
)
|
|
52
47
|
from langchain_core.load import dumpd
|
|
53
48
|
from langchain_core.messages import (
|
|
54
|
-
AIMessage,
|
|
55
|
-
BaseMessage,
|
|
56
49
|
convert_to_messages,
|
|
57
|
-
get_buffer_string,
|
|
58
50
|
)
|
|
59
51
|
from langchain_core.outputs import Generation, GenerationChunk, LLMResult, RunInfo
|
|
60
52
|
from langchain_core.prompt_values import ChatPromptValue, PromptValue, StringPromptValue
|
|
@@ -76,16 +68,14 @@ def _log_error_once(msg: str) -> None:
|
|
|
76
68
|
def create_base_retry_decorator(
|
|
77
69
|
error_types: list[type[BaseException]],
|
|
78
70
|
max_retries: int = 1,
|
|
79
|
-
run_manager:
|
|
80
|
-
Union[AsyncCallbackManagerForLLMRun, CallbackManagerForLLMRun]
|
|
81
|
-
] = None,
|
|
71
|
+
run_manager: AsyncCallbackManagerForLLMRun | CallbackManagerForLLMRun | None = None,
|
|
82
72
|
) -> Callable[[Any], Any]:
|
|
83
73
|
"""Create a retry decorator for a given LLM and provided a list of error types.
|
|
84
74
|
|
|
85
75
|
Args:
|
|
86
76
|
error_types: List of error types to retry on.
|
|
87
|
-
max_retries: Number of retries.
|
|
88
|
-
run_manager: Callback manager for the run.
|
|
77
|
+
max_retries: Number of retries.
|
|
78
|
+
run_manager: Callback manager for the run.
|
|
89
79
|
|
|
90
80
|
Returns:
|
|
91
81
|
A retry decorator.
|
|
@@ -101,13 +91,17 @@ def create_base_retry_decorator(
|
|
|
101
91
|
if isinstance(run_manager, AsyncCallbackManagerForLLMRun):
|
|
102
92
|
coro = run_manager.on_retry(retry_state)
|
|
103
93
|
try:
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
# and be awaited somewhere
|
|
108
|
-
loop.create_task(coro) # noqa: RUF006
|
|
109
|
-
else:
|
|
94
|
+
try:
|
|
95
|
+
loop = asyncio.get_event_loop()
|
|
96
|
+
except RuntimeError:
|
|
110
97
|
asyncio.run(coro)
|
|
98
|
+
else:
|
|
99
|
+
if loop.is_running():
|
|
100
|
+
# TODO: Fix RUF006 - this task should have a reference
|
|
101
|
+
# and be awaited somewhere
|
|
102
|
+
loop.create_task(coro) # noqa: RUF006
|
|
103
|
+
else:
|
|
104
|
+
asyncio.run(coro)
|
|
111
105
|
except Exception as e:
|
|
112
106
|
_log_error_once(f"Error in on_retry: {e}")
|
|
113
107
|
else:
|
|
@@ -129,8 +123,9 @@ def create_base_retry_decorator(
|
|
|
129
123
|
)
|
|
130
124
|
|
|
131
125
|
|
|
132
|
-
def _resolve_cache(*, cache:
|
|
126
|
+
def _resolve_cache(*, cache: BaseCache | bool | None) -> BaseCache | None:
|
|
133
127
|
"""Resolve the cache."""
|
|
128
|
+
llm_cache: BaseCache | None
|
|
134
129
|
if isinstance(cache, BaseCache):
|
|
135
130
|
llm_cache = cache
|
|
136
131
|
elif cache is None:
|
|
@@ -155,14 +150,14 @@ def _resolve_cache(*, cache: Union[BaseCache, bool, None]) -> Optional[BaseCache
|
|
|
155
150
|
def get_prompts(
|
|
156
151
|
params: dict[str, Any],
|
|
157
152
|
prompts: list[str],
|
|
158
|
-
cache:
|
|
153
|
+
cache: BaseCache | bool | None = None, # noqa: FBT001
|
|
159
154
|
) -> tuple[dict[int, list], str, list[int], list[str]]:
|
|
160
155
|
"""Get prompts that are already cached.
|
|
161
156
|
|
|
162
157
|
Args:
|
|
163
158
|
params: Dictionary of parameters.
|
|
164
159
|
prompts: List of prompts.
|
|
165
|
-
cache: Cache object.
|
|
160
|
+
cache: Cache object.
|
|
166
161
|
|
|
167
162
|
Returns:
|
|
168
163
|
A tuple of existing prompts, llm_string, missing prompt indexes,
|
|
@@ -191,14 +186,14 @@ def get_prompts(
|
|
|
191
186
|
async def aget_prompts(
|
|
192
187
|
params: dict[str, Any],
|
|
193
188
|
prompts: list[str],
|
|
194
|
-
cache:
|
|
189
|
+
cache: BaseCache | bool | None = None, # noqa: FBT001
|
|
195
190
|
) -> tuple[dict[int, list], str, list[int], list[str]]:
|
|
196
191
|
"""Get prompts that are already cached. Async version.
|
|
197
192
|
|
|
198
193
|
Args:
|
|
199
194
|
params: Dictionary of parameters.
|
|
200
195
|
prompts: List of prompts.
|
|
201
|
-
cache: Cache object.
|
|
196
|
+
cache: Cache object.
|
|
202
197
|
|
|
203
198
|
Returns:
|
|
204
199
|
A tuple of existing prompts, llm_string, missing prompt indexes,
|
|
@@ -224,13 +219,13 @@ async def aget_prompts(
|
|
|
224
219
|
|
|
225
220
|
|
|
226
221
|
def update_cache(
|
|
227
|
-
cache:
|
|
222
|
+
cache: BaseCache | bool | None, # noqa: FBT001
|
|
228
223
|
existing_prompts: dict[int, list],
|
|
229
224
|
llm_string: str,
|
|
230
225
|
missing_prompt_idxs: list[int],
|
|
231
226
|
new_results: LLMResult,
|
|
232
227
|
prompts: list[str],
|
|
233
|
-
) ->
|
|
228
|
+
) -> dict | None:
|
|
234
229
|
"""Update the cache and get the LLM output.
|
|
235
230
|
|
|
236
231
|
Args:
|
|
@@ -257,13 +252,13 @@ def update_cache(
|
|
|
257
252
|
|
|
258
253
|
|
|
259
254
|
async def aupdate_cache(
|
|
260
|
-
cache:
|
|
255
|
+
cache: BaseCache | bool | None, # noqa: FBT001
|
|
261
256
|
existing_prompts: dict[int, list],
|
|
262
257
|
llm_string: str,
|
|
263
258
|
missing_prompt_idxs: list[int],
|
|
264
259
|
new_results: LLMResult,
|
|
265
260
|
prompts: list[str],
|
|
266
|
-
) ->
|
|
261
|
+
) -> dict | None:
|
|
267
262
|
"""Update the cache and get the LLM output. Async version.
|
|
268
263
|
|
|
269
264
|
Args:
|
|
@@ -295,26 +290,10 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
295
290
|
It should take in a prompt and return a string.
|
|
296
291
|
"""
|
|
297
292
|
|
|
298
|
-
callback_manager: Optional[BaseCallbackManager] = Field(default=None, exclude=True)
|
|
299
|
-
"""[DEPRECATED]"""
|
|
300
|
-
|
|
301
293
|
model_config = ConfigDict(
|
|
302
294
|
arbitrary_types_allowed=True,
|
|
303
295
|
)
|
|
304
296
|
|
|
305
|
-
@model_validator(mode="before")
|
|
306
|
-
@classmethod
|
|
307
|
-
def raise_deprecation(cls, values: dict) -> Any:
|
|
308
|
-
"""Raise deprecation warning if callback_manager is used."""
|
|
309
|
-
if values.get("callback_manager") is not None:
|
|
310
|
-
warnings.warn(
|
|
311
|
-
"callback_manager is deprecated. Please use callbacks instead.",
|
|
312
|
-
DeprecationWarning,
|
|
313
|
-
stacklevel=5,
|
|
314
|
-
)
|
|
315
|
-
values["callbacks"] = values.pop("callback_manager", None)
|
|
316
|
-
return values
|
|
317
|
-
|
|
318
297
|
@functools.cached_property
|
|
319
298
|
def _serialized(self) -> dict[str, Any]:
|
|
320
299
|
return dumpd(self)
|
|
@@ -324,7 +303,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
324
303
|
@property
|
|
325
304
|
@override
|
|
326
305
|
def OutputType(self) -> type[str]:
|
|
327
|
-
"""Get the input type for this
|
|
306
|
+
"""Get the input type for this `Runnable`."""
|
|
328
307
|
return str
|
|
329
308
|
|
|
330
309
|
def _convert_input(self, model_input: LanguageModelInput) -> PromptValue:
|
|
@@ -342,7 +321,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
342
321
|
|
|
343
322
|
def _get_ls_params(
|
|
344
323
|
self,
|
|
345
|
-
stop:
|
|
324
|
+
stop: list[str] | None = None,
|
|
346
325
|
**kwargs: Any,
|
|
347
326
|
) -> LangSmithParams:
|
|
348
327
|
"""Get standard params for tracing."""
|
|
@@ -356,7 +335,9 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
356
335
|
ls_params["ls_stop"] = stop
|
|
357
336
|
|
|
358
337
|
# model
|
|
359
|
-
if
|
|
338
|
+
if "model" in kwargs and isinstance(kwargs["model"], str):
|
|
339
|
+
ls_params["ls_model_name"] = kwargs["model"]
|
|
340
|
+
elif hasattr(self, "model") and isinstance(self.model, str):
|
|
360
341
|
ls_params["ls_model_name"] = self.model
|
|
361
342
|
elif hasattr(self, "model_name") and isinstance(self.model_name, str):
|
|
362
343
|
ls_params["ls_model_name"] = self.model_name
|
|
@@ -379,9 +360,9 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
379
360
|
def invoke(
|
|
380
361
|
self,
|
|
381
362
|
input: LanguageModelInput,
|
|
382
|
-
config:
|
|
363
|
+
config: RunnableConfig | None = None,
|
|
383
364
|
*,
|
|
384
|
-
stop:
|
|
365
|
+
stop: list[str] | None = None,
|
|
385
366
|
**kwargs: Any,
|
|
386
367
|
) -> str:
|
|
387
368
|
config = ensure_config(config)
|
|
@@ -404,9 +385,9 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
404
385
|
async def ainvoke(
|
|
405
386
|
self,
|
|
406
387
|
input: LanguageModelInput,
|
|
407
|
-
config:
|
|
388
|
+
config: RunnableConfig | None = None,
|
|
408
389
|
*,
|
|
409
|
-
stop:
|
|
390
|
+
stop: list[str] | None = None,
|
|
410
391
|
**kwargs: Any,
|
|
411
392
|
) -> str:
|
|
412
393
|
config = ensure_config(config)
|
|
@@ -426,7 +407,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
426
407
|
def batch(
|
|
427
408
|
self,
|
|
428
409
|
inputs: list[LanguageModelInput],
|
|
429
|
-
config:
|
|
410
|
+
config: RunnableConfig | list[RunnableConfig] | None = None,
|
|
430
411
|
*,
|
|
431
412
|
return_exceptions: bool = False,
|
|
432
413
|
**kwargs: Any,
|
|
@@ -473,7 +454,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
473
454
|
async def abatch(
|
|
474
455
|
self,
|
|
475
456
|
inputs: list[LanguageModelInput],
|
|
476
|
-
config:
|
|
457
|
+
config: RunnableConfig | list[RunnableConfig] | None = None,
|
|
477
458
|
*,
|
|
478
459
|
return_exceptions: bool = False,
|
|
479
460
|
**kwargs: Any,
|
|
@@ -519,9 +500,9 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
519
500
|
def stream(
|
|
520
501
|
self,
|
|
521
502
|
input: LanguageModelInput,
|
|
522
|
-
config:
|
|
503
|
+
config: RunnableConfig | None = None,
|
|
523
504
|
*,
|
|
524
|
-
stop:
|
|
505
|
+
stop: list[str] | None = None,
|
|
525
506
|
**kwargs: Any,
|
|
526
507
|
) -> Iterator[str]:
|
|
527
508
|
if type(self)._stream == BaseLLM._stream: # noqa: SLF001
|
|
@@ -556,7 +537,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
556
537
|
run_id=config.pop("run_id", None),
|
|
557
538
|
batch_size=1,
|
|
558
539
|
)
|
|
559
|
-
generation:
|
|
540
|
+
generation: GenerationChunk | None = None
|
|
560
541
|
try:
|
|
561
542
|
for chunk in self._stream(
|
|
562
543
|
prompt, stop=stop, run_manager=run_manager, **kwargs
|
|
@@ -586,9 +567,9 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
586
567
|
async def astream(
|
|
587
568
|
self,
|
|
588
569
|
input: LanguageModelInput,
|
|
589
|
-
config:
|
|
570
|
+
config: RunnableConfig | None = None,
|
|
590
571
|
*,
|
|
591
|
-
stop:
|
|
572
|
+
stop: list[str] | None = None,
|
|
592
573
|
**kwargs: Any,
|
|
593
574
|
) -> AsyncIterator[str]:
|
|
594
575
|
if (
|
|
@@ -626,7 +607,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
626
607
|
run_id=config.pop("run_id", None),
|
|
627
608
|
batch_size=1,
|
|
628
609
|
)
|
|
629
|
-
generation:
|
|
610
|
+
generation: GenerationChunk | None = None
|
|
630
611
|
try:
|
|
631
612
|
async for chunk in self._astream(
|
|
632
613
|
prompt,
|
|
@@ -659,20 +640,42 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
659
640
|
def _generate(
|
|
660
641
|
self,
|
|
661
642
|
prompts: list[str],
|
|
662
|
-
stop:
|
|
663
|
-
run_manager:
|
|
643
|
+
stop: list[str] | None = None,
|
|
644
|
+
run_manager: CallbackManagerForLLMRun | None = None,
|
|
664
645
|
**kwargs: Any,
|
|
665
646
|
) -> LLMResult:
|
|
666
|
-
"""Run the LLM on the given prompts.
|
|
647
|
+
"""Run the LLM on the given prompts.
|
|
648
|
+
|
|
649
|
+
Args:
|
|
650
|
+
prompts: The prompts to generate from.
|
|
651
|
+
stop: Stop words to use when generating. Model output is cut off at the
|
|
652
|
+
first occurrence of any of the stop substrings.
|
|
653
|
+
If stop tokens are not supported consider raising NotImplementedError.
|
|
654
|
+
run_manager: Callback manager for the run.
|
|
655
|
+
|
|
656
|
+
Returns:
|
|
657
|
+
The LLM result.
|
|
658
|
+
"""
|
|
667
659
|
|
|
668
660
|
async def _agenerate(
|
|
669
661
|
self,
|
|
670
662
|
prompts: list[str],
|
|
671
|
-
stop:
|
|
672
|
-
run_manager:
|
|
663
|
+
stop: list[str] | None = None,
|
|
664
|
+
run_manager: AsyncCallbackManagerForLLMRun | None = None,
|
|
673
665
|
**kwargs: Any,
|
|
674
666
|
) -> LLMResult:
|
|
675
|
-
"""Run the LLM on the given prompts.
|
|
667
|
+
"""Run the LLM on the given prompts.
|
|
668
|
+
|
|
669
|
+
Args:
|
|
670
|
+
prompts: The prompts to generate from.
|
|
671
|
+
stop: Stop words to use when generating. Model output is cut off at the
|
|
672
|
+
first occurrence of any of the stop substrings.
|
|
673
|
+
If stop tokens are not supported consider raising NotImplementedError.
|
|
674
|
+
run_manager: Callback manager for the run.
|
|
675
|
+
|
|
676
|
+
Returns:
|
|
677
|
+
The LLM result.
|
|
678
|
+
"""
|
|
676
679
|
return await run_in_executor(
|
|
677
680
|
None,
|
|
678
681
|
self._generate,
|
|
@@ -685,8 +688,8 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
685
688
|
def _stream(
|
|
686
689
|
self,
|
|
687
690
|
prompt: str,
|
|
688
|
-
stop:
|
|
689
|
-
run_manager:
|
|
691
|
+
stop: list[str] | None = None,
|
|
692
|
+
run_manager: CallbackManagerForLLMRun | None = None,
|
|
690
693
|
**kwargs: Any,
|
|
691
694
|
) -> Iterator[GenerationChunk]:
|
|
692
695
|
"""Stream the LLM on the given prompt.
|
|
@@ -705,16 +708,16 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
705
708
|
**kwargs: Arbitrary additional keyword arguments. These are usually passed
|
|
706
709
|
to the model provider API call.
|
|
707
710
|
|
|
708
|
-
|
|
709
|
-
|
|
711
|
+
Yields:
|
|
712
|
+
Generation chunks.
|
|
710
713
|
"""
|
|
711
714
|
raise NotImplementedError
|
|
712
715
|
|
|
713
716
|
async def _astream(
|
|
714
717
|
self,
|
|
715
718
|
prompt: str,
|
|
716
|
-
stop:
|
|
717
|
-
run_manager:
|
|
719
|
+
stop: list[str] | None = None,
|
|
720
|
+
run_manager: AsyncCallbackManagerForLLMRun | None = None,
|
|
718
721
|
**kwargs: Any,
|
|
719
722
|
) -> AsyncIterator[GenerationChunk]:
|
|
720
723
|
"""An async version of the _stream method.
|
|
@@ -731,8 +734,8 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
731
734
|
**kwargs: Arbitrary additional keyword arguments. These are usually passed
|
|
732
735
|
to the model provider API call.
|
|
733
736
|
|
|
734
|
-
|
|
735
|
-
|
|
737
|
+
Yields:
|
|
738
|
+
Generation chunks.
|
|
736
739
|
"""
|
|
737
740
|
iterator = await run_in_executor(
|
|
738
741
|
None,
|
|
@@ -758,8 +761,8 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
758
761
|
def generate_prompt(
|
|
759
762
|
self,
|
|
760
763
|
prompts: list[PromptValue],
|
|
761
|
-
stop:
|
|
762
|
-
callbacks:
|
|
764
|
+
stop: list[str] | None = None,
|
|
765
|
+
callbacks: Callbacks | list[Callbacks] | None = None,
|
|
763
766
|
**kwargs: Any,
|
|
764
767
|
) -> LLMResult:
|
|
765
768
|
prompt_strings = [p.to_string() for p in prompts]
|
|
@@ -769,8 +772,8 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
769
772
|
async def agenerate_prompt(
|
|
770
773
|
self,
|
|
771
774
|
prompts: list[PromptValue],
|
|
772
|
-
stop:
|
|
773
|
-
callbacks:
|
|
775
|
+
stop: list[str] | None = None,
|
|
776
|
+
callbacks: Callbacks | list[Callbacks] | None = None,
|
|
774
777
|
**kwargs: Any,
|
|
775
778
|
) -> LLMResult:
|
|
776
779
|
prompt_strings = [p.to_string() for p in prompts]
|
|
@@ -781,7 +784,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
781
784
|
def _generate_helper(
|
|
782
785
|
self,
|
|
783
786
|
prompts: list[str],
|
|
784
|
-
stop:
|
|
787
|
+
stop: list[str] | None,
|
|
785
788
|
run_managers: list[CallbackManagerForLLMRun],
|
|
786
789
|
*,
|
|
787
790
|
new_arg_supported: bool,
|
|
@@ -804,7 +807,9 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
804
807
|
run_manager.on_llm_error(e, response=LLMResult(generations=[]))
|
|
805
808
|
raise
|
|
806
809
|
flattened_outputs = output.flatten()
|
|
807
|
-
for manager, flattened_output in zip(
|
|
810
|
+
for manager, flattened_output in zip(
|
|
811
|
+
run_managers, flattened_outputs, strict=False
|
|
812
|
+
):
|
|
808
813
|
manager.on_llm_end(flattened_output)
|
|
809
814
|
if run_managers:
|
|
810
815
|
output.run = [
|
|
@@ -815,13 +820,13 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
815
820
|
def generate(
|
|
816
821
|
self,
|
|
817
822
|
prompts: list[str],
|
|
818
|
-
stop:
|
|
819
|
-
callbacks:
|
|
823
|
+
stop: list[str] | None = None,
|
|
824
|
+
callbacks: Callbacks | list[Callbacks] | None = None,
|
|
820
825
|
*,
|
|
821
|
-
tags:
|
|
822
|
-
metadata:
|
|
823
|
-
run_name:
|
|
824
|
-
run_id:
|
|
826
|
+
tags: list[str] | list[list[str]] | None = None,
|
|
827
|
+
metadata: dict[str, Any] | list[dict[str, Any]] | None = None,
|
|
828
|
+
run_name: str | list[str] | None = None,
|
|
829
|
+
run_id: uuid.UUID | list[uuid.UUID | None] | None = None,
|
|
825
830
|
**kwargs: Any,
|
|
826
831
|
) -> LLMResult:
|
|
827
832
|
"""Pass a sequence of prompts to a model and return generations.
|
|
@@ -830,16 +835,17 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
830
835
|
API.
|
|
831
836
|
|
|
832
837
|
Use this method when you want to:
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
838
|
+
|
|
839
|
+
1. Take advantage of batched calls,
|
|
840
|
+
2. Need more output from the model than just the top generated value,
|
|
841
|
+
3. Are building chains that are agnostic to the underlying language model
|
|
842
|
+
type (e.g., pure text completion models vs chat models).
|
|
837
843
|
|
|
838
844
|
Args:
|
|
839
845
|
prompts: List of string prompts.
|
|
840
846
|
stop: Stop words to use when generating. Model output is cut off at the
|
|
841
847
|
first occurrence of any of these substrings.
|
|
842
|
-
callbacks: Callbacks to pass through. Used for executing additional
|
|
848
|
+
callbacks: `Callbacks` to pass through. Used for executing additional
|
|
843
849
|
functionality, such as logging or streaming, throughout generation.
|
|
844
850
|
tags: List of tags to associate with each prompt. If provided, the length
|
|
845
851
|
of the list must match the length of the prompts list.
|
|
@@ -853,9 +859,14 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
853
859
|
**kwargs: Arbitrary additional keyword arguments. These are usually passed
|
|
854
860
|
to the model provider API call.
|
|
855
861
|
|
|
862
|
+
Raises:
|
|
863
|
+
ValueError: If prompts is not a list.
|
|
864
|
+
ValueError: If the length of `callbacks`, `tags`, `metadata`, or
|
|
865
|
+
`run_name` (if provided) does not match the length of prompts.
|
|
866
|
+
|
|
856
867
|
Returns:
|
|
857
|
-
An LLMResult
|
|
858
|
-
prompt and additional model provider-specific output.
|
|
868
|
+
An `LLMResult`, which contains a list of candidate `Generations` for each
|
|
869
|
+
input prompt and additional model provider-specific output.
|
|
859
870
|
"""
|
|
860
871
|
if not isinstance(prompts, list):
|
|
861
872
|
msg = (
|
|
@@ -905,14 +916,12 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
905
916
|
msg = "run_name must be a list of the same length as prompts"
|
|
906
917
|
raise ValueError(msg)
|
|
907
918
|
callbacks = cast("list[Callbacks]", callbacks)
|
|
908
|
-
tags_list = cast(
|
|
909
|
-
"list[Optional[list[str]]]", tags or ([None] * len(prompts))
|
|
910
|
-
)
|
|
919
|
+
tags_list = cast("list[list[str] | None]", tags or ([None] * len(prompts)))
|
|
911
920
|
metadata_list = cast(
|
|
912
|
-
"list[
|
|
921
|
+
"list[dict[str, Any] | None]", metadata or ([{}] * len(prompts))
|
|
913
922
|
)
|
|
914
923
|
run_name_list = run_name or cast(
|
|
915
|
-
"list[
|
|
924
|
+
"list[str | None]", ([None] * len(prompts))
|
|
916
925
|
)
|
|
917
926
|
callback_managers = [
|
|
918
927
|
CallbackManager.configure(
|
|
@@ -924,7 +933,9 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
924
933
|
meta,
|
|
925
934
|
self.metadata,
|
|
926
935
|
)
|
|
927
|
-
for callback, tag, meta in zip(
|
|
936
|
+
for callback, tag, meta in zip(
|
|
937
|
+
callbacks, tags_list, metadata_list, strict=False
|
|
938
|
+
)
|
|
928
939
|
]
|
|
929
940
|
else:
|
|
930
941
|
# We've received a single callbacks arg to apply to all inputs
|
|
@@ -939,7 +950,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
939
950
|
self.metadata,
|
|
940
951
|
)
|
|
941
952
|
] * len(prompts)
|
|
942
|
-
run_name_list = [cast("
|
|
953
|
+
run_name_list = [cast("str | None", run_name)] * len(prompts)
|
|
943
954
|
run_ids_list = self._get_run_ids_list(run_id, prompts)
|
|
944
955
|
params = self.dict()
|
|
945
956
|
params["stop"] = stop
|
|
@@ -965,7 +976,11 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
965
976
|
run_id=run_id_,
|
|
966
977
|
)[0]
|
|
967
978
|
for callback_manager, prompt, run_name, run_id_ in zip(
|
|
968
|
-
callback_managers,
|
|
979
|
+
callback_managers,
|
|
980
|
+
prompts,
|
|
981
|
+
run_name_list,
|
|
982
|
+
run_ids_list,
|
|
983
|
+
strict=False,
|
|
969
984
|
)
|
|
970
985
|
]
|
|
971
986
|
return self._generate_helper(
|
|
@@ -1015,7 +1030,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1015
1030
|
|
|
1016
1031
|
@staticmethod
|
|
1017
1032
|
def _get_run_ids_list(
|
|
1018
|
-
run_id:
|
|
1033
|
+
run_id: uuid.UUID | list[uuid.UUID | None] | None, prompts: list
|
|
1019
1034
|
) -> list:
|
|
1020
1035
|
if run_id is None:
|
|
1021
1036
|
return [None] * len(prompts)
|
|
@@ -1032,7 +1047,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1032
1047
|
async def _agenerate_helper(
|
|
1033
1048
|
self,
|
|
1034
1049
|
prompts: list[str],
|
|
1035
|
-
stop:
|
|
1050
|
+
stop: list[str] | None,
|
|
1036
1051
|
run_managers: list[AsyncCallbackManagerForLLMRun],
|
|
1037
1052
|
*,
|
|
1038
1053
|
new_arg_supported: bool,
|
|
@@ -1062,7 +1077,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1062
1077
|
*[
|
|
1063
1078
|
run_manager.on_llm_end(flattened_output)
|
|
1064
1079
|
for run_manager, flattened_output in zip(
|
|
1065
|
-
run_managers, flattened_outputs
|
|
1080
|
+
run_managers, flattened_outputs, strict=False
|
|
1066
1081
|
)
|
|
1067
1082
|
]
|
|
1068
1083
|
)
|
|
@@ -1075,13 +1090,13 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1075
1090
|
async def agenerate(
|
|
1076
1091
|
self,
|
|
1077
1092
|
prompts: list[str],
|
|
1078
|
-
stop:
|
|
1079
|
-
callbacks:
|
|
1093
|
+
stop: list[str] | None = None,
|
|
1094
|
+
callbacks: Callbacks | list[Callbacks] | None = None,
|
|
1080
1095
|
*,
|
|
1081
|
-
tags:
|
|
1082
|
-
metadata:
|
|
1083
|
-
run_name:
|
|
1084
|
-
run_id:
|
|
1096
|
+
tags: list[str] | list[list[str]] | None = None,
|
|
1097
|
+
metadata: dict[str, Any] | list[dict[str, Any]] | None = None,
|
|
1098
|
+
run_name: str | list[str] | None = None,
|
|
1099
|
+
run_id: uuid.UUID | list[uuid.UUID | None] | None = None,
|
|
1085
1100
|
**kwargs: Any,
|
|
1086
1101
|
) -> LLMResult:
|
|
1087
1102
|
"""Asynchronously pass a sequence of prompts to a model and return generations.
|
|
@@ -1090,16 +1105,17 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1090
1105
|
API.
|
|
1091
1106
|
|
|
1092
1107
|
Use this method when you want to:
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1108
|
+
|
|
1109
|
+
1. Take advantage of batched calls,
|
|
1110
|
+
2. Need more output from the model than just the top generated value,
|
|
1111
|
+
3. Are building chains that are agnostic to the underlying language model
|
|
1112
|
+
type (e.g., pure text completion models vs chat models).
|
|
1097
1113
|
|
|
1098
1114
|
Args:
|
|
1099
1115
|
prompts: List of string prompts.
|
|
1100
1116
|
stop: Stop words to use when generating. Model output is cut off at the
|
|
1101
1117
|
first occurrence of any of these substrings.
|
|
1102
|
-
callbacks: Callbacks to pass through. Used for executing additional
|
|
1118
|
+
callbacks: `Callbacks` to pass through. Used for executing additional
|
|
1103
1119
|
functionality, such as logging or streaming, throughout generation.
|
|
1104
1120
|
tags: List of tags to associate with each prompt. If provided, the length
|
|
1105
1121
|
of the list must match the length of the prompts list.
|
|
@@ -1113,9 +1129,13 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1113
1129
|
**kwargs: Arbitrary additional keyword arguments. These are usually passed
|
|
1114
1130
|
to the model provider API call.
|
|
1115
1131
|
|
|
1132
|
+
Raises:
|
|
1133
|
+
ValueError: If the length of `callbacks`, `tags`, `metadata`, or
|
|
1134
|
+
`run_name` (if provided) does not match the length of prompts.
|
|
1135
|
+
|
|
1116
1136
|
Returns:
|
|
1117
|
-
An LLMResult
|
|
1118
|
-
prompt and additional model provider-specific output.
|
|
1137
|
+
An `LLMResult`, which contains a list of candidate `Generations` for each
|
|
1138
|
+
input prompt and additional model provider-specific output.
|
|
1119
1139
|
"""
|
|
1120
1140
|
if isinstance(metadata, list):
|
|
1121
1141
|
metadata = [
|
|
@@ -1155,14 +1175,12 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1155
1175
|
msg = "run_name must be a list of the same length as prompts"
|
|
1156
1176
|
raise ValueError(msg)
|
|
1157
1177
|
callbacks = cast("list[Callbacks]", callbacks)
|
|
1158
|
-
tags_list = cast(
|
|
1159
|
-
"list[Optional[list[str]]]", tags or ([None] * len(prompts))
|
|
1160
|
-
)
|
|
1178
|
+
tags_list = cast("list[list[str] | None]", tags or ([None] * len(prompts)))
|
|
1161
1179
|
metadata_list = cast(
|
|
1162
|
-
"list[
|
|
1180
|
+
"list[dict[str, Any] | None]", metadata or ([{}] * len(prompts))
|
|
1163
1181
|
)
|
|
1164
1182
|
run_name_list = run_name or cast(
|
|
1165
|
-
"list[
|
|
1183
|
+
"list[str | None]", ([None] * len(prompts))
|
|
1166
1184
|
)
|
|
1167
1185
|
callback_managers = [
|
|
1168
1186
|
AsyncCallbackManager.configure(
|
|
@@ -1174,7 +1192,9 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1174
1192
|
meta,
|
|
1175
1193
|
self.metadata,
|
|
1176
1194
|
)
|
|
1177
|
-
for callback, tag, meta in zip(
|
|
1195
|
+
for callback, tag, meta in zip(
|
|
1196
|
+
callbacks, tags_list, metadata_list, strict=False
|
|
1197
|
+
)
|
|
1178
1198
|
]
|
|
1179
1199
|
else:
|
|
1180
1200
|
# We've received a single callbacks arg to apply to all inputs
|
|
@@ -1189,7 +1209,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1189
1209
|
self.metadata,
|
|
1190
1210
|
)
|
|
1191
1211
|
] * len(prompts)
|
|
1192
|
-
run_name_list = [cast("
|
|
1212
|
+
run_name_list = [cast("str | None", run_name)] * len(prompts)
|
|
1193
1213
|
run_ids_list = self._get_run_ids_list(run_id, prompts)
|
|
1194
1214
|
params = self.dict()
|
|
1195
1215
|
params["stop"] = stop
|
|
@@ -1219,7 +1239,11 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1219
1239
|
run_id=run_id_,
|
|
1220
1240
|
)
|
|
1221
1241
|
for callback_manager, prompt, run_name, run_id_ in zip(
|
|
1222
|
-
callback_managers,
|
|
1242
|
+
callback_managers,
|
|
1243
|
+
prompts,
|
|
1244
|
+
run_name_list,
|
|
1245
|
+
run_ids_list,
|
|
1246
|
+
strict=False,
|
|
1223
1247
|
)
|
|
1224
1248
|
]
|
|
1225
1249
|
)
|
|
@@ -1272,64 +1296,14 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1272
1296
|
generations = [existing_prompts[i] for i in range(len(prompts))]
|
|
1273
1297
|
return LLMResult(generations=generations, llm_output=llm_output, run=run_info)
|
|
1274
1298
|
|
|
1275
|
-
@deprecated("0.1.7", alternative="invoke", removal="1.0")
|
|
1276
|
-
def __call__(
|
|
1277
|
-
self,
|
|
1278
|
-
prompt: str,
|
|
1279
|
-
stop: Optional[list[str]] = None,
|
|
1280
|
-
callbacks: Callbacks = None,
|
|
1281
|
-
*,
|
|
1282
|
-
tags: Optional[list[str]] = None,
|
|
1283
|
-
metadata: Optional[dict[str, Any]] = None,
|
|
1284
|
-
**kwargs: Any,
|
|
1285
|
-
) -> str:
|
|
1286
|
-
"""Check Cache and run the LLM on the given prompt and input.
|
|
1287
|
-
|
|
1288
|
-
Args:
|
|
1289
|
-
prompt: The prompt to generate from.
|
|
1290
|
-
stop: Stop words to use when generating. Model output is cut off at the
|
|
1291
|
-
first occurrence of any of these substrings.
|
|
1292
|
-
callbacks: Callbacks to pass through. Used for executing additional
|
|
1293
|
-
functionality, such as logging or streaming, throughout generation.
|
|
1294
|
-
tags: List of tags to associate with the prompt.
|
|
1295
|
-
metadata: Metadata to associate with the prompt.
|
|
1296
|
-
**kwargs: Arbitrary additional keyword arguments. These are usually passed
|
|
1297
|
-
to the model provider API call.
|
|
1298
|
-
|
|
1299
|
-
Returns:
|
|
1300
|
-
The generated text.
|
|
1301
|
-
|
|
1302
|
-
Raises:
|
|
1303
|
-
ValueError: If the prompt is not a string.
|
|
1304
|
-
"""
|
|
1305
|
-
if not isinstance(prompt, str):
|
|
1306
|
-
msg = (
|
|
1307
|
-
"Argument `prompt` is expected to be a string. Instead found "
|
|
1308
|
-
f"{type(prompt)}. If you want to run the LLM on multiple prompts, use "
|
|
1309
|
-
"`generate` instead."
|
|
1310
|
-
)
|
|
1311
|
-
raise ValueError(msg) # noqa: TRY004
|
|
1312
|
-
return (
|
|
1313
|
-
self.generate(
|
|
1314
|
-
[prompt],
|
|
1315
|
-
stop=stop,
|
|
1316
|
-
callbacks=callbacks,
|
|
1317
|
-
tags=tags,
|
|
1318
|
-
metadata=metadata,
|
|
1319
|
-
**kwargs,
|
|
1320
|
-
)
|
|
1321
|
-
.generations[0][0]
|
|
1322
|
-
.text
|
|
1323
|
-
)
|
|
1324
|
-
|
|
1325
1299
|
async def _call_async(
|
|
1326
1300
|
self,
|
|
1327
1301
|
prompt: str,
|
|
1328
|
-
stop:
|
|
1302
|
+
stop: list[str] | None = None,
|
|
1329
1303
|
callbacks: Callbacks = None,
|
|
1330
1304
|
*,
|
|
1331
|
-
tags:
|
|
1332
|
-
metadata:
|
|
1305
|
+
tags: list[str] | None = None,
|
|
1306
|
+
metadata: dict[str, Any] | None = None,
|
|
1333
1307
|
**kwargs: Any,
|
|
1334
1308
|
) -> str:
|
|
1335
1309
|
"""Check Cache and run the LLM on the given prompt and input."""
|
|
@@ -1343,52 +1317,8 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1343
1317
|
)
|
|
1344
1318
|
return result.generations[0][0].text
|
|
1345
1319
|
|
|
1346
|
-
@deprecated("0.1.7", alternative="invoke", removal="1.0")
|
|
1347
|
-
@override
|
|
1348
|
-
def predict(
|
|
1349
|
-
self, text: str, *, stop: Optional[Sequence[str]] = None, **kwargs: Any
|
|
1350
|
-
) -> str:
|
|
1351
|
-
stop_ = None if stop is None else list(stop)
|
|
1352
|
-
return self(text, stop=stop_, **kwargs)
|
|
1353
|
-
|
|
1354
|
-
@deprecated("0.1.7", alternative="invoke", removal="1.0")
|
|
1355
|
-
@override
|
|
1356
|
-
def predict_messages(
|
|
1357
|
-
self,
|
|
1358
|
-
messages: list[BaseMessage],
|
|
1359
|
-
*,
|
|
1360
|
-
stop: Optional[Sequence[str]] = None,
|
|
1361
|
-
**kwargs: Any,
|
|
1362
|
-
) -> BaseMessage:
|
|
1363
|
-
text = get_buffer_string(messages)
|
|
1364
|
-
stop_ = None if stop is None else list(stop)
|
|
1365
|
-
content = self(text, stop=stop_, **kwargs)
|
|
1366
|
-
return AIMessage(content=content)
|
|
1367
|
-
|
|
1368
|
-
@deprecated("0.1.7", alternative="ainvoke", removal="1.0")
|
|
1369
|
-
@override
|
|
1370
|
-
async def apredict(
|
|
1371
|
-
self, text: str, *, stop: Optional[Sequence[str]] = None, **kwargs: Any
|
|
1372
|
-
) -> str:
|
|
1373
|
-
stop_ = None if stop is None else list(stop)
|
|
1374
|
-
return await self._call_async(text, stop=stop_, **kwargs)
|
|
1375
|
-
|
|
1376
|
-
@deprecated("0.1.7", alternative="ainvoke", removal="1.0")
|
|
1377
|
-
@override
|
|
1378
|
-
async def apredict_messages(
|
|
1379
|
-
self,
|
|
1380
|
-
messages: list[BaseMessage],
|
|
1381
|
-
*,
|
|
1382
|
-
stop: Optional[Sequence[str]] = None,
|
|
1383
|
-
**kwargs: Any,
|
|
1384
|
-
) -> BaseMessage:
|
|
1385
|
-
text = get_buffer_string(messages)
|
|
1386
|
-
stop_ = None if stop is None else list(stop)
|
|
1387
|
-
content = await self._call_async(text, stop=stop_, **kwargs)
|
|
1388
|
-
return AIMessage(content=content)
|
|
1389
|
-
|
|
1390
1320
|
def __str__(self) -> str:
|
|
1391
|
-
"""
|
|
1321
|
+
"""Return a string representation of the object for printing."""
|
|
1392
1322
|
cls_name = f"\033[1m{self.__class__.__name__}\033[0m"
|
|
1393
1323
|
return f"{cls_name}\nParams: {self._identifying_params}"
|
|
1394
1324
|
|
|
@@ -1404,7 +1334,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1404
1334
|
starter_dict["_type"] = self._llm_type
|
|
1405
1335
|
return starter_dict
|
|
1406
1336
|
|
|
1407
|
-
def save(self, file_path:
|
|
1337
|
+
def save(self, file_path: Path | str) -> None:
|
|
1408
1338
|
"""Save the LLM.
|
|
1409
1339
|
|
|
1410
1340
|
Args:
|
|
@@ -1414,11 +1344,9 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1414
1344
|
ValueError: If the file path is not a string or Path object.
|
|
1415
1345
|
|
|
1416
1346
|
Example:
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
llm.save(file_path="path/llm.yaml")
|
|
1421
|
-
|
|
1347
|
+
```python
|
|
1348
|
+
llm.save(file_path="path/llm.yaml")
|
|
1349
|
+
```
|
|
1422
1350
|
"""
|
|
1423
1351
|
# Convert file to Path object.
|
|
1424
1352
|
save_path = Path(file_path)
|
|
@@ -1430,10 +1358,10 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1430
1358
|
prompt_dict = self.dict()
|
|
1431
1359
|
|
|
1432
1360
|
if save_path.suffix == ".json":
|
|
1433
|
-
with save_path.open("w") as f:
|
|
1361
|
+
with save_path.open("w", encoding="utf-8") as f:
|
|
1434
1362
|
json.dump(prompt_dict, f, indent=4)
|
|
1435
1363
|
elif save_path.suffix.endswith((".yaml", ".yml")):
|
|
1436
|
-
with save_path.open("w") as f:
|
|
1364
|
+
with save_path.open("w", encoding="utf-8") as f:
|
|
1437
1365
|
yaml.dump(prompt_dict, f, default_flow_style=False)
|
|
1438
1366
|
else:
|
|
1439
1367
|
msg = f"{save_path} must be json or yaml"
|
|
@@ -1474,8 +1402,8 @@ class LLM(BaseLLM):
|
|
|
1474
1402
|
def _call(
|
|
1475
1403
|
self,
|
|
1476
1404
|
prompt: str,
|
|
1477
|
-
stop:
|
|
1478
|
-
run_manager:
|
|
1405
|
+
stop: list[str] | None = None,
|
|
1406
|
+
run_manager: CallbackManagerForLLMRun | None = None,
|
|
1479
1407
|
**kwargs: Any,
|
|
1480
1408
|
) -> str:
|
|
1481
1409
|
"""Run the LLM on the given input.
|
|
@@ -1498,8 +1426,8 @@ class LLM(BaseLLM):
|
|
|
1498
1426
|
async def _acall(
|
|
1499
1427
|
self,
|
|
1500
1428
|
prompt: str,
|
|
1501
|
-
stop:
|
|
1502
|
-
run_manager:
|
|
1429
|
+
stop: list[str] | None = None,
|
|
1430
|
+
run_manager: AsyncCallbackManagerForLLMRun | None = None,
|
|
1503
1431
|
**kwargs: Any,
|
|
1504
1432
|
) -> str:
|
|
1505
1433
|
"""Async version of the _call method.
|
|
@@ -1532,11 +1460,10 @@ class LLM(BaseLLM):
|
|
|
1532
1460
|
def _generate(
|
|
1533
1461
|
self,
|
|
1534
1462
|
prompts: list[str],
|
|
1535
|
-
stop:
|
|
1536
|
-
run_manager:
|
|
1463
|
+
stop: list[str] | None = None,
|
|
1464
|
+
run_manager: CallbackManagerForLLMRun | None = None,
|
|
1537
1465
|
**kwargs: Any,
|
|
1538
1466
|
) -> LLMResult:
|
|
1539
|
-
"""Run the LLM on the given prompt and input."""
|
|
1540
1467
|
# TODO: add caching here.
|
|
1541
1468
|
generations = []
|
|
1542
1469
|
new_arg_supported = inspect.signature(self._call).parameters.get("run_manager")
|
|
@@ -1552,11 +1479,10 @@ class LLM(BaseLLM):
|
|
|
1552
1479
|
async def _agenerate(
|
|
1553
1480
|
self,
|
|
1554
1481
|
prompts: list[str],
|
|
1555
|
-
stop:
|
|
1556
|
-
run_manager:
|
|
1482
|
+
stop: list[str] | None = None,
|
|
1483
|
+
run_manager: AsyncCallbackManagerForLLMRun | None = None,
|
|
1557
1484
|
**kwargs: Any,
|
|
1558
1485
|
) -> LLMResult:
|
|
1559
|
-
"""Async run the LLM on the given prompt and input."""
|
|
1560
1486
|
generations = []
|
|
1561
1487
|
new_arg_supported = inspect.signature(self._acall).parameters.get("run_manager")
|
|
1562
1488
|
for prompt in prompts:
|