-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
125 lines (107 loc) · 4.33 KB
/
run.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
import discord
from discord import app_commands
import aiohttp
import json
import os
# Bot setup
TOKEN = "YOUR_DISCORD_BOT_TOKEN"
# Constants
MAX_INPUT_LENGTH = 1000 # Input limit
MAX_OUTPUT_LENGTH = 1900 # Output limit (leaving room for formatting)
# Set up specific intents
intents = discord.Intents.default()
intents.message_content = True
intents.guilds = True
intents.messages = True
client = discord.Client(intents=intents)
tree = app_commands.CommandTree(client)
def truncate_response(text, max_length=MAX_OUTPUT_LENGTH):
if len(text) <= max_length:
return text
# Try to cut at the last complete sentence
last_period = text[:max_length].rfind('.')
if last_period > 0:
return text[:last_period + 1]
# If no sentence break found, cut at max_length
return text[:max_length] + "..."
@tree.command(
name="llama70b",
description="Ask Llama70B AI a question (max 1000 characters)."
)
async def slash_llama70b(interaction: discord.Interaction, message: str):
# Check message length
if len(message) > MAX_INPUT_LENGTH:
return await interaction.response.send_message(
f"Your message is too long. Please keep it under {MAX_INPUT_LENGTH} characters.",
ephemeral=True
)
# Defer the response to allow time for processing
await interaction.response.defer()
payload = {
"messages": [
{
"role": "user",
"content": message
}
],
"model": "meta-llama/Meta-Llama-3.1-70B-Instruct",
"max_tokens": 2048,
"temperature": 0.7,
"top_p": 0.9
}
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer YOUR_API_ACCESS_TOKEN"
}
max_retries = 3
for attempt in range(max_retries):
try:
timeout = aiohttp.ClientTimeout(total=30) # 30 second timeout
async with aiohttp.ClientSession(timeout=timeout) as session:
async with session.post(
"https://api.hyperbolic.xyz/v1/chat/completions",
json=payload,
headers=headers
) as response:
response_text = await response.text()
print(f"Attempt {attempt + 1} - Status: {response.status}")
if response.status == 200:
try:
response_data = json.loads(response_text)
ai_response = response_data["choices"][0]["message"]["content"]
truncated_response = truncate_response(ai_response)
return await interaction.followup.send(truncated_response)
except (KeyError, json.JSONDecodeError) as e:
print(f"Error parsing response: {e}")
continue
elif response.status == 524: # Timeout error
print("Timeout error, retrying...")
if attempt < max_retries - 1:
continue
else:
return await interaction.followup.send(
"Sorry, the service is taking too long to respond. Please try again in a moment."
)
else:
print(f"API Error: {response.status} - {response_text}")
return await interaction.followup.send(
"Sorry, I'm having trouble connecting to the service. Please try again in a few minutes."
)
except Exception as e:
print(f"Attempt {attempt + 1} failed: {str(e)}")
if attempt == max_retries - 1:
return await interaction.followup.send(
"Sorry, I'm having trouble connecting right now. Please try again in a few minutes."
)
@client.event
async def on_ready():
print(f"Logged in as {client.user}")
try:
print("Syncing commands globally...")
await tree.sync()
print("Commands synced successfully!")
except Exception as e:
print(f"Error syncing commands: {e}")
print("Bot is ready!")
# Run the bot
client.run(TOKEN)