-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathcreateAssistant.ts
49 lines (45 loc) · 1.68 KB
/
createAssistant.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
import {
CreateAssistantRequestRegionEnum,
ManageAssistantsApi as ManageAssistantsControlApi,
} from '../../pinecone-generated-ts-fetch/assistant_control';
import type { CreateAssistantOptions, AssistantModel } from './types';
import { CreateAssistantOptionsType } from './types';
import { ValidateObjectProperties } from '../../utils/validateObjectProperties';
import { PineconeArgumentError } from '../../errors';
export const createAssistant = (api: ManageAssistantsControlApi) => {
return async (options: CreateAssistantOptions): Promise<AssistantModel> => {
validateCreateAssistantOptions(options);
return (await api.createAssistant({
createAssistantRequest: {
name: options.name,
instructions: options?.instructions,
metadata: options?.metadata,
region: options?.region as CreateAssistantRequestRegionEnum,
},
})) as AssistantModel;
};
};
const validateCreateAssistantOptions = (options: CreateAssistantOptions) => {
if (!options) {
throw new PineconeArgumentError(
'You must pass an object with required properties (`name`) to create an Assistant.'
);
}
ValidateObjectProperties(options, CreateAssistantOptionsType);
if (options.region) {
let region: CreateAssistantRequestRegionEnum =
CreateAssistantRequestRegionEnum.Us;
if (
!Object.values(CreateAssistantRequestRegionEnum)
.toString()
.includes(options.region.toLowerCase())
) {
throw new PineconeArgumentError(
'Invalid region specified. Must be one of "us" or "eu"'
);
} else {
region = options.region.toLowerCase() as CreateAssistantRequestRegionEnum;
}
options.region = region;
}
};