I am using websocket to communicate with an AI agent and based on the user prompt the agent shall perform certain function calls on the client side.
async def async_Task1():
"""Run Task1 and interact with WebSocket client."""
if websocket_client is None:
print("WebSocket client is not connected.")
return "Task not performed"
try:
print("Sending Task1 request")
await websocket_client.send(json.dumps({"type": "action_call", "content": "Task1"}))
# Waiting for a response with a timeout
print("Waiting for Task1 response")
try:
response = await asyncio.wait_for(websocket_client.recv(), timeout=5) # Adjust timeout as needed
response_data = json.loads(response)
print(f"Received response from WebSocket server: {response_data}")
return response_data.get("content", "No content in response")
except asyncio.TimeoutError:
print("Timeout waiting for response")
return "Timeout waiting for response"
except Exception as e:
print(f"Error in Task1: {e}")
return "Error in Task1"
@tool
def Task1():
"""Runs Task1
No args
"""
return asyncio.run(async_Task1())