-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli_error_app.py
61 lines (53 loc) · 2.28 KB
/
cli_error_app.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
import argparse
import openai
import os
import dotenv
import platform
import sys
from termcolor import colored
# Load environment variables from .env file
dotenv.load_dotenv()
# Function to communicate with the ChatGPT API
def ask_chatgpt(api_key, error_message, input=None, environment_info=None):
message = f"""
I ran the following command:\n{input}\n
I received the following error message:\n{error_message}\n
Here is everything I know about my environment:\n{environment_info}\n
{colored('How can I fix it?', 'black', attrs=['bold'])}
"""
print(message) # Debugging line
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
api_key=api_key,
messages=[
{"role":"system", "content":"You are a very helpful technical assistant. Provide code solutions when possible. Please keep explanations as simple as possible."},
{"role":"user", "content":message},
]
)
formatted_message = response['choices'][0]['message']['content']
return formatted_message
def get_environment_info():
environment_info = {
"operating_system": platform.system(),
"os_version": platform.release(),
"python_version": sys.version,
"environment_variables": "\n" + "\n".join([f"{k}: {v}" for k, v in os.environ.items()])
}
formatted_info = "\n".join([colored(f"{key}: {value}",'blue') for key, value in environment_info.items()])
return formatted_info
# Main function
def main():
# Define and parse CLI arguments
parser = argparse.ArgumentParser(description="Interpret and fix terminal errors using ChatGPT")
parser.add_argument("error_message", type=str, help="Error message from the terminal")
args = parser.parse_args()
command, error_message = args.error_message.split('|||')
#strip the equal api_key= from the api_key
api_key = os.getenv("OPENAI_API_KEY")
# Ask ChatGPT for a solution to the error
solution = ask_chatgpt(api_key=api_key,error_message=colored(error_message, 'red'), input=colored(command, 'blue'), environment_info=get_environment_info())
# Display the solution to the user
print(colored("ChatGPT suggests the following solution:", 'green', attrs=['bold']))
print(colored(solution, 'black', attrs=['bold']))
if __name__ == "__main__":
main()