Skip to main content

LlamaIndex Llms Integration: Llama Api

Prerequisites

  1. API Key: Obtain an API key from Llama API.
  2. Python 3.x: Ensure you have Python installed on your system.

Installation

  1. 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/

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

llama_index_llms_llama_api-0.7.0.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

llama_index_llms_llama_api-0.7.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file llama_index_llms_llama_api-0.7.0.tar.gz.

File metadata

  • Download URL: llama_index_llms_llama_api-0.7.0.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using 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}

File hashes

Hashes for llama_index_llms_llama_api-0.7.0.tar.gz
Algorithm Hash digest
SHA256 fda52b725fd3249560710542a8cd1d215a7b6950b263576ffba05586622e668e
MD5 ab980826e36ed26ab8ef3ca1069265b3
BLAKE2b-256 c05bf9121339c7126798d20c84c7bcb80e580bf97fa1242d26d504f6d701d554

See more details on using hashes here.

File details

Details for the file llama_index_llms_llama_api-0.7.0-py3-none-any.whl.

File metadata

  • Download URL: llama_index_llms_llama_api-0.7.0-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using 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}

File hashes

Hashes for llama_index_llms_llama_api-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 72560f9c26463ac44597fa199b9e831bf9f46969a0fd4c9c684784b0bc3fe7a0
MD5 ca0fb7fd94d7af9d4e76de038fa7a772
BLAKE2b-256 f48b470321f01a8e29b0d716eb2b219b35ebe70da956a52908db16321e0e8269

See more details on using hashes here.

Release history Release notifications | RSS feed

0.7.1

2 files

This release

0.7.0 This release

2 files

0.6.0

2 files

0.5.1

2 files

0.5.0

2 files

0.4.1

2 files

0.4.0

2 files

0.3.0

2 files

0.2.2

2 files

0.2.1

2 files

0.2.0

2 files

0.1.4

2 files

0.1.3

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

0.0.1

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page