text
stringlengths
1
93.6k
except AuthenticationError as e:
raise Exception("Your API key is invalid. Please check your configuration. IT IS NOT A BUG OF BUKKITGPT.")
except Exception as e:
raise e
logger(f"askgpt: response {response}")
if "Too many requests" in str(response):
logger("Too many requests. Please try again later.")
raise Exception("Your LLM provider has rate limited you. Please try again later. IT IS NOT A BUG OF BUKKITGPT.")
# Extract the assistant's reply
try:
assistant_reply = response.choices[0].message.content
logger(f"askgpt: extracted reply {assistant_reply}")
except Exception as e:
logger(f"askgpt: error extracting reply {e}")
raise Exception("Your LLM didn't return a valid response. Check if the API provider supportes OpenAI response format.")
return assistant_reply
def response_to_action(msg) -> str:
"""
Converts a response from the LLM to an action.
Args:
msg (str): The response from the LLM.
Returns:
str: The action to take.
"""
pattern = r"```json(.*?)```"
matches = re.findall(pattern, msg, re.DOTALL)
if not matches:
raise Exception("Invalid response format from LLM. Expected JSON code block.")
json_codes = matches[0].strip()
text = json.loads(json_codes)
codes = text["codes"]
for section in codes:
file = section["file"]
code = section["code"].replace("%linefeed%", "\n")
paths = file.split("/")
# Join the list elements to form a path
path = os.path.join(*paths)
# Get the directory path and the file name
dir_path, file_name = os.path.split(path)
# Create directories, if they don't exist
try:
os.makedirs(dir_path, exist_ok=True)
except FileNotFoundError:
pass
# Create the file
with open(path, "w") as f:
f.write(code) # Write an empty string to the file
def mixed_decode(text: str) -> str:
"""
Decode a mixed text containing both normal text and a byte sequence.
Args:
text (str): The mixed text to be decoded.
Returns:
str: The decoded text, where the byte sequence has been converted to its corresponding characters.
"""
# Split the normal text and the byte sequence
# Assuming the byte sequence is everything after the last colon and space ": "
try:
normal_text, byte_text = text.rsplit(": ", 1)
except (TypeError, ValueError):
# The text only contains normal text
return text
# Convert the byte sequence to actual bytes
byte_sequence = byte_text.encode(
"latin1"
) # latin1 encoding maps byte values directly to unicode code points
# Detect the encoding of the byte sequence
detected_encoding = chardet.detect(byte_sequence)
encoding = detected_encoding["encoding"]
# Decode the byte sequence
decoded_text = byte_sequence.decode(encoding)
# Combine the normal text with the decoded byte sequence
final_text = normal_text + ": " + decoded_text
return final_text