hibobmaster / matrix_chatgpt_bot

A simple matrix bot that supports image generation and chatting using ChatGPT, Langchain
https://matrix.to/#/#public:matrix.qqs.tw
MIT License
75 stars 13 forks source link

Sometimes, there is no response to prompts, #47

Closed mwnu closed 2 months ago

mwnu commented 2 months ago

And errors only appear in the logs after a reboot: image

  1. File and Line Numbers:

    • File "/app/src/main.py", line 125: The error occurs at line 125 in main.py, where asyncio.run(main()) is called to execute the main function.
    • File "/app/src/main.py", line 120: At line 120 in the main function, there is an await sync_task statement waiting for an asynchronous task to complete.
  2. Error Type:

    • asyncio.exceptions.CancelledError: This is an exception specific to asyncio, indicating that an asynchronous task was cancelled.
  3. Context of the Error:

    • The error occurs within the sync_forever function, where the program awaits a response (await self.run_response_callbacks([await response])). This function is called in a loop to continuously sync data.
    • The error happens in the get method of asyncio/queues.py, indicating that the task was cancelled while attempting to retrieve it from a queue.
  4. Timestamps in the Log:

    • The error happens at 2024-04-24 16:04:56.
    • Shortly after, at 2024-04-24 16:05:02, the log indicates "matrix chatgpt bot start.....", suggesting that despite the error, the program might be trying to restart or continue execution.
  5. Possible Causes:

    • CancelledError typically results from parts of the program explicitly cancelling a task, or from tasks not being properly completed during shutdown/restart processes.
    • In this case, the cancellation might be triggered by the Bot closed! event (indicated by INFO - Bot closed! in the log), initiating some cleanup process that cancels the ongoing asynchronous tasks.
  6. Handling:

    • Review how asynchronous tasks are managed in the program to ensure they are properly completed or orderly cancelled when the program attempts to shut down.
    • Consider adding appropriate error handling logic in the code to manage CancelledError, such as using try...except blocks to catch this exception and perform necessary cleanup.
hibobmaster commented 2 months ago

This error can be safely ignored.

If you stuck at bot starting, try restart it. image

mwnu commented 2 months ago

This error can be safely ignored.

If you stuck at bot starting, try restart it. image

I've figured out where the problem lies. The format of the submission events sent from different clients is not the same. I'm not sure why, but this is a message sent from Element Android tagged with @bot, which appears as a rich text link (even though I've turned off rich text input on the mobile app). The bot does not respond to this message:

"content": {
    "body": "GPT:  hello - element android",
    "format": "org.matrix.custom.html",
    "formatted_body": "<a href=\"https://matrix.to/#/@gptbot2:xxx.xyz\">GPT</a>:  hello - element android",
    "msgtype": "m.text"
  },

The messages I send from Element PC to @bot are normal submission events, and the bot will respond accordingly:

"content": {
    "body": "GPT: hello - element desktop",
    "format": "org.matrix.custom.html",
    "formatted_body": "<a href=\"https://matrix.to/#/@gptbot2:xxx.xyz\">GPT</a>: hello - element desktop",
    "m.mentions": {
      "user_ids": [
        "@gptbot2:xxx.xyz"
      ]
    },
    "msgtype": "m.text"
  },

I suggest adding a customizable command (such as -gpt prompt) in addition to the @ mention to trigger replies within a thread.

hibobmaster commented 2 months ago

Thank you for providing this debug information, i will find a solution.

Edit: i can reproduce it on my device.

hibobmaster commented 2 months ago

https://github.com/hibobmaster/matrix_chatgpt_bot/commit/b9a5884fd11246a8756a7d72baaa72efea0778b0

1713954711777 1713954711772

hibobmaster commented 2 months ago

Try image: hibobmaster/matrixchatgptbot:sha-b9a5884fd11246a8756a7d72baaa72efea0778b0

mwnu commented 2 months ago

sha-b9a5884fd11246a8756a7d72baaa72efea0778b0

Try image: hibobmaster/matrixchatgptbot:sha-b9a5884fd11246a8756a7d72baaa72efea0778b0 尝试镜像: hibobmaster/matrixchatgptbot:sha-b9a5884fd11246a8756a7d72baaa72efea0778b0

Success! Thank you.👏

hibobmaster commented 2 months ago

https://github.com/hibobmaster/matrix_chatgpt_bot/releases/tag/v1.7.1