Documentation Index
Fetch the complete documentation index at: https://wb-21fd5541-weave-caching.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Weave は、LlamaIndex Python ライブラリ を通じて行われるすべての呼び出しの追跡とログ記録を簡素化するように設計されています。
LLM を扱う際、デバッグは避けて通れません。モデル呼び出しの失敗、出力のフォーマットミス、あるいは入れ子になったモデル呼び出しによる混乱など、問題の特定は困難な場合があります。LlamaIndex アプリケーションは多くの場合、複数のステップと LLM 呼び出しの実行で構成されるため、チェーンやエージェントの内部動作を理解することが極めて重要です。
Weave は、LlamaIndex アプリケーションの Traces を自動的にキャプチャすることで、このプロセスを簡素化します。これにより、アプリケーションのパフォーマンスを監視および分析できるようになり、LLM ワークフローのデバッグや最適化が容易になります。また、Weave は評価ワークフローにも役立ちます。
はじめに
開始するには、スクリプトの冒頭で weave.init() を呼び出すだけです。weave.init() の引数には、Traces を整理するのに役立つ Projects 名を指定します。
import weave
from llama_index.core.chat_engine import SimpleChatEngine
# プロジェクト名で Weave を初期化
weave.init("llamaindex_demo")
chat_engine = SimpleChatEngine.from_defaults()
response = chat_engine.chat(
"Say something profound and romantic about fourth of July"
)
print(response)
上記の例では、内部で OpenAI 呼び出しを行うシンプルな LlamaIndex チャットエンジンを作成しています。以下のトレースを確認してください:
Tracing
LlamaIndex は、データと LLM を簡単に接続できることで知られています。シンプルな RAG アプリケーションには、埋め込みステップ、リトリーバル(検索)ステップ、そしてレスポンス合成ステップが必要です。複雑さが増すにつれて、開発とプロダクションの両方のフェーズで、個々のステップの Traces を中央データベースに保存することが重要になります。
これらの Traces は、アプリケーションのデバッグと改善に不可欠です。Weave は、プロンプトテンプレート、LLM 呼び出し、ツール、エージェントのステップなど、LlamaIndex ライブラリを通じて行われるすべての呼び出しを自動的に追跡します。Traces は Weave のウェブインターフェースで表示できます。
以下は、LlamaIndex の Starter Tutorial (OpenAI) に基づくシンプルな RAG パイプラインの例です:
import weave
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
# プロジェクト名で Weave を初期化
weave.init("llamaindex_demo")
# `data` ディレクトリーに `.txt` ファイルがあることを想定
documents = SimpleDirectoryReader("data").load_data()
index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine()
response = query_engine.query("What did the author do growing up?")
print(response)
トレースのタイムラインは「イベント」をキャプチャするだけでなく、実行時間、コスト、および該当する場合はトークン数も記録します。トレースをドリルダウンして、各ステップの入力と出力を確認できます。
ワンクリック・オブザーバビリティ(可観測性) 🔭
LlamaIndex は、プロダクション環境で原則に基づいた LLM アプリケーションを構築できるように、ワンクリック・オブザーバビリティ 🔭 を提供しています。
私たちのインテグレーションはこの LlamaIndex の機能を活用し、WeaveCallbackHandler() を自動的に llama_index.core.global_handler に設定します。したがって、LlamaIndex と Weave のユーザーとして必要なのは、Weave の Run を初期化する weave.init(<name-of-project>) を実行することだけです。
実験を容易にするための Model 作成
プロンプト、モデル設定、推論パラメータなど、複数のコンポーネントがある様々なユースケースにおいて、アプリケーション内の LLM を整理し評価することは困難です。weave.Model を使用すると、システムプロンプトや使用するモデルなどの実験の詳細をキャプチャして整理でき、異なるイテレーション間の比較が容易になります。
次の例は、weave/data フォルダにあるデータを使用して、WeaveModel 内で LlamaIndex クエリエンジンを構築する方法を示しています。
import weave
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.core.node_parser import SentenceSplitter
from llama_index.llms.openai import OpenAI
from llama_index.core import PromptTemplate
PROMPT_TEMPLATE = """
You are given with relevant information about Paul Graham. Answer the user query only based on the information provided. Don't make up stuff.
User Query: {query_str}
Context: {context_str}
Answer:
"""
class SimpleRAGPipeline(weave.Model):
chat_llm: str = "gpt-4"
temperature: float = 0.1
similarity_top_k: int = 2
chunk_size: int = 256
chunk_overlap: int = 20
prompt_template: str = PROMPT_TEMPLATE
def get_llm(self):
return OpenAI(temperature=self.temperature, model=self.chat_llm)
def get_template(self):
return PromptTemplate(self.prompt_template)
def load_documents_and_chunk(self, data):
documents = SimpleDirectoryReader(data).load_data()
splitter = SentenceSplitter(
chunk_size=self.chunk_size,
chunk_overlap=self.chunk_overlap,
)
nodes = splitter.get_nodes_from_documents(documents)
return nodes
def get_query_engine(self, data):
nodes = self.load_documents_and_chunk(data)
index = VectorStoreIndex(nodes)
llm = self.get_llm()
prompt_template = self.get_template()
return index.as_query_engine(
similarity_top_k=self.similarity_top_k,
llm=llm,
text_qa_template=prompt_template,
)
@weave.op()
def predict(self, query: str):
query_engine = self.get_query_engine(
# このデータは weave リポジトリの data/paul_graham にあります
"data/paul_graham",
)
response = query_engine.query(query)
return {"response": response.response}
weave.init("test-llamaindex-weave")
rag_pipeline = SimpleRAGPipeline()
response = rag_pipeline.predict("What did the author do growing up?")
print(response)
この weave.Model を継承した SimpleRAGPipeline クラスは、この RAG パイプラインの重要なパラメータを整理します。predict メソッドを weave.op() でデコレートすることで、追跡が可能になります。
weave.Evaluation による評価の実施
Evaluations は、アプリケーションのパフォーマンスを測定するのに役立ちます。weave.Evaluation クラスを使用することで、特定のタスクやデータセットに対してモデルがどれだけ適切に機能するかをキャプチャでき、異なるモデルやアプリケーションのイテレーションを比較しやすくなります。次の例は、作成したモデルを評価する方法を示しています。
import asyncio
from llama_index.core.evaluation import CorrectnessEvaluator
eval_examples = [
{
"id": "0",
"query": "What programming language did Paul Graham learn to teach himself AI when he was in college?",
"ground_truth": "Paul Graham learned Lisp to teach himself AI when he was in college.",
},
{
"id": "1",
"query": "What was the name of the startup Paul Graham co-founded that was eventually acquired by Yahoo?",
"ground_truth": "The startup Paul Graham co-founded that was eventually acquired by Yahoo was called Viaweb.",
},
{
"id": "2",
"query": "What is the capital city of France?",
"ground_truth": "I cannot answer this question because no information was provided in the text.",
},
]
llm_judge = OpenAI(model="gpt-4", temperature=0.0)
evaluator = CorrectnessEvaluator(llm=llm_judge)
@weave.op()
def correctness_evaluator(query: str, ground_truth: str, output: dict):
result = evaluator.evaluate(
query=query, reference=ground_truth, response=output["response"]
)
return {"correctness": float(result.score)}
evaluation = weave.Evaluation(dataset=eval_examples, scorers=[correctness_evaluator])
rag_pipeline = SimpleRAGPipeline()
asyncio.run(evaluation.evaluate(rag_pipeline))
この評価は前のセクションの例に基づいています。weave.Evaluation を使用した評価には、評価データセット、スコアラー関数、および weave.Model が必要です。これら 3 つの主要コンポーネントに関する注意点は以下の通りです。
- 評価サンプルの辞書のキーが、スコアラー関数の引数および
weave.Model の predict メソッドの引数と一致していることを確認してください。
weave.Model には、predict、infer、または forward という名前のメソッドが必要です。追跡のためにこのメソッドを weave.op() でデコレートしてください。
- スコアラー関数は
weave.op() でデコレートし、output を名前付き引数として持つ必要があります。
Weave を LlamaIndex と統合することで、LLM アプリケーションの包括的なログ記録と監視が保証され、評価を使用したデバッグとパフォーマンスの最適化が容易になります。