From f0e9053adf6ee8e786c51b3d48aec522c17acc8e Mon Sep 17 00:00:00 2001 From: nex Date: Sun, 12 Nov 2023 23:14:41 +0000 Subject: [PATCH 1/3] Stop being ratelimited --- cogs/other.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/cogs/other.py b/cogs/other.py index 3be259a..51d1fa1 100644 --- a/cogs/other.py +++ b/cogs/other.py @@ -1837,6 +1837,11 @@ class OtherCog(commands.Cog): @commands.max_concurrency(1, commands.BucketType.user, wait=True) async def ollama(self, ctx: commands.Context, *, query: str): """:3""" + msg = await ctx.reply( + embed=discord.Embed( + description='Loading, please wait.' + ) + ) content = None try_hosts = { "127.0.0.1:11434": "localhost", @@ -1900,7 +1905,7 @@ class OtherCog(commands.Cog): ) embed.set_footer(text="Using server {} ({})".format(host, try_hosts.get(host, "Other"))) - msg = await ctx.reply(embed=embed) + await msg.edit(embed=embed) async with httpx.AsyncClient(base_url=f"http://{host}/api", follow_redirects=True) as client: # get models try: @@ -1949,7 +1954,7 @@ class OtherCog(commands.Cog): lines[status] = status embed.description = "\n".join(f"`{k}`: {v}" for k, v in lines.items()) - if (time() - msg.created_at.timestamp()) >= 5: + if (time() - msg.edited_at.timestamp()) >= 5: await msg.edit(embed=embed) embed.title = f"Downloaded {model}!" embed.colour = discord.Colour.green() @@ -1967,7 +1972,7 @@ class OtherCog(commands.Cog): return await msg.edit(embed=embed) embed = discord.Embed( - title=f"{model} says:", + title=f"{model} says (connecting):", description="", colour=discord.Colour.blurple(), timestamp=discord.utils.utcnow() @@ -2000,7 +2005,9 @@ class OtherCog(commands.Cog): return await msg.edit(embed=embed) self.ollama_locks[msg] = asyncio.Event() view = self.OllamaKillSwitchView(ctx, msg) - await msg.edit(view=view) + embed.title = f"{model} says:" + await msg.edit(embed=embed, view=view) + last_edit = time() async for chunk in ollama_stream_reader(response): if "done" not in chunk.keys() or "response" not in chunk.keys(): continue @@ -2015,11 +2022,11 @@ class OtherCog(commands.Cog): icon_url="https://cdn.discordapp.com/emojis/1101463077586735174.gif" ) embed.description += chunk["response"] - last_edit = msg.edited_at.timestamp() if msg.edited_at else msg.created_at.timestamp() - if (time() - last_edit) >= 5 or chunk["done"] is True: + if (time() - last_edit) >= 6: await msg.edit(content=content, embed=embed, view=view) if self.ollama_locks[msg].is_set(): embed.title = embed.title[:-1] + " (Aborted)" + embed.remove_author() embed.colour = discord.Colour.red() return await msg.edit(embed=embed, view=None) if len(embed.description) >= 4000: @@ -2030,6 +2037,7 @@ class OtherCog(commands.Cog): embed.title = embed.title[:-1] + " (Aborted)" embed.colour = discord.Colour.red() embed.description = embed.description[:4096] + embed.remove_author() break else: embed.colour = discord.Colour.green() From b98651e77a480f8899908da00fae3afbb6e36e6b Mon Sep 17 00:00:00 2001 From: nex Date: Sun, 12 Nov 2023 23:17:12 +0000 Subject: [PATCH 2/3] Change loading message --- cogs/other.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cogs/other.py b/cogs/other.py index 51d1fa1..426c90f 100644 --- a/cogs/other.py +++ b/cogs/other.py @@ -1839,7 +1839,7 @@ class OtherCog(commands.Cog): """:3""" msg = await ctx.reply( embed=discord.Embed( - description='Loading, please wait.' + description='Finding an available server, please wait.' ) ) content = None From 3ebea2b28996fe87eda5cb7df8e9001c3a0430fd Mon Sep 17 00:00:00 2001 From: nex Date: Sun, 12 Nov 2023 23:25:17 +0000 Subject: [PATCH 3/3] fix timestamp selection --- cogs/other.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/cogs/other.py b/cogs/other.py index 426c90f..1e52d62 100644 --- a/cogs/other.py +++ b/cogs/other.py @@ -1128,15 +1128,17 @@ class OtherCog(commands.Cog): ) else: parsed_qs = parse_qs(url) - if "t" in parsed_qs and parsed_qs["t"] and parsed_qs["t"][0].isdigit(): + if "t" in parsed_qs and parsed_qs['t']: # Assume is timestamp - timestamp = round(float(parsed_qs["t"][0])) - end_timestamp = None - if len(parsed_qs["t"]) >= 2: - end_timestamp = round(float(parsed_qs["t"][1])) + try: + timestamp, end_timestamp = map(round, map(float, parsed_qs["t"])) if end_timestamp < timestamp: - end_timestamp, timestamp = reversed((end_timestamp, timestamp)) - _end = "to %s" % end_timestamp if len(parsed_qs["t"]) == 2 else "onward" + timestamp, end_timestamp = reversed((timestamp, end_timestamp)) + except ValueError: + timestamp = round(float(parsed_qs["t"][0])) + end_timestamp = None + + _end = "to %s" % end_timestamp if end_timestamp is not None else "onward" embed = discord.Embed( title="Trimming...", description=f"Trimming from {timestamp} seconds {_end}.\nThis may take a while.",