跳转到内容

Wandb 回调处理器

Weights & Biases Prompts 是一套专为开发基于大语言模型应用而构建的LLMOps工具套件。

WandbCallbackHandler 与 W&B Prompts 集成,用于可视化和检查索引构建的执行流程,或对索引进行查询等操作。您可以使用此处理程序将创建的索引持久化为 W&B Artifacts,从而实现对索引的版本控制。

%pip install llama-index-callbacks-wandb
%pip install llama-index-llms-openai
import os
from getpass import getpass
if os.getenv("OPENAI_API_KEY") is None:
os.environ["OPENAI_API_KEY"] = getpass(
"Paste your OpenAI key from:"
" https://platform.openai.com/account/api-keys\n"
)
assert os.getenv("OPENAI_API_KEY", "").startswith(
"sk-"
), "This doesn't look like a valid OpenAI API key"
print("OpenAI API key configured")
OpenAI API key configured
from llama_index.core.callbacks import CallbackManager
from llama_index.core.callbacks import LlamaDebugHandler
from llama_index.callbacks.wandb import WandbCallbackHandler
from llama_index.core import (
VectorStoreIndex,
SimpleDirectoryReader,
SimpleKeywordTableIndex,
StorageContext,
)
from llama_index.llms.openai import OpenAI
from llama_index.core import Settings
Settings.llm = OpenAI(model="gpt-4", temperature=0)

选项1: 设置全局评估处理器

import llama_index.core
from llama_index.core import set_global_handler
set_global_handler("wandb", run_args={"project": "llamaindex"})
wandb_callback = llama_index.core.global_handler

选项2: 手动配置回调处理器

同时配置一个调试器处理器以增强笔记本可见性。

llama_debug = LlamaDebugHandler(print_trace_on_end=True)
# wandb.init args
run_args = dict(
project="llamaindex",
)
wandb_callback = WandbCallbackHandler(run_args=run_args)
Settings.callback_manager = CallbackManager([llama_debug, wandb_callback])

运行上述单元格后,您将获得W&B运行页面URL。在这里您将找到使用Weights and Biases的Prompts功能追踪的所有事件构成的追踪表。

下载数据

!mkdir -p 'data/paul_graham/'
!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'
docs = SimpleDirectoryReader("./data/paul_graham/").load_data()
index = VectorStoreIndex.from_documents(docs)
**********
Trace: index_construction
|_node_parsing -> 0.295179 seconds
|_chunking -> 0.293976 seconds
|_embedding -> 0.494492 seconds
|_embedding -> 0.346162 seconds
**********
wandb: Logged trace tree to W&B.
wandb_callback.persist_index(index, index_name="simple_vector_store")
wandb: Adding directory to artifact (/Users/loganmarkewich/llama_index/docs/examples/callbacks/wandb/run-20230801_152955-ds93prxa/files/storage)... Done. 0.0s
from llama_index.core import load_index_from_storage
storage_context = wandb_callback.load_storage_context(
artifact_url="ayut/llamaindex/simple_vector_store:v0"
)
# Load the index and initialize a query engine
index = load_index_from_storage(
storage_context,
)
wandb: 3 of 3 files downloaded.
**********
Trace: index_construction
**********
query_engine = index.as_query_engine()
response = query_engine.query("What did the author do growing up?")
print(response, sep="\n")
**********
Trace: query
|_query -> 2.695958 seconds
|_retrieve -> 0.806379 seconds
|_embedding -> 0.802871 seconds
|_synthesize -> 1.8893 seconds
|_llm -> 1.842434 seconds
**********
wandb: Logged trace tree to W&B.
The text does not provide information on what the author did growing up.

当我们完成事件追踪后,可以关闭 wandb 运行。

wandb_callback.finish()