microsoft / autogen

A programming framework for agentic AI 🤖
https://microsoft.github.io/autogen/
Creative Commons Attribution 4.0 International
34.85k stars 5.05k forks source link

[Bug]: Websocket demo not working #3146

Open saptarshi091 opened 4 months ago

saptarshi091 commented 4 months ago

Describe the bug

I ran the code in https://microsoft.github.io/autogen/docs/notebooks/agentchat_websockets/ without HTML i.e. the first 2 blocks and the agent keeps on producing this

ConnectionClosedOK                        Traceback (most recent call last)
Cell In[14], line 63
     60 websocket.send("Check out the weather in Paris and write a poem about it.")
     62 while True:
---> 63     message = websocket.recv()
     64     message = message.decode("utf-8") if isinstance(message, bytes) else message
     66     print(message, end="", flush=True)

File ~/Desktop/multi-agent-assistant-poc/env/lib/python3.11/site-packages/websockets/sync/connection.py:201, in Connection.recv(self, timeout)
    199     return self.recv_messages.get(timeout)
    200 except EOFError:
--> 201     raise self.protocol.close_exc from self.recv_events_exc
    202 except RuntimeError:
    203     raise RuntimeError(
    204         "cannot call recv while another thread "
    205         "is already running recv or recv_streaming"
    206     ) from None

ConnectionClosedOK: received 1000 (OK); then sent 1000 (OK)

Steps to reproduce

from datetime import datetime

def on_connect(iostream: IOWebsockets) -> None:
    print(f" - on_connect(): Connected to client using IOWebsockets {iostream}", flush=True)

    print(" - on_connect(): Receiving message from client.", flush=True)

    # 1. Receive Initial Message
    initial_msg = iostream.input()

    # 2. Instantiate ConversableAgent
    agent = autogen.ConversableAgent(
        name="chatbot",
        system_message="Complete a task given to you and reply TERMINATE when the task is done. If asked about the weather, use tool 'weather_forecast(city)' to get the weather forecast for a city.",
        llm_config= <MY CONFIGURATION>,
                    "stream": True},
    )

    # 3. Define UserProxyAgent
    user_proxy = autogen.UserProxyAgent(
        name="user_proxy",
        system_message="A proxy for the user.",
        is_termination_msg=lambda x: x.get("content", "") and x.get("content", "").rstrip().endswith("TERMINATE"),
        human_input_mode="NEVER",
        max_consecutive_auto_reply=10,
        code_execution_config=False,
    )

    # 4. Define Agent-specific Functions
    def weather_forecast(city: str) -> str:
        return f"The weather forecast for {city} at {datetime.now()} is sunny."

    autogen.register_function(
        weather_forecast, caller=agent, executor=user_proxy, description="Weather forecast for a city"
    )

    # 5. Initiate conversation
    print(
        f" - on_connect(): Initiating chat with agent {agent} using message '{initial_msg}'",
        flush=True,
    )
    user_proxy.initiate_chat(  # noqa: F704
        agent,
        message=initial_msg,
    )

with IOWebsockets.run_server_in_thread(on_connect=on_connect, port=8765) as uri:
    print(f" - test_setup() with websocket server running on {uri}.", flush=True)

    with ws_connect(uri) as websocket:
        print(f" - Connected to server on {uri}", flush=True)

        print(" - Sending message to server.", flush=True)
        # websocket.send("2+2=?")
        websocket.send("Check out the weather in Paris and write a poem about it.")

        while True:
            message = websocket.recv()
            message = message.decode("utf-8") if isinstance(message, bytes) else message

            print(message, end="", flush=True)

            if "TERMINATE" in message:
                print()
                print(" - Received TERMINATE message. Exiting.", flush=True)
                break

Model Used

gpt-35-turbo-0125

Expected Behavior

No response

Screenshots and logs

No response

Additional Information

No response

fanghuafan commented 2 months ago

Is it solved?