LlamaIndex - 学习LLM

LlamaIndex - 学习LLM

原文链接: https://developers.llamaindex.ai/python/framework/understanding/using_llms/

安装依赖

# 首先,先安装相关的库
# !pip install llama-index-llms-dashscope 
!pip install llama-index-llms-openai-like dotenv
    Requirement already satisfied: llama-index-llms-openai-like in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (0.8.0)
    Requirement already satisfied: dotenv in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (0.9.9)
    Requirement already satisfied: llama-index-core<0.15,>=0.14.3 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from llama-index-llms-openai-like) (0.14.24)
    Requirement already satisfied: llama-index-llms-openai<0.8,>=0.7.0 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from llama-index-llms-openai-like) (0.7.10)
    Requirement already satisfied: aiohttp<4,>=3.8.6 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (3.14.3)
    Requirement already satisfied: aiosqlite in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (0.22.1)
    Requirement already satisfied: banks<3,>=2.3.0 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (2.5.1)
    Requirement already satisfied: dataclasses-json in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (0.6.7)
    Requirement already satisfied: deprecated>=1.2.9.3 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (1.3.1)
    Requirement already satisfied: dirtyjson<2,>=1.0.8 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (1.0.8)
    Requirement already satisfied: filetype<2,>=1.2.0 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (1.2.0)
    Requirement already satisfied: fsspec>=2023.5.0 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (2026.7.0)
    Requirement already satisfied: httpx in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (0.28.1)
    Requirement already satisfied: llama-index-workflows<3,>=2.14.0 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (2.24.0)
    Requirement already satisfied: nest-asyncio<2,>=1.5.8 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (1.6.0)
    Requirement already satisfied: networkx>=3.0 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (3.6.1)
    Requirement already satisfied: nltk>=3.9.3 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (3.10.3)
    Requirement already satisfied: numpy in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (2.5.3)
    Requirement already satisfied: pillow>=9.0.0 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (12.3.0)
    Requirement already satisfied: platformdirs in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (4.11.8)
    Requirement already satisfied: pydantic>=2.8.0 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (2.13.5)
    Requirement already satisfied: pyyaml>=6.0.1 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (6.0.3)
    Requirement already satisfied: requests>=2.31.0 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (2.34.2)
    Requirement already satisfied: setuptools>=80.9.0 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (84.0.0)
    Requirement already satisfied: sqlalchemy>=1.4.49 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from sqlalchemy[asyncio]>=1.4.49->llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (2.0.54)
    Requirement already satisfied: tenacity!=8.4.0,<10.0.0,>=8.2.0 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (9.1.4)
    Requirement already satisfied: tiktoken>=0.7.0 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (0.14.0)
    Requirement already satisfied: tinytag>=2.2.0 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (2.3.2)
    Requirement already satisfied: tqdm<5,>=4.66.1 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (4.70.1)
    Requirement already satisfied: typing-extensions>=4.5.0 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (4.16.0)
    Requirement already satisfied: typing-inspect>=0.8.0 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (0.9.0)
    Requirement already satisfied: wrapt in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (2.4.1)
    Requirement already satisfied: aiohappyeyeballs>=2.5.0 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from aiohttp<4,>=3.8.6->llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (2.7.1)
    Requirement already satisfied: aiosignal>=1.4.0 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from aiohttp<4,>=3.8.6->llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (1.4.0)
    Requirement already satisfied: attrs>=17.3.0 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from aiohttp<4,>=3.8.6->llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (26.1.0)
    Requirement already satisfied: frozenlist>=1.1.1 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from aiohttp<4,>=3.8.6->llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (1.8.0)
    Requirement already satisfied: multidict<7.0,>=4.5 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from aiohttp<4,>=3.8.6->llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (6.8.0)
    Requirement already satisfied: propcache>=0.2.0 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from aiohttp<4,>=3.8.6->llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (0.5.4)
    Requirement already satisfied: yarl<2.0,>=1.17.0 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from aiohttp<4,>=3.8.6->llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (1.25.1)
    Requirement already satisfied: griffe in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from banks<3,>=2.3.0->llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (2.3.0)
    Requirement already satisfied: jinja2 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from banks<3,>=2.3.0->llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (3.1.6)
    Requirement already satisfied: openai<3,>=1.108.1 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from llama-index-llms-openai<0.8,>=0.7.0->llama-index-llms-openai-like) (2.54.0)
    Requirement already satisfied: llama-index-instrumentation>=0.4.3 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from llama-index-workflows<3,>=2.14.0->llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (0.6.0)
    Requirement already satisfied: anyio<5,>=3.5.0 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from openai<3,>=1.108.1->llama-index-llms-openai<0.8,>=0.7.0->llama-index-llms-openai-like) (4.15.1)
    Requirement already satisfied: distro<2,>=1.7.0 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from openai<3,>=1.108.1->llama-index-llms-openai<0.8,>=0.7.0->llama-index-llms-openai-like) (1.9.0)
    Requirement already satisfied: jiter<1,>=0.10.0 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from openai<3,>=1.108.1->llama-index-llms-openai<0.8,>=0.7.0->llama-index-llms-openai-like) (0.16.0)
    Requirement already satisfied: sniffio in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from openai<3,>=1.108.1->llama-index-llms-openai<0.8,>=0.7.0->llama-index-llms-openai-like) (1.3.1)
    Requirement already satisfied: idna>=2.8 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from anyio<5,>=3.5.0->openai<3,>=1.108.1->llama-index-llms-openai<0.8,>=0.7.0->llama-index-llms-openai-like) (3.19)
    Requirement already satisfied: certifi in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from httpx->llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (2026.7.22)
    Requirement already satisfied: httpcore==1.* in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from httpx->llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (1.0.9)
    Requirement already satisfied: h11>=0.16 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from httpcore==1.*->httpx->llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (0.16.0)
    Requirement already satisfied: annotated-types>=0.6.0 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from pydantic>=2.8.0->llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (0.8.0)
    Requirement already satisfied: pydantic-core==2.46.5 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from pydantic>=2.8.0->llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (2.46.5)
    Requirement already satisfied: typing-inspection>=0.4.2 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from pydantic>=2.8.0->llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (0.4.4)
    Requirement already satisfied: colorama in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from tqdm<5,>=4.66.1->llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (0.4.6)
    Requirement already satisfied: python-dotenv in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from dotenv) (1.2.3)
    Requirement already satisfied: defusedxml in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from nltk>=3.9.3->llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (0.7.1)
    Requirement already satisfied: click in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from nltk>=3.9.3->llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (8.5.0)
    Requirement already satisfied: joblib in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from nltk>=3.9.3->llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (1.6.0)
    Requirement already satisfied: regex>=2021.8.3 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from nltk>=3.9.3->llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (2026.9.10)
    Requirement already satisfied: charset_normalizer<4,>=2 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from requests>=2.31.0->llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (3.5.1)
    Requirement already satisfied: urllib3<3,>=1.26 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from requests>=2.31.0->llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (2.7.0)
    Requirement already satisfied: greenlet>=1 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from sqlalchemy>=1.4.49->sqlalchemy[asyncio]>=1.4.49->llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (3.5.6)
    Requirement already satisfied: mypy-extensions>=0.3.0 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from typing-inspect>=0.8.0->llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (1.1.0)
    Requirement already satisfied: marshmallow<4.0.0,>=3.18.0 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from dataclasses-json->llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (3.26.2)
    Requirement already satisfied: packaging>=17.0 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from marshmallow<4.0.0,>=3.18.0->dataclasses-json->llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (26.3)
    Requirement already satisfied: griffecli==2.3.0 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from griffe->banks<3,>=2.3.0->llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (2.3.0)
    Requirement already satisfied: griffelib==2.3.0 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from griffe->banks<3,>=2.3.0->llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (2.3.0)
    Requirement already satisfied: MarkupSafe>=2.0 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from jinja2->banks<3,>=2.3.0->llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (3.0.3)
    Requirement already satisfied: cloudpickle>=3.0 in E:\XiaoHuiChaoProject\2026\learn-python-ai\.venv\Lib\site-packages (from joblib->nltk>=3.9.3->llama-index-core<0.15,>=0.14.3->llama-index-llms-openai-like) (3.1.2)


