案例目标
Llama API 是一个托管的 Llama 2 API 服务,支持函数调用功能。本案例展示了如何通过 LlamaIndex 集成 Llama API,实现基本的文本补全、对话交互、函数调用和结构化数据提取功能。Llama API 为开发者提供了一个便捷的方式来使用 Llama 2 模型,无需本地部署,可以直接通过 API 调用模型服务,大大简化了使用流程。同时,该 API 支持函数调用功能,使得模型能够与外部工具和服务进行交互,扩展了应用场景。
Llama API 提供托管式 Llama 2 服务,支持函数调用。通过 LlamaIndex 集成该 API,可实现文本补全、对话交互、函数调用及结构化数据提取。示例涵盖环境配置、密钥设置及 Pydantic 模型定义,演示了如何从非结构化文本中提取专辑信息。利用 OpenAI 兼容性接口简化开发,无需本地部署即可快速构建应用,并提供错误处理与性能监控等扩展建议。

Llama API 是一个托管的 Llama 2 API 服务,支持函数调用功能。本案例展示了如何通过 LlamaIndex 集成 Llama API,实现基本的文本补全、对话交互、函数调用和结构化数据提取功能。Llama API 为开发者提供了一个便捷的方式来使用 Llama 2 模型,无需本地部署,可以直接通过 API 调用模型服务,大大简化了使用流程。同时,该 API 支持函数调用功能,使得模型能够与外部工具和服务进行交互,扩展了应用场景。
安装必要的依赖包:
pip install llama-index-program-openai
pip install llama-index-llms-llama-api
pip install llama-index
要运行此示例,您需要从 Llama API 官网获取 API 密钥。
导入必要的库并设置 API 密钥:
from llama_index.llms.llama_api import LlamaAPI
api_key = "LL-your-key"
llm = LlamaAPI(api_key=api_key)
使用 complete 方法进行文本补全:
resp = llm.complete("Paul Graham is ")
print(resp)
输出示例:
Paul Graham is a well-known computer scientist and entrepreneur, best known for his work as a co-founder of Viaweb and later Y Combinator, a successful startup accelerator. He is also a prominent essayist and has written extensively on topics such as entrepreneurship, software development, and the tech industry.
使用 chat 方法进行对话交互:
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)
输出示例:
assistant: Arrrr, me hearty! Me name be Captain Blackbeak, the scurviest dog on the seven seas! Yer lookin' fer a swashbucklin' adventure, eh? Well, hoist the sails and set course fer the high seas, matey! I be here to help ye find yer treasure and battle any scurvy dogs who dare cross our path! So, what be yer first question, landlubber?
使用函数调用功能,定义一个 Song 模型:
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)
输出示例:
{'name': 'Song', 'arguments': {'name': 'Happy', 'artist': 'Pharrell Williams'}}
定义 Album 和 Song 模型,用于结构化数据提取:
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]
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(""""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.""" )
输出示例:
Function call: Album with args: {'name': 'Echoes of Eternity', 'artist': 'Seraphina Rivers', 'songs': [{'title': 'Stardust Serenade', 'length_mins': 6}, {'title': 'Eclipse of the Soul', 'length_mins': 8}, {'title': 'Infinity Embrace', 'length_mins': 10}]}
output
输出示例:
Album(name='Echoes of Eternity', artist='Seraphina Rivers', songs=[Song(title='Stardust Serenade', length_mins=6), Song(title='Eclipse of the Soul', length_mins=8), Song(title='Infinity Embrace', length_mins=10)])
本案例展示了 Llama API 的多种功能和应用场景:
本案例的实现基于以下思路:
Llama API 为开发者提供了一个便捷的方式来使用 Llama 2 模型,无需本地部署,可以直接通过 API 调用模型服务。通过 LlamaIndex 的集成,开发者可以使用简单的 API 调用实现文本补全、对话交互、函数调用和结构化数据提取等功能。特别是函数调用和结构化数据提取功能,使得模型能够与外部工具和服务进行交互,大大扩展了应用场景。Llama API 的 OpenAI 兼容性也使得开发者可以复用现有的 OpenAI 工具和库,降低了学习成本。总体而言,Llama API 是一个值得考虑的 Llama 2 模型服务方案,特别适合那些希望快速部署 Llama 2 应用的开发者。

微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,online
生成新的随机RSA私钥和公钥pem证书。 在线工具,RSA密钥对生成器在线工具,online
基于 Mermaid.js 实时预览流程图、时序图等图表,支持源码编辑与即时渲染。 在线工具,Mermaid 预览与可视化编辑在线工具,online
解析常见 curl 参数并生成 fetch、axios、PHP curl 或 Python requests 示例代码。 在线工具,curl 转代码在线工具,online
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online