Add Base OpenAI response agent
This commit is contained in:
parent
7a92e602ec
commit
fe5f5e2d3a
|
@ -0,0 +1,4 @@
|
|||
import envyaml
|
||||
import os
|
||||
|
||||
config = envyaml.EnvYAML(os.environ.get('CONFIG_PATH', 'config.yaml'))
|
|
@ -0,0 +1,10 @@
|
|||
discord:
|
||||
token: "${DISCORD_TOKEN}"
|
||||
prefix: '!'
|
||||
status: "Shitty memes on your mom's TV"
|
||||
|
||||
nlp:
|
||||
source: "OpenAI"
|
||||
token: "${OPENAI_TOKEN}"
|
||||
model: "gpt-3.5-turbo"
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
import openai
|
||||
import discord
|
||||
from config import config
|
||||
import nlp
|
||||
import logging
|
||||
|
||||
completion = nlp.CompletionOpenAI(config["nlp.token"])
|
||||
|
||||
async def parser(client, message: str):
|
||||
"""
|
||||
Parse given natural language message, and come up with a reply
|
||||
"""
|
||||
|
||||
reply = completion.complete(message.content)
|
||||
await message.channel.send(reply)
|
||||
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
#!/usr/bin/python3
|
||||
import discord
|
||||
import os
|
||||
import sys
|
||||
import logging
|
||||
|
||||
from config import config
|
||||
from language_parser import parser as lang_parser
|
||||
#from command_parser import parser as cmd_parser
|
||||
|
||||
intents = discord.Intents.default()
|
||||
client = discord.Client(intents=intents)
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
@client.event
|
||||
async def on_ready():
|
||||
logging.info("Logged in as {0.user}".format(client))
|
||||
# Set bot status
|
||||
await client.change_presence(activity=discord.Game(name=config["discord.status"]))
|
||||
|
||||
@client.event
|
||||
async def on_message(message):
|
||||
print(message)
|
||||
logging.info(f"Message received : {message.content}")
|
||||
if message.author == client.user:
|
||||
return
|
||||
if len(message.content) == 0: # Ignore empty messages
|
||||
return
|
||||
#if message.content.startswith(config["discord.prefix"]):
|
||||
# await cmd_parser(client, message)
|
||||
else:
|
||||
await lang_parser(client, message)
|
||||
|
||||
def main() -> int:
|
||||
logging.info("Starting bot...")
|
||||
client.run(config["discord.token"])
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
|
@ -0,0 +1,17 @@
|
|||
class CompletionMeta(type):
|
||||
"""
|
||||
The meta class for completion interface
|
||||
"""
|
||||
|
||||
class Completion():
|
||||
def complete(self, message: str) -> str:
|
||||
"""
|
||||
Perform a text completion using the language model
|
||||
"""
|
||||
pass
|
||||
|
||||
def getModelName(self) -> str:
|
||||
"""
|
||||
Return the model name
|
||||
"""
|
||||
pass
|
|
@ -0,0 +1,23 @@
|
|||
import openai
|
||||
from .Completion import Completion
|
||||
|
||||
class CompletionOpenAI(Completion):
|
||||
# Constructor
|
||||
def __init__(self, token: str, model: str = "gpt-3.5-turbo"):
|
||||
self.token = token
|
||||
self.model = model
|
||||
openai.api_key = self.token
|
||||
|
||||
def complete(self, message: str) -> str:
|
||||
chat_completion = openai.ChatCompletion.create(
|
||||
model=self.model,
|
||||
messages=[
|
||||
{
|
||||
"role": "user",
|
||||
"content": message
|
||||
}
|
||||
])
|
||||
return chat_completion.choices[0].message.content
|
||||
|
||||
def getModelName(self) -> str:
|
||||
return self.model
|
|
@ -0,0 +1,11 @@
|
|||
from .Completion import Completion
|
||||
from .CompletionOpenAI import CompletionOpenAI
|
||||
|
||||
def getCompletionEngine(config) -> Completion:
|
||||
source = config["nlp.source"]
|
||||
if source == "OpenAI":
|
||||
token = config["nlp.token"]
|
||||
model = config["nlp.model"]
|
||||
return CompletionOpenAI(token, model=model)
|
||||
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
aiohttp==3.8.4
|
||||
aiosignal==1.3.1
|
||||
async-timeout==4.0.2
|
||||
attrs==23.1.0
|
||||
charset-normalizer==3.1.0
|
||||
discord==2.2.3
|
||||
discord.py==2.2.3
|
||||
frozenlist==1.3.3
|
||||
idna==3.4
|
||||
multidict==6.0.4
|
||||
yarl==1.9.2
|
Loading…
Reference in New Issue