> ## Documentation Index
> Fetch the complete documentation index at: https://docs.coreweave.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Haystack

> WeaveConnector インテグレーションを使用して、Deepset Haystack パイプラインを W&B Weave でトレースします。

[Haystack](https://haystack.deepset.ai/) は、検索アプリケーションや LLM アプリケーションを構築するためのオープンソースのフレームワークです。Deepset は、Haystack パイプラインのトレースを W\&B Weave に転送する WeaveConnector コンポーネントを提供しています。これにより、Weights & Biases の UI でコンポーネントの run、プロンプト、出力を確認できます。

このガイドは、パイプラインに可観測性を追加したい Haystack 開発者を対象としています。

API の詳細やその他のサンプルについては、以下の Deepset のリソースを参照してください。

* Haystack の [WeaveConnector](https://docs.haystack.deepset.ai/docs/weaveconnector)
* Haystack の [Weave インテグレーション API リファレンス](https://docs.haystack.deepset.ai/reference/integrations-weave)
* RAG パイプラインを使用した Haystack の [Trace with W\&B Weave](https://docs.cloud.deepset.ai/docs/use-weights-and-biases) のサンプル

<h2 id="prerequisites">
  前提条件
</h2>

開始する前に、以下を完了しておく必要があります。

* W\&B の [APIキー](https://forge.coreweave.com/settings) を使用して、環境変数 `WANDB_API_KEY` を設定します。これにより、コネクタが W\&B Weave に対して認証されます。
* パイプラインを実行する前に、`HAYSTACK_CONTENT_TRACING_ENABLED` を `true` に設定します。これにより、Haystack がトレースデータを出力し、コネクタがそのデータを転送できるようになります。

<h2 id="install">
  インストール
</h2>

`pip` を使用して必須の依存関係をインストールします。

```bash lines theme={"system"}
pip install weave-haystack
```

このパッケージは、依存関係として `haystack-ai` と `weave` の利用可能なバージョンを宣言します。

<h2 id="trace-a-haystack-pipeline-with-weave">
  Weave で Haystack パイプラインをトレースする
</h2>

次の例では、Haystack の `WeaveConnector` を Haystack の [`Pipeline`](https://docs.haystack.deepset.ai/docs/pipelines) に追加し、W\&B Weave と連携してパイプラインのコンポーネントをトレースおよび監視します。渡した `pipeline_name` は、そのパイプラインから送信されるトレースの Weave プロジェクト名として使用されます。

Haystack パイプラインでは、`WeaveConnector` を他のコンポーネントに接続しないでください。

```python lines theme={"system"}
import os

os.environ["HAYSTACK_CONTENT_TRACING_ENABLED"] = "true"

from haystack import Pipeline
from haystack.components.builders import ChatPromptBuilder
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.dataclasses import ChatMessage
from haystack_integrations.components.connectors.weave import WeaveConnector

pipe = Pipeline()
pipe.add_component("prompt_builder", ChatPromptBuilder())
pipe.add_component("llm", OpenAIChatGenerator(model="gpt-3.5-turbo"))
pipe.connect("prompt_builder.prompt", "llm.messages")

# pipeline_name は W&B プロジェクト名になります。
connector = WeaveConnector(pipeline_name="haystack_demo")
# コネクタをパイプラインに追加しますが、ほかのコンポーネントには接続しません。
pipe.add_component("weave", connector)

messages = [
    ChatMessage.from_system(
        "Always respond in German even if some input data is in other languages.",
    ),
    ChatMessage.from_user("Tell me about {{location}}"),
]

response = pipe.run(
    data={
        "prompt_builder": {
            "template_variables": {"location": "Berlin"},
            "template": messages,
        },
    },
)

print(response["llm"]["replies"][0])
```

パイプラインの実行後、W\&B の Workspace を開き、`pipeline_name` で指定した名前の project を選択して、**Traces** に移動し、完了したトレースを確認します。
