From 7f356751bbb550f29fd459d09204b2f985eda92d Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Thu, 2 Jan 2025 21:33:39 -0800 Subject: [PATCH] refac --- README.md | 13 +++++++ bot/__init__.py => __init__.py | 0 bot/env.py => env.py | 2 +- bot/main.py => examples/ollama.py | 0 main.py | 61 +++++++++++++++++++++++++++++++ bot/utils.py => utils.py | 0 6 files changed, 75 insertions(+), 1 deletion(-) rename bot/__init__.py => __init__.py (100%) rename bot/env.py => env.py (88%) rename bot/main.py => examples/ollama.py (100%) create mode 100644 main.py rename bot/utils.py => utils.py (100%) diff --git a/README.md b/README.md index 31319e2..beb8ebf 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,19 @@ This repository provides an experimental boilerplate for building bots compatibl - **Extensible Framework**: Designed as a foundation for further development, with plans to enhance APIs, developer tooling, and usability. - **Asynchronous Communication**: Leverages Open WebUI Channels for event-driven workflows. +## 🛠️ Getting Started with Examples +This repository includes an `/examples` folder containing runnable example bots that demonstrate basic functionality. + +To run an example, execute the corresponding module using the `-m` flag in Python. For example, to run the `ollama` example: + +```bash +python -m examples.ollama +``` + +> **Note**: Ensure that your current working directory (PWD) is the root of this repository when running examples, as this is required for proper execution. + +Replace `ollama` in the command above with the specific example you’d like to execute from the `/examples` folder. + ## 🚧 Disclaimer This project is an early-stage proof of concept. **APIs will break** and existing functionality may change as Open WebUI evolves to include native bot support. This repository is not production-ready and primarily serves experimental and exploratory purposes. diff --git a/bot/__init__.py b/__init__.py similarity index 100% rename from bot/__init__.py rename to __init__.py diff --git a/bot/env.py b/env.py similarity index 88% rename from bot/env.py rename to env.py index 4cc234d..14be0dd 100644 --- a/bot/env.py +++ b/env.py @@ -3,7 +3,7 @@ import os try: from dotenv import load_dotenv - load_dotenv("../.env") + load_dotenv() except ImportError: print("dotenv not installed, skipping...") diff --git a/bot/main.py b/examples/ollama.py similarity index 100% rename from bot/main.py rename to examples/ollama.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..ed1d5e3 --- /dev/null +++ b/main.py @@ -0,0 +1,61 @@ +import asyncio +import socketio +from env import WEBUI_URL, TOKEN +from utils import send_message, send_typing + +# Create an asynchronous Socket.IO client instance +sio = socketio.AsyncClient(logger=False, engineio_logger=False) + + +# Event handlers +@sio.event +async def connect(): + print("Connected!") + + +@sio.event +async def disconnect(): + print("Disconnected from the server!") + + +# Define a function to handle channel events +def events(user_id): + @sio.on("channel-events") + async def channel_events(data): + if data["user"]["id"] == user_id: + # Ignore events from the bot itself + return + + if data["data"]["type"] == "message": + print(f'{data["user"]["name"]}: {data["data"]["data"]["content"]}') + await send_typing(sio, data["channel_id"]) + await asyncio.sleep(1) # Simulate a delay + await send_message(data["channel_id"], "Pong!") + + +# Define an async function for the main workflow +async def main(): + try: + print(f"Connecting to {WEBUI_URL}...") + await sio.connect( + WEBUI_URL, socketio_path="/ws/socket.io", transports=["websocket"] + ) + print("Connection established!") + except Exception as e: + print(f"Failed to connect: {e}") + return + + # Callback function for user-join + async def join_callback(data): + events(data["id"]) # Attach the event handlers dynamically + + # Authenticate with the server + await sio.emit("user-join", {"auth": {"token": TOKEN}}, callback=join_callback) + + # Wait indefinitely to keep the connection open + await sio.wait() + + +# Actually run the async `main` function using `asyncio` +if __name__ == "__main__": + asyncio.run(main()) diff --git a/bot/utils.py b/utils.py similarity index 100% rename from bot/utils.py rename to utils.py