-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathopenai.w
83 lines (69 loc) · 1.87 KB
/
openai.w
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
bring cloud;
bring util;
pub struct CompletionParams {
model: str?;
maxTokens: num?;
}
pub struct OpenAIProps {
apiKey: str?;
org: str?;
apiKeySecret: cloud.Secret?;
orgSecret: cloud.Secret?;
}
inflight interface IClient {
inflight createCompletion(params: Json): Json;
}
inflight class Sim impl IClient {
pub createCompletion(req: Json): Json {
return {
choices: [
{
message: {
content: Json.stringify({ mock: req })
}
}
]
};
}
}
pub class OpenAI {
apiKey: cloud.Secret?;
org: cloud.Secret?;
keyOverride: str?;
orgOverride: str?;
mock: bool;
inflight openai: IClient;
new(props: OpenAIProps?) {
this.apiKey = props?.apiKeySecret;
this.org = props?.orgSecret;
this.keyOverride = props?.apiKey;
this.orgOverride = props?.org;
this.mock = util.env("WING_TARGET") == "sim" && nodeof(this).app.isTestEnvironment;
}
inflight new() {
// TODO: https://github.com/winglang/wing/issues/5944
let UNDEFINED = "<UNDEFINED>";
let apiKey = this.keyOverride ?? this.apiKey?.value() ?? UNDEFINED;
if apiKey == UNDEFINED {
throw "Either `apiKeySecret` or `apiKey` are required";
}
let var org: str? = this.orgOverride;
if org == nil {
org = this.org?.value();
}
if this.mock {
this.openai = new Sim();
} else {
this.openai = OpenAI.createNewInflightClient(apiKey, org);
}
}
pub inflight createCompletion(prompt: str, params: CompletionParams?): str {
let resp = this.openai.createCompletion({
max_tokens: params?.maxTokens ?? 2048,
model: params?.model ?? "gpt-3.5-turbo",
messages: [ { role: "user", content: prompt } ]
});
return resp["choices"][0]["message"]["content"].asStr();
}
extern "./openai.js" pub static inflight createNewInflightClient(apiKey: str, org: str?): IClient;
}