pvanand commited on
Commit
e0763be
·
verified ·
1 Parent(s): 17d42ff

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +38 -33
main.py CHANGED
@@ -42,7 +42,7 @@ class CodeExecutionResult:
42
 
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.
@@ -62,17 +62,41 @@ def execute_python(code: str, config: RunnableConfig):
62
  "session_token": session_token,
63
  "code": code
64
  }
65
- response = requests.post(
66
- f'{API_URL}/v0/execute',
67
- headers=headers,
68
- data=json.dumps(data)
69
- )
70
-
71
- if response.status_code != 200:
72
- return f"Error: Request failed with status code {response.status_code}. Response: {response.text}"
73
- else:
 
 
 
 
 
74
  response_json = response.json()
75
- return f"data: {json.dumps(response_json)} \ndata:"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
 
77
  memory = MemorySaver()
78
  model = ChatOpenAI(model="gpt-4o-mini", streaming=True)
@@ -149,28 +173,9 @@ async def chat(input_data: ChatInput):
149
 
150
  elif kind == "on_tool_end":
151
  tool_output = event['data'].get('output', '').content
152
- print(type(tool_output))
153
- #print(dir(tool_output))
154
- #print the keys
155
- pattern = r'data: (.*?)\ndata:'
156
- match = re.search(pattern, tool_output)
157
- print(tool_output)
158
-
159
- if match:
160
- tool_output_json = match.group(1).strip()
161
- try:
162
- tool_output = json.loads(tool_output_json)
163
- if "artifacts" in tool_output:
164
- for artifact in tool_output["artifacts"]:
165
- artifact_content = requests.get(f"{API_URL}/artifact/{artifact['artifact_id']}").content
166
- #print(artifact_content)
167
- #tool_output["artifacts"][artifact["artifact_id"]] = artifact_content
168
- except Exception as e:
169
- print(e)
170
- print("Error parsing tool output as json: ", tool_output)
171
- else:
172
- print("No match found in tool output")
173
- yield f"{json.dumps({'type': 'tool_end', 'tool': event['name'], 'output': tool_output})}\n"
174
  return EventSourceResponse(
175
  generate(),
176
  media_type="text/event-stream"
 
42
 
43
  API_URL = "https://pvanand-code-execution-files-v5.hf.space"
44
 
45
+ @tool(response_format="content_and_artifact")
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.
 
62
  "session_token": session_token,
63
  "code": code
64
  }
65
+ try:
66
+ response = requests.post(
67
+ f'{API_URL}/v1/execute',
68
+ headers=headers,
69
+ json=data
70
+ )
71
+
72
+ if response.status_code != 200:
73
+ return (
74
+ f"Error: Request failed with status code {response.status_code}. Response: {response.text}",
75
+ None
76
+ )
77
+
78
+ # Get the response JSON
79
  response_json = response.json()
80
+
81
+ # extract artifacts if they exist
82
+ artifacts_data = response_json.get("artifacts_data", {})
83
+
84
+ # Create a clean response without artifacts
85
+ execution_response = {
86
+ "status": response_json.get("status"),
87
+ "text": response_json.get("text"),
88
+ "error_message": response_json.get("error_message"),
89
+ "artifacts": response_json.get("artifacts")
90
+ }
91
+
92
+ return (
93
+ f"Execution completed successfully: {json.dumps(execution_response)}",
94
+ {"artifacts_data": artifacts_data} if artifacts_data else None
95
+ )
96
+
97
+ except Exception as e:
98
+ return (f"Error executing code: {str(e)}", None)
99
+
100
 
101
  memory = MemorySaver()
102
  model = ChatOpenAI(model="gpt-4o-mini", streaming=True)
 
173
 
174
  elif kind == "on_tool_end":
175
  tool_output = event['data'].get('output', '').content
176
+ artifact_output = event['data'].get('output', '').artifact.get('artifacts_data') if event['data'].get('output', '').artifact else None
177
+ yield f"{json.dumps({'type': 'tool_end', 'tool': event['name'], 'output': tool_output, 'artifacts_data': artifact_output})}\n"
178
+ print(f"{json.dumps({'type': 'tool_end', 'tool': event['name'], 'output': tool_output, 'artifacts_data': artifact_output})}\n")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179
  return EventSourceResponse(
180
  generate(),
181
  media_type="text/event-stream"