forked from olrea/openai-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path09-instances.cpp
58 lines (50 loc) · 1.71 KB
/
09-instances.cpp
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
#include "openai.hpp"
#include <iostream>
// A class which handles a openai instance by reference
class Foo {
std::reference_wrapper<openai::OpenAI> openai_;
// Or
// OpenAI& openai_;
public:
Foo(openai::OpenAI& openai) : openai_{openai} {
auto res = openai_.get().completion.create({
{"model", "text-davinci-003"},
{"prompt", "Say Foo class ctor called"}
}); // parameters channel, username, iconemoji reused from bar()
std::cout << res << "\n\n";
}
};
// Pass by ref in parameter function
void bar(openai::OpenAI& openai) {
// You can try catch API method if an error occur
try {
auto res = openai.completion.create({
{"model", "text-davinci-003"},
{"prompt", "Say bar() function called"}
});
std::cout << res << "\n\n";
}
catch(std::exception const& e) {
std::cerr << "Exception:" << e.what() << "\n\n";
}
}
int main() {
// Create openai instance
openai::OpenAI openai_instance{};
bar(openai_instance);
Foo foo{openai_instance};
// You can create other openai instances with different tokens and parameters
{
openai::OpenAI another_openai_instance{"wrong apy key"};
// another_openai_instance.setThrowException(false); // You can set throw exception to false if you want
try {
another_openai_instance.completion.create({
{"model", "text-davinci-003"},
{"prompt", "Say this should throw since token is invalid here"}
});
}
catch(std::exception const& e) {
std::cerr << "02-basic failed purposely because of " << e.what() << "\n\n";
}
}
}