langchain-core 1.0.0a5__py3-none-any.whl → 1.0.3__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 +20 -22
- 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 +436 -513
- 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 +98 -90
- 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 +1 -1
- 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 +53 -162
- langchain_core/language_models/chat_models.py +298 -387
- langchain_core/language_models/fake.py +11 -11
- langchain_core/language_models/fake_chat_models.py +42 -36
- langchain_core/language_models/llms.py +125 -235
- 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 +58 -52
- langchain_core/messages/block_translators/__init__.py +27 -17
- 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 +505 -20
- 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 +337 -328
- 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 +474 -504
- 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 +16 -21
- 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 +10 -11
- langchain_core/outputs/llm_result.py +10 -10
- langchain_core/prompt_values.py +11 -17
- langchain_core/prompts/__init__.py +3 -27
- langchain_core/prompts/base.py +48 -56
- langchain_core/prompts/chat.py +275 -325
- langchain_core/prompts/dict.py +5 -5
- 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 +3 -3
- 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 +1478 -1630
- langchain_core/runnables/branch.py +53 -57
- langchain_core/runnables/config.py +72 -89
- langchain_core/runnables/configurable.py +120 -137
- langchain_core/runnables/fallbacks.py +83 -79
- langchain_core/runnables/graph.py +91 -97
- langchain_core/runnables/graph_ascii.py +27 -28
- langchain_core/runnables/graph_mermaid.py +38 -50
- langchain_core/runnables/graph_png.py +15 -16
- langchain_core/runnables/history.py +135 -148
- langchain_core/runnables/passthrough.py +124 -150
- langchain_core/runnables/retry.py +46 -51
- langchain_core/runnables/router.py +25 -30
- 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 +27 -29
- langchain_core/tools/__init__.py +1 -14
- langchain_core/tools/base.py +285 -229
- 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 +89 -186
- langchain_core/utils/html.py +7 -8
- langchain_core/utils/input.py +6 -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 +33 -35
- 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.3.dist-info/METADATA +69 -0
- langchain_core-1.0.3.dist-info/RECORD +172 -0
- {langchain_core-1.0.0a5.dist-info → langchain_core-1.0.3.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.0a5.dist-info/METADATA +0 -77
- langchain_core-1.0.0a5.dist-info/RECORD +0 -181
- langchain_core-1.0.0a5.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,8 +643,8 @@ 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.
|
|
@@ -682,8 +663,8 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
682
663
|
async def _agenerate(
|
|
683
664
|
self,
|
|
684
665
|
prompts: list[str],
|
|
685
|
-
stop:
|
|
686
|
-
run_manager:
|
|
666
|
+
stop: list[str] | None = None,
|
|
667
|
+
run_manager: AsyncCallbackManagerForLLMRun | None = None,
|
|
687
668
|
**kwargs: Any,
|
|
688
669
|
) -> LLMResult:
|
|
689
670
|
"""Run the LLM on the given prompts.
|
|
@@ -710,8 +691,8 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
710
691
|
def _stream(
|
|
711
692
|
self,
|
|
712
693
|
prompt: str,
|
|
713
|
-
stop:
|
|
714
|
-
run_manager:
|
|
694
|
+
stop: list[str] | None = None,
|
|
695
|
+
run_manager: CallbackManagerForLLMRun | None = None,
|
|
715
696
|
**kwargs: Any,
|
|
716
697
|
) -> Iterator[GenerationChunk]:
|
|
717
698
|
"""Stream the LLM on the given prompt.
|
|
@@ -738,8 +719,8 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
738
719
|
async def _astream(
|
|
739
720
|
self,
|
|
740
721
|
prompt: str,
|
|
741
|
-
stop:
|
|
742
|
-
run_manager:
|
|
722
|
+
stop: list[str] | None = None,
|
|
723
|
+
run_manager: AsyncCallbackManagerForLLMRun | None = None,
|
|
743
724
|
**kwargs: Any,
|
|
744
725
|
) -> AsyncIterator[GenerationChunk]:
|
|
745
726
|
"""An async version of the _stream method.
|
|
@@ -783,8 +764,8 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
783
764
|
def generate_prompt(
|
|
784
765
|
self,
|
|
785
766
|
prompts: list[PromptValue],
|
|
786
|
-
stop:
|
|
787
|
-
callbacks:
|
|
767
|
+
stop: list[str] | None = None,
|
|
768
|
+
callbacks: Callbacks | list[Callbacks] | None = None,
|
|
788
769
|
**kwargs: Any,
|
|
789
770
|
) -> LLMResult:
|
|
790
771
|
prompt_strings = [p.to_string() for p in prompts]
|
|
@@ -794,8 +775,8 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
794
775
|
async def agenerate_prompt(
|
|
795
776
|
self,
|
|
796
777
|
prompts: list[PromptValue],
|
|
797
|
-
stop:
|
|
798
|
-
callbacks:
|
|
778
|
+
stop: list[str] | None = None,
|
|
779
|
+
callbacks: Callbacks | list[Callbacks] | None = None,
|
|
799
780
|
**kwargs: Any,
|
|
800
781
|
) -> LLMResult:
|
|
801
782
|
prompt_strings = [p.to_string() for p in prompts]
|
|
@@ -806,7 +787,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
806
787
|
def _generate_helper(
|
|
807
788
|
self,
|
|
808
789
|
prompts: list[str],
|
|
809
|
-
stop:
|
|
790
|
+
stop: list[str] | None,
|
|
810
791
|
run_managers: list[CallbackManagerForLLMRun],
|
|
811
792
|
*,
|
|
812
793
|
new_arg_supported: bool,
|
|
@@ -829,7 +810,9 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
829
810
|
run_manager.on_llm_error(e, response=LLMResult(generations=[]))
|
|
830
811
|
raise
|
|
831
812
|
flattened_outputs = output.flatten()
|
|
832
|
-
for manager, flattened_output in zip(
|
|
813
|
+
for manager, flattened_output in zip(
|
|
814
|
+
run_managers, flattened_outputs, strict=False
|
|
815
|
+
):
|
|
833
816
|
manager.on_llm_end(flattened_output)
|
|
834
817
|
if run_managers:
|
|
835
818
|
output.run = [
|
|
@@ -840,13 +823,13 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
840
823
|
def generate(
|
|
841
824
|
self,
|
|
842
825
|
prompts: list[str],
|
|
843
|
-
stop:
|
|
844
|
-
callbacks:
|
|
826
|
+
stop: list[str] | None = None,
|
|
827
|
+
callbacks: Callbacks | list[Callbacks] | None = None,
|
|
845
828
|
*,
|
|
846
|
-
tags:
|
|
847
|
-
metadata:
|
|
848
|
-
run_name:
|
|
849
|
-
run_id:
|
|
829
|
+
tags: list[str] | list[list[str]] | None = None,
|
|
830
|
+
metadata: dict[str, Any] | list[dict[str, Any]] | None = None,
|
|
831
|
+
run_name: str | list[str] | None = None,
|
|
832
|
+
run_id: uuid.UUID | list[uuid.UUID | None] | None = None,
|
|
850
833
|
**kwargs: Any,
|
|
851
834
|
) -> LLMResult:
|
|
852
835
|
"""Pass a sequence of prompts to a model and return generations.
|
|
@@ -859,13 +842,13 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
859
842
|
1. Take advantage of batched calls,
|
|
860
843
|
2. Need more output from the model than just the top generated value,
|
|
861
844
|
3. Are building chains that are agnostic to the underlying language model
|
|
862
|
-
|
|
845
|
+
type (e.g., pure text completion models vs chat models).
|
|
863
846
|
|
|
864
847
|
Args:
|
|
865
848
|
prompts: List of string prompts.
|
|
866
849
|
stop: Stop words to use when generating. Model output is cut off at the
|
|
867
850
|
first occurrence of any of these substrings.
|
|
868
|
-
callbacks: Callbacks to pass through. Used for executing additional
|
|
851
|
+
callbacks: `Callbacks` to pass through. Used for executing additional
|
|
869
852
|
functionality, such as logging or streaming, throughout generation.
|
|
870
853
|
tags: List of tags to associate with each prompt. If provided, the length
|
|
871
854
|
of the list must match the length of the prompts list.
|
|
@@ -881,12 +864,12 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
881
864
|
|
|
882
865
|
Raises:
|
|
883
866
|
ValueError: If prompts is not a list.
|
|
884
|
-
ValueError: If the length of
|
|
885
|
-
|
|
867
|
+
ValueError: If the length of `callbacks`, `tags`, `metadata`, or
|
|
868
|
+
`run_name` (if provided) does not match the length of prompts.
|
|
886
869
|
|
|
887
870
|
Returns:
|
|
888
|
-
An LLMResult
|
|
889
|
-
prompt and additional model provider-specific output.
|
|
871
|
+
An `LLMResult`, which contains a list of candidate `Generations` for each
|
|
872
|
+
input prompt and additional model provider-specific output.
|
|
890
873
|
"""
|
|
891
874
|
if not isinstance(prompts, list):
|
|
892
875
|
msg = (
|
|
@@ -936,14 +919,12 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
936
919
|
msg = "run_name must be a list of the same length as prompts"
|
|
937
920
|
raise ValueError(msg)
|
|
938
921
|
callbacks = cast("list[Callbacks]", callbacks)
|
|
939
|
-
tags_list = cast(
|
|
940
|
-
"list[Optional[list[str]]]", tags or ([None] * len(prompts))
|
|
941
|
-
)
|
|
922
|
+
tags_list = cast("list[list[str] | None]", tags or ([None] * len(prompts)))
|
|
942
923
|
metadata_list = cast(
|
|
943
|
-
"list[
|
|
924
|
+
"list[dict[str, Any] | None]", metadata or ([{}] * len(prompts))
|
|
944
925
|
)
|
|
945
926
|
run_name_list = run_name or cast(
|
|
946
|
-
"list[
|
|
927
|
+
"list[str | None]", ([None] * len(prompts))
|
|
947
928
|
)
|
|
948
929
|
callback_managers = [
|
|
949
930
|
CallbackManager.configure(
|
|
@@ -955,7 +936,9 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
955
936
|
meta,
|
|
956
937
|
self.metadata,
|
|
957
938
|
)
|
|
958
|
-
for callback, tag, meta in zip(
|
|
939
|
+
for callback, tag, meta in zip(
|
|
940
|
+
callbacks, tags_list, metadata_list, strict=False
|
|
941
|
+
)
|
|
959
942
|
]
|
|
960
943
|
else:
|
|
961
944
|
# We've received a single callbacks arg to apply to all inputs
|
|
@@ -970,7 +953,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
970
953
|
self.metadata,
|
|
971
954
|
)
|
|
972
955
|
] * len(prompts)
|
|
973
|
-
run_name_list = [cast("
|
|
956
|
+
run_name_list = [cast("str | None", run_name)] * len(prompts)
|
|
974
957
|
run_ids_list = self._get_run_ids_list(run_id, prompts)
|
|
975
958
|
params = self.dict()
|
|
976
959
|
params["stop"] = stop
|
|
@@ -996,7 +979,11 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
996
979
|
run_id=run_id_,
|
|
997
980
|
)[0]
|
|
998
981
|
for callback_manager, prompt, run_name, run_id_ in zip(
|
|
999
|
-
callback_managers,
|
|
982
|
+
callback_managers,
|
|
983
|
+
prompts,
|
|
984
|
+
run_name_list,
|
|
985
|
+
run_ids_list,
|
|
986
|
+
strict=False,
|
|
1000
987
|
)
|
|
1001
988
|
]
|
|
1002
989
|
return self._generate_helper(
|
|
@@ -1046,7 +1033,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1046
1033
|
|
|
1047
1034
|
@staticmethod
|
|
1048
1035
|
def _get_run_ids_list(
|
|
1049
|
-
run_id:
|
|
1036
|
+
run_id: uuid.UUID | list[uuid.UUID | None] | None, prompts: list
|
|
1050
1037
|
) -> list:
|
|
1051
1038
|
if run_id is None:
|
|
1052
1039
|
return [None] * len(prompts)
|
|
@@ -1063,7 +1050,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1063
1050
|
async def _agenerate_helper(
|
|
1064
1051
|
self,
|
|
1065
1052
|
prompts: list[str],
|
|
1066
|
-
stop:
|
|
1053
|
+
stop: list[str] | None,
|
|
1067
1054
|
run_managers: list[AsyncCallbackManagerForLLMRun],
|
|
1068
1055
|
*,
|
|
1069
1056
|
new_arg_supported: bool,
|
|
@@ -1093,7 +1080,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1093
1080
|
*[
|
|
1094
1081
|
run_manager.on_llm_end(flattened_output)
|
|
1095
1082
|
for run_manager, flattened_output in zip(
|
|
1096
|
-
run_managers, flattened_outputs
|
|
1083
|
+
run_managers, flattened_outputs, strict=False
|
|
1097
1084
|
)
|
|
1098
1085
|
]
|
|
1099
1086
|
)
|
|
@@ -1106,13 +1093,13 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1106
1093
|
async def agenerate(
|
|
1107
1094
|
self,
|
|
1108
1095
|
prompts: list[str],
|
|
1109
|
-
stop:
|
|
1110
|
-
callbacks:
|
|
1096
|
+
stop: list[str] | None = None,
|
|
1097
|
+
callbacks: Callbacks | list[Callbacks] | None = None,
|
|
1111
1098
|
*,
|
|
1112
|
-
tags:
|
|
1113
|
-
metadata:
|
|
1114
|
-
run_name:
|
|
1115
|
-
run_id:
|
|
1099
|
+
tags: list[str] | list[list[str]] | None = None,
|
|
1100
|
+
metadata: dict[str, Any] | list[dict[str, Any]] | None = None,
|
|
1101
|
+
run_name: str | list[str] | None = None,
|
|
1102
|
+
run_id: uuid.UUID | list[uuid.UUID | None] | None = None,
|
|
1116
1103
|
**kwargs: Any,
|
|
1117
1104
|
) -> LLMResult:
|
|
1118
1105
|
"""Asynchronously pass a sequence of prompts to a model and return generations.
|
|
@@ -1125,13 +1112,13 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1125
1112
|
1. Take advantage of batched calls,
|
|
1126
1113
|
2. Need more output from the model than just the top generated value,
|
|
1127
1114
|
3. Are building chains that are agnostic to the underlying language model
|
|
1128
|
-
|
|
1115
|
+
type (e.g., pure text completion models vs chat models).
|
|
1129
1116
|
|
|
1130
1117
|
Args:
|
|
1131
1118
|
prompts: List of string prompts.
|
|
1132
1119
|
stop: Stop words to use when generating. Model output is cut off at the
|
|
1133
1120
|
first occurrence of any of these substrings.
|
|
1134
|
-
callbacks: Callbacks to pass through. Used for executing additional
|
|
1121
|
+
callbacks: `Callbacks` to pass through. Used for executing additional
|
|
1135
1122
|
functionality, such as logging or streaming, throughout generation.
|
|
1136
1123
|
tags: List of tags to associate with each prompt. If provided, the length
|
|
1137
1124
|
of the list must match the length of the prompts list.
|
|
@@ -1146,12 +1133,12 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1146
1133
|
to the model provider API call.
|
|
1147
1134
|
|
|
1148
1135
|
Raises:
|
|
1149
|
-
ValueError: If the length of
|
|
1150
|
-
|
|
1136
|
+
ValueError: If the length of `callbacks`, `tags`, `metadata`, or
|
|
1137
|
+
`run_name` (if provided) does not match the length of prompts.
|
|
1151
1138
|
|
|
1152
1139
|
Returns:
|
|
1153
|
-
An LLMResult
|
|
1154
|
-
prompt and additional model provider-specific output.
|
|
1140
|
+
An `LLMResult`, which contains a list of candidate `Generations` for each
|
|
1141
|
+
input prompt and additional model provider-specific output.
|
|
1155
1142
|
"""
|
|
1156
1143
|
if isinstance(metadata, list):
|
|
1157
1144
|
metadata = [
|
|
@@ -1191,14 +1178,12 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1191
1178
|
msg = "run_name must be a list of the same length as prompts"
|
|
1192
1179
|
raise ValueError(msg)
|
|
1193
1180
|
callbacks = cast("list[Callbacks]", callbacks)
|
|
1194
|
-
tags_list = cast(
|
|
1195
|
-
"list[Optional[list[str]]]", tags or ([None] * len(prompts))
|
|
1196
|
-
)
|
|
1181
|
+
tags_list = cast("list[list[str] | None]", tags or ([None] * len(prompts)))
|
|
1197
1182
|
metadata_list = cast(
|
|
1198
|
-
"list[
|
|
1183
|
+
"list[dict[str, Any] | None]", metadata or ([{}] * len(prompts))
|
|
1199
1184
|
)
|
|
1200
1185
|
run_name_list = run_name or cast(
|
|
1201
|
-
"list[
|
|
1186
|
+
"list[str | None]", ([None] * len(prompts))
|
|
1202
1187
|
)
|
|
1203
1188
|
callback_managers = [
|
|
1204
1189
|
AsyncCallbackManager.configure(
|
|
@@ -1210,7 +1195,9 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1210
1195
|
meta,
|
|
1211
1196
|
self.metadata,
|
|
1212
1197
|
)
|
|
1213
|
-
for callback, tag, meta in zip(
|
|
1198
|
+
for callback, tag, meta in zip(
|
|
1199
|
+
callbacks, tags_list, metadata_list, strict=False
|
|
1200
|
+
)
|
|
1214
1201
|
]
|
|
1215
1202
|
else:
|
|
1216
1203
|
# We've received a single callbacks arg to apply to all inputs
|
|
@@ -1225,7 +1212,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1225
1212
|
self.metadata,
|
|
1226
1213
|
)
|
|
1227
1214
|
] * len(prompts)
|
|
1228
|
-
run_name_list = [cast("
|
|
1215
|
+
run_name_list = [cast("str | None", run_name)] * len(prompts)
|
|
1229
1216
|
run_ids_list = self._get_run_ids_list(run_id, prompts)
|
|
1230
1217
|
params = self.dict()
|
|
1231
1218
|
params["stop"] = stop
|
|
@@ -1255,7 +1242,11 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1255
1242
|
run_id=run_id_,
|
|
1256
1243
|
)
|
|
1257
1244
|
for callback_manager, prompt, run_name, run_id_ in zip(
|
|
1258
|
-
callback_managers,
|
|
1245
|
+
callback_managers,
|
|
1246
|
+
prompts,
|
|
1247
|
+
run_name_list,
|
|
1248
|
+
run_ids_list,
|
|
1249
|
+
strict=False,
|
|
1259
1250
|
)
|
|
1260
1251
|
]
|
|
1261
1252
|
)
|
|
@@ -1308,64 +1299,14 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1308
1299
|
generations = [existing_prompts[i] for i in range(len(prompts))]
|
|
1309
1300
|
return LLMResult(generations=generations, llm_output=llm_output, run=run_info)
|
|
1310
1301
|
|
|
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
1302
|
async def _call_async(
|
|
1362
1303
|
self,
|
|
1363
1304
|
prompt: str,
|
|
1364
|
-
stop:
|
|
1305
|
+
stop: list[str] | None = None,
|
|
1365
1306
|
callbacks: Callbacks = None,
|
|
1366
1307
|
*,
|
|
1367
|
-
tags:
|
|
1368
|
-
metadata:
|
|
1308
|
+
tags: list[str] | None = None,
|
|
1309
|
+
metadata: dict[str, Any] | None = None,
|
|
1369
1310
|
**kwargs: Any,
|
|
1370
1311
|
) -> str:
|
|
1371
1312
|
"""Check Cache and run the LLM on the given prompt and input."""
|
|
@@ -1379,50 +1320,6 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1379
1320
|
)
|
|
1380
1321
|
return result.generations[0][0].text
|
|
1381
1322
|
|
|
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
1323
|
def __str__(self) -> str:
|
|
1427
1324
|
"""Return a string representation of the object for printing."""
|
|
1428
1325
|
cls_name = f"\033[1m{self.__class__.__name__}\033[0m"
|
|
@@ -1440,7 +1337,7 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1440
1337
|
starter_dict["_type"] = self._llm_type
|
|
1441
1338
|
return starter_dict
|
|
1442
1339
|
|
|
1443
|
-
def save(self, file_path:
|
|
1340
|
+
def save(self, file_path: Path | str) -> None:
|
|
1444
1341
|
"""Save the LLM.
|
|
1445
1342
|
|
|
1446
1343
|
Args:
|
|
@@ -1450,11 +1347,9 @@ class BaseLLM(BaseLanguageModel[str], ABC):
|
|
|
1450
1347
|
ValueError: If the file path is not a string or Path object.
|
|
1451
1348
|
|
|
1452
1349
|
Example:
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
llm.save(file_path="path/llm.yaml")
|
|
1457
|
-
|
|
1350
|
+
```python
|
|
1351
|
+
llm.save(file_path="path/llm.yaml")
|
|
1352
|
+
```
|
|
1458
1353
|
"""
|
|
1459
1354
|
# Convert file to Path object.
|
|
1460
1355
|
save_path = Path(file_path)
|
|
@@ -1499,19 +1394,14 @@ class LLM(BaseLLM):
|
|
|
1499
1394
|
`astream` will use `_astream` if provided, otherwise it will implement
|
|
1500
1395
|
a fallback behavior that will use `_stream` if `_stream` is implemented,
|
|
1501
1396
|
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
1397
|
"""
|
|
1508
1398
|
|
|
1509
1399
|
@abstractmethod
|
|
1510
1400
|
def _call(
|
|
1511
1401
|
self,
|
|
1512
1402
|
prompt: str,
|
|
1513
|
-
stop:
|
|
1514
|
-
run_manager:
|
|
1403
|
+
stop: list[str] | None = None,
|
|
1404
|
+
run_manager: CallbackManagerForLLMRun | None = None,
|
|
1515
1405
|
**kwargs: Any,
|
|
1516
1406
|
) -> str:
|
|
1517
1407
|
"""Run the LLM on the given input.
|
|
@@ -1534,8 +1424,8 @@ class LLM(BaseLLM):
|
|
|
1534
1424
|
async def _acall(
|
|
1535
1425
|
self,
|
|
1536
1426
|
prompt: str,
|
|
1537
|
-
stop:
|
|
1538
|
-
run_manager:
|
|
1427
|
+
stop: list[str] | None = None,
|
|
1428
|
+
run_manager: AsyncCallbackManagerForLLMRun | None = None,
|
|
1539
1429
|
**kwargs: Any,
|
|
1540
1430
|
) -> str:
|
|
1541
1431
|
"""Async version of the _call method.
|
|
@@ -1568,8 +1458,8 @@ class LLM(BaseLLM):
|
|
|
1568
1458
|
def _generate(
|
|
1569
1459
|
self,
|
|
1570
1460
|
prompts: list[str],
|
|
1571
|
-
stop:
|
|
1572
|
-
run_manager:
|
|
1461
|
+
stop: list[str] | None = None,
|
|
1462
|
+
run_manager: CallbackManagerForLLMRun | None = None,
|
|
1573
1463
|
**kwargs: Any,
|
|
1574
1464
|
) -> LLMResult:
|
|
1575
1465
|
# TODO: add caching here.
|
|
@@ -1587,8 +1477,8 @@ class LLM(BaseLLM):
|
|
|
1587
1477
|
async def _agenerate(
|
|
1588
1478
|
self,
|
|
1589
1479
|
prompts: list[str],
|
|
1590
|
-
stop:
|
|
1591
|
-
run_manager:
|
|
1480
|
+
stop: list[str] | None = None,
|
|
1481
|
+
run_manager: AsyncCallbackManagerForLLMRun | None = None,
|
|
1592
1482
|
**kwargs: Any,
|
|
1593
1483
|
) -> LLMResult:
|
|
1594
1484
|
generations = []
|