​    
    [notice] A new release of pip is available: 26.0.1 -> 26.2.1
    [notice] To update, run: python.exe -m pip install --upgrade pip

加载环境变量

import os
from dotenv import load_dotenv, find_dotenv

# 加载环境变量的值
_ = load_dotenv(find_dotenv())

实例化LLM

如果像查看OpenAILike的API文档,请点击: https://developers.llamaindex.ai/python/framework-api-reference/llms/openai_like/链接。
如果你想看LlamaIndex如何切换为其他的LLM模型,点击https://developers.llamaindex.ai/python/framework/integrations/llm/dashscope/链接,这里是千问的支持的DashScope文档地址。

class OpenAI和class OpenAILike的区别的就是OpenAI是与OpenAI官方深度绑定的,所以提供的OpenAILike定义是兼容OpenAI的方法支持,与OpenAI官方解耦的一个API。

这是LlamaIndex对OpenAILike的一个解释:

OpenAILike LLM。 OpenAILike 是 OpenAI 模型的一个轻量级封装,使其能够与提供 OpenAI 兼容 API 的第三方工具兼容。
from llama_index.llms.openai_like import OpenAILike
# from llama_index.llms.openai import OpenAI
# 使用限时免费的qwen3.8-max模型
llm = OpenAILike(
    # 这里还是使用字符串指定保险,如果使用DashScopeGenerationModels枚举,则模型不全还老旧,慎用。
    model="qwen3.8-max",
    api_base=os.getenv("DASHSCOPE_HTTP_BASE_URL"),
    api_key=os.getenv('DASHSCOPE_API_KEY'),
    # 模型是否使用chat或completion端点。默认为False。
    is_chat_model=True,
    # 开启FC
    is_function_calling_model=True,
)

特别说明:

OpenAILike文档直达链接
is_function_calling_model默认值为False,如果不设置为True的话,在之后的predict_and_call()方法中使用Tools工具就会出现以下错误:

