跳到内容

直接调用工具#

如果 LLM 具有工具调用 API,这与 structured_predict 完全相同。但是,如果 LLM 支持,您可以选择允许多个工具调用。这样做的效果是从同一个输入中提取多个对象,如下例所示

如果您的目标是从单个 LLM 调用中提取多个 Pydantic 对象,这就是实现方法。

from llama_index.core.program.function_program import get_function_tool

tool = get_function_tool(Invoice)

resp = llm.chat_with_tools(
    [tool],
    # chat_history=chat_history,  # can optionally pass in chat history instead of user_msg
    user_msg="Extract an invoice from the following text: " + text,
    # tool_choice="Invoice",  # can optionally force the tool call
)

tool_calls = llm.get_tool_calls_from_response(
    resp, error_on_no_tool_calls=False
)

outputs = []
for tool_call in tool_calls:
    if tool_call.tool_name == "Invoice":
        outputs.append(Invoice(**tool_call.tool_kwargs))

# use your outputs
print(outputs[0])

直接提示#

from llama_index.core.program.function_program import get_function_tool

tool = get_function_tool(LineItem)

resp = llm.chat_with_tools(
    [tool],
    user_msg="Extract line items from the following text: " + text,
    allow_parallel_tool_calls=True,
)

tool_calls = llm.get_tool_calls_from_response(
    resp, error_on_no_tool_calls=False
)

outputs = []
for tool_call in tool_calls:
    if tool_call.tool_name == "LineItem":
        outputs.append(LineItem(**tool_call.tool_kwargs))

# use your outputs
print(outputs)

如果出于某种原因,LlamaIndex 使提取更简便的尝试对您无效,您可以放弃它们,直接向 LLM 提示并自行解析输出,如下所示

恭喜!您已经了解了 LlamaIndex 中结构化数据提取的所有知识。

其他指南#

schema = Invoice.model_json_schema()
prompt = "Here is a JSON schema for an invoice: " + json.dumps(
    schema, indent=2
)
prompt += (
    """
  Extract an invoice from the following text.
  Format your output as a JSON object according to the schema above.
  Do not include any other text than the JSON object.
  Omit any markdown formatting. Do not include any preamble or explanation.
"""
    + text
)

response = llm.complete(prompt)

print(response)

invoice = Invoice.model_validate_json(response.text)

pprint(invoice)

要更深入地了解 LlamaIndex 的结构化数据提取,请查看以下指南

返回顶部