-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.bal
40 lines (33 loc) · 987 Bytes
/
main.bal
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
import ballerina/http;
import ballerina/io;
configurable string openAIToken = ?;
final http:Client openAIClient = check new ("https://api.openai.com", {auth: {token: openAIToken}});
type Message record {
string role;
string content;
};
type ChoicesItem record {
Message message;
};
type OpenAiChatResponse record {
ChoicesItem[] choices;
};
public function main(string filePath) returns error? {
string inputString = check io:fileReadString(filePath);
OpenAiChatResponse response = check openAIClient->/v1/chat/completions.post(
{
model: "gpt-3.5-turbo",
messages: [
{
role: "user",
content: "Fix grammar and spelling mistakes."
},
{
role: "user",
content: inputString
}
]
}
);
io:println(string `Corrected: ${response.choices[0].message.content}`);
}