-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
googleai.py
272 lines (246 loc) · 10.8 KB
/
googleai.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
from discord.ext import commands
import discord
from discord import app_commands
import aiohttp
import time
import os
import base64
import json
from util_discord import command_check, description_helper, get_guild_prefix
headers = {'Content-Type': 'application/json'}
def palm_proxy(model: str) -> str:
return f"{os.getenv('PROXY')}/v1beta/models/{model}?key={os.getenv('PALM')}"
def check_response(response_data) -> bool:
return response_data.get("candidates", []) and \
response_data["candidates"][0].get("content", {}).get("parts", []) and \
response_data["candidates"][0]["content"]["parts"][0].get("text", "")
def check_response_palm(response_data) -> bool:
return response_data.get("candidates", []) and \
response_data["candidates"][0].get("output", "")
def get_error(response_data) -> str:
if response_data.get("promptFeedback", {}):
result = "**Error! :(**\n"
for entry in response_data.get("promptFeedback", {}).get("safetyRatings", []):
if entry['probability'] != 'NEGLIGIBLE':
result += f"{entry['category']}: {entry['probability']}\n"
return result
error_message = response_data.get('error', {}).get('message', 'Unknown error')
error_type = response_data.get('errorType', '')
return f"**Error! :(**\n{error_message}" if "error" in response_data else f"**Error! :(**\n{error_type}"
def get_error_palm(response_data) -> str:
if response_data.get('safetyFeedback', []):
result = "**Error! :(**\n"
for entry in response_data.get('safetyFeedback', []):
if entry['rating']['probability'] != 'NEGLIGIBLE':
result += f"{entry['rating']['category']}: {entry['rating']['probability']}\n"
return result
error_message = response_data.get('error', {}).get('message', 'Unknown error')
error_type = response_data.get('errorType', '')
return f"**Error! :(**\n{error_message}" if "error" in response_data else f"**Error! :(**\n{error_type}"
def get_text(response_data) -> str:
# with open('gemini_response.json', 'w') as json_file:
# json.dump(response_data, json_file, indent=4)
# print(f"Response saved to 'gemini_response.json'")
if check_response(response_data):
return response_data["candidates"][0]["content"]["parts"][0]["text"]
else:
return get_error(response_data)
def get_text_palm(response_data) -> str:
# with open('gemini_response.json', 'w') as json_file:
# json.dump(response_data, json_file, indent=4)
# print(f"Response saved to 'gemini_response.json'")
if check_response_palm(response_data):
return response_data["candidates"][0]["output"]
else:
return get_error_palm(response_data)
# ugly
def strip_dash(text: str, prefix: str):
words = text.split()
for i, word in enumerate(words):
if word.startswith(prefix) and i != len(words)-1:
words = words[:i] + words[i+1:]
break
return " ".join(words)
# i really love this function, improved
async def loopMsg(message: discord.Message, prefix: str):
role = "model" if message.author.bot else "user"
content = message.content if message.author.bot else strip_dash(message.content, prefix)
content = "Hello!" if content and content.startswith(prefix) else content
base64_data, mime = None, None
if len(message.attachments) > 0:
attachment = message.attachments[0]
image_data = await attachment.read()
base64_data = base64.b64encode(image_data).decode('utf-8')
mime = attachment.content_type
base_data = [
{
"role": role,
"parts": [
{"text": content},
{
"inline_data": {
"mime_type": mime,
"data": base64_data
}
} if base64_data else None
]
}
]
if not message.reference: return base_data
try:
repliedMessage = await message.channel.fetch_message(message.reference.message_id)
except:
print("Exception in loopMsg:googleai")
return base_data
previousMessages = await loopMsg(repliedMessage, prefix)
return previousMessages + base_data
async def json_data(msg: discord.Message, prefix: str):
messagesArray = await loopMsg(msg, prefix)
return {"contents": messagesArray}
async def json_data_slash(prompt: str, image: discord.Attachment):
if image:
image_data = await image.read()
base64_data = base64.b64encode(image_data).decode('utf-8')
mime = image.content_type
base_data = [
{
"role": "user",
"parts": [
{"text": prompt},
{
"inline_data": {
"mime_type": mime,
"data": base64_data
}
} if image else None
]
}
]
return {"contents": base_data}
def json_data_palm(arg: str, safe: bool):
return {
"prompt": {
"text": arg if arg else "Explain who you are, your functions, capabilities, limitations, and purpose."
},
"safetySettings": [
{
"category": "HARM_CATEGORY_UNSPECIFIED",
"threshold": "BLOCK_NONE"
},
{
"category": "HARM_CATEGORY_DEROGATORY",
"threshold": "BLOCK_NONE"
},
{
"category": "HARM_CATEGORY_TOXICITY",
"threshold": "BLOCK_NONE"
},
{
"category": "HARM_CATEGORY_VIOLENCE",
"threshold": "BLOCK_NONE"
},
{
"category": "HARM_CATEGORY_SEXUAL",
"threshold": "BLOCK_NONE"
},
{
"category": "HARM_CATEGORY_MEDICAL",
"threshold": "BLOCK_NONE"
},
{
"category": "HARM_CATEGORY_DANGEROUS",
"threshold": "BLOCK_NONE"
}
] if not safe else None,
}
async def req_real(url, json, headers, palm):
async with aiohttp.ClientSession() as session:
async with session.post(url, json=json, headers=headers) as response:
if response.status == 200:
return get_text_palm(await response.json()) if palm else get_text(await response.json())
else: print(await response.content.read())
models = [
"text-bison-001", # dead
"gemini-1.5-pro",
"gemini-1.5-flash",
]
async def GEMINI_REST(ctx: commands.Context | discord.Interaction, model: int, palm: bool, prompt: str=None, image: discord.Attachment=None):
if await command_check(ctx, "googleai", "ai"):
if isinstance(ctx, commands.Context):
return await ctx.reply("command disabled", ephemeral=True)
if isinstance(ctx, discord.Interaction):
return await ctx.response.send_message("command disabled", ephemeral=True)
# async with ctx.typing():
if isinstance(ctx, commands.Context):
msg = await ctx.reply(f"{models[model]}\nGenerating response…")
if isinstance(ctx, discord.Interaction):
await ctx.response.send_message(f"{models[model]}\nGenerating response…")
old = round(time.time() * 1000)
text = None
prefix = await get_guild_prefix(ctx)
# rewrite
if palm:
proxy = palm_proxy(f"{models[model]}:generateText")
payload = json_data_palm(strip_dash(ctx.message.content, prefix), not ctx.channel.nsfw)
else:
proxy = palm_proxy(f"{models[model]}:generateContent")
payload = await json_data(ctx.message, prefix) if not prompt else await json_data_slash(prompt, image)
text = await req_real(proxy, payload, headers, palm)
# silly
if not text:
if isinstance(ctx, commands.Context):
return await msg.edit(content=f"**Error! :(**")
if isinstance(ctx, discord.Interaction):
return await ctx.edit_original_response(content=f"**Error! :(**")
chunks = [text[i:i+2000] for i in range(0, len(text), 2000)]
replyFirst = True
for chunk in chunks:
if isinstance(ctx, discord.Interaction): await ctx.followup.send(chunk)
if isinstance(ctx, commands.Context):
if replyFirst:
replyFirst = False
await ctx.reply(chunk)
else:
await ctx.send(chunk)
if isinstance(ctx, commands.Context):
await msg.edit(content=f"{models[model]}\n**Took {round(time.time() * 1000)-old}ms**")
if isinstance(ctx, discord.Interaction):
await ctx.edit_original_response(content=f"{models[model]}\n**Took {round(time.time() * 1000)-old}ms**")
async def help_google(ctx: commands.Context):
if await command_check(ctx, "googleai", "ai"): return await ctx.reply("command disabled", ephemeral=True)
p = await get_guild_prefix(ctx)
text = [
f"`{p}gemini` {models[1]}",
f"`{p}flash` {models[2]}",
# f"`{p}palm` {models[0]}"
]
await ctx.reply("\n".join(text))
class CogGoogle(commands.Cog):
def __init__(self, bot):
self.bot = bot
# @commands.command()
# async def palm(self, ctx: commands.Context):
# await GEMINI_REST(ctx, 0, True)
@commands.command(aliases=["ge"]) # alias
async def gemini(self, ctx: commands.Context):
await GEMINI_REST(ctx, 1, False)
@app_commands.command(name="gemini", description=f"{description_helper['emojis']['ai']} {models[1]}")
@app_commands.describe(prompt="Text prompt")
@app_commands.allowed_installs(guilds=True, users=True)
@app_commands.allowed_contexts(guilds=True, dms=True, private_channels=True)
async def gemini_slash(self, ctx: discord.Interaction, prompt: str, image: discord.Attachment=None):
await GEMINI_REST(ctx, 1, False, prompt, image)
@commands.command()
async def flash(self, ctx: commands.Context):
await GEMINI_REST(ctx, 2, False)
@app_commands.command(name="flash", description=f"{description_helper['emojis']['ai']} {models[2]}")
@app_commands.describe(prompt="Text prompt")
@app_commands.allowed_installs(guilds=True, users=True)
@app_commands.allowed_contexts(guilds=True, dms=True, private_channels=True)
async def flash_slash(self, ctx: discord.Interaction, prompt: str, image: discord.Attachment=None):
await GEMINI_REST(ctx, 2, False, prompt, image)
@commands.command()
async def googleai(self, ctx: commands.Context):
await help_google(ctx)
async def setup(bot: commands.Bot):
await bot.add_cog(CogGoogle(bot))