Skip to main content

Installation

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

Quick Start

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

// Initialize Fallom
await trace.init({ apiKey: process.env.FALLOM_API_KEY });

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

// Set session context (configKey, sessionId, userId)
trace.setSession("my-app", "session-123", "user-456");

// 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 = trace.wrapGoogleAI(
  genAI.getGenerativeModel({
    model: "gemini-1.5-pro",
    systemInstruction: "You are a helpful assistant.",
  })
);

trace.setSession("my-agent", "session-123");

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 { trace, models } from "@fallom/trace";
import { GoogleGenerativeAI } from "@google/generative-ai";

// Initialize both trace and models
await trace.init({ apiKey: "your-fallom-api-key" });
models.init({ apiKey: "your-fallom-api-key" });

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

// Get assigned model
const modelId = await models.get("my-experiment", "session-123", {
  fallback: "gemini-1.5-flash",
});

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

trace.setSession("my-experiment", "session-123");

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