Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
|
@@ -19,6 +19,9 @@ import requests
|
|
| 19 |
import uvicorn
|
| 20 |
import re
|
| 21 |
from fastapi.staticfiles import StaticFiles
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
app = FastAPI()
|
| 24 |
|
|
@@ -40,7 +43,7 @@ class CodeExecutionResult:
|
|
| 40 |
API_URL = "https://pvanand-code-execution-files-v5.hf.space"
|
| 41 |
|
| 42 |
@tool
|
| 43 |
-
def execute_python(code: str
|
| 44 |
"""Execute Python code in an IPython interactiveshell and return the output.
|
| 45 |
The returned artifacts (if present) are automatically rendered in the UI and visible to the user.
|
| 46 |
Args:
|
|
@@ -54,7 +57,7 @@ def execute_python(code: str) -> str:
|
|
| 54 |
'Content-Type': 'application/json'
|
| 55 |
}
|
| 56 |
data = {
|
| 57 |
-
"session_token":
|
| 58 |
"code": code
|
| 59 |
}
|
| 60 |
response = requests.post(
|
|
@@ -69,20 +72,36 @@ def execute_python(code: str) -> str:
|
|
| 69 |
response_json = response.json()
|
| 70 |
return f"data: {json.dumps(response_json)} \ndata:"
|
| 71 |
|
| 72 |
-
# Configure the memory and model"
|
| 73 |
memory = MemorySaver()
|
| 74 |
model = ChatOpenAI(model="gpt-4o-mini", streaming=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
def state_modifier(state) -> list[BaseMessage]:
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
# Create the agent with the Python execution tool
|
| 88 |
agent = create_react_agent(
|
|
|
|
| 19 |
import uvicorn
|
| 20 |
import re
|
| 21 |
from fastapi.staticfiles import StaticFiles
|
| 22 |
+
from langchain_core.runnables import RunnableConfig
|
| 23 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 24 |
+
from datetime import datetime
|
| 25 |
|
| 26 |
app = FastAPI()
|
| 27 |
|
|
|
|
| 43 |
API_URL = "https://pvanand-code-execution-files-v5.hf.space"
|
| 44 |
|
| 45 |
@tool
|
| 46 |
+
def execute_python(code: str, config: RunnableConfig):
|
| 47 |
"""Execute Python code in an IPython interactiveshell and return the output.
|
| 48 |
The returned artifacts (if present) are automatically rendered in the UI and visible to the user.
|
| 49 |
Args:
|
|
|
|
| 57 |
'Content-Type': 'application/json'
|
| 58 |
}
|
| 59 |
data = {
|
| 60 |
+
"session_token": config.configurable.get("thread_id", "test"),
|
| 61 |
"code": code
|
| 62 |
}
|
| 63 |
response = requests.post(
|
|
|
|
| 72 |
response_json = response.json()
|
| 73 |
return f"data: {json.dumps(response_json)} \ndata:"
|
| 74 |
|
|
|
|
| 75 |
memory = MemorySaver()
|
| 76 |
model = ChatOpenAI(model="gpt-4o-mini", streaming=True)
|
| 77 |
+
prompt = ChatPromptTemplate.from_messages([
|
| 78 |
+
("system", f"You are a Data Visualization assistant.You have access to a jupyter client with access to internet for python code execution.\
|
| 79 |
+
Your taks is to assist users with your data analysis and visualization expertise. Use Plotly for creating visualizations. Today's date is \
|
| 80 |
+
{datetime.now().strftime('%Y-%m-%d')}. The current folder contains the following files: {{collection_files}}"),
|
| 81 |
+
("placeholder", "{messages}"),
|
| 82 |
+
])
|
| 83 |
|
| 84 |
def state_modifier(state) -> list[BaseMessage]:
|
| 85 |
+
collection_files = "None"
|
| 86 |
+
try:
|
| 87 |
+
formatted_prompt = prompt.invoke({
|
| 88 |
+
"collection_files": collection_files,
|
| 89 |
+
"messages": state["messages"]
|
| 90 |
+
})
|
| 91 |
+
print(state["messages"])
|
| 92 |
+
return trim_messages(
|
| 93 |
+
formatted_prompt,
|
| 94 |
+
token_counter=len,
|
| 95 |
+
max_tokens=16000,
|
| 96 |
+
strategy="last",
|
| 97 |
+
start_on="human",
|
| 98 |
+
include_system=True,
|
| 99 |
+
allow_partial=False,
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
except Exception as e:
|
| 103 |
+
print(f"Error in state modifier: {str(e)}")
|
| 104 |
+
return state["messages"]
|
| 105 |
|
| 106 |
# Create the agent with the Python execution tool
|
| 107 |
agent = create_react_agent(
|