langchain-core 1.0.0a6__py3-none-any.whl → 1.0.4__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.
- langchain_core/__init__.py +1 -1
- langchain_core/_api/__init__.py +3 -4
- langchain_core/_api/beta_decorator.py +23 -26
- langchain_core/_api/deprecation.py +51 -64
- langchain_core/_api/path.py +3 -6
- langchain_core/_import_utils.py +3 -4
- langchain_core/agents.py +55 -48
- langchain_core/caches.py +65 -66
- langchain_core/callbacks/__init__.py +1 -8
- langchain_core/callbacks/base.py +321 -336
- langchain_core/callbacks/file.py +44 -44
- langchain_core/callbacks/manager.py +454 -514
- langchain_core/callbacks/stdout.py +29 -30
- langchain_core/callbacks/streaming_stdout.py +32 -32
- langchain_core/callbacks/usage.py +60 -57
- langchain_core/chat_history.py +53 -68
- langchain_core/document_loaders/base.py +27 -25
- langchain_core/document_loaders/blob_loaders.py +1 -1
- langchain_core/document_loaders/langsmith.py +44 -48
- langchain_core/documents/__init__.py +23 -3
- langchain_core/documents/base.py +102 -94
- langchain_core/documents/compressor.py +10 -10
- langchain_core/documents/transformers.py +34 -35
- langchain_core/embeddings/fake.py +50 -54
- langchain_core/example_selectors/length_based.py +2 -2
- langchain_core/example_selectors/semantic_similarity.py +28 -32
- langchain_core/exceptions.py +21 -20
- langchain_core/globals.py +3 -151
- langchain_core/indexing/__init__.py +1 -1
- langchain_core/indexing/api.py +121 -126
- langchain_core/indexing/base.py +73 -75
- langchain_core/indexing/in_memory.py +4 -6
- langchain_core/language_models/__init__.py +14 -29
- langchain_core/language_models/_utils.py +58 -61
- langchain_core/language_models/base.py +82 -172
- langchain_core/language_models/chat_models.py +329 -402
- langchain_core/language_models/fake.py +11 -11
- langchain_core/language_models/fake_chat_models.py +42 -36
- langchain_core/language_models/llms.py +189 -269
- langchain_core/load/dump.py +9 -12
- langchain_core/load/load.py +18 -28
- langchain_core/load/mapping.py +2 -4
- langchain_core/load/serializable.py +42 -40
- langchain_core/messages/__init__.py +10 -16
- langchain_core/messages/ai.py +148 -148
- langchain_core/messages/base.py +53 -51
- langchain_core/messages/block_translators/__init__.py +19 -22
- langchain_core/messages/block_translators/anthropic.py +6 -6
- langchain_core/messages/block_translators/bedrock_converse.py +5 -5
- langchain_core/messages/block_translators/google_genai.py +10 -7
- langchain_core/messages/block_translators/google_vertexai.py +4 -32
- langchain_core/messages/block_translators/groq.py +117 -21
- langchain_core/messages/block_translators/langchain_v0.py +5 -5
- langchain_core/messages/block_translators/openai.py +11 -11
- langchain_core/messages/chat.py +2 -6
- langchain_core/messages/content.py +339 -330
- langchain_core/messages/function.py +6 -10
- langchain_core/messages/human.py +24 -31
- langchain_core/messages/modifier.py +2 -2
- langchain_core/messages/system.py +19 -29
- langchain_core/messages/tool.py +74 -90
- langchain_core/messages/utils.py +484 -510
- langchain_core/output_parsers/__init__.py +13 -10
- langchain_core/output_parsers/base.py +61 -61
- langchain_core/output_parsers/format_instructions.py +9 -4
- langchain_core/output_parsers/json.py +12 -10
- langchain_core/output_parsers/list.py +21 -23
- langchain_core/output_parsers/openai_functions.py +49 -47
- langchain_core/output_parsers/openai_tools.py +30 -23
- langchain_core/output_parsers/pydantic.py +13 -14
- langchain_core/output_parsers/string.py +5 -5
- langchain_core/output_parsers/transform.py +15 -17
- langchain_core/output_parsers/xml.py +35 -34
- langchain_core/outputs/__init__.py +1 -1
- langchain_core/outputs/chat_generation.py +18 -18
- langchain_core/outputs/chat_result.py +1 -3
- langchain_core/outputs/generation.py +16 -16
- langchain_core/outputs/llm_result.py +10 -10
- langchain_core/prompt_values.py +13 -19
- langchain_core/prompts/__init__.py +3 -27
- langchain_core/prompts/base.py +81 -86
- langchain_core/prompts/chat.py +308 -351
- langchain_core/prompts/dict.py +6 -6
- langchain_core/prompts/few_shot.py +81 -88
- langchain_core/prompts/few_shot_with_templates.py +11 -13
- langchain_core/prompts/image.py +12 -14
- langchain_core/prompts/loading.py +4 -6
- langchain_core/prompts/message.py +7 -7
- langchain_core/prompts/prompt.py +24 -39
- langchain_core/prompts/string.py +26 -10
- langchain_core/prompts/structured.py +49 -53
- langchain_core/rate_limiters.py +51 -60
- langchain_core/retrievers.py +61 -198
- langchain_core/runnables/base.py +1551 -1656
- langchain_core/runnables/branch.py +68 -70
- langchain_core/runnables/config.py +72 -89
- langchain_core/runnables/configurable.py +145 -161
- langchain_core/runnables/fallbacks.py +102 -96
- langchain_core/runnables/graph.py +91 -97
- langchain_core/runnables/graph_ascii.py +27 -28
- langchain_core/runnables/graph_mermaid.py +42 -51
- langchain_core/runnables/graph_png.py +43 -16
- langchain_core/runnables/history.py +175 -177
- langchain_core/runnables/passthrough.py +151 -167
- langchain_core/runnables/retry.py +46 -51
- langchain_core/runnables/router.py +30 -35
- langchain_core/runnables/schema.py +75 -80
- langchain_core/runnables/utils.py +60 -67
- langchain_core/stores.py +85 -121
- langchain_core/structured_query.py +8 -8
- langchain_core/sys_info.py +29 -29
- langchain_core/tools/__init__.py +1 -14
- langchain_core/tools/base.py +306 -245
- langchain_core/tools/convert.py +160 -155
- langchain_core/tools/render.py +10 -10
- langchain_core/tools/retriever.py +12 -11
- langchain_core/tools/simple.py +19 -24
- langchain_core/tools/structured.py +32 -39
- langchain_core/tracers/__init__.py +1 -9
- langchain_core/tracers/base.py +97 -99
- langchain_core/tracers/context.py +29 -52
- langchain_core/tracers/core.py +49 -53
- langchain_core/tracers/evaluation.py +11 -11
- langchain_core/tracers/event_stream.py +65 -64
- langchain_core/tracers/langchain.py +21 -21
- langchain_core/tracers/log_stream.py +45 -45
- langchain_core/tracers/memory_stream.py +3 -3
- langchain_core/tracers/root_listeners.py +16 -16
- langchain_core/tracers/run_collector.py +2 -4
- langchain_core/tracers/schemas.py +0 -129
- langchain_core/tracers/stdout.py +3 -3
- langchain_core/utils/__init__.py +1 -4
- langchain_core/utils/_merge.py +2 -2
- langchain_core/utils/aiter.py +57 -61
- langchain_core/utils/env.py +9 -9
- langchain_core/utils/function_calling.py +94 -188
- langchain_core/utils/html.py +7 -8
- langchain_core/utils/input.py +9 -6
- langchain_core/utils/interactive_env.py +1 -1
- langchain_core/utils/iter.py +36 -40
- langchain_core/utils/json.py +4 -3
- langchain_core/utils/json_schema.py +9 -9
- langchain_core/utils/mustache.py +8 -10
- langchain_core/utils/pydantic.py +35 -37
- langchain_core/utils/strings.py +6 -9
- langchain_core/utils/usage.py +1 -1
- langchain_core/utils/utils.py +66 -62
- langchain_core/vectorstores/base.py +182 -216
- langchain_core/vectorstores/in_memory.py +101 -176
- langchain_core/vectorstores/utils.py +5 -5
- langchain_core/version.py +1 -1
- langchain_core-1.0.4.dist-info/METADATA +69 -0
- langchain_core-1.0.4.dist-info/RECORD +172 -0
- {langchain_core-1.0.0a6.dist-info → langchain_core-1.0.4.dist-info}/WHEEL +1 -1
- langchain_core/memory.py +0 -120
- langchain_core/messages/block_translators/ollama.py +0 -47
- langchain_core/prompts/pipeline.py +0 -138
- 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 -31
- langchain_core/utils/loading.py +0 -35
- langchain_core-1.0.0a6.dist-info/METADATA +0 -67
- langchain_core-1.0.0a6.dist-info/RECORD +0 -181
- langchain_core-1.0.0a6.dist-info/entry_points.txt +0 -4
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
"""Base interface for large language models to expose.
|
|
1
|
+
"""Base interface for traditional large language models (LLMs) to expose.
|
|
2
|
+
|
|
3
|
+
These are traditionally older models (newer models generally are chat models).
|
|
4
|
+
"""
|
|
2
5
|
|
|
3
6
|
from __future__ import annotations
|
|
4
7
|
|
|
@@ -7,21 +10,17 @@ import functools
|
|
|
7
10
|
import inspect
|
|
8
11
|
import json
|
|
9
12
|
import logging
|
|
10
|
-
import warnings
|
|
11
13
|
from abc import ABC, abstractmethod
|
|
12
|
-
from collections.abc import AsyncIterator, Iterator, Sequence
|
|
14
|
+
from collections.abc import AsyncIterator, Callable, Iterator, Sequence
|
|
13
15
|
from pathlib import Path
|
|
14
16
|
from typing import (
|
|
15
17
|
TYPE_CHECKING,
|
|
16
18
|
Any,
|
|
17
|
-
Callable,
|
|
18
|
-
Optional,
|
|
19
|
-
Union,
|
|
20
19
|
cast,
|
|
21
20
|
)
|
|
22
21
|
|
|
23
22
|
import yaml
|
|
24
|
-
from pydantic import ConfigDict
|
|
23
|
+
from pydantic import ConfigDict
|
|
25
24
|
from tenacity import (
|
|
26
25
|
RetryCallState,
|
|
27
26
|
before_sleep_log,
|
|
@@ -33,7 +32,6 @@ from tenacity import (
|
|
|
33
32
|
)
|
|
34
33
|
from typing_extensions import override
|
|
35
34
|
|
|
36
|
-
from langchain_core._api import deprecated
|
|
37
35
|
from langchain_core.caches import BaseCache
|
|
38
36
|
from langchain_core.callbacks import (
|
|
39
37
|
AsyncCallbackManager,
|
|
@@ -51,10 +49,7 @@ from langchain_core.language_models.base import (
|
|
|
51
49
|
)
|
|
52
50
|
from langchain_core.load import dumpd
|
|
53
51
|
from langchain_core.messages import (
|
|
54
|
-
AIMessage,
|
|
55
|
-
BaseMessage,
|
|
56
52
|
convert_to_messages,
|
|
57
|
-
get_buffer_string,
|
|
58
53
|
)
|
|
59
54
|
from langchain_core.outputs import Generation, GenerationChunk, LLMResult, RunInfo
|
|
60
55
|
from langchain_core.prompt_values import ChatPromptValue, PromptValue, StringPromptValue
|
|
@@ -76,16 +71,14 @@ def _log_error_once(msg: str) -> None:
|
|
|
76
71
|
def create_base_retry_decorator(
|
|
77
72
|
error_types: list[type[BaseException]],
|
|
78
73
|
max_retries: int = 1,
|
|
79
|
-
run_manager:
|
|
80
|
-
Union[AsyncCallbackManagerForLLMRun, CallbackManagerForLLMRun]
|
|
81
|
-
] = None,
|
|
74
|
+
run_manager: AsyncCallbackManagerForLLMRun | CallbackManagerForLLMRun | None = None,
|
|
82
75
|
) -> Callable[[Any], Any]:
|
|
83
76
|
"""Create a retry decorator for a given LLM and provided a list of error types.
|
|
84
77
|
|
|
85
78
|
Args:
|
|
86
79
|
error_types: List of error types to retry on.
|
|
87
|
-
max_retries: Number of retries.
|
|
88
|
-
run_manager: Callback manager for the run.
|
|
80
|
+
max_retries: Number of retries.
|
|
81
|
+
run_manager: Callback manager for the run.
|
|
89
82
|
|
|
90
83
|
Returns:
|
|
91
84
|
A retry decorator.
|
|
@@ -101,13 +94,17 @@ def create_base_retry_decorator(
|
|
|
101
94
|
if isinstance(run_manager, AsyncCallbackManagerForLLMRun):
|
|
102
95
|
coro = run_manager.on_retry(retry_state)
|
|
103
96
|
try:
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
# and be awaited somewhere
|
|
108
|
-
loop.create_task(coro) # noqa: RUF006
|
|
109
|
-
else:
|
|
97
|
+
try:
|
|
98
|
+
loop = asyncio.get_event_loop()
|
|
99
|
+
except RuntimeError:
|
|
110
100
|
asyncio.run(coro)
|
|
101
|
+
else:
|
|
102
|
+
if loop.is_running():
|
|
103
|
+
# TODO: Fix RUF006 - this task should have a reference
|
|
104
|
+
# and be awaited somewhere
|
|
105
|
+
loop.create_task(coro) # noqa: RUF006
|
|
106
|
+
else:
|
|
107
|
+
asyncio.run(coro)
|
|
111
108
|
except Exception as e:
|
|
112
109
|
_log_error_once(f"Error in on_retry: {e}")
|
|
113
110
|
else:
|
|
@@ -129,9 +126,9 @@ def create_base_retry_decorator(
|
|
|
129
126
|
)
|
|
130
127
|
|
|
131
128
|
|
|
132
|
-
def _resolve_cache(*, cache:
|
|
129
|
+
def _resolve_cache(*, cache: BaseCache | bool | None) -> BaseCache | None:
|
|
133
130
|
"""Resolve the cache."""
|
|
134
|
-
llm_cache:
|
|
131
|
+
llm_cache: BaseCache | None
|
|
135
132
|
if isinstance(cache, BaseCache):
|
|
136
133
|
llm_cache = cache
|
|
137
134
|
elif cache is None:
|
|
@@ -156,14 +153,14 @@ def _resolve_cache(*, cache: Union[BaseCache, bool, None]) -> Optional[BaseCache
|
|
|
156
153
|
def get_prompts(
|
|
157
154
|
params: dict[str, Any],
|
|
158
155
|
prompts: list[str],
|
|
159
|
-
cache:
|
|
156
|
+
cache: BaseCache | bool | None = None, # noqa: FBT001
|
|
160
157
|
) -> tuple[dict[int, list], str, list[int], list[str]]:
|
|
161
158
|
"""Get prompts that are already cached.
|
|
162
159
|
|
|
163
160
|
Args:
|
|
164
161
|
params: Dictionary of parameters.
|
|
165
162
|
prompts: List of prompts.
|
|
166
|
-
cache: Cache object.
|
|
163
|
+
cache: Cache object.
|
|
167
164
|
|
|
168
165
|
Returns:
|
|
169
166
|
A tuple of existing prompts, llm_string, missing prompt indexes,
|
|
@@ -192,14 +189,14 @@ def get_prompts(
|
|
|
192
189
|
async def aget_prompts(
|
|
193
190
|
params: dict[str, Any],
|
|
194
191
|
prompts: list[str],
|
|
195
|
-
cache:
|
|
192
|
+
cache: BaseCache | bool | None = None, # noqa: FBT001
|
|
196
193
|
) -> tuple[dict[int, list], str, list[int], list[str]]:
|
|
197
194
|
"""Get prompts that are already cached. Async version.
|
|
198
195
|
|
|
199
196
|
Args:
|
|
200
197
|
params: Dictionary of parameters.
|
|
201
198
|
prompts: List of prompts.
|
|
202
|
-
cache: Cache object.
|
|
199
|
+
cache: Cache object.
|
|
203
200
|
|
|
204
201
|
Returns:
|
|
205
202
|
A tuple of existing prompts, llm_string, missing prompt indexes,
|
|
@@ -225,13 +222,13 @@ async def aget_prompts(
|
|
|
225
222
|
|
|
226
223
|
|
|
227
224
|
def update_cache(
|
|
228
|
-
cache:
|
|
225
|
+
cache: BaseCache | bool | None, # noqa: FBT001
|
|
229
226
|
existing_prompts: dict[int, list],
|
|
230
227
|
llm_string: str,
|
|
231
228
|
missing_prompt_idxs: list[int],
|
|
232
229
|
new_results: LLMResult,
|
|
233
230
|
prompts: list[str],
|
|
234
|
-
) ->
|
|
231
|
+
) -> dict | None:
|
|
235
232
|
"""Update the cache and get the LLM output.
|
|
236
233
|
|
|
237
234
|
Args:
|
|
@@ -258,13 +255,13 @@ def update_cache(
|
|
|
258
255
|
|
|
259
256
|
|
|
260
257
|
async def aupdate_cache(
|
|
261
|
-
cache:
|
|
258
|
+
cache: BaseCache | bool | None, # noqa: FBT001
|
|
262
259
|
existing_prompts: dict[int, list],
|
|
263
260
|
llm_string: str,
|
|
264
261
|
missing_prompt_idxs: list[int],
|
|
265
262
|
new_results: LLMResult,
|
|
266
263
|
prompts: list[str],
|
|
267
|
-
) ->
|
|
264
|
+
) -> dict | None:
|
|
268
265
|
"""Update the cache and get the LLM output. Async version.
|
|
269
266
|
|
|
270
267
|
Args:
|
|
@@ -296,26 +293,10 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
296
293
|
It should take in a prompt and return a string.
|
|
297
294
|
"""
|
|
298
295
|
|
|
299
|
-
callback_manager: Optional[BaseCallbackManager] = Field(default=None, exclude=True)
|
|
300
|
-
"""[DEPRECATED]"""
|
|
301
|
-
|
|
302
296
|
model_config = ConfigDict(
|
|
303
297
|
arbitrary_types_allowed=True,
|
|
304
298
|
)
|
|
305
299
|
|
|
306
|
-
@model_validator(mode="before")
|
|
307
|
-
@classmethod
|
|
308
|
-
def raise_deprecation(cls, values: dict) -> Any:
|
|
309
|
-
"""Raise deprecation warning if callback_manager is used."""
|
|
310
|
-
if values.get("callback_manager") is not None:
|
|
311
|
-
warnings.warn(
|
|
312
|
-
"callback_manager is deprecated. Please use callbacks instead.",
|
|
313
|
-
DeprecationWarning,
|
|
314
|
-
stacklevel=5,
|
|
315
|
-
)
|
|
316
|
-
values["callbacks"] = values.pop("callback_manager", None)
|
|
317
|
-
return values
|
|
318
|
-
|
|
319
300
|
@functools.cached_property
|
|
320
301
|
def _serialized(self) -> dict[str, Any]:
|
|
321
302
|
return dumpd(self)
|
|
@@ -325,7 +306,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
325
306
|
@property
|
|
326
307
|
@override
|
|
327
308
|
def OutputType(self) -> type[str]:
|
|
328
|
-
"""Get the input type for this
|
|
309
|
+
"""Get the input type for this `Runnable`."""
|
|
329
310
|
return str
|
|
330
311
|
|
|
331
312
|
def _convert_input(self, model_input: LanguageModelInput) -> PromptValue:
|
|
@@ -343,7 +324,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
343
324
|
|
|
344
325
|
def _get_ls_params(
|
|
345
326
|
self,
|
|
346
|
-
stop:
|
|
327
|
+
stop: list[str] | None = None,
|
|
347
328
|
**kwargs: Any,
|
|
348
329
|
) -> LangSmithParams:
|
|
349
330
|
"""Get standard params for tracing."""
|
|
@@ -382,9 +363,9 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
382
363
|
def invoke(
|
|
383
364
|
self,
|
|
384
365
|
input: LanguageModelInput,
|
|
385
|
-
config:
|
|
366
|
+
config: RunnableConfig | None = None,
|
|
386
367
|
*,
|
|
387
|
-
stop:
|
|
368
|
+
stop: list[str] | None = None,
|
|
388
369
|
**kwargs: Any,
|
|
389
370
|
) -> str:
|
|
390
371
|
config = ensure_config(config)
|
|
@@ -407,9 +388,9 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
407
388
|
async def ainvoke(
|
|
408
389
|
self,
|
|
409
390
|
input: LanguageModelInput,
|
|
410
|
-
config:
|
|
391
|
+
config: RunnableConfig | None = None,
|
|
411
392
|
*,
|
|
412
|
-
stop:
|
|
393
|
+
stop: list[str] | None = None,
|
|
413
394
|
**kwargs: Any,
|
|
414
395
|
) -> str:
|
|
415
396
|
config = ensure_config(config)
|
|
@@ -429,7 +410,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
429
410
|
def batch(
|
|
430
411
|
self,
|
|
431
412
|
inputs: list[LanguageModelInput],
|
|
432
|
-
config:
|
|
413
|
+
config: RunnableConfig | list[RunnableConfig] | None = None,
|
|
433
414
|
*,
|
|
434
415
|
return_exceptions: bool = False,
|
|
435
416
|
**kwargs: Any,
|
|
@@ -476,7 +457,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
476
457
|
async def abatch(
|
|
477
458
|
self,
|
|
478
459
|
inputs: list[LanguageModelInput],
|
|
479
|
-
config:
|
|
460
|
+
config: RunnableConfig | list[RunnableConfig] | None = None,
|
|
480
461
|
*,
|
|
481
462
|
return_exceptions: bool = False,
|
|
482
463
|
**kwargs: Any,
|
|
@@ -522,9 +503,9 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
522
503
|
def stream(
|
|
523
504
|
self,
|
|
524
505
|
input: LanguageModelInput,
|
|
525
|
-
config:
|
|
506
|
+
config: RunnableConfig | None = None,
|
|
526
507
|
*,
|
|
527
|
-
stop:
|
|
508
|
+
stop: list[str] | None = None,
|
|
528
509
|
**kwargs: Any,
|
|
529
510
|
) -> Iterator[str]:
|
|
530
511
|
if type(self)._stream == BaseLLM._stream: # noqa: SLF001
|
|
@@ -559,7 +540,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
559
540
|
run_id=config.pop("run_id", None),
|
|
560
541
|
batch_size=1,
|
|
561
542
|
)
|
|
562
|
-
generation:
|
|
543
|
+
generation: GenerationChunk | None = None
|
|
563
544
|
try:
|
|
564
545
|
for chunk in self._stream(
|
|
565
546
|
prompt, stop=stop, run_manager=run_manager, **kwargs
|
|
@@ -589,9 +570,9 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
589
570
|
async def astream(
|
|
590
571
|
self,
|
|
591
572
|
input: LanguageModelInput,
|
|
592
|
-
config:
|
|
573
|
+
config: RunnableConfig | None = None,
|
|
593
574
|
*,
|
|
594
|
-
stop:
|
|
575
|
+
stop: list[str] | None = None,
|
|
595
576
|
**kwargs: Any,
|
|
596
577
|
) -> AsyncIterator[str]:
|
|
597
578
|
if (
|
|
@@ -629,7 +610,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
629
610
|
run_id=config.pop("run_id", None),
|
|
630
611
|
batch_size=1,
|
|
631
612
|
)
|
|
632
|
-
generation:
|
|
613
|
+
generation: GenerationChunk | None = None
|
|
633
614
|
try:
|
|
634
615
|
async for chunk in self._astream(
|
|
635
616
|
prompt,
|
|
@@ -662,17 +643,20 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
662
643
|
def _generate(
|
|
663
644
|
self,
|
|
664
645
|
prompts: list[str],
|
|
665
|
-
stop:
|
|
666
|
-
run_manager:
|
|
646
|
+
stop: list[str] | None = None,
|
|
647
|
+
run_manager: CallbackManagerForLLMRun | None = None,
|
|
667
648
|
**kwargs: Any,
|
|
668
649
|
) -> LLMResult:
|
|
669
650
|
"""Run the LLM on the given prompts.
|
|
670
651
|
|
|
671
652
|
Args:
|
|
672
653
|
prompts: The prompts to generate from.
|
|
673
|
-
stop: Stop words to use when generating.
|
|
674
|
-
|
|
675
|
-
|
|
654
|
+
stop: Stop words to use when generating.
|
|
655
|
+
|
|
656
|
+
Model output is cut off at the first occurrence of any of these
|
|
657
|
+
substrings.
|
|
658
|
+
|
|
659
|
+
If stop tokens are not supported consider raising `NotImplementedError`.
|
|
676
660
|
run_manager: Callback manager for the run.
|
|
677
661
|
|
|
678
662
|
Returns:
|
|
@@ -682,17 +666,20 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
682
666
|
async def _agenerate(
|
|
683
667
|
self,
|
|
684
668
|
prompts: list[str],
|
|
685
|
-
stop:
|
|
686
|
-
run_manager:
|
|
669
|
+
stop: list[str] | None = None,
|
|
670
|
+
run_manager: AsyncCallbackManagerForLLMRun | None = None,
|
|
687
671
|
**kwargs: Any,
|
|
688
672
|
) -> LLMResult:
|
|
689
673
|
"""Run the LLM on the given prompts.
|
|
690
674
|
|
|
691
675
|
Args:
|
|
692
676
|
prompts: The prompts to generate from.
|
|
693
|
-
stop: Stop words to use when generating.
|
|
694
|
-
|
|
695
|
-
|
|
677
|
+
stop: Stop words to use when generating.
|
|
678
|
+
|
|
679
|
+
Model output is cut off at the first occurrence of any of these
|
|
680
|
+
substrings.
|
|
681
|
+
|
|
682
|
+
If stop tokens are not supported consider raising `NotImplementedError`.
|
|
696
683
|
run_manager: Callback manager for the run.
|
|
697
684
|
|
|
698
685
|
Returns:
|
|
@@ -710,8 +697,8 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
710
697
|
def _stream(
|
|
711
698
|
self,
|
|
712
699
|
prompt: str,
|
|
713
|
-
stop:
|
|
714
|
-
run_manager:
|
|
700
|
+
stop: list[str] | None = None,
|
|
701
|
+
run_manager: CallbackManagerForLLMRun | None = None,
|
|
715
702
|
**kwargs: Any,
|
|
716
703
|
) -> Iterator[GenerationChunk]:
|
|
717
704
|
"""Stream the LLM on the given prompt.
|
|
@@ -724,11 +711,14 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
724
711
|
|
|
725
712
|
Args:
|
|
726
713
|
prompt: The prompt to generate from.
|
|
727
|
-
stop: Stop words to use when generating.
|
|
728
|
-
|
|
714
|
+
stop: Stop words to use when generating.
|
|
715
|
+
|
|
716
|
+
Model output is cut off at the first occurrence of any of these
|
|
717
|
+
substrings.
|
|
729
718
|
run_manager: Callback manager for the run.
|
|
730
|
-
**kwargs: Arbitrary additional keyword arguments.
|
|
731
|
-
|
|
719
|
+
**kwargs: Arbitrary additional keyword arguments.
|
|
720
|
+
|
|
721
|
+
These are usually passed to the model provider API call.
|
|
732
722
|
|
|
733
723
|
Yields:
|
|
734
724
|
Generation chunks.
|
|
@@ -738,8 +728,8 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
738
728
|
async def _astream(
|
|
739
729
|
self,
|
|
740
730
|
prompt: str,
|
|
741
|
-
stop:
|
|
742
|
-
run_manager:
|
|
731
|
+
stop: list[str] | None = None,
|
|
732
|
+
run_manager: AsyncCallbackManagerForLLMRun | None = None,
|
|
743
733
|
**kwargs: Any,
|
|
744
734
|
) -> AsyncIterator[GenerationChunk]:
|
|
745
735
|
"""An async version of the _stream method.
|
|
@@ -750,11 +740,14 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
750
740
|
|
|
751
741
|
Args:
|
|
752
742
|
prompt: The prompt to generate from.
|
|
753
|
-
stop: Stop words to use when generating.
|
|
754
|
-
|
|
743
|
+
stop: Stop words to use when generating.
|
|
744
|
+
|
|
745
|
+
Model output is cut off at the first occurrence of any of these
|
|
746
|
+
substrings.
|
|
755
747
|
run_manager: Callback manager for the run.
|
|
756
|
-
**kwargs: Arbitrary additional keyword arguments.
|
|
757
|
-
|
|
748
|
+
**kwargs: Arbitrary additional keyword arguments.
|
|
749
|
+
|
|
750
|
+
These are usually passed to the model provider API call.
|
|
758
751
|
|
|
759
752
|
Yields:
|
|
760
753
|
Generation chunks.
|
|
@@ -783,8 +776,8 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
783
776
|
def generate_prompt(
|
|
784
777
|
self,
|
|
785
778
|
prompts: list[PromptValue],
|
|
786
|
-
stop:
|
|
787
|
-
callbacks:
|
|
779
|
+
stop: list[str] | None = None,
|
|
780
|
+
callbacks: Callbacks | list[Callbacks] | None = None,
|
|
788
781
|
**kwargs: Any,
|
|
789
782
|
) -> LLMResult:
|
|
790
783
|
prompt_strings = [p.to_string() for p in prompts]
|
|
@@ -794,8 +787,8 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
794
787
|
async def agenerate_prompt(
|
|
795
788
|
self,
|
|
796
789
|
prompts: list[PromptValue],
|
|
797
|
-
stop:
|
|
798
|
-
callbacks:
|
|
790
|
+
stop: list[str] | None = None,
|
|
791
|
+
callbacks: Callbacks | list[Callbacks] | None = None,
|
|
799
792
|
**kwargs: Any,
|
|
800
793
|
) -> LLMResult:
|
|
801
794
|
prompt_strings = [p.to_string() for p in prompts]
|
|
@@ -806,7 +799,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
806
799
|
def _generate_helper(
|
|
807
800
|
self,
|
|
808
801
|
prompts: list[str],
|
|
809
|
-
stop:
|
|
802
|
+
stop: list[str] | None,
|
|
810
803
|
run_managers: list[CallbackManagerForLLMRun],
|
|
811
804
|
*,
|
|
812
805
|
new_arg_supported: bool,
|
|
@@ -829,7 +822,9 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
829
822
|
run_manager.on_llm_error(e, response=LLMResult(generations=[]))
|
|
830
823
|
raise
|
|
831
824
|
flattened_outputs = output.flatten()
|
|
832
|
-
for manager, flattened_output in zip(
|
|
825
|
+
for manager, flattened_output in zip(
|
|
826
|
+
run_managers, flattened_outputs, strict=False
|
|
827
|
+
):
|
|
833
828
|
manager.on_llm_end(flattened_output)
|
|
834
829
|
if run_managers:
|
|
835
830
|
output.run = [
|
|
@@ -840,13 +835,13 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
840
835
|
def generate(
|
|
841
836
|
self,
|
|
842
837
|
prompts: list[str],
|
|
843
|
-
stop:
|
|
844
|
-
callbacks:
|
|
838
|
+
stop: list[str] | None = None,
|
|
839
|
+
callbacks: Callbacks | list[Callbacks] | None = None,
|
|
845
840
|
*,
|
|
846
|
-
tags:
|
|
847
|
-
metadata:
|
|
848
|
-
run_name:
|
|
849
|
-
run_id:
|
|
841
|
+
tags: list[str] | list[list[str]] | None = None,
|
|
842
|
+
metadata: dict[str, Any] | list[dict[str, Any]] | None = None,
|
|
843
|
+
run_name: str | list[str] | None = None,
|
|
844
|
+
run_id: uuid.UUID | list[uuid.UUID | None] | None = None,
|
|
850
845
|
**kwargs: Any,
|
|
851
846
|
) -> LLMResult:
|
|
852
847
|
"""Pass a sequence of prompts to a model and return generations.
|
|
@@ -859,14 +854,18 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
859
854
|
1. Take advantage of batched calls,
|
|
860
855
|
2. Need more output from the model than just the top generated value,
|
|
861
856
|
3. Are building chains that are agnostic to the underlying language model
|
|
862
|
-
|
|
857
|
+
type (e.g., pure text completion models vs chat models).
|
|
863
858
|
|
|
864
859
|
Args:
|
|
865
860
|
prompts: List of string prompts.
|
|
866
|
-
stop: Stop words to use when generating.
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
861
|
+
stop: Stop words to use when generating.
|
|
862
|
+
|
|
863
|
+
Model output is cut off at the first occurrence of any of these
|
|
864
|
+
substrings.
|
|
865
|
+
callbacks: `Callbacks` to pass through.
|
|
866
|
+
|
|
867
|
+
Used for executing additional functionality, such as logging or
|
|
868
|
+
streaming, throughout generation.
|
|
870
869
|
tags: List of tags to associate with each prompt. If provided, the length
|
|
871
870
|
of the list must match the length of the prompts list.
|
|
872
871
|
metadata: List of metadata dictionaries to associate with each prompt. If
|
|
@@ -876,17 +875,18 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
876
875
|
length of the list must match the length of the prompts list.
|
|
877
876
|
run_id: List of run IDs to associate with each prompt. If provided, the
|
|
878
877
|
length of the list must match the length of the prompts list.
|
|
879
|
-
**kwargs: Arbitrary additional keyword arguments.
|
|
880
|
-
|
|
878
|
+
**kwargs: Arbitrary additional keyword arguments.
|
|
879
|
+
|
|
880
|
+
These are usually passed to the model provider API call.
|
|
881
881
|
|
|
882
882
|
Raises:
|
|
883
883
|
ValueError: If prompts is not a list.
|
|
884
|
-
ValueError: If the length of
|
|
885
|
-
|
|
884
|
+
ValueError: If the length of `callbacks`, `tags`, `metadata`, or
|
|
885
|
+
`run_name` (if provided) does not match the length of prompts.
|
|
886
886
|
|
|
887
887
|
Returns:
|
|
888
|
-
An LLMResult
|
|
889
|
-
prompt and additional model provider-specific output.
|
|
888
|
+
An `LLMResult`, which contains a list of candidate `Generations` for each
|
|
889
|
+
input prompt and additional model provider-specific output.
|
|
890
890
|
"""
|
|
891
891
|
if not isinstance(prompts, list):
|
|
892
892
|
msg = (
|
|
@@ -936,14 +936,12 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
936
936
|
msg = "run_name must be a list of the same length as prompts"
|
|
937
937
|
raise ValueError(msg)
|
|
938
938
|
callbacks = cast("list[Callbacks]", callbacks)
|
|
939
|
-
tags_list = cast(
|
|
940
|
-
"list[Optional[list[str]]]", tags or ([None] * len(prompts))
|
|
941
|
-
)
|
|
939
|
+
tags_list = cast("list[list[str] | None]", tags or ([None] * len(prompts)))
|
|
942
940
|
metadata_list = cast(
|
|
943
|
-
"list[
|
|
941
|
+
"list[dict[str, Any] | None]", metadata or ([{}] * len(prompts))
|
|
944
942
|
)
|
|
945
943
|
run_name_list = run_name or cast(
|
|
946
|
-
"list[
|
|
944
|
+
"list[str | None]", ([None] * len(prompts))
|
|
947
945
|
)
|
|
948
946
|
callback_managers = [
|
|
949
947
|
CallbackManager.configure(
|
|
@@ -955,7 +953,9 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
955
953
|
meta,
|
|
956
954
|
self.metadata,
|
|
957
955
|
)
|
|
958
|
-
for callback, tag, meta in zip(
|
|
956
|
+
for callback, tag, meta in zip(
|
|
957
|
+
callbacks, tags_list, metadata_list, strict=False
|
|
958
|
+
)
|
|
959
959
|
]
|
|
960
960
|
else:
|
|
961
961
|
# We've received a single callbacks arg to apply to all inputs
|
|
@@ -970,7 +970,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
970
970
|
self.metadata,
|
|
971
971
|
)
|
|
972
972
|
] * len(prompts)
|
|
973
|
-
run_name_list = [cast("
|
|
973
|
+
run_name_list = [cast("str | None", run_name)] * len(prompts)
|
|
974
974
|
run_ids_list = self._get_run_ids_list(run_id, prompts)
|
|
975
975
|
params = self.dict()
|
|
976
976
|
params["stop"] = stop
|
|
@@ -996,7 +996,11 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
996
996
|
run_id=run_id_,
|
|
997
997
|
)[0]
|
|
998
998
|
for callback_manager, prompt, run_name, run_id_ in zip(
|
|
999
|
-
callback_managers,
|
|
999
|
+
callback_managers,
|
|
1000
|
+
prompts,
|
|
1001
|
+
run_name_list,
|
|
1002
|
+
run_ids_list,
|
|
1003
|
+
strict=False,
|
|
1000
1004
|
)
|
|
1001
1005
|
]
|
|
1002
1006
|
return self._generate_helper(
|
|
@@ -1046,7 +1050,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1046
1050
|
|
|
1047
1051
|
@staticmethod
|
|
1048
1052
|
def _get_run_ids_list(
|
|
1049
|
-
run_id:
|
|
1053
|
+
run_id: uuid.UUID | list[uuid.UUID | None] | None, prompts: list
|
|
1050
1054
|
) -> list:
|
|
1051
1055
|
if run_id is None:
|
|
1052
1056
|
return [None] * len(prompts)
|
|
@@ -1063,7 +1067,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1063
1067
|
async def _agenerate_helper(
|
|
1064
1068
|
self,
|
|
1065
1069
|
prompts: list[str],
|
|
1066
|
-
stop:
|
|
1070
|
+
stop: list[str] | None,
|
|
1067
1071
|
run_managers: list[AsyncCallbackManagerForLLMRun],
|
|
1068
1072
|
*,
|
|
1069
1073
|
new_arg_supported: bool,
|
|
@@ -1093,7 +1097,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1093
1097
|
*[
|
|
1094
1098
|
run_manager.on_llm_end(flattened_output)
|
|
1095
1099
|
for run_manager, flattened_output in zip(
|
|
1096
|
-
run_managers, flattened_outputs
|
|
1100
|
+
run_managers, flattened_outputs, strict=False
|
|
1097
1101
|
)
|
|
1098
1102
|
]
|
|
1099
1103
|
)
|
|
@@ -1106,13 +1110,13 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1106
1110
|
async def agenerate(
|
|
1107
1111
|
self,
|
|
1108
1112
|
prompts: list[str],
|
|
1109
|
-
stop:
|
|
1110
|
-
callbacks:
|
|
1113
|
+
stop: list[str] | None = None,
|
|
1114
|
+
callbacks: Callbacks | list[Callbacks] | None = None,
|
|
1111
1115
|
*,
|
|
1112
|
-
tags:
|
|
1113
|
-
metadata:
|
|
1114
|
-
run_name:
|
|
1115
|
-
run_id:
|
|
1116
|
+
tags: list[str] | list[list[str]] | None = None,
|
|
1117
|
+
metadata: dict[str, Any] | list[dict[str, Any]] | None = None,
|
|
1118
|
+
run_name: str | list[str] | None = None,
|
|
1119
|
+
run_id: uuid.UUID | list[uuid.UUID | None] | None = None,
|
|
1116
1120
|
**kwargs: Any,
|
|
1117
1121
|
) -> LLMResult:
|
|
1118
1122
|
"""Asynchronously pass a sequence of prompts to a model and return generations.
|
|
@@ -1125,14 +1129,18 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1125
1129
|
1. Take advantage of batched calls,
|
|
1126
1130
|
2. Need more output from the model than just the top generated value,
|
|
1127
1131
|
3. Are building chains that are agnostic to the underlying language model
|
|
1128
|
-
|
|
1132
|
+
type (e.g., pure text completion models vs chat models).
|
|
1129
1133
|
|
|
1130
1134
|
Args:
|
|
1131
1135
|
prompts: List of string prompts.
|
|
1132
|
-
stop: Stop words to use when generating.
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
+
stop: Stop words to use when generating.
|
|
1137
|
+
|
|
1138
|
+
Model output is cut off at the first occurrence of any of these
|
|
1139
|
+
substrings.
|
|
1140
|
+
callbacks: `Callbacks` to pass through.
|
|
1141
|
+
|
|
1142
|
+
Used for executing additional functionality, such as logging or
|
|
1143
|
+
streaming, throughout generation.
|
|
1136
1144
|
tags: List of tags to associate with each prompt. If provided, the length
|
|
1137
1145
|
of the list must match the length of the prompts list.
|
|
1138
1146
|
metadata: List of metadata dictionaries to associate with each prompt. If
|
|
@@ -1142,16 +1150,17 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1142
1150
|
length of the list must match the length of the prompts list.
|
|
1143
1151
|
run_id: List of run IDs to associate with each prompt. If provided, the
|
|
1144
1152
|
length of the list must match the length of the prompts list.
|
|
1145
|
-
**kwargs: Arbitrary additional keyword arguments.
|
|
1146
|
-
|
|
1153
|
+
**kwargs: Arbitrary additional keyword arguments.
|
|
1154
|
+
|
|
1155
|
+
These are usually passed to the model provider API call.
|
|
1147
1156
|
|
|
1148
1157
|
Raises:
|
|
1149
|
-
ValueError: If the length of
|
|
1150
|
-
|
|
1158
|
+
ValueError: If the length of `callbacks`, `tags`, `metadata`, or
|
|
1159
|
+
`run_name` (if provided) does not match the length of prompts.
|
|
1151
1160
|
|
|
1152
1161
|
Returns:
|
|
1153
|
-
An LLMResult
|
|
1154
|
-
prompt and additional model provider-specific output.
|
|
1162
|
+
An `LLMResult`, which contains a list of candidate `Generations` for each
|
|
1163
|
+
input prompt and additional model provider-specific output.
|
|
1155
1164
|
"""
|
|
1156
1165
|
if isinstance(metadata, list):
|
|
1157
1166
|
metadata = [
|
|
@@ -1191,14 +1200,12 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1191
1200
|
msg = "run_name must be a list of the same length as prompts"
|
|
1192
1201
|
raise ValueError(msg)
|
|
1193
1202
|
callbacks = cast("list[Callbacks]", callbacks)
|
|
1194
|
-
tags_list = cast(
|
|
1195
|
-
"list[Optional[list[str]]]", tags or ([None] * len(prompts))
|
|
1196
|
-
)
|
|
1203
|
+
tags_list = cast("list[list[str] | None]", tags or ([None] * len(prompts)))
|
|
1197
1204
|
metadata_list = cast(
|
|
1198
|
-
"list[
|
|
1205
|
+
"list[dict[str, Any] | None]", metadata or ([{}] * len(prompts))
|
|
1199
1206
|
)
|
|
1200
1207
|
run_name_list = run_name or cast(
|
|
1201
|
-
"list[
|
|
1208
|
+
"list[str | None]", ([None] * len(prompts))
|
|
1202
1209
|
)
|
|
1203
1210
|
callback_managers = [
|
|
1204
1211
|
AsyncCallbackManager.configure(
|
|
@@ -1210,7 +1217,9 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1210
1217
|
meta,
|
|
1211
1218
|
self.metadata,
|
|
1212
1219
|
)
|
|
1213
|
-
for callback, tag, meta in zip(
|
|
1220
|
+
for callback, tag, meta in zip(
|
|
1221
|
+
callbacks, tags_list, metadata_list, strict=False
|
|
1222
|
+
)
|
|
1214
1223
|
]
|
|
1215
1224
|
else:
|
|
1216
1225
|
# We've received a single callbacks arg to apply to all inputs
|
|
@@ -1225,7 +1234,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1225
1234
|
self.metadata,
|
|
1226
1235
|
)
|
|
1227
1236
|
] * len(prompts)
|
|
1228
|
-
run_name_list = [cast("
|
|
1237
|
+
run_name_list = [cast("str | None", run_name)] * len(prompts)
|
|
1229
1238
|
run_ids_list = self._get_run_ids_list(run_id, prompts)
|
|
1230
1239
|
params = self.dict()
|
|
1231
1240
|
params["stop"] = stop
|
|
@@ -1255,7 +1264,11 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1255
1264
|
run_id=run_id_,
|
|
1256
1265
|
)
|
|
1257
1266
|
for callback_manager, prompt, run_name, run_id_ in zip(
|
|
1258
|
-
callback_managers,
|
|
1267
|
+
callback_managers,
|
|
1268
|
+
prompts,
|
|
1269
|
+
run_name_list,
|
|
1270
|
+
run_ids_list,
|
|
1271
|
+
strict=False,
|
|
1259
1272
|
)
|
|
1260
1273
|
]
|
|
1261
1274
|
)
|
|
@@ -1308,64 +1321,14 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1308
1321
|
generations = [existing_prompts[i] for i in range(len(prompts))]
|
|
1309
1322
|
return LLMResult(generations=generations, llm_output=llm_output, run=run_info)
|
|
1310
1323
|
|
|
1311
|
-
@deprecated("0.1.7", alternative="invoke", removal="1.0")
|
|
1312
|
-
def __call__(
|
|
1313
|
-
self,
|
|
1314
|
-
prompt: str,
|
|
1315
|
-
stop: Optional[list[str]] = None,
|
|
1316
|
-
callbacks: Callbacks = None,
|
|
1317
|
-
*,
|
|
1318
|
-
tags: Optional[list[str]] = None,
|
|
1319
|
-
metadata: Optional[dict[str, Any]] = None,
|
|
1320
|
-
**kwargs: Any,
|
|
1321
|
-
) -> str:
|
|
1322
|
-
"""Check Cache and run the LLM on the given prompt and input.
|
|
1323
|
-
|
|
1324
|
-
Args:
|
|
1325
|
-
prompt: The prompt to generate from.
|
|
1326
|
-
stop: Stop words to use when generating. Model output is cut off at the
|
|
1327
|
-
first occurrence of any of these substrings.
|
|
1328
|
-
callbacks: Callbacks to pass through. Used for executing additional
|
|
1329
|
-
functionality, such as logging or streaming, throughout generation.
|
|
1330
|
-
tags: List of tags to associate with the prompt.
|
|
1331
|
-
metadata: Metadata to associate with the prompt.
|
|
1332
|
-
**kwargs: Arbitrary additional keyword arguments. These are usually passed
|
|
1333
|
-
to the model provider API call.
|
|
1334
|
-
|
|
1335
|
-
Returns:
|
|
1336
|
-
The generated text.
|
|
1337
|
-
|
|
1338
|
-
Raises:
|
|
1339
|
-
ValueError: If the prompt is not a string.
|
|
1340
|
-
"""
|
|
1341
|
-
if not isinstance(prompt, str):
|
|
1342
|
-
msg = (
|
|
1343
|
-
"Argument `prompt` is expected to be a string. Instead found "
|
|
1344
|
-
f"{type(prompt)}. If you want to run the LLM on multiple prompts, use "
|
|
1345
|
-
"`generate` instead."
|
|
1346
|
-
)
|
|
1347
|
-
raise ValueError(msg) # noqa: TRY004
|
|
1348
|
-
return (
|
|
1349
|
-
self.generate(
|
|
1350
|
-
[prompt],
|
|
1351
|
-
stop=stop,
|
|
1352
|
-
callbacks=callbacks,
|
|
1353
|
-
tags=tags,
|
|
1354
|
-
metadata=metadata,
|
|
1355
|
-
**kwargs,
|
|
1356
|
-
)
|
|
1357
|
-
.generations[0][0]
|
|
1358
|
-
.text
|
|
1359
|
-
)
|
|
1360
|
-
|
|
1361
1324
|
async def _call_async(
|
|
1362
1325
|
self,
|
|
1363
1326
|
prompt: str,
|
|
1364
|
-
stop:
|
|
1327
|
+
stop: list[str] | None = None,
|
|
1365
1328
|
callbacks: Callbacks = None,
|
|
1366
1329
|
*,
|
|
1367
|
-
tags:
|
|
1368
|
-
metadata:
|
|
1330
|
+
tags: list[str] | None = None,
|
|
1331
|
+
metadata: dict[str, Any] | None = None,
|
|
1369
1332
|
**kwargs: Any,
|
|
1370
1333
|
) -> str:
|
|
1371
1334
|
"""Check Cache and run the LLM on the given prompt and input."""
|
|
@@ -1379,50 +1342,6 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1379
1342
|
)
|
|
1380
1343
|
return result.generations[0][0].text
|
|
1381
1344
|
|
|
1382
|
-
@deprecated("0.1.7", alternative="invoke", removal="1.0")
|
|
1383
|
-
@override
|
|
1384
|
-
def predict(
|
|
1385
|
-
self, text: str, *, stop: Optional[Sequence[str]] = None, **kwargs: Any
|
|
1386
|
-
) -> str:
|
|
1387
|
-
stop_ = None if stop is None else list(stop)
|
|
1388
|
-
return self(text, stop=stop_, **kwargs)
|
|
1389
|
-
|
|
1390
|
-
@deprecated("0.1.7", alternative="invoke", removal="1.0")
|
|
1391
|
-
@override
|
|
1392
|
-
def predict_messages(
|
|
1393
|
-
self,
|
|
1394
|
-
messages: list[BaseMessage],
|
|
1395
|
-
*,
|
|
1396
|
-
stop: Optional[Sequence[str]] = None,
|
|
1397
|
-
**kwargs: Any,
|
|
1398
|
-
) -> BaseMessage:
|
|
1399
|
-
text = get_buffer_string(messages)
|
|
1400
|
-
stop_ = None if stop is None else list(stop)
|
|
1401
|
-
content = self(text, stop=stop_, **kwargs)
|
|
1402
|
-
return AIMessage(content=content)
|
|
1403
|
-
|
|
1404
|
-
@deprecated("0.1.7", alternative="ainvoke", removal="1.0")
|
|
1405
|
-
@override
|
|
1406
|
-
async def apredict(
|
|
1407
|
-
self, text: str, *, stop: Optional[Sequence[str]] = None, **kwargs: Any
|
|
1408
|
-
) -> str:
|
|
1409
|
-
stop_ = None if stop is None else list(stop)
|
|
1410
|
-
return await self._call_async(text, stop=stop_, **kwargs)
|
|
1411
|
-
|
|
1412
|
-
@deprecated("0.1.7", alternative="ainvoke", removal="1.0")
|
|
1413
|
-
@override
|
|
1414
|
-
async def apredict_messages(
|
|
1415
|
-
self,
|
|
1416
|
-
messages: list[BaseMessage],
|
|
1417
|
-
*,
|
|
1418
|
-
stop: Optional[Sequence[str]] = None,
|
|
1419
|
-
**kwargs: Any,
|
|
1420
|
-
) -> BaseMessage:
|
|
1421
|
-
text = get_buffer_string(messages)
|
|
1422
|
-
stop_ = None if stop is None else list(stop)
|
|
1423
|
-
content = await self._call_async(text, stop=stop_, **kwargs)
|
|
1424
|
-
return AIMessage(content=content)
|
|
1425
|
-
|
|
1426
1345
|
def __str__(self) -> str:
|
|
1427
1346
|
"""Return a string representation of the object for printing."""
|
|
1428
1347
|
cls_name = f"\033[1m{self.__class__.__name__}\033[0m"
|
|
@@ -1440,7 +1359,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1440
1359
|
starter_dict["_type"] = self._llm_type
|
|
1441
1360
|
return starter_dict
|
|
1442
1361
|
|
|
1443
|
-
def save(self, file_path:
|
|
1362
|
+
def save(self, file_path: Path | str) -> None:
|
|
1444
1363
|
"""Save the LLM.
|
|
1445
1364
|
|
|
1446
1365
|
Args:
|
|
@@ -1450,11 +1369,9 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1450
1369
|
ValueError: If the file path is not a string or Path object.
|
|
1451
1370
|
|
|
1452
1371
|
Example:
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
llm.save(file_path="path/llm.yaml")
|
|
1457
|
-
|
|
1372
|
+
```python
|
|
1373
|
+
llm.save(file_path="path/llm.yaml")
|
|
1374
|
+
```
|
|
1458
1375
|
"""
|
|
1459
1376
|
# Convert file to Path object.
|
|
1460
1377
|
save_path = Path(file_path)
|
|
@@ -1499,19 +1416,14 @@ class LLM(BaseLLM):
|
|
|
1499
1416
|
`astream` will use `_astream` if provided, otherwise it will implement
|
|
1500
1417
|
a fallback behavior that will use `_stream` if `_stream` is implemented,
|
|
1501
1418
|
and use `_acall` if `_stream` is not implemented.
|
|
1502
|
-
|
|
1503
|
-
Please see the following guide for more information on how to
|
|
1504
|
-
implement a custom LLM:
|
|
1505
|
-
|
|
1506
|
-
https://python.langchain.com/docs/how_to/custom_llm/
|
|
1507
1419
|
"""
|
|
1508
1420
|
|
|
1509
1421
|
@abstractmethod
|
|
1510
1422
|
def _call(
|
|
1511
1423
|
self,
|
|
1512
1424
|
prompt: str,
|
|
1513
|
-
stop:
|
|
1514
|
-
run_manager:
|
|
1425
|
+
stop: list[str] | None = None,
|
|
1426
|
+
run_manager: CallbackManagerForLLMRun | None = None,
|
|
1515
1427
|
**kwargs: Any,
|
|
1516
1428
|
) -> str:
|
|
1517
1429
|
"""Run the LLM on the given input.
|
|
@@ -1520,12 +1432,16 @@ class LLM(BaseLLM):
|
|
|
1520
1432
|
|
|
1521
1433
|
Args:
|
|
1522
1434
|
prompt: The prompt to generate from.
|
|
1523
|
-
stop: Stop words to use when generating.
|
|
1524
|
-
|
|
1525
|
-
|
|
1435
|
+
stop: Stop words to use when generating.
|
|
1436
|
+
|
|
1437
|
+
Model output is cut off at the first occurrence of any of these
|
|
1438
|
+
substrings.
|
|
1439
|
+
|
|
1440
|
+
If stop tokens are not supported consider raising `NotImplementedError`.
|
|
1526
1441
|
run_manager: Callback manager for the run.
|
|
1527
|
-
**kwargs: Arbitrary additional keyword arguments.
|
|
1528
|
-
|
|
1442
|
+
**kwargs: Arbitrary additional keyword arguments.
|
|
1443
|
+
|
|
1444
|
+
These are usually passed to the model provider API call.
|
|
1529
1445
|
|
|
1530
1446
|
Returns:
|
|
1531
1447
|
The model output as a string. SHOULD NOT include the prompt.
|
|
@@ -1534,8 +1450,8 @@ class LLM(BaseLLM):
|
|
|
1534
1450
|
async def _acall(
|
|
1535
1451
|
self,
|
|
1536
1452
|
prompt: str,
|
|
1537
|
-
stop:
|
|
1538
|
-
run_manager:
|
|
1453
|
+
stop: list[str] | None = None,
|
|
1454
|
+
run_manager: AsyncCallbackManagerForLLMRun | None = None,
|
|
1539
1455
|
**kwargs: Any,
|
|
1540
1456
|
) -> str:
|
|
1541
1457
|
"""Async version of the _call method.
|
|
@@ -1546,12 +1462,16 @@ class LLM(BaseLLM):
|
|
|
1546
1462
|
|
|
1547
1463
|
Args:
|
|
1548
1464
|
prompt: The prompt to generate from.
|
|
1549
|
-
stop: Stop words to use when generating.
|
|
1550
|
-
|
|
1551
|
-
|
|
1465
|
+
stop: Stop words to use when generating.
|
|
1466
|
+
|
|
1467
|
+
Model output is cut off at the first occurrence of any of these
|
|
1468
|
+
substrings.
|
|
1469
|
+
|
|
1470
|
+
If stop tokens are not supported consider raising `NotImplementedError`.
|
|
1552
1471
|
run_manager: Callback manager for the run.
|
|
1553
|
-
**kwargs: Arbitrary additional keyword arguments.
|
|
1554
|
-
|
|
1472
|
+
**kwargs: Arbitrary additional keyword arguments.
|
|
1473
|
+
|
|
1474
|
+
These are usually passed to the model provider API call.
|
|
1555
1475
|
|
|
1556
1476
|
Returns:
|
|
1557
1477
|
The model output as a string. SHOULD NOT include the prompt.
|
|
@@ -1568,8 +1488,8 @@ class LLM(BaseLLM):
|
|
|
1568
1488
|
def _generate(
|
|
1569
1489
|
self,
|
|
1570
1490
|
prompts: list[str],
|
|
1571
|
-
stop:
|
|
1572
|
-
run_manager:
|
|
1491
|
+
stop: list[str] | None = None,
|
|
1492
|
+
run_manager: CallbackManagerForLLMRun | None = None,
|
|
1573
1493
|
**kwargs: Any,
|
|
1574
1494
|
) -> LLMResult:
|
|
1575
1495
|
# TODO: add caching here.
|
|
@@ -1587,8 +1507,8 @@ class LLM(BaseLLM):
|
|
|
1587
1507
|
async def _agenerate(
|
|
1588
1508
|
self,
|
|
1589
1509
|
prompts: list[str],
|
|
1590
|
-
stop:
|
|
1591
|
-
run_manager:
|
|
1510
|
+
stop: list[str] | None = None,
|
|
1511
|
+
run_manager: AsyncCallbackManagerForLLMRun | None = None,
|
|
1592
1512
|
**kwargs: Any,
|
|
1593
1513
|
) -> LLMResult:
|
|
1594
1514
|
generations = []
|