pushing changes
All checks were successful
OpenWebUI Discord Bot / Build-and-Push (push) Successful in 1m37s
All checks were successful
OpenWebUI Discord Bot / Build-and-Push (push) Successful in 1m37s
This commit is contained in:
parent
6b5d7849f8
commit
1dd3e50729
@ -11,7 +11,7 @@ RUN apt-get update && apt-get install -y \
|
|||||||
&& rm -rf /var/lib/apt/lists/*
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
# Install required packages
|
# 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 the bot script
|
||||||
COPY /scripts/discordbot.py .
|
COPY /scripts/discordbot.py .
|
||||||
|
@ -7,6 +7,10 @@ import requests
|
|||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from collections import deque
|
from collections import deque
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
import json
|
||||||
|
import datetime
|
||||||
|
import aiohttp
|
||||||
|
from typing import Dict, Any, List
|
||||||
|
|
||||||
# Load environment variables
|
# Load environment variables
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
@ -57,24 +61,55 @@ async def get_chat_history(channel, limit=100):
|
|||||||
messages.append(content)
|
messages.append(content)
|
||||||
return "\n".join(reversed(messages))
|
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):
|
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
|
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."""
|
||||||
text_content = f"##CONTEXT##\n{context}\n##ENDCONTEXT##\n\n{user_message}"
|
|
||||||
messages[0]["content"].append({"type": "text", "text": text_content})
|
|
||||||
|
|
||||||
# Add image content if present
|
messages = [
|
||||||
|
{"role": "system", "content": system_message},
|
||||||
|
{"role": "user", "content": [] if image_urls else user_message}
|
||||||
|
]
|
||||||
|
|
||||||
|
# Handle messages with images differently
|
||||||
if image_urls:
|
if image_urls:
|
||||||
|
content_parts = [{"type": "text", "text": user_message}]
|
||||||
|
|
||||||
for url in image_urls:
|
for url in image_urls:
|
||||||
base64_image = await download_image(url)
|
base64_image = await download_image(url)
|
||||||
if base64_image:
|
if base64_image:
|
||||||
messages[0]["content"].append({
|
content_parts.append({
|
||||||
"type": "image_url",
|
"type": "image_url",
|
||||||
"image_url": {
|
"image_url": {
|
||||||
"url": f"data:image/jpeg;base64,{base64_image}"
|
"url": f"data:image/jpeg;base64,{base64_image}"
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
messages[1]["content"] = content_parts
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response = client.chat.completions.create(
|
response = client.chat.completions.create(
|
||||||
|
Loading…
Reference in New Issue
Block a user