メインコンテンツへスキップ
DSPy は、特に LM がパイプライン内で複数回使用される場合に、LM のプロンプトと重みをアルゴリズムによって最適化するためのフレームワークです。Weave は、DSPy のモジュールや関数を使用したcallを自動的にトラッキングしてログします.

トレース

開発時と本番環境の両方で、言語モデルアプリケーションのトレースを一元的に保存することが重要です。これらのトレースは、デバッグに役立つほか、アプリケーションの改善に役立つデータセットとしても活用できます。 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.")
dspy_trace.png Weave は、DSPy プログラム内のすべての LM のcallをログし、入力、出力、メタデータの詳細を提供します。

独自のDSPyモジュールとシグネチャをトラッキングする

Module は、プロンプティング手法を抽象化する、学習可能なパラメーターを備えた DSPy プログラムの構成要素です。Signature は、DSPy Module の入出力の挙動を宣言的に定義する仕様です。Weave は、DSPy プログラム内の組み込みおよびカスタムの Signature と Module をすべて自動的にトラッキングします。
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):
    """トピックの詳細な概要を作成する。"""

    topic: str = dspy.InputField()
    title: str = dspy.OutputField()
    sections: list[str] = dspy.OutputField()
    section_subheadings: dict[str, list[str]] = dspy.OutputField(
        desc="セクション見出しとサブ見出しのマッピング"
    )


class DraftSection(dspy.Signature):
    """記事のトップレベルセクションの草稿を作成する。"""

    topic: str = dspy.InputField()
    section_heading: str = dspy.InputField()
    section_subheadings: list[str] = dspy.InputField()
    content: str = dspy.OutputField(desc="Markdown形式のセクション")


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")
モジュール実行フローとトレース詳細を表示した、Weave 内の DSPy カスタムモジュールのトレース

DSPy プログラムの最適化と評価

Weave は、DSPy オプティマイザと評価 call のトレースも自動的に自動取得します。これを使用すると、開発セットでの 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
)
最適化プロセスとパフォーマンス改善を示す、Weave の DSPy optimizer トレース