Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import type { InternalProviderAPIConfig } from '@shared/types/ai-providers/config';
import {
FunctionName,
type SuperAgentsTarget,
} from '@shared/types/api/request';
import { AIProvider } from '@shared/types/constants';
import { getAccessToken, getBucketAndFile, getModelAndProvider } from './utils';
const getApiVersion = (provider: string): string => {
if (provider === 'meta') return 'v1beta1';
return 'v1';
};
const getProjectRoute = (
saTarget: SuperAgentsTarget,
inputModel: string,
): string => {
const { vertex_project_id, vertex_region, vertex_service_account_json } =
saTarget;
let projectId = vertex_project_id;
if (vertex_service_account_json) {
projectId = (
vertex_service_account_json as unknown as { project_id: string }
).project_id;
}
const { provider } = getModelAndProvider(inputModel as string);
const routeVersion = getApiVersion(provider);
return `/${routeVersion}/projects/${projectId}/locations/${vertex_region}`;
};
const FILE_ENDPOINTS = [
'uploadFile',
'retrieveFileContent',
'deleteFile',
'listFiles',
'retrieveFile',
];
const BATCH_ENDPOINTS = [
'createBatch',
'retrieveBatch',
'getBatchOutput',
'listBatches',
'cancelBatch',
'createFinetune',
'retrieveFinetune',
'listFinetunes',
'cancelFinetune',
];
const NON_INFERENCE_ENDPOINTS = [...FILE_ENDPOINTS, ...BATCH_ENDPOINTS];
// Good reference for using REST: https://cloud.google.com/vertex-ai/generative-ai/docs/start/quickstarts/quickstart-multimodal#gemini-beginner-samples-drest
// Difference versus Studio AI: https://cloud.google.com/vertex-ai/docs/start/ai-platform-users
export const vertexAPIConfig: InternalProviderAPIConfig = {
getBaseURL: ({ saTarget, saRequestData }) => {
const { vertex_region } = saTarget;
if (FILE_ENDPOINTS.includes(saRequestData.functionName as string)) {
return `https://storage.googleapis.com`;
}
return `https://${vertex_region}-aiplatform.googleapis.com`;
},
headers: async ({ saTarget: providerOptions }) => {
const { api_key, vertex_service_account_json } = providerOptions;
let authToken = api_key;
if (vertex_service_account_json) {
authToken = await getAccessToken(
vertex_service_account_json as unknown as Record<string, string>,
);
}
return {
'Content-Type': 'application/json',
Authorization: `Bearer ${authToken}`,
};
},
getEndpoint: ({ saTarget, saRequestData }) => {
const { vertex_project_id, vertex_region, vertex_service_account_json } =
saTarget;
if (NON_INFERENCE_ENDPOINTS.includes(saRequestData.functionName)) {
const jobIdIndex = [
'cancelBatch',
'retrieveFileContent',
'cancelFinetune',
].includes(saRequestData.functionName)
? -2
: -1;
const jobId = saRequestData.url.split('/').at(jobIdIndex);
const url = new URL(saRequestData.url);
const searchParams = url.searchParams;
const pageSize = searchParams.get('limit') ?? 20;
const after = searchParams.get('after') ?? '';
let projectId = vertex_project_id;
if (!projectId || vertex_service_account_json) {
projectId = (
vertex_service_account_json as unknown as { project_id: string }
).project_id;
}
switch (saRequestData.functionName) {
case FunctionName.GET_BATCH_OUTPUT:
return `/v1/projects/${projectId}/locations/${vertex_region}/batchPredictionJobs/${jobId}`;
case FunctionName.LIST_BATCHES: {
return `/v1/projects/${projectId}/locations/${vertex_region}/batchPredictionJobs?pageSize=${pageSize}&pageToken=${after}`;
}
case FunctionName.CANCEL_BATCH: {
return `/v1/projects/${projectId}/locations/${vertex_region}/batchPredictionJobs/${jobId}:cancel`;
}
case FunctionName.UPLOAD_FILE:
// We handle file upload in a separate request handler
return '';
case FunctionName.RETRIEVE_FILE:
return '';
case FunctionName.RETRIEVE_FILE_CONTENT: {
const { bucket, file } = getBucketAndFile(jobId ?? '');
return `/${bucket}/${file}`;
}
case FunctionName.CREATE_BATCH:
return `/v1/projects/${projectId}/locations/${vertex_region}/batchPredictionJobs`;
case FunctionName.CREATE_FINE_TUNING_JOB:
return `/v1/projects/${projectId}/locations/${vertex_region}/tuningJobs`;
case FunctionName.LIST_FINE_TUNING_JOBS: {
const pageSize = searchParams.get('limit') ?? 20;
const after = searchParams.get('after') ?? '';
return `/v1/projects/${projectId}/locations/${vertex_region}/tuningJobs?pageSize=${pageSize}&pageToken=${after}`;
}
case FunctionName.RETRIEVE_FINE_TUNING_JOB:
return `/v1/projects/${projectId}/locations/${vertex_region}/tuningJobs/${jobId}`;
case FunctionName.CANCEL_FINE_TUNING_JOB: {
return `/v1/projects/${projectId}/locations/${vertex_region}/tuningJobs/${jobId}:cancel`;
}
}
}
// Manually doing logic (over .includes) for better static typing
else if (
saRequestData.functionName === FunctionName.CHAT_COMPLETE ||
saRequestData.functionName === FunctionName.STREAM_CHAT_COMPLETE ||
saRequestData.functionName === FunctionName.EMBED ||
saRequestData.functionName === FunctionName.GENERATE_IMAGE
) {
const model = saRequestData.requestBody?.model;
const innerProvider = saTarget.inner_provider;
const projectRoute = getProjectRoute(saTarget, model as string);
const googleUrlMap = new Map<string, string>([
[
'chatComplete',
`${projectRoute}/publishers/${innerProvider}/models/${model}:generateContent`,
],
[
'stream-chatComplete',
`${projectRoute}/publishers/${innerProvider}/models/${model}:streamGenerateContent?alt=sse`,
],
[
'embed',
`${projectRoute}/publishers/${innerProvider}/models/${model}:predict`,
],
[
'imageGenerate',
`${projectRoute}/publishers/${innerProvider}/models/${model}:predict`,
],
]);
switch (innerProvider) {
case AIProvider.GOOGLE: {
return (
googleUrlMap.get(saRequestData.functionName) || `${projectRoute}`
);
}
case AIProvider.ANTHROPIC: {
if (saRequestData.functionName === FunctionName.CHAT_COMPLETE) {
return `${projectRoute}/publishers/${innerProvider}/models/${model}:rawPredict`;
} else if (
saRequestData.functionName === FunctionName.STREAM_CHAT_COMPLETE
) {
return `${projectRoute}/publishers/${innerProvider}/models/${model}:streamRawPredict`;
}
return `${projectRoute}`;
}
// case AIProvider.META: {
// return `${projectRoute}/endpoints/openapi/chat/completions`;
// }
// case AIProvider.ENDPOINTS: {
// return `${projectRoute}/endpoints/${model}/chat/completions`;
// }
default:
return `${projectRoute}`;
}
}
throw new Error(`Unknown function name: ${saRequestData.functionName}`);
},
};
|