langchain_community.llms.pipelineai
.PipelineAI¶
注意
PipelineAI 实现了标准的 Runnable Interface
。🏃
Runnable Interface
具有在 runnables 上可用的附加方法,例如 with_types
、 with_retry
、 assign
、 bind
、 get_graph
等。
- class langchain_community.llms.pipelineai.PipelineAI[source]¶
基类:
LLM
,BaseModel
PipelineAI 大型语言模型。
要使用,您应该安装
pipeline-ai
python 包,并使用您的 API 密钥设置环境变量PIPELINE_API_KEY
。任何有效的参数都可以传递给调用,即使该类上没有显式保存这些参数。
示例
from langchain_community.llms import PipelineAI pipeline = PipelineAI(pipeline_key="")
- param cache: Union[BaseCache, bool, None] = None¶
是否缓存响应。
如果为 true,将使用全局缓存。
如果为 false,将不使用缓存
如果为 None,如果已设置全局缓存,则使用全局缓存,否则不使用缓存。
如果是 BaseCache 的实例,将使用提供的缓存。
模型的流式方法目前不支持缓存。
- param callback_manager: Optional[BaseCallbackManager] = None¶
[已弃用]
- param callbacks: Callbacks = None¶
要添加到运行轨迹的回调。
- param custom_get_token_ids: Optional[Callable[[str], List[int]]] = None¶
用于计算令牌的可选编码器。
- param metadata: Optional[Dict[str, Any]] = None¶
要添加到运行轨迹的元数据。
- param pipeline_api_key: Optional[SecretStr] = None¶
- 约束
type = string
writeOnly = True
format = password
- param pipeline_key: str = ''¶
目标流水线的 ID 或标签
- param pipeline_kwargs: Dict[str, Any] [Optional]¶
包含任何对 create 调用有效的,但未显式指定的流水线参数。
- param tags: Optional[List[str]] = None¶
要添加到运行轨迹的标签。
- param verbose: bool [Optional]¶
是否打印输出响应文本。
- __call__(prompt: str, stop: Optional[List[str]] = None, callbacks: Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]] = None, *, tags: Optional[List[str]] = None, metadata: Optional[Dict[str, Any]] = None, **kwargs: Any) str ¶
Deprecated since version langchain-core==0.1.7: Use
invoke
instead.检查缓存并在给定的 prompt 和输入上运行 LLM。
- 参数
prompt (str) – 要从中生成的提示。
stop (Optional[List[str]]) – 生成时要使用的停止词。模型输出在首次出现任何这些子字符串时被截断。
callbacks (Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]]) – 要传递的回调。用于在整个生成过程中执行其他功能,例如日志记录或流式传输。
tags (Optional[List[str]]) – 要与提示关联的标签列表。
metadata (Optional[Dict[str, Any]]) – 要与提示关联的元数据。
**kwargs (Any) – 任意附加关键字参数。这些通常传递给模型提供商 API 调用。
- 返回值
生成的文本。
- Raises
ValueError – 如果提示不是字符串。
- 返回类型
str
- async abatch(inputs: List[Union[PromptValue, str, Sequence[Union[BaseMessage, List[str], Tuple[str, str], str, Dict[str, Any]]]]], config: Optional[Union[RunnableConfig, List[RunnableConfig]]] = None, *, return_exceptions: bool = False, **kwargs: Any) List[str] ¶
默认实现使用 asyncio.gather 并行运行 ainvoke。
batch 的默认实现非常适用于 IO 绑定 runnable。
如果子类可以更有效地进行批处理,则应覆盖此方法;例如,如果底层 Runnable 使用支持批处理模式的 API。
- 参数
inputs (List[Union[PromptValue, str, Sequence[Union[BaseMessage, List[str], Tuple[str, str], str, Dict[str, Any]]]]]) – Runnable 的输入列表。
config (Optional[Union[RunnableConfig, List[RunnableConfig]]]) – 调用 Runnable 时要使用的配置。该配置支持标准键,例如用于跟踪目的的 ‘tags’、‘metadata’,用于控制并行执行多少工作的 ‘max_concurrency’ 以及其他键。有关更多详细信息,请参阅 RunnableConfig。默认为 None。
return_exceptions (bool) – 是否返回异常而不是引发异常。默认为 False。
kwargs (Any) – 要传递给 Runnable 的其他关键字参数。
- 返回值
来自 Runnable 的输出列表。
- 返回类型
List[str]
- async abatch_as_completed(inputs: Sequence[Input], config: Optional[Union[RunnableConfig, Sequence[RunnableConfig]]] = None, *, return_exceptions: bool = False, **kwargs: Optional[Any]) AsyncIterator[Tuple[int, Union[Output, Exception]]] ¶
并行运行 ainvoke 处理输入列表,并在完成时生成结果。
- 参数
inputs (Sequence[Input]) – Runnable 的输入列表。
config (Optional[Union[RunnableConfig, Sequence[RunnableConfig]]]) – 调用 Runnable 时要使用的配置。该配置支持标准键,例如用于跟踪目的的 ‘tags’、‘metadata’,用于控制并行执行多少工作的 ‘max_concurrency’ 以及其他键。有关更多详细信息,请参阅 RunnableConfig。默认为 None。 默认为 None。
return_exceptions (bool) – 是否返回异常而不是引发异常。默认为 False。
kwargs (Optional[Any]) – 要传递给 Runnable 的其他关键字参数。
- Yields
输入索引和 Runnable 输出的元组。
- 返回类型
AsyncIterator[Tuple[int, Union[Output, Exception]]]
- async agenerate(prompts: List[str], stop: Optional[List[str]] = None, callbacks: Union[List[BaseCallbackHandler], BaseCallbackManager], None, List[Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]]]] = None, *, tags: Optional[Union[List[str], List[List[str]]]] = None, metadata: Optional[Union[Dict[str, Any], List[Dict[str, Any]]]] = None, run_name: Optional[Union[str, List[str]]] = None, run_id: Optional[Union[UUID, List[Optional[UUID]]]] = None, **kwargs: Any) LLMResult ¶
异步地将一系列提示传递给模型并返回生成结果。
此方法应利用模型的批量调用,这些模型公开了批量 API。
- 当您想要
利用批量调用,
需要从模型获得比仅生成的首要值更多的输出,
- 正在构建对底层语言模型不可知的链
类型(例如,纯文本完成模型与聊天模型)。
- 参数
prompts (List[str]) – 字符串提示列表。
stop (Optional[List[str]]) – 生成时要使用的停止词。模型输出在首次出现任何这些子字符串时被截断。
callbacks (Union[List[BaseCallbackHandler], BaseCallbackManager, None, List[Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]]]]) – 要传递的回调函数。用于在整个生成过程中执行额外的功能,例如日志记录或流式传输。
tags (Optional[Union[List[str], List[List[str]]]]) – 与每个提示关联的标签列表。如果提供,则列表的长度必须与提示列表的长度匹配。
metadata (Optional[Union[Dict[str, Any], List[Dict[str, Any]]]]) – 与每个提示关联的元数据字典列表。如果提供,则列表的长度必须与提示列表的长度匹配。
run_name (Optional[Union[str, List[str]]]) – 与每个提示关联的运行名称列表。如果提供,则列表的长度必须与提示列表的长度匹配。
run_id (Optional[Union[UUID, List[Optional[UUID]]]]) – 与每个提示关联的运行 ID 列表。如果提供,则列表的长度必须与提示列表的长度匹配。
**kwargs (Any) – 任意附加关键字参数。这些通常传递给模型提供商 API 调用。
- 返回值
- An LLMResult, which contains a list of candidate Generations for each input
prompt and additional model provider-specific output.
- 返回类型
- async agenerate_prompt(prompts: List[PromptValue], stop: Optional[List[str]] = None, callbacks: Union[List[BaseCallbackHandler], BaseCallbackManager, None, List[Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]]]] = None, **kwargs: Any) LLMResult ¶
Asynchronously pass a sequence of prompts and return model generations.
此方法应利用模型的批量调用,这些模型公开了批量 API。
- 当您想要
利用批量调用,
需要从模型获得比仅生成的首要值更多的输出,
- 正在构建对底层语言模型不可知的链
类型(例如,纯文本完成模型与聊天模型)。
- 参数
prompts (List[PromptValue]) – PromptValue 列表。PromptValue 是一个可以转换为匹配任何语言模型格式的对象(纯文本生成模型的字符串和聊天模型的消息)。
stop (Optional[List[str]]) – 生成时要使用的停止词。模型输出在首次出现任何这些子字符串时被截断。
callbacks (Union[List[BaseCallbackHandler], BaseCallbackManager, None, List[Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]]]]) – 要传递的回调函数。用于在整个生成过程中执行额外的功能,例如日志记录或流式传输。
**kwargs (Any) – 任意附加关键字参数。这些通常传递给模型提供商 API 调用。
- 返回值
- An LLMResult, which contains a list of candidate Generations for each input
prompt and additional model provider-specific output.
- 返回类型
- async ainvoke(input: Union[PromptValue, str, Sequence[Union[BaseMessage, List[str], Tuple[str, str], str, Dict[str, Any]]]], config: Optional[RunnableConfig] = None, *, stop: Optional[List[str]] = None, **kwargs: Any) str ¶
Default implementation of ainvoke, calls invoke from a thread.
The default implementation allows usage of async code even if the Runnable did not implement a native async version of invoke.
Subclasses should override this method if they can run asynchronously.
- 参数
input (Union[PromptValue, str, Sequence[Union[BaseMessage, List[str], Tuple[str, str], str, Dict[str, Any]]]]) –
config (Optional[RunnableConfig]) –
stop (Optional[List[str]]) –
kwargs (Any) –
- 返回类型
str
- async apredict(text: str, *, stop: Optional[Sequence[str]] = None, **kwargs: Any) str ¶
Deprecated since version langchain-core==0.1.7: Use
ainvoke
instead.- 参数
text (str) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
- 返回类型
str
- async apredict_messages(messages: List[BaseMessage], *, stop: Optional[Sequence[str]] = None, **kwargs: Any) BaseMessage ¶
Deprecated since version langchain-core==0.1.7: Use
ainvoke
instead.- 参数
messages (List[BaseMessage]) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
- 返回类型
- as_tool(args_schema: Optional[Type[BaseModel]] = None, *, name: Optional[str] = None, description: Optional[str] = None, arg_types: Optional[Dict[str, Type]] = None) BaseTool ¶
Beta
This API is in beta and may change in the future.
Create a BaseTool from a Runnable.
as_tool
will instantiate a BaseTool with a name, description, andargs_schema
from a Runnable. Where possible, schemas are inferred fromrunnable.get_input_schema
. Alternatively (e.g., if the Runnable takes a dict as input and the specific dict keys are not typed), the schema can be specified directly withargs_schema
. You can also passarg_types
to just specify the required arguments and their types.- 参数
args_schema (Optional[Type[BaseModel]]) – 工具的模式。默认为 None。
name (Optional[str]) – 工具的名称。默认为 None。
description (Optional[str]) – 工具的描述。默认为 None。
arg_types (Optional[Dict[str, Type]]) – 参数名称到类型的字典。默认为 None。
- 返回值
A BaseTool instance.
- 返回类型
Typed dict input
from typing import List from typing_extensions import TypedDict from langchain_core.runnables import RunnableLambda class Args(TypedDict): a: int b: List[int] def f(x: Args) -> str: return str(x["a"] * max(x["b"])) runnable = RunnableLambda(f) as_tool = runnable.as_tool() as_tool.invoke({"a": 3, "b": [1, 2]})
dict
input, specifying schema viaargs_schema
from typing import Any, Dict, List from langchain_core.pydantic_v1 import BaseModel, Field from langchain_core.runnables import RunnableLambda def f(x: Dict[str, Any]) -> str: return str(x["a"] * max(x["b"])) class FSchema(BaseModel): """Apply a function to an integer and list of integers.""" a: int = Field(..., description="Integer") b: List[int] = Field(..., description="List of ints") runnable = RunnableLambda(f) as_tool = runnable.as_tool(FSchema) as_tool.invoke({"a": 3, "b": [1, 2]})
dict
input, specifying schema viaarg_types
from typing import Any, Dict, List from langchain_core.runnables import RunnableLambda def f(x: Dict[str, Any]) -> str: return str(x["a"] * max(x["b"])) runnable = RunnableLambda(f) as_tool = runnable.as_tool(arg_types={"a": int, "b": List[int]}) as_tool.invoke({"a": 3, "b": [1, 2]})
String input
from langchain_core.runnables import RunnableLambda def f(x: str) -> str: return x + "a" def g(x: str) -> str: return x + "z" runnable = RunnableLambda(f) | g as_tool = runnable.as_tool() as_tool.invoke("b")
New in version 0.2.14.
- async astream(input: Union[PromptValue, str, Sequence[Union[BaseMessage, List[str], Tuple[str, str], str, Dict[str, Any]]]], config: Optional[RunnableConfig] = None, *, stop: Optional[List[str]] = None, **kwargs: Any) AsyncIterator[str] ¶
Default implementation of astream, which calls ainvoke. Subclasses should override this method if they support streaming output.
- 参数
input (Union[PromptValue, str, Sequence[Union[BaseMessage, List[str], Tuple[str, str], str, Dict[str, Any]]]]) – Runnable 的输入。
config (Optional[RunnableConfig]) – 用于 Runnable 的配置。默认为 None。
kwargs (Any) – 要传递给 Runnable 的其他关键字参数。
stop (Optional[List[str]]) –
- Yields
The output of the Runnable.
- 返回类型
AsyncIterator[str]
- astream_events(input: Any, config: Optional[RunnableConfig] = None, *, version: Literal['v1', 'v2'], include_names: Optional[Sequence[str]] = None, include_types: Optional[Sequence[str]] = None, include_tags: Optional[Sequence[str]] = None, exclude_names: Optional[Sequence[str]] = None, exclude_types: Optional[Sequence[str]] = None, exclude_tags: Optional[Sequence[str]] = None, **kwargs: Any) AsyncIterator[Union[StandardStreamEvent, CustomStreamEvent]] ¶
Beta
This API is in beta and may change in the future.
Generate a stream of events.
Use to create an iterator over StreamEvents that provide real-time information about the progress of the Runnable, including StreamEvents from intermediate results.
A StreamEvent is a dictionary with the following schema
event
: str - Event names are of theformat: on_[runnable_type]_(start|stream|end).
name
: str - The name of the Runnable that generated the event.run_id
: str - randomly generated ID associated with the given execution ofthe Runnable that emitted the event. A child Runnable that gets invoked as part of the execution of a parent Runnable is assigned its own unique ID.
parent_ids
: List[str] - The IDs of the parent runnables thatgenerated the event. The root Runnable will have an empty list. The order of the parent IDs is from the root to the immediate parent. Only available for v2 version of the API. The v1 version of the API will return an empty list.
tags
: Optional[List[str]] - The tags of the Runnable that generatedthe event.
metadata
: Optional[Dict[str, Any]] - The metadata of the Runnablethat generated the event.
data
: Dict[str, Any]
Below is a table that illustrates some evens that might be emitted by various chains. Metadata fields have been omitted from the table for brevity. Chain definitions have been included after the table.
ATTENTION This reference table is for the V2 version of the schema.
event
name
chunk
input
output
on_chat_model_start
[model name]
{“messages”: [[SystemMessage, HumanMessage]]}
on_chat_model_stream
[model name]
AIMessageChunk(content=”hello”)
on_chat_model_end
[model name]
{“messages”: [[SystemMessage, HumanMessage]]}
AIMessageChunk(content=”hello world”)
on_llm_start
[model name]
{‘input’: ‘hello’}
on_llm_stream
[model name]
‘Hello’
on_llm_end
[model name]
‘Hello human!’
on_chain_start
format_docs
on_chain_stream
format_docs
“hello world!, goodbye world!”
on_chain_end
format_docs
[Document(…)]
“hello world!, goodbye world!”
on_tool_start
some_tool
{“x”: 1, “y”: “2”}
on_tool_end
some_tool
{“x”: 1, “y”: “2”}
on_retriever_start
[retriever name]
{“query”: “hello”}
on_retriever_end
[retriever name]
{“query”: “hello”}
[Document(…), ..]
on_prompt_start
[template_name]
{“question”: “hello”}
on_prompt_end
[template_name]
{“question”: “hello”}
ChatPromptValue(messages: [SystemMessage, …])
In addition to the standard events, users can also dispatch custom events (see example below).
Custom events will be only be surfaced with in the v2 version of the API!
A custom event has following format
Attribute
Type
Description
name
str
A user defined name for the event.
data
Any
The data associated with the event. This can be anything, though we suggest making it JSON serializable.
Here are declarations associated with the standard events shown above
format_docs:
def format_docs(docs: List[Document]) -> str: '''Format the docs.''' return ", ".join([doc.page_content for doc in docs]) format_docs = RunnableLambda(format_docs)
some_tool:
@tool def some_tool(x: int, y: str) -> dict: '''Some_tool.''' return {"x": x, "y": y}
prompt:
template = ChatPromptTemplate.from_messages( [("system", "You are Cat Agent 007"), ("human", "{question}")] ).with_config({"run_name": "my_template", "tags": ["my_template"]})
示例
from langchain_core.runnables import RunnableLambda async def reverse(s: str) -> str: return s[::-1] chain = RunnableLambda(func=reverse) events = [ event async for event in chain.astream_events("hello", version="v2") ] # will produce the following events (run_id, and parent_ids # has been omitted for brevity): [ { "data": {"input": "hello"}, "event": "on_chain_start", "metadata": {}, "name": "reverse", "tags": [], }, { "data": {"chunk": "olleh"}, "event": "on_chain_stream", "metadata": {}, "name": "reverse", "tags": [], }, { "data": {"output": "olleh"}, "event": "on_chain_end", "metadata": {}, "name": "reverse", "tags": [], }, ]
Example: Dispatch Custom Event
from langchain_core.callbacks.manager import ( adispatch_custom_event, ) from langchain_core.runnables import RunnableLambda, RunnableConfig import asyncio async def slow_thing(some_input: str, config: RunnableConfig) -> str: """Do something that takes a long time.""" await asyncio.sleep(1) # Placeholder for some slow operation await adispatch_custom_event( "progress_event", {"message": "Finished step 1 of 3"}, config=config # Must be included for python < 3.10 ) await asyncio.sleep(1) # Placeholder for some slow operation await adispatch_custom_event( "progress_event", {"message": "Finished step 2 of 3"}, config=config # Must be included for python < 3.10 ) await asyncio.sleep(1) # Placeholder for some slow operation return "Done" slow_thing = RunnableLambda(slow_thing) async for event in slow_thing.astream_events("some_input", version="v2"): print(event)
- 参数
input (Any) – Runnable 的输入。
config (Optional[RunnableConfig]) – 用于 Runnable 的配置。
version (Literal['v1', 'v2']) – 要使用的模式版本,v2 或 v1。用户应使用 v2。v1 用于向后兼容,将在 0.4.0 版本中弃用。在 API 稳定之前,不会分配默认值。自定义事件仅在 v2 中显示。
include_names (Optional[Sequence[str]]) – 仅包含来自具有匹配名称的 runnables 的事件。
include_types (Optional[Sequence[str]]) – 仅包含来自具有匹配类型的 runnables 的事件。
include_tags (Optional[Sequence[str]]) – 仅包含来自具有匹配标签的 runnables 的事件。
exclude_names (Optional[Sequence[str]]) – 排除来自具有匹配名称的 runnables 的事件。
exclude_types (Optional[Sequence[str]]) – 排除来自具有匹配类型的 runnables 的事件。
exclude_tags (Optional[Sequence[str]]) – 排除来自具有匹配标签的 runnables 的事件。
kwargs (Any) – 要传递给 Runnable 的其他关键字参数。这些参数将传递给 astream_log,因为 astream_events 的此实现构建在 astream_log 之上。
- Yields
An async stream of StreamEvents.
- Raises
NotImplementedError – If the version is not v1 or v2.
- 返回类型
AsyncIterator[Union[StandardStreamEvent, CustomStreamEvent]]
- batch(inputs: List[Union[PromptValue, str, Sequence[Union[BaseMessage, List[str], Tuple[str, str], str, Dict[str, Any]]]]], config: Optional[Union[RunnableConfig, List[RunnableConfig]]] = None, *, return_exceptions: bool = False, **kwargs: Any) List[str] ¶
默认实现使用线程池执行器并行运行 invoke。
batch 的默认实现非常适用于 IO 绑定 runnable。
如果子类可以更有效地进行批处理,则应覆盖此方法;例如,如果底层 Runnable 使用支持批处理模式的 API。
- 参数
inputs (List[Union[PromptValue, str, Sequence[Union[BaseMessage, List[str], Tuple[str, str], str, Dict[str, Any]]]]]) –
config (Optional[Union[RunnableConfig, List[RunnableConfig]]]) –
return_exceptions (bool) –
kwargs (Any) –
- 返回类型
List[str]
- batch_as_completed(inputs: Sequence[Input], config: Optional[Union[RunnableConfig, Sequence[RunnableConfig]]] = None, *, return_exceptions: bool = False, **kwargs: Optional[Any]) Iterator[Tuple[int, Union[Output, Exception]]] ¶
并行运行 invoke 处理输入列表,并在结果完成时生成结果。
- 参数
inputs (Sequence[Input]) –
config (Optional[Union[RunnableConfig, Sequence[RunnableConfig]]]) –
return_exceptions (bool) –
kwargs (Optional[Any]) –
- 返回类型
Iterator[Tuple[int, Union[Output, Exception]]]
- configurable_alternatives(which: ConfigurableField, *, default_key: str = 'default', prefix_keys: bool = False, **kwargs: Union[Runnable[Input, Output], Callable[[], Runnable[Input, Output]]]) RunnableSerializable[Input, Output] ¶
配置可在运行时设置的 Runnable 的备选项。
- 参数
which (ConfigurableField) – 将用于选择备选项的 ConfigurableField 实例。
default_key (str) – 如果未选择备选项,则使用的默认键。默认为 “default”。
prefix_keys (bool) – 是否为键添加 ConfigurableField id 前缀。默认为 False。
**kwargs (Union[Runnable[Input, Output], Callable[[], Runnable[Input, Output]]]) – 键到 Runnable 实例或返回 Runnable 实例的可调用对象的字典。
- 返回值
配置了备选项的新 Runnable。
- 返回类型
RunnableSerializable[Input, Output]
from langchain_anthropic import ChatAnthropic from langchain_core.runnables.utils import ConfigurableField from langchain_openai import ChatOpenAI model = ChatAnthropic( model_name="claude-3-sonnet-20240229" ).configurable_alternatives( ConfigurableField(id="llm"), default_key="anthropic", openai=ChatOpenAI() ) # uses the default model ChatAnthropic print(model.invoke("which organization created you?").content) # uses ChatOpenAI print( model.with_config( configurable={"llm": "openai"} ).invoke("which organization created you?").content )
- configurable_fields(**kwargs: Union[ConfigurableField, ConfigurableFieldSingleOption, ConfigurableFieldMultiOption]) RunnableSerializable[Input, Output] ¶
在运行时配置特定的 Runnable 字段。
- 参数
**kwargs (Union[ConfigurableField, ConfigurableFieldSingleOption, ConfigurableFieldMultiOption]) – 要配置的 ConfigurableField 实例的字典。
- 返回值
配置了字段的新 Runnable。
- 返回类型
RunnableSerializable[Input, Output]
from langchain_core.runnables import ConfigurableField from langchain_openai import ChatOpenAI model = ChatOpenAI(max_tokens=20).configurable_fields( max_tokens=ConfigurableField( id="output_token_number", name="Max tokens in the output", description="The maximum number of tokens in the output", ) ) # max_tokens = 20 print( "max_tokens_20: ", model.invoke("tell me something about chess").content ) # max_tokens = 200 print("max_tokens_200: ", model.with_config( configurable={"output_token_number": 200} ).invoke("tell me something about chess").content )
- generate(prompts: List[str], stop: Optional[List[str]] = None, callbacks: Union[List[BaseCallbackHandler], BaseCallbackManager, None, List[Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]]]] = None, *, tags: Optional[Union[List[str], List[List[str]]]] = None, metadata: Optional[Union[Dict[str, Any], List[Dict[str, Any]]]] = None, run_name: Optional[Union[str, List[str]]] = None, run_id: Optional[Union[UUID, List[Optional[UUID]]]] = None, **kwargs: Any) LLMResult ¶
将提示序列传递给模型并返回生成结果。
此方法应利用模型的批量调用,这些模型公开了批量 API。
- 当您想要
利用批量调用,
需要从模型获得比仅生成的首要值更多的输出,
- 正在构建对底层语言模型不可知的链
类型(例如,纯文本完成模型与聊天模型)。
- 参数
prompts (List[str]) – 字符串提示列表。
stop (Optional[List[str]]) – 生成时要使用的停止词。模型输出在首次出现任何这些子字符串时被截断。
callbacks (Union[List[BaseCallbackHandler], BaseCallbackManager, None, List[Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]]]]) – 要传递的回调函数。用于在整个生成过程中执行额外的功能,例如日志记录或流式传输。
tags (Optional[Union[List[str], List[List[str]]]]) – 与每个提示关联的标签列表。如果提供,则列表的长度必须与提示列表的长度匹配。
metadata (Optional[Union[Dict[str, Any], List[Dict[str, Any]]]]) – 与每个提示关联的元数据字典列表。如果提供,则列表的长度必须与提示列表的长度匹配。
run_name (Optional[Union[str, List[str]]]) – 与每个提示关联的运行名称列表。如果提供,则列表的长度必须与提示列表的长度匹配。
run_id (Optional[Union[UUID, List[Optional[UUID]]]]) – 与每个提示关联的运行 ID 列表。如果提供,则列表的长度必须与提示列表的长度匹配。
**kwargs (Any) – 任意附加关键字参数。这些通常传递给模型提供商 API 调用。
- 返回值
- An LLMResult, which contains a list of candidate Generations for each input
prompt and additional model provider-specific output.
- 返回类型
- generate_prompt(prompts: List[PromptValue], stop: Optional[List[str]] = None, callbacks: Union[List[BaseCallbackHandler], BaseCallbackManager, None, List[Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]]]] = None, **kwargs: Any) LLMResult ¶
将提示序列传递给模型并返回模型生成结果。
此方法应利用模型的批量调用,这些模型公开了批量 API。
- 当您想要
利用批量调用,
需要从模型获得比仅生成的首要值更多的输出,
- 正在构建对底层语言模型不可知的链
类型(例如,纯文本完成模型与聊天模型)。
- 参数
prompts (List[PromptValue]) – PromptValue 列表。PromptValue 是一个可以转换为匹配任何语言模型格式的对象(纯文本生成模型的字符串和聊天模型的消息)。
stop (Optional[List[str]]) – 生成时要使用的停止词。模型输出在首次出现任何这些子字符串时被截断。
callbacks (Union[List[BaseCallbackHandler], BaseCallbackManager, None, List[Optional[Union[List[BaseCallbackHandler], BaseCallbackManager]]]]) – 要传递的回调函数。用于在整个生成过程中执行额外的功能,例如日志记录或流式传输。
**kwargs (Any) – 任意附加关键字参数。这些通常传递给模型提供商 API 调用。
- 返回值
- An LLMResult, which contains a list of candidate Generations for each input
prompt and additional model provider-specific output.
- 返回类型
- get_num_tokens(text: str) int ¶
获取文本中存在的 token 数量。
用于检查输入是否适合模型的上下文窗口。
- 参数
text (str) – 要分词的字符串输入。
- 返回值
文本中的整数 token 数量。
- 返回类型
int
- get_num_tokens_from_messages(messages: List[BaseMessage]) int ¶
获取消息中的 token 数量。
用于检查输入是否适合模型的上下文窗口。
- 参数
messages (List[BaseMessage]) – 要分词的消息输入。
- 返回值
消息中 token 数量的总和。
- 返回类型
int
- get_token_ids(text: str) List[int] ¶
返回文本中 token 的有序 ID 列表。
- 参数
text (str) – 要分词的字符串输入。
- 返回值
- 与文本中的 token 相对应的 ID 列表,按它们在文本中出现的顺序排列。
在文本中。
- 返回类型
List[int]
- invoke(input: Union[PromptValue, str, Sequence[Union[BaseMessage, List[str], Tuple[str, str], str, Dict[str, Any]]]], config: Optional[RunnableConfig] = None, *, stop: Optional[List[str]] = None, **kwargs: Any) str ¶
将单个输入转换为输出。覆盖此方法以实现。
- 参数
input (Union[PromptValue, str, Sequence[Union[BaseMessage, List[str], Tuple[str, str], str, Dict[str, Any]]]]) – Runnable 的输入。
config (Optional[RunnableConfig]) – 调用 Runnable 时使用的配置。该配置支持标准键,如用于追踪目的的 ‘tags’、‘metadata’,用于控制并行执行量的 ‘max_concurrency’,以及其他键。请参阅 RunnableConfig 以了解更多详情。
stop (Optional[List[str]]) –
kwargs (Any) –
- 返回值
The output of the Runnable.
- 返回类型
str
- predict(text: str, *, stop: Optional[Sequence[str]] = None, **kwargs: Any) str ¶
Deprecated since version langchain-core==0.1.7: Use
invoke
instead.- 参数
text (str) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
- 返回类型
str
- predict_messages(messages: List[BaseMessage], *, stop: Optional[Sequence[str]] = None, **kwargs: Any) BaseMessage ¶
Deprecated since version langchain-core==0.1.7: Use
invoke
instead.- 参数
messages (List[BaseMessage]) –
stop (Optional[Sequence[str]]) –
kwargs (Any) –
- 返回类型
- save(file_path: Union[Path, str]) None ¶
保存 LLM。
- 参数
file_path (Union[Path, str]) – 保存 LLM 的文件路径。
- Raises
ValueError – 如果文件路径不是字符串或 Path 对象。
- 返回类型
None
Example: .. code-block:: python
llm.save(file_path=”path/llm.yaml”)
- stream(input: Union[PromptValue, str, Sequence[Union[BaseMessage, List[str], Tuple[str, str], str, Dict[str, Any]]]], config: Optional[RunnableConfig] = None, *, stop: Optional[List[str]] = None, **kwargs: Any) Iterator[str] ¶
流式传输的默认实现,它调用 invoke。如果子类支持流式输出,则应重写此方法。
- 参数
input (Union[PromptValue, str, Sequence[Union[BaseMessage, List[str], Tuple[str, str], str, Dict[str, Any]]]]) – Runnable 的输入。
config (Optional[RunnableConfig]) – 用于 Runnable 的配置。默认为 None。
kwargs (Any) – 要传递给 Runnable 的其他关键字参数。
stop (Optional[List[str]]) –
- Yields
The output of the Runnable.
- 返回类型
Iterator[str]
- to_json() Union[SerializedConstructor, SerializedNotImplemented] ¶
将 Runnable 序列化为 JSON。
- 返回值
Runnable 的 JSON 可序列化表示。
- 返回类型
- with_structured_output(schema: Union[Dict, Type[BaseModel]], **kwargs: Any) Runnable[Union[PromptValue, str, Sequence[Union[BaseMessage, List[str], Tuple[str, str], str, Dict[str, Any]]]], Union[Dict, BaseModel]] ¶
在此类上未实现。
- 参数
schema (Union[Dict, Type[BaseModel]]) –
kwargs (Any) –
- 返回类型
Runnable[Union[PromptValue, str, Sequence[Union[BaseMessage, List[str], Tuple[str, str], str, Dict[str, Any]]]], Union[Dict, BaseModel]]