-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathchatGPT_prompting.py
80 lines (69 loc) · 2.95 KB
/
chatGPT_prompting.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
#import libraries
import openai
import time
import threading
import traceback
import openai
from openai.error import AuthenticationError
from playbook_extractor import playbook_extractor_function
# Function to validate the provided OpenAI API key
def validate_openai_key(api_key):
# Set the OpenAI API key
openai.api_key = api_key
# Define a test message to check if the API key is correct
messages = [
{"role": "system", "content": "Test if the API key is correct."},
]
success = False
try:
# Test the API key by sending a chat completion request
openai.ChatCompletion.create(model="gpt-4", messages=messages, max_tokens=5)
success = True
except AuthenticationError as e:
# Handle authentication error if the API key is invalid
print("Invalid API key:", e)
except Exception as e:
# Handle other unexpected errors
print("An unexpected error occurred:", e)
return success
# Function responsible for prompting ChatGPT to generate playbooks
def chatGPT_prompter(prompt, model_name, conversation_history):
# Define an inner function to make the API call asynchronously
def api_call():
nonlocal response
try:
# Create messages including the conversation history and user prompt
messages = [
{"role": "system", "content": "You are a network engineer trying to automate a network."},
]
messages.extend(conversation_history)
messages.append({"role": "user", "content": prompt})
# Call ChatGPT to generate a response
response = openai.ChatCompletion.create(
model=model_name,
messages=messages,
max_tokens=1500,
)
except Exception as e:
# Handle errors during playbook generation
print("An error occurred during playbook generation:", e)
traceback.print_exc()
response = None
response = None
# Display a waiting message while generating the playbook
print("\n\nPlease wait while a playbook is being generated", end='', flush=True)
# Start a thread to make the API call asynchronously
animation_thread = threading.Thread(target=api_call)
animation_thread.start()
# Display an animation while waiting for the response
while animation_thread.is_alive():
for _ in range(5):
time.sleep(1.0)
print('.', end='', flush=True)
print('\b\b\b \b\b\b', end='', flush=True) # Erase the dots
# Wait for the animation thread to finish
animation_thread.join()
# Extract relevant information from the response
text_before_code, code, text_after_code, conversation_history = playbook_extractor_function(response,
conversation_history)
return text_before_code, code, text_after_code, conversation_history