索引
LLM #
基础:BaseLLM
LLM 类是用于与语言模型交互的主要类。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
system_prompt
|
str | None
|
用于 LLM 调用的系统提示词。 |
无
|
messages_to_prompt
|
MessagesToPromptType | None
|
将消息列表转换为 LLM 提示词的函数。 |
无
|
completion_to_prompt
|
CompletionToPromptType | None
|
将补全转换为 LLM 提示词的函数。 |
无
|
output_parser
|
BaseOutputParser | None
|
用于以编程方式解析、验证和纠正错误的输出解析器。 |
无
|
pydantic_program_mode
|
PydanticProgramMode
|
|
<PydanticProgramMode.DEFAULT: 'default'>
|
query_wrapper_prompt
|
BasePromptTemplate | None
|
用于 LLM 调用的查询包装器提示词。 |
无
|
属性
名称 | 类型 | 描述 |
---|---|---|
system_prompt |
Optional[str]
|
用于 LLM 调用的系统提示词。 |
messages_to_prompt |
可调用对象
|
将消息列表转换为 LLM 提示词的函数。 |
completion_to_prompt |
可调用对象
|
将补全转换为 LLM 提示词的函数。 |
output_parser |
Optional[BaseOutputParser]
|
用于以编程方式解析、验证和纠正错误的输出解析器。 |
pydantic_program_mode |
PydanticProgramMode
|
用于结构化预测的 Pydantic 程序模式。 |
metadata 抽象方法
属性
#
metadata: LLMMetadata
LLM 元数据。
返回
名称 | 类型 | 描述 |
---|---|---|
LLMMetadata |
LLMMetadata
|
包含关于 LLM 的各种信息的 LLM 元数据。 |
as_query_component #
as_query_component(partial: Optional[Dict[str, Any]] = None, **kwargs: Any) -> QueryComponent
获取查询组件。
convert_chat_messages #
convert_chat_messages(messages: Sequence[ChatMessage]) -> List[Any]
将聊天消息转换为 LLM 特定的消息格式。
chat 抽象方法
#
chat(messages: Sequence[ChatMessage], **kwargs: Any) -> ChatResponse
LLM 的聊天端点。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
messages
|
Sequence[ChatMessage]
|
聊天消息序列。 |
必需 |
kwargs
|
Any
|
传递给 LLM 的额外关键字参数。 |
{}
|
返回
名称 | 类型 | 描述 |
---|---|---|
ChatResponse |
ChatResponse
|
LLM 的聊天响应。 |
示例
from llama_index.core.llms import ChatMessage
response = llm.chat([ChatMessage(role="user", content="Hello")])
print(response.content)
complete 抽象方法
#
complete(prompt: str, formatted: bool = False, **kwargs: Any) -> CompletionResponse
LLM 的补全端点。
如果 LLM 是聊天模型,提示词将被转换为单个 user
消息。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
提示词
|
str
|
发送给 LLM 的提示词。 |
必需 |
已格式化
|
bool
|
提示词是否已为 LLM 格式化,默认为 False。 |
False
|
kwargs
|
Any
|
传递给 LLM 的额外关键字参数。 |
{}
|
返回
名称 | 类型 | 描述 |
---|---|---|
CompletionResponse |
CompletionResponse
|
LLM 的补全响应。 |
示例
response = llm.complete("your prompt")
print(response.text)
stream_chat 抽象方法
#
stream_chat(messages: Sequence[ChatMessage], **kwargs: Any) -> ChatResponseGen
LLM 的流式聊天端点。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
messages
|
Sequence[ChatMessage]
|
聊天消息序列。 |
必需 |
kwargs
|
Any
|
传递给 LLM 的额外关键字参数。 |
{}
|
生成
名称 | 类型 | 描述 |
---|---|---|
ChatResponse |
ChatResponseGen
|
ChatResponse 对象的生成器,每个对象包含响应的新标记。 |
示例
from llama_index.core.llms import ChatMessage
gen = llm.stream_chat([ChatMessage(role="user", content="Hello")])
for response in gen:
print(response.delta, end="", flush=True)
stream_complete 抽象方法
#
stream_complete(prompt: str, formatted: bool = False, **kwargs: Any) -> CompletionResponseGen
LLM 的流式补全端点。
如果 LLM 是聊天模型,提示词将被转换为单个 user
消息。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
提示词
|
str
|
发送给 LLM 的提示词。 |
必需 |
已格式化
|
bool
|
提示词是否已为 LLM 格式化,默认为 False。 |
False
|
kwargs
|
Any
|
传递给 LLM 的额外关键字参数。 |
{}
|
生成
名称 | 类型 | 描述 |
---|---|---|
CompletionResponse |
CompletionResponseGen
|
CompletionResponse 对象的生成器,每个对象包含响应的新标记。 |
示例
gen = llm.stream_complete("your prompt")
for response in gen:
print(response.text, end="", flush=True)
achat 抽象方法
异步
#
achat(messages: Sequence[ChatMessage], **kwargs: Any) -> ChatResponse
LLM 的异步聊天端点。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
messages
|
Sequence[ChatMessage]
|
聊天消息序列。 |
必需 |
kwargs
|
Any
|
传递给 LLM 的额外关键字参数。 |
{}
|
返回
名称 | 类型 | 描述 |
---|---|---|
ChatResponse |
ChatResponse
|
LLM 的聊天响应。 |
示例
from llama_index.core.llms import ChatMessage
response = await llm.achat([ChatMessage(role="user", content="Hello")])
print(response.content)
acomplete 抽象方法
异步
#
acomplete(prompt: str, formatted: bool = False, **kwargs: Any) -> CompletionResponse
LLM 的异步补全端点。
如果 LLM 是聊天模型,提示词将被转换为单个 user
消息。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
提示词
|
str
|
发送给 LLM 的提示词。 |
必需 |
已格式化
|
bool
|
提示词是否已为 LLM 格式化,默认为 False。 |
False
|
kwargs
|
Any
|
传递给 LLM 的额外关键字参数。 |
{}
|
返回
名称 | 类型 | 描述 |
---|---|---|
CompletionResponse |
CompletionResponse
|
LLM 的补全响应。 |
示例
response = await llm.acomplete("your prompt")
print(response.text)
astream_chat 抽象方法
异步
#
astream_chat(messages: Sequence[ChatMessage], **kwargs: Any) -> ChatResponseAsyncGen
LLM 的异步流式聊天端点。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
messages
|
Sequence[ChatMessage]
|
聊天消息序列。 |
必需 |
kwargs
|
Any
|
传递给 LLM 的额外关键字参数。 |
{}
|
生成
名称 | 类型 | 描述 |
---|---|---|
ChatResponse |
ChatResponseAsyncGen
|
ChatResponse 对象的异步生成器,每个对象包含响应的新标记。 |
示例
from llama_index.core.llms import ChatMessage
gen = await llm.astream_chat([ChatMessage(role="user", content="Hello")])
async for response in gen:
print(response.delta, end="", flush=True)
astream_complete 抽象方法
异步
#
astream_complete(prompt: str, formatted: bool = False, **kwargs: Any) -> CompletionResponseAsyncGen
LLM 的异步流式补全端点。
如果 LLM 是聊天模型,提示词将被转换为单个 user
消息。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
提示词
|
str
|
发送给 LLM 的提示词。 |
必需 |
已格式化
|
bool
|
提示词是否已为 LLM 格式化,默认为 False。 |
False
|
kwargs
|
Any
|
传递给 LLM 的额外关键字参数。 |
{}
|
生成
名称 | 类型 | 描述 |
---|---|---|
CompletionResponse |
CompletionResponseAsyncGen
|
CompletionResponse 对象的异步生成器,每个对象包含响应的新标记。 |
示例
gen = await llm.astream_complete("your prompt")
async for response in gen:
print(response.text, end="", flush=True)
structured_predict #
structured_predict(output_cls: Type[Model], prompt: PromptTemplate, llm_kwargs: Optional[Dict[str, Any]] = None, **prompt_args: Any) -> Model
结构化预测。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
输出类
|
BaseModel
|
用于结构化预测的输出类。 |
必需 |
提示词
|
PromptTemplate
|
用于结构化预测的提示词模板。 |
必需 |
llm_kwargs
|
Optional[Dict[str, Any]]
|
传递给程序调用的 LLM 的参数。 |
无
|
prompt_args
|
Any
|
用于格式化提示词的额外参数。 |
{}
|
返回
名称 | 类型 | 描述 |
---|---|---|
BaseModel |
模型
|
结构化预测输出。 |
示例
from pydantic import BaseModel
class Test(BaseModel):
\"\"\"My test class.\"\"\"
name: str
from llama_index.core.prompts import PromptTemplate
prompt = PromptTemplate("Please predict a Test with a random name related to {topic}.")
output = llm.structured_predict(Test, prompt, topic="cats")
print(output.name)
astructured_predict 异步
#
astructured_predict(output_cls: Type[Model], prompt: PromptTemplate, llm_kwargs: Optional[Dict[str, Any]] = None, **prompt_args: Any) -> Model
异步结构化预测。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
输出类
|
BaseModel
|
用于结构化预测的输出类。 |
必需 |
提示词
|
PromptTemplate
|
用于结构化预测的提示词模板。 |
必需 |
llm_kwargs
|
Optional[Dict[str, Any]]
|
传递给程序调用的 LLM 的参数。 |
无
|
prompt_args
|
Any
|
用于格式化提示词的额外参数。 |
{}
|
返回
名称 | 类型 | 描述 |
---|---|---|
BaseModel |
模型
|
结构化预测输出。 |
示例
from pydantic import BaseModel
class Test(BaseModel):
\"\"\"My test class.\"\"\"
name: str
from llama_index.core.prompts import PromptTemplate
prompt = PromptTemplate("Please predict a Test with a random name related to {topic}.")
output = await llm.astructured_predict(Test, prompt, topic="cats")
print(output.name)
stream_structured_predict #
stream_structured_predict(output_cls: Type[Model], prompt: PromptTemplate, llm_kwargs: Optional[Dict[str, Any]] = None, **prompt_args: Any) -> Generator[Union[Model, FlexibleModel], None, None]
流式结构化预测。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
输出类
|
BaseModel
|
用于结构化预测的输出类。 |
必需 |
提示词
|
PromptTemplate
|
用于结构化预测的提示词模板。 |
必需 |
llm_kwargs
|
Optional[Dict[str, Any]]
|
传递给程序调用的 LLM 的参数。 |
无
|
prompt_args
|
Any
|
用于格式化提示词的额外参数。 |
{}
|
返回
名称 | 类型 | 描述 |
---|---|---|
生成器 |
无
|
返回模型或模型列表的部分副本的生成器。 |
示例
from pydantic import BaseModel
class Test(BaseModel):
\"\"\"My test class.\"\"\"
name: str
from llama_index.core.prompts import PromptTemplate
prompt = PromptTemplate("Please predict a Test with a random name related to {topic}.")
stream_output = llm.stream_structured_predict(Test, prompt, topic="cats")
for partial_output in stream_output:
# stream partial outputs until completion
print(partial_output.name)
astream_structured_predict 异步
#
astream_structured_predict(output_cls: Type[Model], prompt: PromptTemplate, llm_kwargs: Optional[Dict[str, Any]] = None, **prompt_args: Any) -> AsyncGenerator[Union[Model, FlexibleModel], None]
异步流式结构化预测。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
输出类
|
BaseModel
|
用于结构化预测的输出类。 |
必需 |
提示词
|
PromptTemplate
|
用于结构化预测的提示词模板。 |
必需 |
llm_kwargs
|
Optional[Dict[str, Any]]
|
传递给程序调用的 LLM 的参数。 |
无
|
prompt_args
|
Any
|
用于格式化提示词的额外参数。 |
{}
|
返回
名称 | 类型 | 描述 |
---|---|---|
生成器 |
AsyncGenerator[Union[Model, FlexibleModel], None]
|
返回模型或模型列表的部分副本的生成器。 |
示例
from pydantic import BaseModel
class Test(BaseModel):
\"\"\"My test class.\"\"\"
name: str
from llama_index.core.prompts import PromptTemplate
prompt = PromptTemplate("Please predict a Test with a random name related to {topic}.")
stream_output = await llm.astream_structured_predict(Test, prompt, topic="cats")
async for partial_output in stream_output:
# stream partial outputs until completion
print(partial_output.name)
predict #
predict(prompt: BasePromptTemplate, **prompt_args: Any) -> str
对给定的提示词进行预测。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
提示词
|
BasePromptTemplate
|
用于预测的提示词。 |
必需 |
prompt_args
|
Any
|
用于格式化提示词的额外参数。 |
{}
|
返回
名称 | 类型 | 描述 |
---|---|---|
str |
str
|
预测输出。 |
示例
from llama_index.core.prompts import PromptTemplate
prompt = PromptTemplate("Please write a random name related to {topic}.")
output = llm.predict(prompt, topic="cats")
print(output)
stream #
stream(prompt: BasePromptTemplate, **prompt_args: Any) -> TokenGen
对给定的提示词进行流式预测。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
提示词
|
BasePromptTemplate
|
用于预测的提示词。 |
必需 |
prompt_args
|
Any
|
用于格式化提示词的额外参数。 |
{}
|
生成
名称 | 类型 | 描述 |
---|---|---|
str |
TokenGen
|
每个流式标记。 |
示例
from llama_index.core.prompts import PromptTemplate
prompt = PromptTemplate("Please write a random name related to {topic}.")
gen = llm.stream_predict(prompt, topic="cats")
for token in gen:
print(token, end="", flush=True)
apredict 异步
#
apredict(prompt: BasePromptTemplate, **prompt_args: Any) -> str
对给定的提示词进行异步预测。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
提示词
|
BasePromptTemplate
|
用于预测的提示词。 |
必需 |
prompt_args
|
Any
|
用于格式化提示词的额外参数。 |
{}
|
返回
名称 | 类型 | 描述 |
---|---|---|
str |
str
|
预测输出。 |
示例
from llama_index.core.prompts import PromptTemplate
prompt = PromptTemplate("Please write a random name related to {topic}.")
output = await llm.apredict(prompt, topic="cats")
print(output)
astream 异步
#
astream(prompt: BasePromptTemplate, **prompt_args: Any) -> TokenAsyncGen
对给定的提示词进行异步流式预测。
prompt (BasePromptTemplate):用于预测的提示词。prompt_args (Any):用于格式化提示词的额外参数。
生成
名称 | 类型 | 描述 |
---|---|---|
str |
TokenAsyncGen
|
一个生成标记字符串的异步生成器。 |
示例
from llama_index.core.prompts import PromptTemplate
prompt = PromptTemplate("Please write a random name related to {topic}.")
gen = await llm.astream_predict(prompt, topic="cats")
async for token in gen:
print(token, end="", flush=True)
predict_and_call #
predict_and_call(tools: List[BaseTool], user_msg: Optional[Union[str, ChatMessage]] = None, chat_history: Optional[List[ChatMessage]] = None, verbose: bool = False, **kwargs: Any) -> AgentChatResponse
预测并调用工具。
默认情况下使用 ReAct 代理进行工具调用(通过文本提示),但函数调用 LLM 会以不同的方式实现此功能。
apredict_and_call 异步
#
apredict_and_call(tools: List[BaseTool], user_msg: Optional[Union[str, ChatMessage]] = None, chat_history: Optional[List[ChatMessage]] = None, verbose: bool = False, **kwargs: Any) -> AgentChatResponse
预测并调用工具。
as_structured_llm #
as_structured_llm(output_cls: Type[BaseModel], **kwargs: Any) -> StructuredLLM
返回给定对象的结构化 LLM。