-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcode
31 lines (27 loc) · 1.14 KB
/
mcode
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
// This function sends a message to the OpenAI GPT-3 API using the provided API key, role, and content,
// and returns the response as a table.
// by Moosy Research - 2023
(apikey as any, apirole as any, apicontent as any) =>
let
// Define the request parameters
req = [
openai_url = "https://api.openai.com/v1/chat/completions",
openai_headers = [#"Authorization" = "Bearer " & apikey,#"Content-Type" = "application/json"],
openai_payload = "{""model"": ""gpt-3.5-turbo"", ""messages"": [{""role"": """&apirole&""", ""content"": """&apicontent&"""}]}",
openai_params = [Headers = openai_headers, Content = Text.ToBinary(openai_payload)]
],
// Send the HTTP POST request to the OpenAI API endpoint and parse the response as JSON
response = Web.Contents(req[openai_url], req[openai_params]),
statusCode = Value.Metadata(response)[Response.Status],
content =
if statusCode = 200 then
Json.Document(response)
else if statusCode = 404 then
"Not found"
else if statusCode >= 500 then
"Server error"
else
null
in
// Return string
content[choices]{0}[message]