设置的值取决于所使用的模型是否支持FC(Function Calling),如果支持,请设置为True,业界标准用法,效果还稳定。

ValidationError: 1 validation error for ReActAgent
formatter
  Input should be a valid dictionary or instance of ReActChatFormatter [type=model_type, input_value=None, input_type=NoneType]

运行

# 同步输出
response = llm.complete("William Shakespeare is")
print(response)

William Shakespeare is widely regarded as one of the greatest writers in the English language, known for his plays, sonnets, and poems, including works such as Hamlet, Macbeth, Romeo and Juliet, and A Midsummer Night’s Dream.

# 当前你也可以流式输出
handle = llm.stream_complete("William Shakespeare is")
for token in handle:
    print(token.delta, end="", flush=True)

William Shakespeare is widely regarded as one of the greatest writers in the English language, known for his plays, sonnets, and poems.

当前complete和stream_complete方法都支持异步方法,分别是acomplete和astream_complete。

LLM类还支持chat方法,chat就是一个聊天集合,如下:

from llama_index.core.llms import ChatMessage

messages = [
    ChatMessage(role="system", content="Your are a helpful assistant."),
    ChatMessage(role="user", content="Tell me a joke."),
]
chat_response = llm.chat(messages)
print(chat_response)
    assistant: Why don’t scientists trust atoms?
    
    Because they make up everything!

# 同步流式输出
stream_response = llm.stream_chat(messages)

for token in stream_response:
    print(token.delta, end="", flush=True)
    Why don’t scientists trust atoms?
    
    Because they make up everything.
# 异步流式输出, 需要额外注意这里使用了python await关键字。
# 这里async返回了Coroutine,但是被await解包了,所以astream_response接收的是一个迭代器,
# 由下面的async for使用。
astream_response = await llm.astream_chat(messages)

async for token in astream_response:
    print(token.delta, end="", flush=True)
    Why don’t scientists trust atoms?
    
    Because they make up everything!
# 多模态LLM,却决于你所使用的模型是否支持。
# 当前LlamaIndex支持在聊天消息中使用内容快来存储文本、图片、音频。

from llama_index.core.llms import ImageBlock, TextBlock

messages = [
    ChatMessage(role="user", blocks=[
        ImageBlock(path="image.jpg"),
        # 请用几句话描述这张图片。
        TextBlock(text="Describe the image in a few sentences.")
    ])
]

resp = llm.chat(messages)
print(resp.message.content)
    This image is a graphic design of a Chinese novice-driver sticker (the "shíxí" or probationary driver mark). It features a yellow teardrop/oval shape outlined with a red ring; across the top, a red arched band carries yellow Chinese text reading "注意刹车 保持车距" ("mind your braking, keep your distance"), while the center displays the large red characters "实习" ("intern/practitioner"). Along the bottom curve, black characters read "请多关照" ("please be considerate / thank you for your patience"). Such signs are commonly affixed to vehicles driven by newly licensed drivers in China, and a stock-image watermark runs vertically along the right edge.

工具调用

现在的LLM模型基本都支持通过API调用来使用提供的工具,在一些如LlamaIndex的框架中意味着你无需特定提示和定义schema结构就可以直接调用兼顾和函数。
调用工具和函数取决于模型根据问题来决定是否调用。
工具调用还有另一个名称,就是Funciton Calling。

from llama_index.core.tools import FunctionTool
import json


def generate_sone(name: str, artist: str) -> dict[str, str]:
    """Generates a song with provided name and artist."""
    return {"name": name, "artist": artist}

tool = FunctionTool.from_defaults(fn=generate_sone)

response = llm.predict_and_call(
    [tool],
    "Pick a random song for me",
)
print(str(response))
print("=======工具定义结构")
openai_tool_schema = tool.metadata.to_openai_tool()
print(json.dumps(openai_tool_schema, indent=2, ensure_ascii=False))
    {'name': 'Midnight City', 'artist': 'M83'}
    =======工具定义结构
    {
      "type": "function",
      "function": {
        "name": "generate_sone",
        "description": "generate_sone(name: str, artist: str) -> dict[str, str]\nGenerates a song with provided name and artist.",
        "parameters": {
          "properties": {
            "name": {
              "title": "Name",
              "type": "string"
            },
            "artist": {
              "title": "Artist",
              "type": "string"
            }
          },
          "required": [
            "name",
            "artist"
          ],
          "type": "object"
        }
      }
    }

个人理解

  1. predict_and_call和complete和run的区别
    predict_and_call和complete都是LLM的方法,并且都不支持SystemPrompt系统提示词,
    而run方法是Agent的(如FunctionAgent),支持定义系统提示词(SystemPrompt)。
    complete不支持FC的调用,仅是文本的对话。
    predict_and_call支持FC的单轮调用,内部会根据LLM所推理的需求,会在内部选择是否调用工具来处理。
    run方法支持系统提示词、多轮对话、方法属于Agent系列的方法,所以基本啥都支持吧,毕竟是智能体。

官方本身也没有对其的区别进行说明,所以以上只是个人的理解。


posted @ 2026-10-08 20:08  星小梦  阅读(3)  评论(0)    收藏  举报