Initialize the Netra SDK with Hugging Face instrumentation enabled. The SDK automatically traces all Transformers operations once initialized.
from netra import Netrafrom transformers import pipelineimport os# Initialize Netra with Hugging Face instrumentationNetra.init( app_name="my-ai-app", headers=f"x-api-key={os.environ.get('NETRA_API_KEY')}", trace_content=True)# Use Transformers pipeline - all calls are automatically tracedgenerator = pipeline("text-generation", model="gpt2")response = generator("What is observability?", max_length=50)print(response[0]['generated_text'])
import { Netra } from "netra-sdk";import { HfInference } from "@huggingface/inference";async function main() { // Initialize Netra with Hugging Face instrumentation (must await) await Netra.init({ appName: "my-ai-app", headers: `x-api-key=${process.env.NETRA_API_KEY}`, traceContent: true }); // Use Hugging Face Inference API - all calls are automatically traced const hf = new HfInference(process.env.HF_TOKEN); const response = await hf.textGeneration({ model: "meta-llama/Llama-2-7b-chat-hf", inputs: "What is observability?" }); console.log(response.generated_text);}main();
The SDK automatically handles streaming responses from Hugging Face models:
from transformers import TextStreamergenerator = pipeline( "text-generation", model="gpt2", streamer=TextStreamer(skip_prompt=True))# Streaming output is automatically capturedgenerator("Tell me a story", max_length=100)
const stream = hf.textGenerationStream({ model: "meta-llama/Llama-2-7b-chat-hf", inputs: "Tell me a story"});for await (const chunk of stream) { process.stdout.write(chunk.token.text);}
Control which integrations are enabled using the instruments or blockInstruments configuration:
from netra import Netrafrom netra.instrumentation.instruments import InstrumentSet# Only enable Hugging Face instrumentationNetra.init( headers=f"x-api-key={os.environ.get('NETRA_API_KEY')}", instruments={InstrumentSet.TRANSFORMERS})# Or block specific instrumentationsNetra.init( headers=f"x-api-key={os.environ.get('NETRA_API_KEY')}", block_instruments={InstrumentSet.HTTPX})
import { Netra, NetraInstruments } from "netra-sdk";// Only enable Hugging Face instrumentationawait Netra.init({ headers: `x-api-key=${process.env.NETRA_API_KEY}`, instruments: new Set([NetraInstruments.HUGGINGFACE])});// Or block specific instrumentationsawait Netra.init({ headers: `x-api-key=${process.env.NETRA_API_KEY}`, blockInstruments: new Set([NetraInstruments.HTTP])});