Llama API Client

0.3.0 · active · verified Tue Apr 21

The `llama-api-client` library is the official TypeScript client for interacting with the Llama API. It provides convenient access to the REST API from both server-side TypeScript and JavaScript environments. Currently at version `0.3.0`, the library exhibits an active development cadence with frequent releases (often weekly or bi-weekly), indicating ongoing feature enhancements and bug fixes. Key differentiators include its TypeScript-first design, comprehensive type definitions for all request parameters and response fields, and robust support for advanced features like streaming responses via Server-Sent Events (SSE) and flexible file uploads using various input types (e.g., `File`, `fs.ReadStream`, `toFile` helper). It is generated with Stainless, ensuring a consistent and well-documented API surface, and includes robust error handling for API and network issues. This client streamlines integration with Llama models for applications requiring conversational AI, content generation, or other generative AI capabilities.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Llama API client, perform a basic chat completion, handle streaming responses, and shows how to approach error handling using `LlamaAPIClient.APIError`.

import LlamaAPIClient from 'llama-api-client';
import { toFile } from 'llama-api-client'; // Included for file upload example context, though commented out for brevity

// Ensure your LLAMA_API_KEY is set as an environment variable (e.g., in a .env file or production config).
// For local development, 'dotenv' package might be used: `require('dotenv').config();`
const client = new LlamaAPIClient({
  apiKey: process.env['LLAMA_API_KEY'] ?? '', // Provide an empty string fallback or handle validation for missing key
});

async function runLlamaClientExamples() {
  try {
    console.log('--- Creating a Chat Completion ---');
    const chatResponse = await client.chat.completions.create({
      messages: [{ content: 'Hello, what is the capital of France?', role: 'user' }],
      model: 'llama-3-8b-instruct', // Using an example model identifier
      max_tokens: 50,
      temperature: 0.7,
    });
    console.log('Chat completion response:', chatResponse.completion_message?.content);

    console.log('\n--- Streaming Response Example ---');
    const stream = await client.chat.completions.create({
      messages: [{ content: 'Tell me a short story about a brave knight.', role: 'user' }],
      model: 'llama-3-8b-instruct',
      stream: true,
      max_tokens: 100,
    });
    process.stdout.write('Streamed story: ');
    for await (const chunk of stream) {
      if (chunk.completion_message) {
        process.stdout.write(chunk.completion_message.content || '');
      }
    }
    process.stdout.write('\n'); // Newline after stream finishes

    // Conceptual example for file upload (requires an 'uploads' endpoint and actual file data)
    // For a real scenario, you would typically use fs.createReadStream, a web File object, or Buffer.
    // const dummyFileContent = Buffer.from('This is a test file for upload.');
    // const dummyFile = await toFile(dummyFileContent, 'my-document.txt');
    // const uploadResponse = await client.uploads.create({ file: dummyFile, purpose: 'fine-tune' });
    // console.log('\nUpload initiated:', uploadResponse);

  } catch (error) {
    if (error instanceof LlamaAPIClient.APIError) {
      console.error('Llama API Error caught:', error.status, error.code, error.message, 'Details:', error.error);
    } else {
      console.error('An unexpected error occurred:', error);
    }
  }
}

runLlamaClientExamples();

view raw JSON →