Weave (Weights & Biases)

0.52.36 · active · verified Sun Apr 12

Weave by Weights & Biases is a Python toolkit designed for developing, observing, and evaluating Generative AI applications. It enables users to log and debug inputs, outputs, and traces of language models, build rigorous evaluations, and organize information across the LLM workflow, from experimentation to production. The library is currently at version 0.52.36 and has an active release cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize Weave, decorate a function with `@weave.op` to trace its execution, and capture LLM interactions. It uses OpenAI as an example, requiring both W&B and OpenAI API keys.

import os
import weave
from openai import OpenAI

# Ensure you have your Weights & Biases API key set as an environment variable
# os.environ['WANDB_API_KEY'] = 'YOUR_WANDB_API_KEY'
# Ensure you have your OpenAI API key set as an environment variable
# os.environ['OPENAI_API_KEY'] = 'YOUR_OPENAI_API_KEY'

# Initialize Weave with your project name
# This will create a new project or connect to an existing one in W&B
weave.init(project_name=os.environ.get('WANDB_PROJECT_NAME', 'my-llm-project'))

client = OpenAI()

@weave.op()
def extract_dinos(sentence: str) -> dict:
    """Extracts dinosaur names and diets from a sentence using OpenAI."""
    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[
            {"role": "system", "content": "In JSON format extract a list of `dinosaurs`, with their `name`, their `common_name`, and whether its `diet` is a herbivore or carnivore."},
            {"role": "user", "content": sentence}
        ],
        response_model=dict # Placeholder if a Pydantic model is not used here
    )
    return response.choices[0].message.content if response.choices else {}

@weave.op()
def main():
    sentence = "The mighty Tyrannosaurus Rex (T-Rex), a carnivore, hunted the herbivorous Triceratops."
    dinosaur_info = extract_dinos(sentence)
    print("Extracted Dinosaur Info:", dinosaur_info)

if __name__ == "__main__":
    main()

view raw JSON →