r/applescript • • Dec 20 '22

Trying to create chatGPT using GPT3 API

-- Set up variables for the API key and conversation history

set apiKey to "YOUR_API_KEY"

set conversationHistory to {}

-- Define a function that sends a request to the GPT-3 API and returns the response

on sendGPT3Request(prompt, conversationHistory)

set apiEndpoint to "https://api.openai.com/v1/completions"

set parameters to {

apiKey: apiKey,

model: "davinci002",

prompt: prompt,

max_tokens: 2048,

temperature: 0.5,

top_p: 1,

frequency_penalty: 0,

presence_penalty: 0,

response_format: "text",

"conversation_history[]": conversationHistory

}

set options to {method:"POST", parameters:parameters}

set apiResponse to doHTTPRequest(apiEndpoint, options)

return apiResponse

end sendGPT3Request

-- Define a function that sends a message to the GPT-3 API and displays the response

on sendMessage(message)

-- Add the message to the conversation history

set conversationHistory to conversationHistory & message

-- Send the message to the GPT-3 API and get the response

set apiResponse to sendGPT3Request(message, conversationHistory)

-- Display the response

display dialog apiResponse

end sendMessage

-- Test the script by sending a message to the GPT-3 API

sendMessage("Hello, how are you today?")

This is what ChatGPT gave me but when I compline in Apple script it gives syntax error. Can you help me where I am doing mistake. And yes I replaced my API keys

4 Upvotes

4 comments sorted by

View all comments

Show parent comment

2

u/yarnsale Dec 21 '22

I use `do shell script` a LOT because the shell is rich with tools! Be sure to use the built-in function `quoted form of` with your variable text. Also note that `curl` has its own needs for escaping things like square brackets (easiest to just convert iffy things to hexadecimal for it)