Skip to main content

Installation

npm install @fallom/trace @google/generative-ai

Quick Start

import fallom from "@fallom/trace";
import { GoogleGenerativeAI } from "@google/generative-ai";

// Initialize Fallom once at app startup
await fallom.init({ apiKey: process.env.FALLOM_API_KEY });

// Create a session for this conversation/request
const session = fallom.session({
  configKey: "my-app",
  sessionId: "session-123",
  customerId: "user-456",
});

// Create and wrap the model
const genAI = new GoogleGenerativeAI(process.env.GOOGLE_API_KEY!);
const model = session.wrapGoogleAI(
  genAI.getGenerativeModel({ model: "gemini-1.5-pro" })
);

// Use as normal - automatically traced!
const result = await model.generateContent("Hello!");

console.log(result.response.text());

With System Instruction

const genAI = new GoogleGenerativeAI(process.env.GOOGLE_API_KEY!);

const model = session.wrapGoogleAI(
  genAI.getGenerativeModel({
    model: "gemini-1.5-pro",
    systemInstruction: "You are a helpful assistant.",
  })
);

const result = await model.generateContent("Tell me about TypeScript.");

Chat Sessions

const chat = model.startChat({
  history: [
    { role: "user", parts: [{ text: "Hello!" }] },
    { role: "model", parts: [{ text: "Hi there! How can I help?" }] },
  ],
});

// Each message is traced
const result = await chat.sendMessage("What's the weather like?");

Streaming

const result = await model.generateContentStream("Write a poem about coding.");

for await (const chunk of result.stream) {
  process.stdout.write(chunk.text());
}

Model A/B Testing

import fallom from "@fallom/trace";
import { GoogleGenerativeAI } from "@google/generative-ai";

await fallom.init({ apiKey: "your-fallom-api-key" });

const session = fallom.session({
  configKey: "my-experiment",
  sessionId: "session-123",
});

const genAI = new GoogleGenerativeAI(process.env.GOOGLE_API_KEY!);

// Get assigned model for this session
const modelId = await session.getModel({ fallback: "gemini-1.5-flash" });

const model = session.wrapGoogleAI(genAI.getGenerativeModel({ model: modelId }));

const result = await model.generateContent("Hello!");

What Gets Traced

FieldDescription
Modelgemini-1.5-pro, gemini-1.5-flash
DurationTotal request time (ms)
TokensInput, output tokens
CostCalculated from token usage
PromptsInput content
CompletionsModel response
SessionYour config key + session ID

Next Steps