Weave (Weights & Biases)
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
- gotcha There is an older, unrelated Python library also named 'weave' (scipy.weave) for embedding C/C++ code. Ensure you are installing and importing the 'weave' library from Weights & Biases for AI application development.
- gotcha Custom Python objects in Weave traces might be truncated if not explicitly converted. For full data retention of complex objects, ensure they are converted into a dictionary of strings before logging.
- breaking Weave requires Python 3.10 or higher. Older Python versions are not supported.
- gotcha Weave's `.call()` method captures exceptions by default, storing them in `call.exception` rather than re-raising. This can obscure immediate error feedback.
- gotcha Full functionality and logging require authentication with a Weights & Biases account. Your W&B API key must be provided.
Install
-
pip install weave
Imports
- weave
import weave
- op
from weave import op
Quickstart
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()