Fireworks 函数调用指南¶
Fireworks.ai 为其大语言模型提供类似 OpenAI 的函数调用功能。这使得用户可以直接描述可用工具/函数集,让模型动态选择需要调用的正确函数,而无需用户进行复杂的提示工程。
由于我们的 Fireworks 大语言模型直接继承自 OpenAI 的类结构,因此可以复用现有的抽象层与 Fireworks 交互。
我们将通过三个层面展示这一功能:直接在模型 API 上使用、作为 Pydantic 程序(结构化输出提取)的一部分使用,以及作为智能体的一部分使用。
In [ ]:
Copied!
%pip install llama-index-llms-fireworks
%pip install llama-index-llms-fireworks
In [ ]:
Copied!
%pip install llama-index
%pip install llama-index
In [ ]:
Copied!
import os
os.environ["FIREWORKS_API_KEY"] = ""
import os
os.environ["FIREWORKS_API_KEY"] = ""
In [ ]:
Copied!
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
大语言模型模块的函数调用功能¶
您可以直接在大语言模型模块中输入函数调用。
In [ ]:
Copied!
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):
"""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)
[ChatCompletionMessageToolCall(id='call_34ZaM0xPl1cveODjVUpO78ra', function=Function(arguments='{"name": "Crazy in Love", "artist": "Beyonce"}', name='Song'), type='function', index=0)]
使用 Pydantic 程序¶
我们的 Pydantic 程序支持将结构化输出提取为 Pydantic 对象。OpenAIPydanticProgram 利用函数调用功能实现结构化输出提取。
In [ ]:
Copied!
from llama_index.program.openai import OpenAIPydanticProgram
from llama_index.program.openai import OpenAIPydanticProgram
In [ ]:
Copied!
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
)
In [ ]:
Copied!
output = program(artist_name="Eminem")
output = program(artist_name="Eminem")
In [ ]:
Copied!
output
output
Out[ ]:
Song(name='Rap God', artist='Eminem')
使用 OpenAI 智能代理¶
In [ ]:
Copied!
from llama_index.agent.openai import OpenAIAgent
from llama_index.agent.openai import OpenAIAgent
In [ ]:
Copied!
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:
"""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)
In [ ]:
Copied!
agent = OpenAIAgent.from_tools(
[multiply_tool, add_tool], llm=llm, verbose=True
)
agent = OpenAIAgent.from_tools(
[multiply_tool, add_tool], llm=llm, verbose=True
)
In [ ]:
Copied!
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.