ChatGPTAutocomplete/autohotkey/GptAutocomplete.py

51 lines
1.7 KiB
Python
Raw Normal View History

2023-11-02 13:48:23 -07:00
# importing the requests library
import requests
import json
retCode, userPrompt = dialog.input_dialog('Gpt Snippet','Enter a prompt')
# exit if no input or user hits cancel
if userPrompt == '' or retCode == 1:
exit(0)
# use prompt as comment
cleanOutput = "// " + userPrompt + "\n"
# post to openai
API_Key = "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"
URL = "https://api.openai.com/v1/chat/completions"
prompt = 'Use the following input to create a code snippet. Only output ES6 Javascript no additional details or explanations, only code. No other output, only JS code. Create a clear, concise output with readable camel case variable names. Make sure the code is clear, understandable and maintainable. If its necessary use existing variable names with the output. Use 4 spaces per tab. The input is:'
HEADERS = {
"Content-Type": "application/json",
"Authorization": "Bearer "+API_Key
}
PARAMS = {
"model": "gpt-4",
"messages": [
{
"role": "user",
"content": prompt + userPrompt
}
]
}
response = requests.post(URL, data = json.dumps(PARAMS), headers = HEADERS)
data = response.json()
# Clean up chat GPT response
gptResponse = str(data['choices'][0]['message']['content'])
gptResponse = gptResponse.replace("```Javascript","")
gptResponse = gptResponse.replace("```javascript","")
gptResponse = gptResponse.replace("```","")
cleanOutput += gptResponse.strip()
#cleanOutput = cleanOutput.replace(" ","\t")
exitCode, userInput = dialog.info_dialog('Gpt Snippet Review',"Generated: \n\n" + cleanOutput + "\n\npress ESC to cancel")
# Dont paste output if user hit escape
if exitCode == 0:
keyboard.send_keys(cleanOutput)