Documentation Index
Fetch the complete documentation index at: https://wb-21fd5541-feature-automate-reference-docs-generation.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
DSPyはアルゴリズム的にLMプロンプトと重みを最適化するためのフレームワークで、特にLMがパイプライン内で1回以上使用される場合に適しています。WeaveはDSPyモジュールと関数を使用して行われた呼び出しを自動的に追跡し、ログに記録します。
トレーシング
言語モデルアプリケーションのトレースを開発中も本番環境でも中央の場所に保存することが重要です。これらのトレースはデバッグに役立ち、アプリケーションの改善に役立つデータセットとしても機能します。
Weaveは自動的にDSPyのトレースをキャプチャします。追跡を開始するには、weave.init(project_name="<YOUR-WANDB-PROJECT-NAME>")を呼び出し、通常通りライブラリを使用します。
import os
import dspy
import weave
os.environ["OPENAI_API_KEY"] = "<YOUR-OPENAI-API-KEY>"
weave.init(project_name="<YOUR-WANDB-PROJECT-NAME>")
lm = dspy.LM('openai/gpt-4o-mini')
dspy.configure(lm=lm)
classify = dspy.Predict("sentence -> sentiment")
classify(sentence="it's a charming and often affecting journey.")
WeaveはDSPyプログラム内のすべてのLM呼び出しをログに記録し、入力、出力、およびメタデータに関する詳細を提供します。
独自のDSPyモジュールとシグネチャを追跡する
Moduleはプロンプト技術を抽象化するDSPyプログラムの学習可能なパラメータを持つ構成要素です。SignatureはDSPyモジュールの入力/出力動作の宣言的な仕様です。Weaveは、DSPyプログラム内のすべての組み込みおよびカスタムシグネチャとモジュールを自動的に追跡します。
import os
import dspy
import weave
os.environ["OPENAI_API_KEY"] = "<YOUR-OPENAI-API-KEY>"
weave.init(project_name="<YOUR-WANDB-PROJECT-NAME>")
class Outline(dspy.Signature):
"""Outline a thorough overview of a topic."""
topic: str = dspy.InputField()
title: str = dspy.OutputField()
sections: list[str] = dspy.OutputField()
section_subheadings: dict[str, list[str]] = dspy.OutputField(
desc="mapping from section headings to subheadings"
)
class DraftSection(dspy.Signature):
"""Draft a top-level section of an article."""
topic: str = dspy.InputField()
section_heading: str = dspy.InputField()
section_subheadings: list[str] = dspy.InputField()
content: str = dspy.OutputField(desc="markdown-formatted section")
class DraftArticle(dspy.Module):
def __init__(self):
self.build_outline = dspy.ChainOfThought(Outline)
self.draft_section = dspy.ChainOfThought(DraftSection)
def forward(self, topic):
outline = self.build_outline(topic=topic)
sections = []
for heading, subheadings in outline.section_subheadings.items():
section, subheadings = (
f"## {heading}",
[f"### {subheading}" for subheading in subheadings],
)
section = self.draft_section(
topic=outline.title,
section_heading=section,
section_subheadings=subheadings,
)
sections.append(section.content)
return dspy.Prediction(title=outline.title, sections=sections)
draft_article = DraftArticle()
article = draft_article(topic="World Cup 2002")
DSPyプログラムの最適化と評価
WeaveはDSPyオプティマイザーと評価呼び出しのトレースも自動的にキャプチャし、開発セットでDSPyプログラムのパフォーマンスを改善および評価するために使用できます。
import os
import dspy
import weave
os.environ["OPENAI_API_KEY"] = "<YOUR-OPENAI-API-KEY>"
weave.init(project_name="<YOUR-WANDB-PROJECT-NAME>")
def accuracy_metric(answer, output, trace=None):
predicted_answer = output["answer"].lower()
return answer["answer"].lower() == predicted_answer
module = dspy.ChainOfThought("question -> answer: str, explanation: str")
optimizer = dspy.BootstrapFewShot(metric=accuracy_metric)
optimized_module = optimizer.compile(
module, trainset=SAMPLE_EVAL_DATASET, valset=SAMPLE_EVAL_DATASET
)
