Skip to content

Commit

Permalink
Add minimal OpenAI integration
Browse files Browse the repository at this point in the history
  • Loading branch information
rce committed May 5, 2023
1 parent c3ef755 commit 53d56d5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions run_bot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function main {
--env WITHINGS_CLIENT_ID \
--env WITHINGS_CLIENT_SECRET \
--env WITHINGS_CALLBACK_URL="http://localhost:8080/auth/withings/callback" \
--env OPENAI_KEY \
--env KANSALLISGALLERIA_API_KEY \
lemon

Expand Down
49 changes: 49 additions & 0 deletions src/openai.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import aiohttp
import os

import http_util
import logger
import retry

log = logger.get("OPENAI")

OPENAI_KEY = os.environ.get("OPENAI_KEY", None)
AUTH_HEADER = {"Authorization": "Bearer {0}".format(OPENAI_KEY)}

def register(client):
if OPENAI_KEY is None:
log.info("OPENAI_KEY not defined, not enabling feature")
return {}

return {
'openai': cmd_openai,
}

async def cmd_openai(client, message, arg):
try:
result = await chat_completions({
"model": "gpt-3.5-turbo",
"messages": [
{"role": "user", "content": arg}
]
})
response = result["choices"][0]["message"]["content"]
await message.channel.send(response)
except Exception:
await util.log_exception(log)
await message.channel.send("Something went wrong, Tommi pls fix")


# https://platform.openai.com/docs/api-reference/chat/create
async def chat_completions(payload):
response = await _call_api("/v1/chat/completions", json_body=payload)
return await response.json()

@retry.on_any_exception(max_attempts = 1, init_delay = 1, max_delay = 30)
async def _call_api(path, json_body=None, query=None):
url = "https://api.openai.com{0}{1}".format(path, http_util.make_query_string(query))
async with aiohttp.ClientSession() as session:
for ratelimit_delay in retry.jitter(retry.exponential(1, 128)):
response = await session.post(url, headers=AUTH_HEADER, json=json_body)
log.info("%s %s %s %s", response.method, response.url, response.status, await response.text())
return response
3 changes: 2 additions & 1 deletion src/run_lemon_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import kansallisgalleria
import pasta
import signal
import openai
import bot_replies

signal.signal(signal.SIGINT, signal.SIG_DFL)
Expand Down Expand Up @@ -94,7 +95,7 @@ async def main():

for module in [casino, sqlcommands, osu, feed, reminder, youtube, lan, steam, anssicommands, trophies, laiva,
faceit_main, muutto, statistics, status, emojicommands, lossimpsonquotes, withings,
groom, shrek, ence_matches, mememaker, kansallisgalleria, pasta]:
groom, shrek, ence_matches, mememaker, kansallisgalleria, pasta, openai]:
commands.update(module.register(client))

try:
Expand Down

0 comments on commit 53d56d5

Please sign in to comment.