Llama API¶
Llama API 是一个托管的 API,支持 Llama 2 的函数调用。
设置¶
首先,访问 https://www.llama-api.com/ 以获取 API 密钥
如果你在 colab 上打开此 Notebook,你可能需要安装 LlamaIndex 🦙。
In [ ]
已复制!
%pip install llama-index-program-openai
%pip install llama-index-llms-llama-api
%pip install llama-index-program-openai %pip install llama-index-llms-llama-api
In [ ]
已复制!
!pip install llama-index
!pip install llama-index
In [ ]
已复制!
from llama_index.llms.llama_api import LlamaAPI
from llama_index.llms.llama_api import LlamaAPI
In [ ]
已复制!
api_key = "LL-your-key"
api_key = "LL-your-key"
In [ ]
已复制!
llm = LlamaAPI(api_key=api_key)
llm = LlamaAPI(api_key=api_key)
基本用法¶
调用 complete
并传入 Prompt¶
In [ ]
已复制!
resp = llm.complete("Paul Graham is ")
resp = llm.complete("Paul Graham is ")
In [ ]
已复制!
print(resp)
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
并传入消息列表¶
In [ ]
已复制!
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)
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)
In [ ]
已复制!
print(resp)
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?
函数调用¶
In [ ]
已复制!
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)
from pydantic import BaseModel from llama_index.core.llms.openai_utils import to_openai_function class Song(BaseModel): """一个包含名称和艺术家的歌曲""" name: str artist: str song_fn = to_openai_function(Song)
In [ ]
已复制!
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)
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
Schema 的简单示例,其中可以包含多首歌曲。
定义输出 Schema
In [ ]
已复制!
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 pydantic import BaseModel from typing import List class Song(BaseModel): """歌曲的数据模型。""" title: str length_mins: int class Album(BaseModel): """专辑的数据模型。""" name: str artist: str songs: List[Song]
定义 pydantic 程序 (Llama API 与 OpenAI 兼容)
In [ ]
已复制!
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,
)
from llama_index.program.openai import OpenAIPydanticProgram prompt_template_str = """\ 从提供的文本中提取专辑和歌曲信息。对于每首歌曲,请确保指定标题和长度(分钟)。 {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, )
运行程序以获取结构化输出。
In [ ]
已复制!
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.
"""
)
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. """ )
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}]}
In [ ]
已复制!
output
output
Out [ ]
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)])