Работаем с Llama API

Урок 3 из 17 курса «Llama: начало работы»: официальный курс Llama Cookbook (Мета Лама) на русском языке. Этот урок бесплатный.

Создавайте с помощью Llama API

Этот notebook познакомит вас с функциональностью, предлагаемой Llama API, чтобы вы могли быстро и эффективно начать работу с новейшими моделями Llama 4.

Запуск этого notebook

Для запуска этого notebook вам потребуется зарегистрировать аккаунт разработчика Llama API на llama.developer.meta.com и получить API key. Вам также потребуется Python 3.8+ и способ установки Llama API Python SDK, например pip.

Установка клиента Llama API для Python

Клиент Llama API для Python представляет собой клиентскую библиотеку с открытым исходным кодом, которая обеспечивает удобный доступ к API endpoints Llama через привычный набор методов запроса.

Установите SDK с помощью pip.

%pip install llama-api-client

Получение и настройка API key

Зарегистрируйтесь или войдите в аккаунт разработчика Llama API на llama.developer.meta.com, затем перейдите на вкладку API keys в панели управления, чтобы создать новый API key.

Присвойте свой API key переменной окружения LLAMA_API_KEY.

import os
os.environ["LLAMA_API_KEY"] = YOUR_API_KEY

Теперь вы можете импортировать SDK и создать его экземпляр. SDK автоматически получит API key из переменной окружения, установленной выше.

from llama_api_client import LlamaAPIClient
client = LlamaAPIClient()

Ваш первый API call

После настройки SDK вы готовы сделать свой первый API call.

Начните с проверки списка доступных моделей:

models = client.models.list()
for model in models:
    print(model.id)
Llama-3.3-70B-Instruct
Llama-3.3-8B-Instruct
Llama-4-Maverick-17B-128E-Instruct-FP8
Llama-4-Scout-17B-16E-Instruct-FP8

Список моделей может меняться в соответствии с их выпусками. Этот notebook будет использовать новейшую модель Llama 4: Llama-4-Maverick-17B-128E-Instruct-FP8.

Завершение чата

Завершение чата с текстом

Используйте chat completions endpoint для простого цикла prompt-и-ответа на основе текста.

response = client.chat.completions.create(
    model="Llama-4-Maverick-17B-128E-Instruct-FP8",
    messages=[
        {
            "role": "user",
            "content": "Hello, how are you?",
        }
    ],
    max_completion_tokens=1024,
    temperature=0.7,
)
  
print(response.completion_message.content.text)
I'm just a language model, so I don't have feelings or emotions like humans do, but I'm functioning properly and ready to help with any questions or tasks you might have! How can I assist you today?

Многоходовое завершение чата

Chat completions endpoint поддерживает отправку нескольких сообщений в одном API call, поэтому вы можете использовать его для продолжения разговора между пользователем и моделью.

response = client.chat.completions.create(
    model="Llama-4-Maverick-17B-128E-Instruct-FP8",
    messages=[
        {
            "role": "system",
            "content": "You know a lot of animal facts"
        },
        {
            "role": "user",
            "content": "Pick an animal"
        },
        {
            "role": "assistant",
            "content": "I've picked an animal... It's the octopus!",
            "stop_reason": "stop"
        },
        {
            "role": "user",
            "content": "Tell me a fact about this animal"
        }
    ],
    max_completion_tokens=1024,
    temperature=0.7,
)
  
print(response.completion_message.content.text)        
Here's a fascinating fact about the octopus:

Octopuses have **three hearts**! Two of the hearts are branchial hearts, which pump blood to the octopus's gills, while the third is a systemic heart that pumps blood to the rest of its body. Isn't that cool?

Streaming

Вы можете быстрее возвращать результаты из API пользователю, установив параметр stream в True. Результаты будут поступать в виде потока event chunks, которые вы можете показывать пользователю по мере их прибытия.

response = client.chat.completions.create(
    messages=[
        {
            "role": "user",
            "content": "Tell me a short story",
        }
    ],
    model="Llama-4-Maverick-17B-128E-Instruct-FP8",
    stream=True,
)
for chunk in response:
    print(chunk.event.delta.text, end="", flush=True)
Here is a short story:

The old, mysterious shop had been on the corner of Main Street for as long as anyone could remember. Its windows were always dusty, and the sign above the door creaked in the wind, reading "Curios and Antiques" in faded letters.

