From 1dd3e5072910bcea7a77e2021e62ed8315c22a6a Mon Sep 17 00:00:00 2001 From: Josh Knapp Date: Mon, 6 Jan 2025 19:44:47 -0800 Subject: [PATCH] pushing changes --- Dockerfile | 2 +- scripts/discordbot.py | 47 +++++++++++++++++++++++++++++++++++++------ 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/Dockerfile b/Dockerfile index 1bcb9f0..39bd0fd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,7 +11,7 @@ RUN apt-get update && apt-get install -y \ && rm -rf /var/lib/apt/lists/* # Install required packages -RUN pip install --no-cache-dir discord.py python-dotenv openai requests +RUN pip install --no-cache-dir discord.py python-dotenv openai requests asyncio # Copy the bot script COPY /scripts/discordbot.py . diff --git a/scripts/discordbot.py b/scripts/discordbot.py index cf24c9e..4c2fd06 100644 --- a/scripts/discordbot.py +++ b/scripts/discordbot.py @@ -7,6 +7,10 @@ import requests from io import BytesIO from collections import deque from dotenv import load_dotenv +import json +import datetime +import aiohttp +from typing import Dict, Any, List # Load environment variables load_dotenv() @@ -57,24 +61,55 @@ async def get_chat_history(channel, limit=100): messages.append(content) return "\n".join(reversed(messages)) +async def get_available_tools() -> List[Dict[str, Any]]: + """Fetch available tools from OpenWebUI API.""" + try: + headers = { + "Content-Type": "application/json", + "Authorization": f"Bearer {OPENAI_API_KEY}" + } + + async with aiohttp.ClientSession() as session: + async with session.get( + f"{OPENWEBUI_API_BASE}/v1/tools/list", + headers=headers + ) as response: + if response.status == 200: + tools = await response.json() + return tools + else: + print(f"Error fetching tools: {await response.text()}") + return [] + except Exception as e: + print(f"Error fetching tools: {str(e)}") + return [] + async def get_ai_response(context, user_message, image_urls=None): - messages = [{"role": "user", "content": []}] + # Fetch available tools + tools = await get_available_tools() + tools_json = json.dumps(tools, indent=2) - # Add text content - text_content = f"##CONTEXT##\n{context}\n##ENDCONTEXT##\n\n{user_message}" - messages[0]["content"].append({"type": "text", "text": text_content}) + system_message = f"\"\"\"Previous conversation context:{context}\nAvailable Tools: {tools_json}\nReturn an empty string if no tools match the query." + """If a function tool matches, construct and return a JSON object in the format {"name": "functionName", "parameters": {"requiredFunctionParamKey": "requiredFunctionParamValue\"}} using the appropriate tool and its parameters. Only return the object and limit the response to the JSON object without additional text.""" + + messages = [ + {"role": "system", "content": system_message}, + {"role": "user", "content": [] if image_urls else user_message} + ] - # Add image content if present + # Handle messages with images differently if image_urls: + content_parts = [{"type": "text", "text": user_message}] + for url in image_urls: base64_image = await download_image(url) if base64_image: - messages[0]["content"].append({ + content_parts.append({ "type": "image_url", "image_url": { "url": f"data:image/jpeg;base64,{base64_image}" } }) + messages[1]["content"] = content_parts try: response = client.chat.completions.create(