Skip to content

Commit 5bd14f3

Browse files
authored
docs: fix Langfuse Agent tracing snippet (#11137)
1 parent 428bcab commit 5bd14f3

11 files changed

Lines changed: 682 additions & 517 deletions

File tree

docs-website/docs/pipeline-components/connectors/langfuseconnector.mdx

Lines changed: 62 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -126,63 +126,78 @@ from haystack import Pipeline
126126

127127
from haystack_integrations.components.connectors.langfuse import LangfuseConnector
128128

129+
129130
@tool
130131
def get_weather(city: Annotated[str, "The city to get weather for"]) -> str:
131-
"""Get current weather information for a city."""
132-
weather_data = {
133-
"Berlin": "18°C, partly cloudy",
134-
"New York": "22°C, sunny",
135-
"Tokyo": "25°C, clear skies"
136-
}
137-
return weather_data.get(city, f"Weather information for {city} not available")
132+
"""Get current weather information for a city."""
133+
weather_data = {
134+
"Berlin": "18°C, partly cloudy",
135+
"New York": "22°C, sunny",
136+
"Tokyo": "25°C, clear skies",
137+
}
138+
return weather_data.get(city, f"Weather information for {city} not available")
139+
138140

139141
@tool
140-
def calculate(operation: Annotated[str, "Mathematical operation: add, subtract, multiply, divide"],
141-
a: Annotated[float, "First number"],
142-
b: Annotated[float, "Second number"]) -> str:
143-
"""Perform basic mathematical calculations."""
144-
if operation == "add":
145-
result = a + b
146-
elif operation == "subtract":
147-
result = a - b
148-
elif operation == "multiply":
149-
result = a * b
150-
elif operation == "divide":
151-
if b == 0:
152-
return "Error: Division by zero"
153-
result = a / b
154-
else:
155-
return f"Error: Unknown operation '{operation}'"
156-
157-
return f"The result of {a} {operation} {b} is {result}"
142+
def calculate(
143+
operation: Annotated[
144+
str,
145+
"Mathematical operation: add, subtract, multiply, divide",
146+
],
147+
a: Annotated[float, "First number"],
148+
b: Annotated[float, "Second number"],
149+
) -> str:
150+
"""Perform basic mathematical calculations."""
151+
if operation == "add":
152+
result = a + b
153+
elif operation == "subtract":
154+
result = a - b
155+
elif operation == "multiply":
156+
result = a * b
157+
elif operation == "divide":
158+
if b == 0:
159+
return "Error: Division by zero"
160+
else:
161+
result = a / b
162+
else:
163+
return f"Error: Unknown operation '{operation}'"
164+
165+
return f"The result of {a} {operation} {b} is {result}"
166+
158167

159168
if __name__ == "__main__":
160-
## Create components
161-
chat_generator = OpenAIChatGenerator()
162-
163-
agent = Agent(
164-
chat_generator=chat_generator,
165-
tools=[get_weather, calculate],
166-
system_prompt="You are a helpful assistant with access to weather and calculator tools. Use them when needed.",
167-
exit_conditions=["text"]
168-
)
169+
## Create components
170+
chat_generator = OpenAIChatGenerator()
171+
172+
agent = Agent(
173+
chat_generator=chat_generator,
174+
tools=[get_weather, calculate],
175+
system_prompt="You are a helpful assistant with access to weather and calculator tools. Use them when needed.",
176+
exit_conditions=["text"],
177+
)
169178

170-
langfuse_connector = LangfuseConnector("Agent Example")
179+
langfuse_connector = LangfuseConnector("Agent Example")
171180

172-
## Create and run pipeline
173-
pipe = Pipeline()
174-
pipe.add_component("tracer", langfuse_connector)
175-
pipe.add_component("agent", agent)
181+
## Create and run pipeline
182+
pipe = Pipeline()
183+
pipe.add_component("tracer", langfuse_connector)
184+
pipe.add_component("agent", agent)
176185

177-
response = pipe.run(
178-
data={
179-
"agent": {"messages": [ChatMessage.from_user("What's the weather in Berlin and calculate 15 + 27?")]},
180-
"tracer": {"invocation_context": {"test": "agent_with_tools"}}
181-
}
182-
)
186+
response = pipe.run(
187+
data={
188+
"agent": {
189+
"messages": [
190+
ChatMessage.from_user(
191+
"What's the weather in Berlin and calculate 15 + 27?",
192+
),
193+
],
194+
},
195+
"tracer": {"invocation_context": {"test": "agent_with_tools"}},
196+
},
197+
)
183198

184-
print(response["agent"]["last_message"].text)
185-
print(response["tracer"]["trace_url"])
199+
print(response["agent"]["last_message"].text)
200+
print(response["tracer"]["trace_url"])
186201
```
187202

188203
## Advanced Usage

docs-website/versioned_docs/version-2.18/pipeline-components/connectors/langfuseconnector.mdx

Lines changed: 62 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -123,63 +123,78 @@ from haystack import Pipeline
123123

124124
from haystack_integrations.components.connectors.langfuse import LangfuseConnector
125125

126+
126127
@tool
127128
def get_weather(city: Annotated[str, "The city to get weather for"]) -> str:
128-
"""Get current weather information for a city."""
129-
weather_data = {
130-
"Berlin": "18°C, partly cloudy",
131-
"New York": "22°C, sunny",
132-
"Tokyo": "25°C, clear skies"
133-
}
134-
return weather_data.get(city, f"Weather information for {city} not available")
129+
"""Get current weather information for a city."""
130+
weather_data = {
131+
"Berlin": "18°C, partly cloudy",
132+
"New York": "22°C, sunny",
133+
"Tokyo": "25°C, clear skies",
134+
}
135+
return weather_data.get(city, f"Weather information for {city} not available")
136+
135137

136138
@tool
137-
def calculate(operation: Annotated[str, "Mathematical operation: add, subtract, multiply, divide"],
138-
a: Annotated[float, "First number"],
139-
b: Annotated[float, "Second number"]) -> str:
140-
"""Perform basic mathematical calculations."""
141-
if operation == "add":
142-
result = a + b
143-
elif operation == "subtract":
144-
result = a - b
145-
elif operation == "multiply":
146-
result = a * b
147-
elif operation == "divide":
148-
if b == 0:
149-
return "Error: Division by zero"
150-
result = a / b
151-
else:
152-
return f"Error: Unknown operation '{operation}'"
153-
154-
return f"The result of {a} {operation} {b} is {result}"
139+
def calculate(
140+
operation: Annotated[
141+
str,
142+
"Mathematical operation: add, subtract, multiply, divide",
143+
],
144+
a: Annotated[float, "First number"],
145+
b: Annotated[float, "Second number"],
146+
) -> str:
147+
"""Perform basic mathematical calculations."""
148+
if operation == "add":
149+
result = a + b
150+
elif operation == "subtract":
151+
result = a - b
152+
elif operation == "multiply":
153+
result = a * b
154+
elif operation == "divide":
155+
if b == 0:
156+
return "Error: Division by zero"
157+
else:
158+
result = a / b
159+
else:
160+
return f"Error: Unknown operation '{operation}'"
161+
162+
return f"The result of {a} {operation} {b} is {result}"
163+
155164

156165
if __name__ == "__main__":
157-
## Create components
158-
chat_generator = OpenAIChatGenerator()
159-
160-
agent = Agent(
161-
chat_generator=chat_generator,
162-
tools=[get_weather, calculate],
163-
system_prompt="You are a helpful assistant with access to weather and calculator tools. Use them when needed.",
164-
exit_conditions=["text"]
165-
)
166+
## Create components
167+
chat_generator = OpenAIChatGenerator()
168+
169+
agent = Agent(
170+
chat_generator=chat_generator,
171+
tools=[get_weather, calculate],
172+
system_prompt="You are a helpful assistant with access to weather and calculator tools. Use them when needed.",
173+
exit_conditions=["text"],
174+
)
166175

167-
langfuse_connector = LangfuseConnector("Agent Example")
176+
langfuse_connector = LangfuseConnector("Agent Example")
168177

169-
## Create and run pipeline
170-
pipe = Pipeline()
171-
pipe.add_component("tracer", langfuse_connector)
172-
pipe.add_component("agent", agent)
178+
## Create and run pipeline
179+
pipe = Pipeline()
180+
pipe.add_component("tracer", langfuse_connector)
181+
pipe.add_component("agent", agent)
173182

174-
response = pipe.run(
175-
data={
176-
"agent": {"messages": [ChatMessage.from_user("What's the weather in Berlin and calculate 15 + 27?")]},
177-
"tracer": {"invocation_context": {"test": "agent_with_tools"}}
178-
}
179-
)
183+
response = pipe.run(
184+
data={
185+
"agent": {
186+
"messages": [
187+
ChatMessage.from_user(
188+
"What's the weather in Berlin and calculate 15 + 27?",
189+
),
190+
],
191+
},
192+
"tracer": {"invocation_context": {"test": "agent_with_tools"}},
193+
},
194+
)
180195

181-
print(response["agent"]["last_message"].text)
182-
print(response["tracer"]["trace_url"])
196+
print(response["agent"]["last_message"].text)
197+
print(response["tracer"]["trace_url"])
183198
```
184199

185200
## Advanced Usage

docs-website/versioned_docs/version-2.19/pipeline-components/connectors/langfuseconnector.mdx

Lines changed: 62 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -126,63 +126,78 @@ from haystack import Pipeline
126126

127127
from haystack_integrations.components.connectors.langfuse import LangfuseConnector
128128

129+
129130
@tool
130131
def get_weather(city: Annotated[str, "The city to get weather for"]) -> str:
131-
"""Get current weather information for a city."""
132-
weather_data = {
133-
"Berlin": "18°C, partly cloudy",
134-
"New York": "22°C, sunny",
135-
"Tokyo": "25°C, clear skies"
136-
}
137-
return weather_data.get(city, f"Weather information for {city} not available")
132+
"""Get current weather information for a city."""
133+
weather_data = {
134+
"Berlin": "18°C, partly cloudy",
135+
"New York": "22°C, sunny",
136+
"Tokyo": "25°C, clear skies",
137+
}
138+
return weather_data.get(city, f"Weather information for {city} not available")
139+
138140

139141
@tool
140-
def calculate(operation: Annotated[str, "Mathematical operation: add, subtract, multiply, divide"],
141-
a: Annotated[float, "First number"],
142-
b: Annotated[float, "Second number"]) -> str:
143-
"""Perform basic mathematical calculations."""
144-
if operation == "add":
145-
result = a + b
146-
elif operation == "subtract":
147-
result = a - b
148-
elif operation == "multiply":
149-
result = a * b
150-
elif operation == "divide":
151-
if b == 0:
152-
return "Error: Division by zero"
153-
result = a / b
154-
else:
155-
return f"Error: Unknown operation '{operation}'"
156-
157-
return f"The result of {a} {operation} {b} is {result}"
142+
def calculate(
143+
operation: Annotated[
144+
str,
145+
"Mathematical operation: add, subtract, multiply, divide",
146+
],
147+
a: Annotated[float, "First number"],
148+
b: Annotated[float, "Second number"],
149+
) -> str:
150+
"""Perform basic mathematical calculations."""
151+
if operation == "add":
152+
result = a + b
153+
elif operation == "subtract":
154+
result = a - b
155+
elif operation == "multiply":
156+
result = a * b
157+
elif operation == "divide":
158+
if b == 0:
159+
return "Error: Division by zero"
160+
else:
161+
result = a / b
162+
else:
163+
return f"Error: Unknown operation '{operation}'"
164+
165+
return f"The result of {a} {operation} {b} is {result}"
166+
158167

159168
if __name__ == "__main__":
160-
## Create components
161-
chat_generator = OpenAIChatGenerator()
162-
163-
agent = Agent(
164-
chat_generator=chat_generator,
165-
tools=[get_weather, calculate],
166-
system_prompt="You are a helpful assistant with access to weather and calculator tools. Use them when needed.",
167-
exit_conditions=["text"]
168-
)
169+
## Create components
170+
chat_generator = OpenAIChatGenerator()
171+
172+
agent = Agent(
173+
chat_generator=chat_generator,
174+
tools=[get_weather, calculate],
175+
system_prompt="You are a helpful assistant with access to weather and calculator tools. Use them when needed.",
176+
exit_conditions=["text"],
177+
)
169178

170-
langfuse_connector = LangfuseConnector("Agent Example")
179+
langfuse_connector = LangfuseConnector("Agent Example")
171180

172-
## Create and run pipeline
173-
pipe = Pipeline()
174-
pipe.add_component("tracer", langfuse_connector)
175-
pipe.add_component("agent", agent)
181+
## Create and run pipeline
182+
pipe = Pipeline()
183+
pipe.add_component("tracer", langfuse_connector)
184+
pipe.add_component("agent", agent)
176185

177-
response = pipe.run(
178-
data={
179-
"agent": {"messages": [ChatMessage.from_user("What's the weather in Berlin and calculate 15 + 27?")]},
180-
"tracer": {"invocation_context": {"test": "agent_with_tools"}}
181-
}
182-
)
186+
response = pipe.run(
187+
data={
188+
"agent": {
189+
"messages": [
190+
ChatMessage.from_user(
191+
"What's the weather in Berlin and calculate 15 + 27?",
192+
),
193+
],
194+
},
195+
"tracer": {"invocation_context": {"test": "agent_with_tools"}},
196+
},
197+
)
183198

184-
print(response["agent"]["last_message"].text)
185-
print(response["tracer"]["trace_url"])
199+
print(response["agent"]["last_message"].text)
200+
print(response["tracer"]["trace_url"])
186201
```
187202

188203
## Advanced Usage

0 commit comments

Comments
 (0)