adding V2
All checks were successful
OpenWebUI Discord Bot / Build-and-Push (push) Successful in 1m0s
All checks were successful
OpenWebUI Discord Bot / Build-and-Push (push) Successful in 1m0s
This commit is contained in:
parent
43f40981db
commit
37b363b317
4
v2/.env.example
Normal file
4
v2/.env.example
Normal file
@ -0,0 +1,4 @@
|
||||
DISCORD_TOKEN=your_discord_bot_token
|
||||
OPENAI_API_KEY=your_openai_api_key
|
||||
OPENWEBUI_API_BASE=http://your.api.endpoint/v1
|
||||
MODEL_NAME="Your_Model_Name"
|
94
v2/bot.py
Normal file
94
v2/bot.py
Normal file
@ -0,0 +1,94 @@
|
||||
import os
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
import openai
|
||||
from collections import deque
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# Load environment variables
|
||||
load_dotenv()
|
||||
|
||||
# Get environment variables
|
||||
DISCORD_TOKEN = os.getenv('DISCORD_TOKEN')
|
||||
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
|
||||
OPENWEBUI_API_BASE = os.getenv('OPENWEBUI_API_BASE')
|
||||
MODEL_NAME = os.getenv('MODEL_NAME')
|
||||
|
||||
# Configure OpenAI
|
||||
openai.api_key = OPENAI_API_KEY
|
||||
openai.api_base = OPENWEBUI_API_BASE
|
||||
|
||||
# Initialize Discord bot
|
||||
intents = discord.Intents.default()
|
||||
intents.message_content = True
|
||||
intents.messages = True
|
||||
bot = commands.Bot(command_prefix='!', intents=intents)
|
||||
|
||||
# Message history cache
|
||||
channel_history = {}
|
||||
|
||||
async def get_chat_history(channel, limit=100):
|
||||
messages = []
|
||||
async for message in channel.history(limit=limit):
|
||||
messages.append(f"{message.author.name}: {message.content}")
|
||||
return "\n".join(reversed(messages))
|
||||
|
||||
async def get_ai_response(context, user_message):
|
||||
formatted_prompt = f"##CONTEXT##\n{context}\n##ENDCONTEXT##\n\n{user_message}"
|
||||
|
||||
try:
|
||||
response = openai.ChatCompletion.create(
|
||||
model=MODEL_NAME,
|
||||
messages=[
|
||||
{"role": "user", "content": formatted_prompt}
|
||||
]
|
||||
)
|
||||
return response.choices[0].message.content
|
||||
except Exception as e:
|
||||
return f"Error: {str(e)}"
|
||||
|
||||
@bot.event
|
||||
async def on_ready():
|
||||
print(f'{bot.user} has connected to Discord!')
|
||||
|
||||
@bot.event
|
||||
async def on_message(message):
|
||||
# Ignore messages from the bot itself
|
||||
if message.author == bot.user:
|
||||
return
|
||||
|
||||
should_respond = False
|
||||
|
||||
# Check if bot was mentioned
|
||||
if bot.user in message.mentions:
|
||||
should_respond = True
|
||||
|
||||
# Check if message is a DM
|
||||
if isinstance(message.channel, discord.DMChannel):
|
||||
should_respond = True
|
||||
|
||||
if should_respond:
|
||||
async with message.channel.typing():
|
||||
# Get chat history
|
||||
history = await get_chat_history(message.channel)
|
||||
|
||||
# Remove bot mention from the message
|
||||
user_message = message.content.replace(f'<@{bot.user.id}>', '').strip()
|
||||
|
||||
# Get AI response
|
||||
response = await get_ai_response(history, user_message)
|
||||
|
||||
# Send response
|
||||
await message.reply(response)
|
||||
|
||||
await bot.process_commands(message)
|
||||
|
||||
def main():
|
||||
if not all([DISCORD_TOKEN, OPENAI_API_KEY, OPENWEBUI_API_BASE, MODEL_NAME]):
|
||||
print("Error: Missing required environment variables")
|
||||
return
|
||||
|
||||
bot.run(DISCORD_TOKEN)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
3
v2/requirements.txt
Normal file
3
v2/requirements.txt
Normal file
@ -0,0 +1,3 @@
|
||||
discord.py
|
||||
openai
|
||||
python-dotenv
|
Loading…
Reference in New Issue
Block a user