Wandb 回调处理器¶
Weights & Biases Prompts 是一套专为开发基于大语言模型(LLM)应用程序构建的LLMOps工具集。
WandbCallbackHandler
与 W&B Prompts 深度集成,可用于可视化并检查索引构建的执行流程、对索引的查询操作等过程。通过该处理器,您可以将创建的索引持久化为 W&B Artifacts,从而实现索引的版本控制。
In [ ]:
Copied!
%pip install llama-index-callbacks-wandb
%pip install llama-index-llms-openai
%pip install llama-index-callbacks-wandb
%pip install llama-index-llms-openai
In [ ]:
Copied!
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")
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
In [ ]:
Copied!
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.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
配置大语言模型¶
In [ ]:
Copied!
from llama_index.core import Settings
Settings.llm = OpenAI(model="gpt-4", temperature=0)
from llama_index.core import Settings
Settings.llm = OpenAI(model="gpt-4", temperature=0)
W&B 回调管理器设置¶
选项1:设置全局评估处理器
In [ ]:
Copied!
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
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:手动配置回调处理器
同时配置调试器处理器以增强笔记本的可视化调试能力。
In [ ]:
Copied!
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])
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 功能记录的所有事件。
1. 索引¶
下载数据
In [ ]:
Copied!
!mkdir -p 'data/paul_graham/'
!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'
!mkdir -p 'data/paul_graham/'
!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'
In [ ]:
Copied!
docs = SimpleDirectoryReader("./data/paul_graham/").load_data()
docs = SimpleDirectoryReader("./data/paul_graham/").load_data()
In [ ]:
Copied!
index = VectorStoreIndex.from_documents(docs)
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.
1.1 将索引持久化为 W&B 工件¶
In [ ]:
Copied!
wandb_callback.persist_index(index, index_name="simple_vector_store")
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
1.2 从 W&B Artifacts 下载索引¶
In [ ]:
Copied!
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,
)
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 **********
2. 索引查询¶
In [ ]:
Copied!
query_engine = index.as_query_engine()
response = query_engine.query("What did the author do growing up?")
print(response, sep="\n")
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.
关闭 W&B 回调处理器¶
当我们完成事件跟踪后,可以关闭 wandb 运行实例。
In [ ]:
Copied!
wandb_callback.finish()
wandb_callback.finish()