Skip to content

Commit

Permalink
Truncate chatgpt response if over 2000 characters
Browse files Browse the repository at this point in the history
  • Loading branch information
Skylark95 committed Jun 15, 2024
1 parent ba2d787 commit 3d56510
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
18 changes: 10 additions & 8 deletions chatgpt/chatgpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async def openai_api_key(self):
await self.bot.set_shared_api_tokens("openai", api_key=openai_api_key)
await self.config.openai_api_key.set(None)
return openai_api_key

@commands.Cog.listener()
async def on_message(self, message: Message):
if message.author.bot:
Expand Down Expand Up @@ -82,6 +82,8 @@ async def do_chatgpt(self, ctx: commands.Context, message: str = None):
messages=messages,
max_tokens=max_tokens
)
if len(reply) > 2000:
reply = reply[:1997] + "..."
await ctx.send(
content=reply,
reference=ctx.message
Expand All @@ -101,7 +103,7 @@ async def build_messages(self, ctx: commands.Context, messages: List[Message], m
await self.build_messages(ctx, messages, message.reference.resolved)

async def call_api(self, messages, model: str, api_key: str, max_tokens: int):
try:
try:
if self.client == None:
self.client = OpenAI(api_key=api_key)
self.client.api_key = api_key
Expand All @@ -126,7 +128,7 @@ async def call_api(self, messages, model: str, api_key: str, max_tokens: int):
@checks.is_owner()
async def getchatgptmodel(self, ctx: commands.Context):
"""Get the model for ChatGPT.
Defaults to `gpt-3.5-turbo` See https://platform.openai.com/docs/models/gpt-3-5 for a list of models."""
model = await self.config.model()
await ctx.send(f"ChatGPT model set to `{model}`")
Expand All @@ -135,7 +137,7 @@ async def getchatgptmodel(self, ctx: commands.Context):
@checks.is_owner()
async def setchatgptmodel(self, ctx: commands.Context, model: str):
"""Set the model for ChatGPT.
Defaults to `gpt-3.5-turbo` See https://platform.openai.com/docs/models/gpt-3-5 for a list of models."""
await self.config.model.set(model)
await ctx.send("ChatGPT model set.")
Expand All @@ -144,7 +146,7 @@ async def setchatgptmodel(self, ctx: commands.Context, model: str):
@checks.is_owner()
async def getchatgpttokens(self, ctx: commands.Context):
"""Get the maximum number of tokens for ChatGPT to generate.
Defaults to `400` See https://platform.openai.com/tokenizer for more details."""
model = await self.config.max_tokens()
await ctx.send(f"ChatGPT maximum number of tokens set to `{model}`")
Expand All @@ -153,7 +155,7 @@ async def getchatgpttokens(self, ctx: commands.Context):
@checks.is_owner()
async def setchatgpttokens(self, ctx: commands.Context, number: str):
"""Set the maximum number of tokens for ChatGPT to generate.
Defaults to `400` See https://platform.openai.com/tokenizer for more details."""
try:
await self.config.max_tokens.set(int(number))
Expand All @@ -165,7 +167,7 @@ async def setchatgpttokens(self, ctx: commands.Context, number: str):
@checks.is_owner()
async def togglechatgptmention(self, ctx: commands.Context):
"""Toggle messages to ChatGPT on mention.
Defaults to `True`."""
mention = not await self.config.mention()
await self.config.mention.set(mention)
Expand All @@ -178,7 +180,7 @@ async def togglechatgptmention(self, ctx: commands.Context):
@checks.is_owner()
async def togglechatgptreply(self, ctx: commands.Context):
"""Toggle messages to ChatGPT on reply.
Defaults to `True`."""
reply = not await self.config.reply()
await self.config.reply.set(reply)
Expand Down
20 changes: 11 additions & 9 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
aiodns==3.0.0
aiohttp==3.9.3
aiohttp-json-rpc==0.13.3
aiosignal==1.3.1
annotated-types==0.7.0
anyio==4.4.0
apsw==3.45.2.0
async-timeout==4.0.3
attrs==23.2.0
Babel==2.14.0
Brotli==1.1.0
cffi==1.15.1
charset-normalizer==3.2.0
certifi==2024.6.2
click==8.1.7
contextlib2==21.6.0
discord.py==2.3.2
distro==1.9.0
frozenlist==1.4.1
h11==0.14.0
httpcore==1.0.5
httpx==0.27.0
idna==3.6
importlib-metadata==6.8.0
Markdown==3.6
markdown-it-py==3.0.0
mdurl==0.1.2
multidict==6.0.5
openai==1.34.0
orjson==3.10.0
packaging==24.0
platformdirs==4.2.0
psutil==5.9.8
pycares==4.3.0
pycparser==2.21
pydantic==2.7.4
pydantic_core==2.18.4
Pygments==2.17.2
python-dateutil==2.9.0.post0
pytz==2023.3.post1
PyYAML==6.0.1
rapidfuzz==3.7.0
Red-Commons==1.0.0
Expand All @@ -37,7 +38,8 @@ Red-Lavalink==0.11.0
rich==13.7.1
schema==0.7.5
six==1.16.0
sniffio==1.3.1
tqdm==4.66.4
typing_extensions==4.10.0
uvloop==0.19.0
yarl==1.9.4
zipp==3.16.2

0 comments on commit 3d56510

Please sign in to comment.