@chainfuse/ai-tools 0.7.1 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,137 +0,0 @@
1
- import { Helpers } from '@chainfuse/helpers';
2
- import haversine from 'haversine-distance';
3
- import { AiBase } from '../base.mjs';
4
- export var PrivacyRegion;
5
- (function (PrivacyRegion) {
6
- PrivacyRegion["Australian_Privacy_Principles"] = "APPs";
7
- PrivacyRegion["Brazil_General_Data_protection_Law"] = "LGPD";
8
- PrivacyRegion["Canada_Personal_Information_Protection_and_Electronic_Documents_Act"] = "PIPEDA";
9
- PrivacyRegion["General_Data_Protection_Regulation"] = "GDPR";
10
- PrivacyRegion["Indian_Personal_Protection"] = "PDP";
11
- PrivacyRegion["Japan_Act_on_the_Protection_of_Personal_Information"] = "APPI";
12
- PrivacyRegion["Korean_Personal_Information_Protection_Act"] = "PIPA";
13
- PrivacyRegion["Norwegian_Personal_Data_Act"] = "NPDA";
14
- PrivacyRegion["SouthAfrica_Protection_Personal_Information_Act"] = "PoPIA";
15
- PrivacyRegion["Swiss_Federal_Act_on_Data_Protection"] = "revFADP";
16
- PrivacyRegion["UK_General_Data_Protection_Regulation"] = "UK-GDPR";
17
- })(PrivacyRegion || (PrivacyRegion = {}));
18
- export class ServerSelector extends AiBase {
19
- servers = new Set();
20
- static determinePrivacyRegion(country, continent) {
21
- const regions = new Set();
22
- if (country) {
23
- switch (country.toUpperCase()) {
24
- case 'AU':
25
- regions.add(PrivacyRegion.Australian_Privacy_Principles);
26
- break;
27
- case 'BR':
28
- regions.add(PrivacyRegion.Brazil_General_Data_protection_Law);
29
- break;
30
- case 'CA':
31
- regions.add(PrivacyRegion.Canada_Personal_Information_Protection_and_Electronic_Documents_Act);
32
- regions.add(PrivacyRegion.General_Data_Protection_Regulation);
33
- regions.add(PrivacyRegion.Swiss_Federal_Act_on_Data_Protection);
34
- regions.add(PrivacyRegion.UK_General_Data_Protection_Regulation);
35
- break;
36
- case 'IN':
37
- regions.add(PrivacyRegion.Indian_Personal_Protection);
38
- break;
39
- case 'JP':
40
- regions.add(PrivacyRegion.Japan_Act_on_the_Protection_of_Personal_Information);
41
- regions.add(PrivacyRegion.General_Data_Protection_Regulation);
42
- regions.add(PrivacyRegion.UK_General_Data_Protection_Regulation);
43
- break;
44
- case 'KR':
45
- regions.add(PrivacyRegion.Korean_Personal_Information_Protection_Act);
46
- regions.add(PrivacyRegion.General_Data_Protection_Regulation);
47
- regions.add(PrivacyRegion.UK_General_Data_Protection_Regulation);
48
- break;
49
- case 'NO':
50
- regions.add(PrivacyRegion.Norwegian_Personal_Data_Act);
51
- regions.add(PrivacyRegion.Canada_Personal_Information_Protection_and_Electronic_Documents_Act);
52
- regions.add(PrivacyRegion.General_Data_Protection_Regulation);
53
- regions.add(PrivacyRegion.Japan_Act_on_the_Protection_of_Personal_Information);
54
- regions.add(PrivacyRegion.Korean_Personal_Information_Protection_Act);
55
- regions.add(PrivacyRegion.Swiss_Federal_Act_on_Data_Protection);
56
- regions.add(PrivacyRegion.UK_General_Data_Protection_Regulation);
57
- break;
58
- case 'ZA':
59
- regions.add(PrivacyRegion.SouthAfrica_Protection_Personal_Information_Act);
60
- break;
61
- case 'CH':
62
- regions.add(PrivacyRegion.Swiss_Federal_Act_on_Data_Protection);
63
- regions.add(PrivacyRegion.Canada_Personal_Information_Protection_and_Electronic_Documents_Act);
64
- regions.add(PrivacyRegion.General_Data_Protection_Regulation);
65
- regions.add(PrivacyRegion.Japan_Act_on_the_Protection_of_Personal_Information);
66
- regions.add(PrivacyRegion.Korean_Personal_Information_Protection_Act);
67
- regions.add(PrivacyRegion.Norwegian_Personal_Data_Act);
68
- regions.add(PrivacyRegion.Swiss_Federal_Act_on_Data_Protection);
69
- regions.add(PrivacyRegion.UK_General_Data_Protection_Regulation);
70
- break;
71
- case 'GB':
72
- regions.add(PrivacyRegion.UK_General_Data_Protection_Regulation);
73
- regions.add(PrivacyRegion.Canada_Personal_Information_Protection_and_Electronic_Documents_Act);
74
- regions.add(PrivacyRegion.General_Data_Protection_Regulation);
75
- regions.add(PrivacyRegion.Japan_Act_on_the_Protection_of_Personal_Information);
76
- regions.add(PrivacyRegion.Korean_Personal_Information_Protection_Act);
77
- regions.add(PrivacyRegion.Norwegian_Personal_Data_Act);
78
- regions.add(PrivacyRegion.Swiss_Federal_Act_on_Data_Protection);
79
- regions.add(PrivacyRegion.UK_General_Data_Protection_Regulation);
80
- break;
81
- }
82
- }
83
- if (continent) {
84
- switch (continent.toUpperCase()) {
85
- case 'EU':
86
- regions.add(PrivacyRegion.General_Data_Protection_Regulation);
87
- regions.add(PrivacyRegion.Canada_Personal_Information_Protection_and_Electronic_Documents_Act);
88
- regions.add(PrivacyRegion.Japan_Act_on_the_Protection_of_Personal_Information);
89
- regions.add(PrivacyRegion.Korean_Personal_Information_Protection_Act);
90
- regions.add(PrivacyRegion.Norwegian_Personal_Data_Act);
91
- regions.add(PrivacyRegion.Swiss_Federal_Act_on_Data_Protection);
92
- regions.add(PrivacyRegion.UK_General_Data_Protection_Regulation);
93
- break;
94
- }
95
- }
96
- return Array.from(regions);
97
- }
98
- closestServers(requiredCapability, userCoordinate = {
99
- lat: this.config.geoRouting?.userCoordinate?.lat ?? '0',
100
- lon: this.config.geoRouting?.userCoordinate?.lon ?? '0',
101
- }, privacyRegion = ServerSelector.determinePrivacyRegion(this.config.geoRouting?.country, this.config.geoRouting?.continent)) {
102
- // Skip over the rest of logic if the server can't handle the incoming request
103
- // @ts-expect-error it's always strings, just sometimes string literals
104
- const featureFilteredServers = requiredCapability ? Array.from(this.servers).filter((server) => server.languageModelAvailability.includes(requiredCapability) || server.textEmbeddingModelAvailability.includes(requiredCapability)) : Array.from(this.servers);
105
- if (featureFilteredServers.length > 0) {
106
- // Skip over servers not in the save privacy region except if undefined, then you can use any
107
- const privacyRegionFilteredServers = featureFilteredServers.filter((server) => privacyRegion.length === 0 || (server.region && privacyRegion.includes(server.region)));
108
- if (privacyRegionFilteredServers.length > 0) {
109
- // Calculate distance for each server and store it as a tuple [Server, distance]
110
- const serversWithDistances = privacyRegionFilteredServers.map((server) => {
111
- // Match decimal point length
112
- return [
113
- server,
114
- haversine({
115
- lat: Helpers.precisionFloat(userCoordinate.lat),
116
- lon: Helpers.precisionFloat(userCoordinate.lon),
117
- }, {
118
- lat: server.coordinate.lat,
119
- lon: server.coordinate.lon,
120
- }),
121
- ];
122
- });
123
- // Sort the servers by distance
124
- serversWithDistances.sort((a, b) => a[1] - b[1]);
125
- // Extract the ids of the sorted servers
126
- const sortedServers = serversWithDistances.map(([server]) => server);
127
- return sortedServers;
128
- }
129
- else {
130
- throw new Error(`No server with the capability ${requiredCapability} available in a region covered under ${JSON.stringify(privacyRegion)}`);
131
- }
132
- }
133
- else {
134
- throw new Error(`No server with the capability ${requiredCapability} available`);
135
- }
136
- }
137
- }
@@ -1,10 +0,0 @@
1
- import type { AzureChatModels, AzureEmbeddingModels, AzureImageModels, Coordinate } from '@chainfuse/types';
2
- import type { PrivacyRegion } from './base.mjs';
3
- export interface Server {
4
- id: string;
5
- coordinate: Coordinate;
6
- region?: PrivacyRegion;
7
- languageModelAvailability: AzureChatModels[] | string[];
8
- imageModelAvailability: AzureImageModels[] | string[];
9
- textEmbeddingModelAvailability: AzureEmbeddingModels[] | string[];
10
- }
@@ -1 +0,0 @@
1
- export {};