Fireworks 函数调用使用指南¶
Fireworks.ai 支持对其 LLM 进行函数调用,类似于 OpenAI。这使得用户可以直接描述可用的工具/函数集,并让模型动态选择正确的函数调用来执行,而无需用户进行复杂的提示词工程。
由于我们的 Fireworks LLM 直接继承自 OpenAI,因此我们可以将现有的抽象应用于 Fireworks。
我们将从三个层面展示这一点:直接在模型 API 上、作为 Pydantic 程序(结构化输出提取)的一部分以及作为代理的一部分。
输入 [ ]
已复制!
%pip install llama-index-llms-fireworks
%pip install llama-index-llms-fireworks
输入 [ ]
已复制!
%pip install llama-index
%pip install llama-index
输入 [ ]
已复制!
import os
os.environ["FIREWORKS_API_KEY"] = ""
import os os.environ["FIREWORKS_API_KEY"] = ""
输入 [ ]
已复制!
from llama_index.llms.fireworks import Fireworks
## define fireworks model
llm = Fireworks(
model="accounts/fireworks/models/firefunction-v1", temperature=0
)
from llama_index.llms.fireworks import Fireworks ## define fireworks model llm = Fireworks( model="accounts/fireworks/models/firefunction-v1", temperature=0 )
/Users/jerryliu/Programming/gpt_index/.venv/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html from .autonotebook import tqdm as notebook_tqdm
在 LLM 模块上进行函数调用¶
您可以在 LLM 模块上直接输入函数调用。
输入 [ ]
已复制!
from pydantic import BaseModel
from llama_index.llms.openai.utils import to_openai_tool
class Song(BaseModel):
"""A song with name and artist"""
name: str
artist: str
# this converts pydantic model into function to extract structured outputs
song_fn = to_openai_tool(Song)
response = llm.complete("Generate a song from Beyonce", tools=[song_fn])
tool_calls = response.additional_kwargs["tool_calls"]
print(tool_calls)
from pydantic import BaseModel from llama_index.llms.openai.utils import to_openai_tool class Song(BaseModel): """一首包含歌曲名称和艺术家的歌曲""" name: str artist: str # 这将 pydantic 模型转换为用于提取结构化输出的函数 song_fn = to_openai_tool(Song) response = llm.complete("Generate a song from Beyonce", tools=[song_fn]) tool_calls = response.additional_kwargs["tool_calls"] print(tool_calls)
[ChatCompletionMessageToolCall(id='call_34ZaM0xPl1cveODjVUpO78ra', function=Function(arguments='{"name": "Crazy in Love", "artist": "Beyonce"}', name='Song'), type='function', index=0)]
使用 Pydantic 程序¶
我们的 Pydantic 程序允许将结构化输出提取到 Pydantic 对象中。OpenAIPydanticProgram
利用函数调用进行结构化输出提取。
输入 [ ]
已复制!
from llama_index.program.openai import OpenAIPydanticProgram
from llama_index.program.openai import OpenAIPydanticProgram
输入 [ ]
已复制!
prompt_template_str = "Generate a song about {artist_name}"
program = OpenAIPydanticProgram.from_defaults(
output_cls=Song, prompt_template_str=prompt_template_str, llm=llm
)
prompt_template_str = "Generate a song about {artist_name}" program = OpenAIPydanticProgram.from_defaults( output_cls=Song, prompt_template_str=prompt_template_str, llm=llm )
输入 [ ]
已复制!
output = program(artist_name="Eminem")
output = program(artist_name="Eminem")
输入 [ ]
已复制!
output
输出
输出 [ ]
Song(name='Rap God', artist='Eminem')
使用 OpenAI 代理¶
输入 [ ]
已复制!
from llama_index.agent.openai import OpenAIAgent
from llama_index.agent.openai import OpenAIAgent
输入 [ ]
已复制!
from llama_index.core.tools import BaseTool, FunctionTool
def multiply(a: int, b: int) -> int:
"""Multiple two integers and returns the result integer"""
return a * b
multiply_tool = FunctionTool.from_defaults(fn=multiply)
def add(a: int, b: int) -> int:
"""Add two integers and returns the result integer"""
return a + b
add_tool = FunctionTool.from_defaults(fn=add)
from llama_index.core.tools import BaseTool, FunctionTool def multiply(a: int, b: int) -> int: """将两个整数相乘并返回结果整数""" return a * b multiply_tool = FunctionTool.from_defaults(fn=multiply) def add(a: int, b: int) -> int: """将两个整数相加并返回结果整数""" return a + b add_tool = FunctionTool.from_defaults(fn=add)
输入 [ ]
已复制!
agent = OpenAIAgent.from_tools(
[multiply_tool, add_tool], llm=llm, verbose=True
)
agent = OpenAIAgent.from_tools( [multiply_tool, add_tool], llm=llm, verbose=True )
输入 [ ]
已复制!
response = agent.chat("What is (121 * 3) + 42?")
print(str(response))
response = agent.chat("What is (121 * 3) + 42?") print(str(response))
Added user message to memory: What is (121 * 3) + 42? === Calling Function === Calling function: multiply with args: {"a": 121, "b": 3} Got output: 363 ======================== === Calling Function === Calling function: add with args: {"a": 363, "b": 42} Got output: 405 ======================== The result of (121 * 3) + 42 is 405.