weave.md
1 --- 2 title: "Weave" 3 id: integrations-weave 4 description: "Weights & Bias integration for Haystack" 5 slug: "/integrations-weave" 6 --- 7 8 <a id="haystack_integrations.components.connectors.weave.weave_connector"></a> 9 10 ## Module haystack\_integrations.components.connectors.weave.weave\_connector 11 12 <a id="haystack_integrations.components.connectors.weave.weave_connector.WeaveConnector"></a> 13 14 ### WeaveConnector 15 16 Collects traces from your pipeline and sends them to Weights & Biases. 17 18 Add this component to your pipeline to integrate with the Weights & Biases Weave framework for tracing and 19 monitoring your pipeline components. 20 21 Note that you need to have the `WANDB_API_KEY` environment variable set to your Weights & Biases API key. 22 23 NOTE: If you don't have a Weights & Biases account it will interactively ask you to set one and your input 24 will then be stored in ~/.netrc 25 26 In addition, you need to set the `HAYSTACK_CONTENT_TRACING_ENABLED` environment variable to `true` in order to 27 enable Haystack tracing in your pipeline. 28 29 To use this connector simply add it to your pipeline without any connections, and it will automatically start 30 sending traces to Weights & Biases. 31 32 **Example**: 33 34 ```python 35 import os 36 37 from haystack import Pipeline 38 from haystack.components.builders import ChatPromptBuilder 39 from haystack.components.generators.chat import OpenAIChatGenerator 40 from haystack.dataclasses import ChatMessage 41 42 from haystack_integrations.components.connectors import WeaveConnector 43 44 os.environ["HAYSTACK_CONTENT_TRACING_ENABLED"] = "true" 45 46 pipe = Pipeline() 47 pipe.add_component("prompt_builder", ChatPromptBuilder()) 48 pipe.add_component("llm", OpenAIChatGenerator(model="gpt-3.5-turbo")) 49 pipe.connect("prompt_builder.prompt", "llm.messages") 50 51 connector = WeaveConnector(pipeline_name="test_pipeline") 52 pipe.add_component("weave", connector) 53 54 messages = [ 55 ChatMessage.from_system( 56 "Always respond in German even if some input data is in other languages." 57 ), 58 ChatMessage.from_user("Tell me about {{location}}"), 59 ] 60 61 response = pipe.run( 62 data={ 63 "prompt_builder": { 64 "template_variables": {"location": "Berlin"}, 65 "template": messages, 66 } 67 } 68 ) 69 print(response["llm"]["replies"][0]) 70 ``` 71 72 You should then head to `https://wandb.ai/<user_name>/projects` and see the complete trace for your pipeline under 73 the pipeline name you specified, when creating the `WeaveConnector` 74 75 <a id="haystack_integrations.components.connectors.weave.weave_connector.WeaveConnector.__init__"></a> 76 77 #### WeaveConnector.\_\_init\_\_ 78 79 ```python 80 def __init__(pipeline_name: str, 81 weave_init_kwargs: dict[str, Any] | None = None) -> None 82 ``` 83 84 Initialize WeaveConnector. 85 86 **Arguments**: 87 88 - `pipeline_name`: The name of the pipeline you want to trace. 89 - `weave_init_kwargs`: Additional arguments to pass to the WeaveTracer client. 90 91 <a id="haystack_integrations.components.connectors.weave.weave_connector.WeaveConnector.warm_up"></a> 92 93 #### WeaveConnector.warm\_up 94 95 ```python 96 def warm_up() -> None 97 ``` 98 99 Initialize the WeaveTracer. 100 101 <a id="haystack_integrations.components.connectors.weave.weave_connector.WeaveConnector.to_dict"></a> 102 103 #### WeaveConnector.to\_dict 104 105 ```python 106 def to_dict() -> dict[str, Any] 107 ``` 108 109 Serializes the component to a dictionary. 110 111 **Returns**: 112 113 Dictionary with all the necessary information to recreate this component. 114 115 <a id="haystack_integrations.components.connectors.weave.weave_connector.WeaveConnector.from_dict"></a> 116 117 #### WeaveConnector.from\_dict 118 119 ```python 120 @classmethod 121 def from_dict(cls, data: dict[str, Any]) -> "WeaveConnector" 122 ``` 123 124 Deserializes the component from a dictionary. 125 126 **Arguments**: 127 128 - `data`: Dictionary to deserialize from. 129 130 **Returns**: 131 132 Deserialized component. 133 134 <a id="haystack_integrations.tracing.weave.tracer"></a> 135 136 ## Module haystack\_integrations.tracing.weave.tracer 137 138 <a id="haystack_integrations.tracing.weave.tracer.WeaveSpan"></a> 139 140 ### WeaveSpan 141 142 A bridge between Haystack's Span interface and Weave's Call object. 143 144 Stores metadata about a component execution and its inputs and outputs, and manages the attributes/tags 145 that describe the operation. 146 147 <a id="haystack_integrations.tracing.weave.tracer.WeaveSpan.set_tag"></a> 148 149 #### WeaveSpan.set\_tag 150 151 ```python 152 def set_tag(key: str, value: Any) -> None 153 ``` 154 155 Set a tag by adding it to the call's inputs. 156 157 **Arguments**: 158 159 - `key`: The tag key. 160 - `value`: The tag value. 161 162 <a id="haystack_integrations.tracing.weave.tracer.WeaveSpan.raw_span"></a> 163 164 #### WeaveSpan.raw\_span 165 166 ```python 167 def raw_span() -> Any 168 ``` 169 170 Access to the underlying Weave Call object. 171 172 <a id="haystack_integrations.tracing.weave.tracer.WeaveSpan.get_correlation_data_for_logs"></a> 173 174 #### WeaveSpan.get\_correlation\_data\_for\_logs 175 176 ```python 177 def get_correlation_data_for_logs() -> dict[str, Any] 178 ``` 179 180 Correlation data for logging. 181 182 <a id="haystack_integrations.tracing.weave.tracer.WeaveTracer"></a> 183 184 ### WeaveTracer 185 186 Implements a Haystack's Tracer to make an interface with Weights and Bias Weave. 187 188 It's responsible for creating and managing Weave calls, and for converting Haystack spans 189 to Weave spans. It creates spans for each Haystack component run. 190 191 <a id="haystack_integrations.tracing.weave.tracer.WeaveTracer.__init__"></a> 192 193 #### WeaveTracer.\_\_init\_\_ 194 195 ```python 196 def __init__(project_name: str, **weave_init_kwargs: Any) -> None 197 ``` 198 199 Initialize the WeaveTracer. 200 201 **Arguments**: 202 203 - `project_name`: The name of the project to trace, this is will be the name appearing in Weave project. 204 - `weave_init_kwargs`: Additional arguments to pass to the Weave client. 205 206 <a id="haystack_integrations.tracing.weave.tracer.WeaveTracer.current_span"></a> 207 208 #### WeaveTracer.current\_span 209 210 ```python 211 def current_span() -> Span | None 212 ``` 213 214 Get the current active span. 215 216 <a id="haystack_integrations.tracing.weave.tracer.WeaveTracer.trace"></a> 217 218 #### WeaveTracer.trace 219 220 ```python 221 @contextlib.contextmanager 222 def trace(operation_name: str, 223 tags: dict[str, Any] | None = None, 224 parent_span: WeaveSpan | None = None) -> Iterator[WeaveSpan] 225 ``` 226 227 A context manager that creates and manages spans for tracking operations in Weights & Biases Weave. 228 229 It has two main workflows: 230 231 A) For regular operations (operation_name != "haystack.component.run"): 232 Creates a Weave Call immediately 233 Creates a WeaveSpan with this call 234 Sets any provided tags 235 Yields the span for use in the with block 236 When the block ends, updates the call with pipeline output data 237 238 B) For component runs (operation_name == "haystack.component.run"): 239 Creates a WeaveSpan WITHOUT a call initially (deferred creation) 240 Sets any provided tags 241 Yields the span for use in the with block 242 Creates the actual Weave Call only at the end, when all component information is available 243 Updates the call with component output data 244 245 This distinction is important because Weave's calls can't be updated once created, but the content 246 tags are only set on the Span at a later stage. To get the inputs on call creation, we need to create 247 the call after we yield the span.