-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
206 lines (182 loc) · 4.55 KB
/
index.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
export type BurritoModels =
| "gpt4"
| "gpt3.5"
| "mistral7b"
| "mixtral"
| "mistral-small"
| "mistral-medium"
| "mistral-large";
export type BurritoQueryParams = {
app: string;
query: string;
schema?: any;
cacheFor?: number;
force?: boolean;
};
export type BurritoQueryDataParams = {
query: string;
schema?: any;
cacheFor?: number;
force?: boolean;
};
export type BurritoInstallRequestParams = {
systemPrompt?: string;
prompt: string;
mode: "each";
model: BurritoModels | string;
app: string;
};
export type BurritoTransformRequestParams = {
hashes?: string[];
prompt: string;
systemPrompt?: string;
model?: BurritoModels | string;
mode?: "each" | "all";
completionType?: "json" | "text";
save?: {
app: string;
};
force?: boolean;
debug?: boolean;
};
export type BurritoData = {
hash?: string;
created?: number;
date?: string;
title?: string;
summary?: string;
description?: string;
caption?: string;
text?: string;
// TODO should add userData and location
};
export type BurritoEmbeddingsData = {
hash: string;
distance: number;
type: string;
created: number;
summary?: string;
title?: string;
description?: string;
};
export type BurritoEmbeddingsRequest = {
vectors?: number[][];
queries?: string[];
num?: number;
};
export type TransformPart<T> = {
hash: string;
completion: T;
};
export type BurritoTransformResponse<T = any> = TransformPart<T>[];
export type GetTransformsResponse = {
[name: string]: {
systemPrompt: string;
prompt: string;
mode: string;
model: string;
};
};
export class Burrito {
private baseUrl: string;
private apiKey: string;
private fetchFunc: typeof fetch;
private fetchDefaults?: RequestInit | any;
constructor({
baseUrl,
apiKey,
fetchFunc,
fetchDefaults,
}: {
baseUrl?: string;
apiKey?: string;
fetchFunc?: typeof fetch;
fetchDefaults?: RequestInit | any;
}) {
// Ensure environment variables are set
if (!baseUrl && !process.env.BURRITO_URL) {
throw new Error(
"Environment variable BURRITO_URL must be set or passed in"
);
}
if (!apiKey && !process.env.BURRITO_KEY) {
throw new Error(
"Environment variable BURRITO_KEY must be set or passed in"
);
}
this.baseUrl = baseUrl ? baseUrl : process.env.BURRITO_URL!;
this.apiKey = apiKey ? apiKey : process.env.BURRITO_KEY!;
this.fetchFunc = fetchFunc ? fetchFunc : fetch;
this.fetchDefaults = fetchDefaults;
}
private async fetcher(url: string, body: any | null, init?: RequestInit) {
return await this.fetchFunc(`${this.baseUrl}/${url}`, {
...this.fetchDefaults,
...init,
method: init?.method || "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${this.apiKey}`,
},
body: body && JSON.stringify(body),
})
.then((res) => res.json())
.catch((err) => console.error(err));
}
public async query<T = BurritoData>(
query: BurritoQueryParams,
init?: RequestInit | any
) {
const data = await this.fetcher("query", query, init).then((d: any) =>
d.data ? (d.data as T) : (d as T)
);
return data;
}
public async queryData<T>(
query: BurritoQueryDataParams,
init?: RequestInit | any
) {
const data = await this.fetcher("query/data", query, init);
return data as T;
}
public async queryEmbeddings(
query: BurritoEmbeddingsRequest,
init?: RequestInit | any
) {
return (await this.fetcher(
"query/embeddings",
query,
init
)) as BurritoEmbeddingsData[];
}
public async install(
query: BurritoInstallRequestParams,
init?: RequestInit | any
) {
return await this.fetcher("install", query, init);
}
public async getTransforms(
init?: RequestInit | any
): Promise<GetTransformsResponse> {
return (await this.fetcher("transforms", null, {
...init,
method: "GET",
})) as GetTransformsResponse;
}
public async transform<T = any>(
query: BurritoTransformRequestParams & { mode: "all" },
init?: RequestInit | any
): Promise<T>;
public async transform<T = any>(
query: BurritoTransformRequestParams & { mode?: "each" },
init?: RequestInit | any
): Promise<BurritoTransformResponse<T>>;
public async transform<T = any>(
query: BurritoTransformRequestParams,
init?: RequestInit | any
) {
const data = await this.fetcher("transform", query, init);
if (query.mode === "each") return data as BurritoTransformResponse<T>;
return data as T;
}
}