-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.ts
153 lines (143 loc) · 3.49 KB
/
common.ts
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import axios from "./axiosInterceptor";
import {
OPENAI_COMPLETIONS_API_ENDPOINT,
OPENAI_EMBEDDINGS_API_ENDPOINT,
OPENAI_MODERTATIONS_API_ENDPOINT,
OPENAI_TRANSCRIPTIONS_API_ENDPOINT,
} from "./consts";
import { TaskResponse, TokenResponse } from "./types";
export async function getToken(taskname: string): Promise<string> {
try {
const { data } = await axios.post<TokenResponse>(
`${process.env.AI_DEVS_API_BASE_URL}/token/${taskname}`,
{
apikey: process.env.AI_DEVS_API_KEY,
}
);
return data.token;
} catch (error) {
console.error(error);
}
return "";
}
export async function getTask<T>(token: string): Promise<T> {
try {
const { data } = await axios.get<T>(
`${process.env.AI_DEVS_API_BASE_URL}/task/${token}`
);
return data;
} catch (error) {
console.error(error);
}
return {} as T;
}
export async function getTaskQuestionAnswer(
token: string,
question: string
): Promise<string> {
const formData = new FormData();
formData.append("question", String(question));
try {
const { data } = await axios.post<TaskResponse & { answer: string }>(
`${process.env.AI_DEVS_API_BASE_URL}/task/${token}`,
formData
);
return data.answer;
} catch (error) {
console.error(error);
}
return "";
}
export async function sendAnswer(token: string, answer: unknown) {
try {
const { data } = await axios.post(
`${process.env.AI_DEVS_API_BASE_URL}/answer/${token}`,
JSON.stringify({ answer })
);
return data;
} catch (error) {
console.error(error);
}
}
const headers = {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
};
export async function getOpenAIModerationAPIResponse(input: string) {
try {
const { data } = await axios.post(
OPENAI_MODERTATIONS_API_ENDPOINT,
{ input },
{ headers }
);
return data;
} catch (error) {
console.error(error);
}
}
export async function openAICompletion(
userMessage: string,
systemPrompt: string = "Act as helpfull assistant",
options?: {
model?: "gpt-3.5-turbo" | "gpt-4";
maxTokens?: number;
temperature?: number;
}
) {
try {
const { data } = await axios.post(
OPENAI_COMPLETIONS_API_ENDPOINT,
{
model: options?.model ?? "gpt-3.5-turbo",
max_tokens: options?.maxTokens ?? 500,
temperature: options?.temperature ?? 0.5,
messages: [
{ role: "system", content: systemPrompt },
{ role: "user", content: userMessage },
],
stream: false,
stop: ["#-#"],
},
{ headers }
);
return data;
} catch (error) {
console.error(error);
}
}
export async function openAIEmbedding(input: string) {
try {
const { data } = await axios.post(
OPENAI_EMBEDDINGS_API_ENDPOINT,
{
input,
model: "text-embedding-ada-002",
},
{ headers }
);
return data;
} catch (error) {
console.error(error);
}
}
export async function openAITranscription(fileName: string): Promise<string> {
const formData = new FormData();
formData.append("file", Bun.file(fileName));
formData.append("model", "whisper-1");
try {
const { data } = await axios.post(
OPENAI_TRANSCRIPTIONS_API_ENDPOINT,
formData,
{
headers: {
...headers,
"Content-Type": "multipart/form-data",
},
}
);
return data.text;
} catch (error) {
console.error(error);
}
return "";
}