LlamaIndex Llms Integration: Llama Api
Prerequisites
- API Key: Obtain an API key from Llama API.
- Python 3.x: Ensure you have Python installed on your system.
Installation
-
Install the required Python packages:
%pip install llama-index-program-openai %pip install llama-index-llms-llama-api !pip install llama-index
Basic Usage
Import Required Libraries
from llama_index.llms.llama_api import LlamaAPI
from llama_index.core.llms import ChatMessage
Initialize LlamaAPI
Set up the API key:
api_key = "LL-your-key"
llm = LlamaAPI(api_key=api_key)
Complete with a Prompt
Generate a response using a prompt:
resp = llm.complete("Paul Graham is ")
print(resp)
Chat with a List of Messages
Interact with the model using a chat interface:
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)
Function Calling
Define a function using Pydantic and call it through LlamaAPI:
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)
response = llm.complete("Generate a song", functions=[song_fn])
function_call = response.additional_kwargs["function_call"]
print(function_call)
Structured Data Extraction
Define schemas for structured output using Pydantic:
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]
Define the prompt template for extracting structured data:
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,
)
Run Program to Get Structured Output
Execute the program to extract structured data from the provided text:
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 Example
You can print the structured output like this:
print(output)
LLM Implementation example
https://docs.llamaindex.ai/en/stable/examples/llm/llama_api/
Metadata
Release files for llama-index-llms-llama-api 0.7.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| llama_index_llms_llama_api-0.7.1.tar.gz | 5.0 kB | Without provenance | Details for llama_index_llms_llama_api-0.7.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| llama_index_llms_llama_api-0.7.1-py3-none-any.whl | Python 3 | none | any | Details for llama_index_llms_llama_api-0.7.1-py3-none-any.whl |
Total release size: 9.6 kB
Release files / llama_index_llms_llama_api-0.7.1.tar.gz
| Download URL | llama_index_llms_llama_api-0.7.1.tar.gz |
|---|---|
| Size | 5.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f521eac060569a8aa40d8df3d8597ee1559f1a1a5e3c1afa78096be7297f83ea
|
|
BLAKE2b-256 checksum How to use checksums |
ed49bbe8510db6ec4eecdf4a8676cc08f0a7e44de371852f0e6e02961ccbb257
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.7 {"installer":{"name":"uv","version":"0.12.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / llama_index_llms_llama_api-0.7.1-py3-none-any.whl
| Download URL | llama_index_llms_llama_api-0.7.1-py3-none-any.whl |
|---|---|
| Size | 4.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b4b9caeb189b5c24f914c8255c71755df74479aadd01fe5d830b7eb5e3e4fb96
|
|
BLAKE2b-256 checksum How to use checksums |
d39d4b4a59e26be4a2013b92f531dd8bdc55d6660371eb0c04d1890df61e10f1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.7 {"installer":{"name":"uv","version":"0.12.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for llama-index-llms-llama-api 0.7.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.