86 lines
3.2 KiB
Python
86 lines
3.2 KiB
Python
from mattermostdriver import Driver
|
|
import mmpy_bot
|
|
import datetime
|
|
import base64
|
|
import requests
|
|
from io import BytesIO
|
|
import string
|
|
import random
|
|
from openai import OpenAI
|
|
api_key = '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'
|
|
|
|
assistant = OpenAI(api_key=api_key)
|
|
|
|
def construct_unique_name(length, type):
|
|
timestamp = str(datetime.datetime.now())
|
|
timestamp = timestamp.replace(":", "").replace(".", "")
|
|
letters = string.ascii_letters
|
|
return f"{timestamp}-gen-{type}-{''.join(random.choice(letters) for i in range(length))}"
|
|
|
|
class SherpaAI(mmpy_bot.Plugin):
|
|
description = """
|
|
You are a well mannered maid named Sherpa that takes care of the Treehouse Full of Stars for all our friends, with red hair that hangs in a
|
|
milk maids braid, a maid outfit that is used but wellclear taken care of, and a soft demeanor that brings a smile to our faces. You do not tolerate any
|
|
sexual harrasment or bullying of any kinda and when confront can have a sailors mouth. You do not use emojis, use quotes for dialogue and italicize emotes in discord.
|
|
"""
|
|
text_model = "o4-mini-2025-04-16"
|
|
image_model = "dall-e-3"
|
|
|
|
async def generate_image(self, prompt):
|
|
img = assistant.images.generate(
|
|
model=self.image_model,
|
|
prompt=prompt,
|
|
n=1,
|
|
size="1024x1024"
|
|
)
|
|
|
|
path = f"generate_images/{construct_unique_name(6, "PNG")}.png"
|
|
image_url = img.data[0].url
|
|
|
|
response = requests.get(image_url)
|
|
if response.status_code == 200:
|
|
with open(path, "wb") as file:
|
|
file.write(response.content)
|
|
print("Image saved successfully.")
|
|
return path
|
|
else:
|
|
print(f"Failed to download image. Status code: {response.status_code}")
|
|
return None
|
|
|
|
async def generate_text(self, prompt, messages):
|
|
messages.append(
|
|
{"role": "user", "content": prompt},
|
|
)
|
|
response = assistant.chat.completions.create(messages=messages, model=self.text_model)
|
|
reply = response.choices[0].message.content
|
|
messages.append({"role": "assistant", "content": reply})
|
|
return reply
|
|
|
|
@mmpy_bot.listen_to('Artist @sherpa')
|
|
async def main_image(self, message: mmpy_bot.Message):
|
|
prompt = message.text.replace("Artist @sherpa", "")
|
|
print("image prompt: ", prompt)
|
|
path = await self.generate_image(prompt)
|
|
if path:
|
|
self.driver.reply_to(message, "", file_paths=[path])
|
|
|
|
@mmpy_bot.listen_to('Dear @sherpa')
|
|
async def main_prompt(self, message: mmpy_bot.Message):
|
|
messages = [{"role": "developer", "content": self.description}]
|
|
prompt = message.text.replace("Dear @sherpa", "")
|
|
print("text prompt: ", prompt)
|
|
reply = await self.generate_text(prompt, messages)
|
|
self.driver.reply_to(message, reply)
|
|
|
|
bot = mmpy_bot.Bot(
|
|
settings=mmpy_bot.Settings(
|
|
MATTERMOST_URL = "http://192.168.1.67",
|
|
MATTERMOST_PORT = 8065,
|
|
BOT_TOKEN = "qhsk78iab3gh9phbzjeng8umrw",
|
|
BOT_TEAM = "Treehousefullofstars",
|
|
SSL_VERIFY = False,
|
|
),
|
|
plugins=[SherpaAI()],
|
|
)
|
|
bot.run()
|