-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata_analysis.py
More file actions
78 lines (61 loc) · 2.52 KB
/
Copy pathdata_analysis.py
File metadata and controls
78 lines (61 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Copyright 2025 Daytona Platforms Inc.
# SPDX-License-Identifier: Apache-2.0
"""LangChain data analysis example using Daytona sandboxes."""
import base64
import os
from dotenv import load_dotenv
from langchain.agents import create_agent # pylint: disable=import-error
from langchain.chat_models import init_chat_model # pylint: disable=import-error
# pylint: disable=import-error
from langchain_daytona_data_analysis import DaytonaDataAnalysisTool
from daytona import ExecutionArtifacts
load_dotenv()
# "provider:model" string, e.g. "fireworks:accounts/fireworks/models/kimi-k2p6".
# See the README for supported providers and their API keys.
MODEL = os.environ.get("MODEL", "anthropic:claude-sonnet-4-5-20250929")
model = init_chat_model(MODEL, temperature=0, max_retries=2)
def process_data_analysis_result(result: ExecutionArtifacts):
# Print the standard output from code execution
print("Result stdout", result.stdout)
result_idx = 0
for chart in result.charts:
if chart.png:
# Save the png to a file
# The png is in base64 format.
with open(f"chart-{result_idx}.png", "wb") as f:
f.write(base64.b64decode(chart.png))
print(f"Chart saved to chart-{result_idx}.png")
result_idx += 1
def main():
data_analysis_tool = DaytonaDataAnalysisTool(on_result=process_data_analysis_result)
try:
with open("./dataset.csv", "rb") as f:
data_analysis_tool.upload_file(
f,
description=(
"This is a CSV file containing vehicle valuations. "
"Relevant columns:\n"
"- 'year': integer, the manufacturing year of the vehicle\n"
"- 'price_in_euro': float, the listed price "
"of the vehicle in Euros\n"
"Drop rows where 'year' or 'price_in_euro' is missing, "
"non-numeric, or an outlier."
),
)
agent = create_agent(model, tools=[data_analysis_tool], debug=True)
agent.invoke(
{
"messages": [
{
"role": "user",
"content": "Analyze how vehicles price varies by "
"manufacturing year. Create a line chart showing "
"average price per year.",
}
]
}
)
finally:
data_analysis_tool.close()
if __name__ == "__main__":
main()