Spaces:
Sleeping
Sleeping
Update src/chat_langraph.py
Browse files- src/chat_langraph.py +48 -0
src/chat_langraph.py
CHANGED
|
@@ -8,6 +8,8 @@ from langgraph.prebuilt.tool_node import ToolNode
|
|
| 8 |
import sqlite3
|
| 9 |
import subprocess
|
| 10 |
import requests
|
|
|
|
|
|
|
| 11 |
from datetime import datetime
|
| 12 |
import os
|
| 13 |
|
|
@@ -155,7 +157,53 @@ def search_tool(query: str) -> dict:
|
|
| 155 |
return response.json()
|
| 156 |
except Exception as e:
|
| 157 |
return {"error": str(e)}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
# ======================== STATE GRAPH ======================== #
|
| 161 |
|
|
|
|
| 8 |
import sqlite3
|
| 9 |
import subprocess
|
| 10 |
import requests
|
| 11 |
+
import matplotlib.pyplot as plt
|
| 12 |
+
import uuid
|
| 13 |
from datetime import datetime
|
| 14 |
import os
|
| 15 |
|
|
|
|
| 157 |
return response.json()
|
| 158 |
except Exception as e:
|
| 159 |
return {"error": str(e)}
|
| 160 |
+
@tool
|
| 161 |
+
def plot_graph(expression: str, variable: str = "x", range_start: float = 0, range_end: float = 10, step: float = 1, title: str = "Graph") -> str:
|
| 162 |
+
"""
|
| 163 |
+
Plot a graph from a dynamic expression.
|
| 164 |
|
| 165 |
+
Args:
|
| 166 |
+
expression (str): Python expression as a function of variable (e.g., "2*x + 3").
|
| 167 |
+
variable (str): Variable name to use in expression (default: "x").
|
| 168 |
+
range_start (float): Start of variable range.
|
| 169 |
+
range_end (float): End of variable range.
|
| 170 |
+
step (float): Step size for variable.
|
| 171 |
+
title (str): Graph title.
|
| 172 |
+
|
| 173 |
+
Returns:
|
| 174 |
+
str: Path to saved image file.
|
| 175 |
+
"""
|
| 176 |
+
try:
|
| 177 |
+
x_values = []
|
| 178 |
+
y_values = []
|
| 179 |
+
safe_locals = {"math": math} # Allow math functions
|
| 180 |
+
|
| 181 |
+
val = range_start
|
| 182 |
+
while val <= range_end:
|
| 183 |
+
safe_locals[variable] = val
|
| 184 |
+
try:
|
| 185 |
+
y = eval(expression, {"__builtins__": None}, safe_locals)
|
| 186 |
+
except Exception as e:
|
| 187 |
+
return f"Error evaluating expression: {e}"
|
| 188 |
+
x_values.append(val)
|
| 189 |
+
y_values.append(y)
|
| 190 |
+
val += step
|
| 191 |
+
|
| 192 |
+
plt.figure()
|
| 193 |
+
plt.plot(x_values, y_values, marker="o")
|
| 194 |
+
plt.title(title)
|
| 195 |
+
plt.xlabel(variable)
|
| 196 |
+
plt.ylabel("Value")
|
| 197 |
+
plt.grid(True)
|
| 198 |
+
|
| 199 |
+
filename = f"/tmp/graph_{uuid.uuid4().hex}.png"
|
| 200 |
+
plt.savefig(filename)
|
| 201 |
+
plt.close()
|
| 202 |
+
|
| 203 |
+
return f"Graph saved at {filename}"
|
| 204 |
+
|
| 205 |
+
except Exception as e:
|
| 206 |
+
return f"Error plotting graph: {e}"
|
| 207 |
|
| 208 |
# ======================== STATE GRAPH ======================== #
|
| 209 |
|