One rainy afternoon, a young woman named Lily ducked into the shop to escape the downpour. As she pushed open the door, a bell above it rang out, and the scent of old books and wood polish wafted out.

The shop was dimly lit, with rows of shelves packed tightly with strange and exotic items: vintage dolls, taxidermied animals, and peculiar trinkets that seemed to serve no purpose. Lily wandered the aisles, running her fingers over the intricate carvings on an ancient wooden box, and marveling at a crystal pendant that glowed with an otherworldly light.

As she reached the back of the shop, she noticed a small, ornate mirror hanging on the wall. The glass was cloudy, and the frame was adorned with symbols that seemed to shimmer and dance in the dim light. Without thinking, Lily reached out to touch the mirror's surface.

As soon as she made contact with the glass, the room around her began to blur and fade. The mirror's surface rippled, like the surface of a pond, and Lily felt herself being pulled into its depths.

When she opened her eyes again, she found herself standing in a lush, vibrant garden, surrounded by flowers that seemed to glow with an ethereal light. A soft, melodious voice whispered in her ear, "Welcome home, Lily."

Lily looked around, bewildered, and saw that the garden was filled with people she had never met, yet somehow knew intimately. They smiled and beckoned her closer, and Lily felt a deep sense of belonging, as if she had finally found a place she had been searching for her entire life.

As she stood there, the rain outside seemed to fade into the distance, and Lily knew that she would never see the world in the same way again. The mysterious shop, and the enchanted mirror, had unlocked a doorway to a new reality – one that was full of wonder, magic, and possibility.

When Lily finally returned to the shop, the rain had stopped, and the sun was shining brightly outside. The shopkeeper, an old man with kind eyes, smiled at her and said, "I see you've found what you were looking for." Lily smiled back, knowing that she had discovered something far more valuable than any curiosity or antique – she had discovered a piece of herself.

Мультимодальное завершение чата

Chat completions endpoint также поддерживает понимание изображений, используя URL-адреса общедоступных изображений или используя локальные изображения, закодированные в Base64.

Вот пример, сравнивающий два изображения, которые доступны по публичным URL-адресам:

response = client.chat.completions.create(
    model="Llama-4-Maverick-17B-128E-Instruct-FP8",
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": "What do these two images have in common?",
                },
                {
                    "type": "image_url",
                    "image_url": {
                        "url": f"https://upload.wikimedia.org/wikipedia/commons/2/2e/Lama_glama_Laguna_Colorada_2.jpg",
                    },
                },
                {
                    "type": "image_url",
                    "image_url": {
                        "url": f"https://upload.wikimedia.org/wikipedia/commons/1/12/Llamas%2C_Laguna_Milluni_y_Nevado_Huayna_Potos%C3%AD_%28La_Paz_-_Bolivia%29.jpg",
                    },
                },
            ],
        },
    ],
)
print(response.completion_message.content.text)
The two images share a common subject matter, featuring llamas as the primary focus. The first image depicts a brown llama and a gray llama standing together in a desert-like environment with a body of water and mountains in the background. In contrast, the second image shows a group of llamas grazing on a hillside, set against a backdrop of mountains and a lake.

**Common Elements:**

*   **Llamas:** Both images feature llamas as the main subjects.
*   **Mountainous Background:** Both scenes are set against a mountainous landscape.
*   **Natural Environment:** Both images showcase the natural habitats of the llamas, highlighting their adaptation to high-altitude environments.

**Shared Themes:**

*   **Wildlife:** The presence of llamas in both images emphasizes their status as wildlife.
*   **Natural Beauty:** The mountainous backdrops in both images contribute to the overall theme of natural beauty.
*   **Serenity:** The calm demeanor of the llamas in both images creates a sense of serenity and tranquility.

In summary, the two images are connected through their depiction of llamas in natural, mountainous environments, highlighting the beauty and serenity of these animals in their habitats.

И вот ещё один пример, который кодирует локальное изображение в Base64 и отправляет его модели:

from PIL import Image
import matplotlib.pyplot as plt
import base64

def display_local_image(image_path):
    img = Image.open(image_path)
    plt.figure(figsize=(5,4), dpi=200)
    plt.imshow(img)
    plt.axis('off')
    plt.show()


def encode_image(image_path):
  with open(image_path, "rb") as img:
    return base64.b64encode(img.read()).decode('utf-8')
  
display_local_image("llama.jpeg")
base64_image = encode_image("llama.jpeg")
<Figure size 1000x800 with 1 Axes>

Полезные гиды