From 696b61d330598df180222959c56c18fcee706620 Mon Sep 17 00:00:00 2001 From: Tomas Pilar Date: Fri, 24 Nov 2023 08:08:13 +0100 Subject: [PATCH] feat(v2): switch to generated fetch API stub Signed-off-by: Tomas Pilar --- .env | 2 +- .eslintignore | 3 + .nvmrc | 2 +- .prettierignore | 2 + examples/generate.ts | 112 +- examples/load_input.ts | 12 +- examples/models.ts | 8 + package.json | 20 +- scripts/generate.sh | 4 + src/api-types.ts | 465 --- src/api/client.ts | 27 + src/api/schema.d.ts | 5163 ++++++++++++++++++++++++++ src/api/streaming-client.ts | 121 + src/{client => }/client.test.ts | 3 +- src/client.ts | 151 + src/client/cache.ts | 15 - src/client/client.ts | 1422 ------- src/client/types.ts | 288 -- src/constants.ts | 1 - src/errors.ts | 131 +- src/index.ts | 6 +- src/langchain/llm.ts | 2 +- src/tests/e2e/client.test.ts | 2 +- src/tests/integration/client.test.ts | 53 +- src/tests/integration/errors.test.ts | 46 + src/tests/mocks/handlers.ts | 12 + src/utils/errors.ts | 38 + src/{ => utils}/types.ts | 6 + yarn.lock | 234 +- 29 files changed, 5754 insertions(+), 2597 deletions(-) create mode 100644 examples/models.ts create mode 100755 scripts/generate.sh delete mode 100644 src/api-types.ts create mode 100644 src/api/client.ts create mode 100644 src/api/schema.d.ts create mode 100644 src/api/streaming-client.ts rename src/{client => }/client.test.ts (96%) create mode 100644 src/client.ts delete mode 100644 src/client/cache.ts delete mode 100644 src/client/client.ts delete mode 100644 src/client/types.ts delete mode 100644 src/constants.ts create mode 100644 src/tests/integration/errors.test.ts create mode 100644 src/utils/errors.ts rename src/{ => utils}/types.ts (57%) diff --git a/.env b/.env index 0188ba8..8b7c406 100644 --- a/.env +++ b/.env @@ -1 +1 @@ -GENAI_DEFAULT_ENDPOINT=https://workbench-api.res.ibm.com +GENAI_DEFAULT_ENDPOINT=https://bam-api.res.ibm.com diff --git a/.eslintignore b/.eslintignore index 81d3950..fa4e77b 100644 --- a/.eslintignore +++ b/.eslintignore @@ -2,3 +2,6 @@ /dist/**/* .eslintrc.cjs tsup.config.ts + +# generated files +/src/api/schema.d.ts \ No newline at end of file diff --git a/.nvmrc b/.nvmrc index b009dfb..68c98aa 100644 --- a/.nvmrc +++ b/.nvmrc @@ -1 +1 @@ -lts/* +v18.18.2 \ No newline at end of file diff --git a/.prettierignore b/.prettierignore index 1b9d86d..0a61adf 100644 --- a/.prettierignore +++ b/.prettierignore @@ -3,3 +3,5 @@ node_modules/ .yarn/ yarn.lock .pnp.* + +/src/api/schema.d.ts \ No newline at end of file diff --git a/examples/generate.ts b/examples/generate.ts index e044f94..d9858de 100644 --- a/examples/generate.ts +++ b/examples/generate.ts @@ -9,69 +9,67 @@ const client = new Client({ const multipleInputs = loadGenerateInput(); const singleInput = multipleInputs[0]; -{ - // Use with a single input to get a promise - const output = await client.generate(singleInput); - console.log(output); -} +// { +// // Use with a single input to get a promise +// const output = await client.generate(singleInput); +// console.log(output); +// } -{ - // Or supply a callback - client.generate(singleInput, (err, output) => { - if (err) console.error(err); - else console.log(output); - }); -} +// { +// // Or supply a callback +// client.generate(singleInput, (err, output) => { +// if (err) console.error(err); +// else console.log(output); +// }); +// } -{ - // Use with multiple inputs to get a promise - const outputs = await Promise.all(client.generate(multipleInputs)); - console.log(outputs); +// { +// // Use with multiple inputs to get a promise +// const outputs = await Promise.all(client.generate(multipleInputs)); +// console.log(outputs); - // Or supply a callback which will be called for each output - // Callback is guaranteed to be called in the order of respective inputs - client.generate(multipleInputs, (err, output) => { - if (err) console.error(err); - else console.log(output); - }); +// // Or supply a callback which will be called for each output +// // Callback is guaranteed to be called in the order of respective inputs +// client.generate(multipleInputs, (err, output) => { +// if (err) console.error(err); +// else console.log(output); +// }); - // The method is optimized for sequential await, order the inputs accordingly - for (const outputPromise of client.generate(multipleInputs)) { - try { - console.log(await outputPromise); - } catch (err) { - console.error(err); - } - } -} +// // The method is optimized for sequential await, order the inputs accordingly +// for (const outputPromise of client.generate(multipleInputs)) { +// try { +// console.log(await outputPromise); +// } catch (err) { +// console.error(err); +// } +// } +// } -{ - // Streaming (callback style) - client.generate( - singleInput, - { - stream: true, - }, - (err, output) => { - if (err) { - console.error(err); - } else if (output === null) { - // END of stream - } else { - console.log(output.stop_reason); - console.log(output.generated_token_count); - console.log(output.input_token_count); - console.log(output.generated_text); - } - }, - ); -} +// { +// // Streaming (callback style) +// client.generate( +// singleInput, +// { +// stream: true, +// }, +// (err, output) => { +// if (err) { +// console.error(err); +// } else if (output === null) { +// // END of stream +// } else { +// console.log(output.stop_reason); +// console.log(output.generated_token_count); +// console.log(output.input_token_count); +// console.log(output.generated_text); +// } +// }, +// ); +// } { // Streaming (async iterators) - const stream = client.generate(singleInput, { - stream: true, - }); + const stream = client.generation_stream(singleInput); for await (const chunk of stream) { console.log(chunk.stop_reason); console.log(chunk.generated_token_count); @@ -82,9 +80,7 @@ const singleInput = multipleInputs[0]; { // Streaming (built-in stream methods) - const stream = client.generate(singleInput, { - stream: true, - }); + const stream = client.generation_stream(singleInput); stream.on('data', (chunk) => { console.log(chunk.stop_reason); console.log(chunk.generated_token_count); diff --git a/examples/load_input.ts b/examples/load_input.ts index 6f35588..10aa590 100644 --- a/examples/load_input.ts +++ b/examples/load_input.ts @@ -1,15 +1,11 @@ import { readFileSync } from 'fs'; -import { GenerateInputSchema } from '../src/index.js'; - export const loadGenerateInput = () => // Input files usually follow JSONL format readFileSync('examples/assets/generate_input.jsonl', 'utf8') .split('\n') .map((line) => JSON.stringify(line)) - .map((input) => - GenerateInputSchema.parse({ - model_id: 'default', - input, - }), - ); + .map((input) => ({ + model_id: 'google/flan-ul2', + input, + })); diff --git a/examples/models.ts b/examples/models.ts new file mode 100644 index 0000000..21af2e7 --- /dev/null +++ b/examples/models.ts @@ -0,0 +1,8 @@ +import { Client } from '../src/index.js'; + +const client = new Client({ + apiKey: process.env.GENAI_API_KEY, +}); + +const models = await client.models({ limit: 100, offset: 0 }); +console.log(models); diff --git a/package.json b/package.json index 2be7976..0c0fc27 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "genai", "ibm" ], - "homepage": "https://workbench.res.ibm.com", + "homepage": "https://bam.res.ibm.com", "license": "MIT", "repository": { "type": "git", @@ -56,12 +56,14 @@ "prepack": "yarn build && pinst --disable", "postinstall": "husky install", "postpack": "pinst --enable", + "generate": "./scripts/generate.sh", "example:run": "ts-node -r dotenv-flow/config", "example:generate": "yarn run example:run examples/generate.ts", "example:tune": "yarn run example:run examples/tune.ts", "example:prompt-template": "yarn run example:run examples/prompt-templates.ts", "example:file": "yarn run example:run examples/file.ts", - "example:chat": "yarn run example:run examples/chat.ts" + "example:chat": "yarn run example:run examples/chat.ts", + "example:models": "yarn run example:run examples/models.ts" }, "peerDependencies": { "@langchain/core": ">=0.1.11" @@ -71,8 +73,7 @@ "@commitlint/config-conventional": "^18.0.0", "@langchain/core": "^0.1.11", "@types/lodash": "^4.14.200", - "@types/node": "^20.8.8", - "@types/promise-retry": "^1.1.5", + "@types/node": "18.18.2", "@typescript-eslint/eslint-plugin": "^6.9.0", "@typescript-eslint/parser": "^6.9.0", "@vitest/coverage-c8": "^0.31.2", @@ -87,6 +88,7 @@ "lint-staged": "^15.0.2", "lodash": "^4.17.21", "msw": "^1.3.2", + "openapi-typescript": "^6.7.1", "pinst": "^3.0.0", "prettier": "^3.0.3", "ts-node": "^10.9.1", @@ -96,16 +98,12 @@ }, "dependencies": { "@ai-zen/node-fetch-event-source": "^2.1.2", - "axios": "^1.5.1", - "axios-cache-interceptor": "^1.3.2", - "form-data": "^4.0.0", - "promise-retry": "^2.0.1", + "cross-fetch": "^4.0.0", + "fetch-retry": "^5.0.6", + "openapi-fetch": "^0.8.1", "yaml": "^2.3.3", "zod": "^3.22.4" }, - "engines": { - "node": ">=16.10.0" - }, "lint-staged": { "*.{cjs,js,jsx,ts,tsx}": [ "eslint --fix" diff --git a/scripts/generate.sh b/scripts/generate.sh new file mode 100755 index 0000000..0df083b --- /dev/null +++ b/scripts/generate.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +source .env +openapi-typescript $GENAI_DEFAULT_ENDPOINT/docs/json -o ./src/api/schema.d.ts \ No newline at end of file diff --git a/src/api-types.ts b/src/api-types.ts deleted file mode 100644 index 0e10247..0000000 --- a/src/api-types.ts +++ /dev/null @@ -1,465 +0,0 @@ -/* eslint-disable @typescript-eslint/no-explicit-any */ - -import { z } from 'zod'; -import type FormData from 'form-data'; - -// COMMON - -const PaginationOutputSchema = z.object({ - totalCount: z.number().int().nonnegative(), - results: z.array(z.any()), -}); - -// ERRORS - -export interface ErrorExtensions { - code: string; - reason?: string; - state?: Record; -} - -export interface ErrorResponse { - status_code: number; - error: string; - message: string; - extensions?: ErrorExtensions; -} - -// USER - -export interface UserOutput { - results: { - firstName?: string; - lastName?: string; - tou_accepted: boolean; - tou_accepted_at?: string; - generate_default?: { - model_id: string; - parameters: Record; - }; - data_usage_consent: boolean; - }; -} - -export const UserGenerateDefaultInputSchema = z.object({ - model_id: z.string(), - parameters: z.optional(z.record(z.any())), -}); - -export type UserGenerateDefaultInput = z.infer< - typeof UserGenerateDefaultInputSchema ->; - -export interface UserGenerateDefaultOutput { - results: { - generate_default?: { - model_id: string; - parameters: Record; - }; - }; -} - -// GENERATE - -const ParametersSchema = z.record(z.any()); - -export const GenerateInputSchema = z.object({ - model_id: z.string().nullish(), - prompt_id: z.string().nullish(), - inputs: z.array(z.string()), - parameters: z.optional(ParametersSchema), - use_default: z.optional(z.boolean()), -}); -export type GenerateInput = z.infer; - -export const GenerateStopReasonSchema = z.enum([ - 'NOT_FINISHED', - 'MAX_TOKENS', - 'EOS_TOKEN', - 'CANCELLED', - 'TIME_LIMIT', - 'STOP_SEQUENCE', - 'TOKEN_LIMIT', - 'ERROR', -]); -export type GenerateStopReason = z.infer; - -const GenerateModerationSchema = z - .object({ - hap: z.optional( - z.array( - z - .object({ - success: z.boolean(), - flagged: z.boolean(), - score: z.number().min(0).max(1), - position: z.object({ - start: z.number().int().min(0), - stop: z.number().int().min(0), - }), - }) - .passthrough(), - ), - ), - }) - .passthrough(); - -export const GenerateResultSchema = z - .object({ - generated_text: z.string(), - generated_token_count: z.number().int().min(0), - input_token_count: z.number().int().min(0), - stop_reason: GenerateStopReasonSchema, - }) - .passthrough(); -export type GenerateResult = z.infer; - -export const GenerateOutputSchema = z - .object({ - model_id: z.string(), - created_at: z.coerce.date(), - results: z.array(GenerateResultSchema), - moderation: GenerateModerationSchema.optional(), - }) - .passthrough(); -export type GenerateOutput = z.infer; - -export const GenerateLimitsOutputSchema = z.object({ - tokenCapacity: z.number().int().nonnegative(), - tokensUsed: z.number().int().nonnegative(), -}); -export type GenerateLimitsOutput = z.output; - -export const GenerateConfigInputSchema = z.object({ - model_id: z.optional(z.string()), - parameters: z.optional(z.record(z.any())), -}); - -export type GenerateConfigInput = z.infer; - -export const GenerateConfigOutputSchema = z.object({ - model_id: z.string().nullish(), - parameters: z.record(z.any()).nullish(), -}); -export type GenerateConfigOutput = z.output; - -export const TokenizeInputSchema = z.object({ - model_id: z.string().nullish(), - inputs: z.array(z.string()), - use_default: z.optional(z.boolean()), - parameters: z.optional(z.object({ return_tokens: z.optional(z.boolean()) })), -}); - -export type TokenizeInput = z.infer; - -export const TokenizeOutputSchema = z.object({ - model_id: z.string(), - created_at: z.string(), - results: z.array( - z.object({ - token_count: z.number().int().nonnegative(), - tokens: z.array(z.string()), - }), - ), -}); -export type TokenizeOutput = z.output; - -// MODELS - -export const ModelsOutputSchema = z.object({ - results: z.array( - z.object({ - id: z.string(), - name: z.string(), - size: z.string(), - token_limit: z.number().int().nonnegative(), - }), - ), -}); -export type ModelsOutput = z.output; - -const ModelSchemaSchema = z.object({ id: z.number().int(), value: z.any() }); - -export const ModelOutputSchema = z.object({ - results: z.object({ - id: z.string(), - name: z.string(), - size: z.string(), - description: z.string(), - token_limit: z.number().int().nonnegative(), - tags: z.array(z.string()), - source_model_id: z.string().nullable(), - tasks: z.array( - z.object({ - id: z.string(), - name: z.string(), - json_example: z.string(), - jsonl_example: z.string(), - }), - ), - model_family: z.object({ - id: z.number().int(), - name: z.string(), - short_description: z.string().nullish(), - description: z.string().nullish(), - }), - schema_generate: ModelSchemaSchema, - schema_tokenize: ModelSchemaSchema, - }), -}); -export type ModelOutput = z.output; - -// TUNES - -export const TuneStatusSchema = z.enum([ - 'INITIALIZING', - 'NOT_STARTED', - 'PENDING', - 'HALTED', - 'RUNNING', - 'QUEUED', - 'COMPLETED', - 'FAILED', -]); -export type TuneStatus = z.infer; - -const TuneFileSchema = z.object({ - id: z.string(), - file_name: z.string(), - created_at: z.string(), -}); - -const TuneMixinSchema = z.object({ - id: z.string(), - name: z.string(), - model_id: z.string(), - method_id: z.string(), - model_name: z.string(), - status: TuneStatusSchema, - task_id: z.string(), - parameters: z.object({ - batch_size: z.number().int().positive(), - num_epochs: z.number().int().positive(), - }), - created_at: z.string(), -}); -type TuneMixin = z.infer; - -export interface TunesOuput { - results: TuneMixin[]; - totalCount: number; -} - -export const TuneInputSchema = z.object({ - name: z.string(), - model_id: z.string(), - task_id: z.string(), - training_file_ids: z.array(z.string()), - validation_file_ids: z.array(z.string()).nullish(), - evaluation_file_ids: z.array(z.string()).nullish(), - method_id: z.string(), - parameters: z.record(z.any()).nullish(), -}); -export type TuneInput = z.input; - -export const TuneOutputSchema = z.object({ - results: TuneMixinSchema.extend({ - validation_files: z.array(TuneFileSchema).nullish(), - training_files: z.array(TuneFileSchema).nullish(), - evaluation_files: z.array(TuneFileSchema).nullish(), - datapoints: z - .object({ - loss: z.array( - z.object({ - data: z.any(), - timestamp: z.string(), - }), - ), - }) - .nullish(), - }), -}); -export type TuneOutput = z.output; - -export const TuneMethodsOutputSchema = z.object({ - results: z.array( - z.object({ - id: z.string(), - name: z.string(), - }), - ), -}); -export type TuneMethodsOutput = z.output; - -// PROMPT TEMPLATES - -export const PromptTemplateInputSchema = z - .object({ - id: z.string(), - }) - .strict(); - -export type PromptTemplateInput = z.output; - -export const PromptTemplateCreateInputSchema = z - .object({ - name: z.string(), - value: z.string(), - }) - .strict(); - -export type PromptTemplateCreateInput = z.input< - typeof PromptTemplateCreateInputSchema ->; - -export const PromptTemplateUpdateInputSchema = PromptTemplateCreateInputSchema; -export type PromptTemplateUpdate = z.input< - typeof PromptTemplateUpdateInputSchema ->; - -const SinglePromptTemplateOutputSchema = z - .object({ - id: z.string(), - name: z.string(), - value: z.string(), - created_at: z.coerce.date(), - }) - .passthrough(); - -export const PromptTemplateOutputSchema = z.object({ - results: SinglePromptTemplateOutputSchema, -}); -export type PromptTemplateOutput = z.infer; - -export const PromptTemplatesOutputSchema = PaginationOutputSchema.extend({ - results: z.array(SinglePromptTemplateOutputSchema), -}).passthrough(); -export type PromptTemplatesOutput = z.infer; - -export const PromptTemplateExecuteInputSchema = z.object({ - inputs: z.array(z.string()), - template: z.union([ - z.object({ id: z.string() }), - z.object({ - value: z.string(), - data: z.object({}).passthrough(), - }), - ]), -}); -export type PromptTemplateExecuteInput = z.input< - typeof PromptTemplateExecuteInputSchema ->; - -export const PromptTemplateExecuteOutputSchema = z.object({ - results: z.array(z.string()), -}); -export type PromptTemplateExecuteOutput = z.infer< - typeof PromptTemplateExecuteOutputSchema ->; - -// HISTORY - -export const HistoryStatusSchema = z.enum(['SUCCESS', 'ERROR']); -export type HistoryStatus = z.infer; - -export const HistoryOriginSchema = z.enum(['API', 'UI']); -export type HistoryOrigin = z.infer; - -export const HistoryInputSchema = z - .object({ - status: HistoryStatusSchema, - origin: HistoryOriginSchema, - }) - .partial(); -export type HistoryInput = z.input; - -export const HistoryOutputSchema = PaginationOutputSchema.extend({ - results: z.array( - z - .object({ - id: z.string(), - duration: z.number().int().min(0), - request: GenerateInputSchema.partial(), - status: HistoryInputSchema.shape.status, - created_at: z.coerce.date(), - response: GenerateOutputSchema.nullable(), - }) - .passthrough(), - ), -}); -export type HistoryOutput = z.infer; - -// FILES - -export const FilePurposeSchema = z.enum(['tune', 'template', 'tune_import']); -export type FilePurpose = z.infer; - -export const FileInputSchema = z - .object({ - id: z.string(), - }) - .strict(); -export type FileInput = z.input; - -export const FileCreateInputSchema = z.custom(); -export type FileCreateInput = z.input; - -const SingleFileOutputSchema = z - .object({ - id: z.string(), - file_name: z.string(), - purpose: FilePurposeSchema, - created_at: z.coerce.date(), - }) - .passthrough(); - -export const FileOutputSchema = z.object({ - results: SingleFileOutputSchema, -}); -export type FileOutput = z.output; - -export const FilesOutputSchema = PaginationOutputSchema.extend({ - results: z.array(SingleFileOutputSchema), -}); -export type FilesOutput = z.output; - -// CHAT - -export const ChatRoleSchema = z.enum(['user', 'system', 'assistant']); -export type ChatRole = z.infer; - -export const ChatInputSchema = z.object({ - model_id: z.string(), - messages: z.array( - z.object({ - role: ChatRoleSchema, - content: z.string(), - }), - ), - conversation_id: z.string().nullish(), - parent_id: z.string().nullish(), - prompt_id: z.string().nullish(), - parameters: ParametersSchema.nullish(), -}); -export type ChatInput = z.input; -export const ChatOutputSchema = z - .object({ - id: z.string(), - model_id: z.string(), - created_at: z.coerce.date(), - conversation_id: z.string(), - results: z.array( - z - .object({ - generated_text: z.string(), - }) - .partial(), - ), - }) - .passthrough(); -export type ChatOutput = z.output; - -export const ChatStreamInputSchema = ChatInputSchema; -export type ChatStreamInput = z.input; -export const ChatStreamOutputSchema = ChatOutputSchema; -export type ChatStreamOutput = z.output; diff --git a/src/api/client.ts b/src/api/client.ts new file mode 100644 index 0000000..af81056 --- /dev/null +++ b/src/api/client.ts @@ -0,0 +1,27 @@ +import createClient, { FetchOptions, FetchResponse } from 'openapi-fetch'; + +import { FilterKeys } from '../utils/types.js'; + +import { components, paths } from './schema.js'; + +export type ApiClient = ReturnType>; + +export function createApiClient( + ...params: Parameters> +): ApiClient { + return createClient(...params); +} + +export type ApiClientOptions< + Method extends keyof ApiClient, + Path extends Parameters[0], +> = FetchOptions>>; + +export type ApiClientResponse< + Method extends keyof ApiClient, + Path extends Parameters[0] = Parameters< + ApiClient[Method] + >[0], +> = FetchResponse>>; + +export type ApiError = components['schemas']['BaseErrorResponse']; diff --git a/src/api/schema.d.ts b/src/api/schema.d.ts new file mode 100644 index 0000000..f74470e --- /dev/null +++ b/src/api/schema.d.ts @@ -0,0 +1,5163 @@ +/** + * This file was auto-generated by openapi-typescript. + * Do not make direct changes to the file. + */ + + +export interface paths { + "/v2/text/tokenization": { + post: { + parameters: { + query: { + version: "2024-01-10"; + }; + }; + requestBody?: { + content: { + "application/json": { + model_id?: string; + prompt_id?: string; + input?: string | string[]; + data?: { + example_file_ids?: string[]; + [key: string]: unknown; + }; + parameters?: ({ + return_options?: { + input_text?: boolean | null; + tokens?: boolean | null; + }; + }) | null; + }; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + model_id: string; + created_at: string; + results: ({ + token_count: number; + tokens?: string[]; + input_text?: string | null; + })[]; + }; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["schemas"]["NotFoundResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + }; + "/v2/text/generation_stream": { + post: { + parameters: { + query: { + version: "2024-01-10"; + }; + }; + requestBody?: { + content: { + "application/json": { + data?: { + example_file_ids?: string[]; + [key: string]: unknown; + }; + /** + * Input string + * @description The input is the prompt to generate completions, passed as a string. Note: The method tokenizes the input internally. It is recommended not to leave any trailing spaces. + */ + input?: string; + /** + * Model ID + * @description The ID of the model or tune to be used for this request. + */ + model_id?: string; + /** Saved prompt Id */ + prompt_id?: string; + parameters?: ({ + /** + * Top K + * @description Set the number of highest probability vocabulary tokens to keep for top-k-filtering. Lower values make it less likely the model will go off topic. + */ + top_k?: number | null; + /** + * Top P (nucleus sampling) + * @description If < 1.0, only the smallest set of most probable tokens with probabilities that add up to `top_p` or higher are used. + */ + top_p?: number | null; + /** + * Typical P + * @description Local typicality measures how similar the conditional probability of predicting a target token next is to the expected conditional probability of predicting a random token next, given the partial text already generated. If set to float < 1, the smallest set of the most locally typical tokens with probabilities that add up to typical_p or higher are kept for generation. 1.00 means a neutral value. + */ + typical_p?: number | null; + /** + * Beam width + * @description At each step, or token, the algorithm keeps track of the n (off=1, 2, or 3) most probable sequences (beams) and selects the one with the highest probability. This continues until the stop sequence is met. + */ + beam_width?: number | null; + /** + * Time limit + * @description Time limit in milliseconds - if not completed within this time, generation will stop. The text generated so far will be returned along with the `TIME_LIMIT` stop reason. + */ + time_limit?: number | null; + /** + * Random seed + * @description Controls the random sampling of the generated tokens when sampling is enabled. Setting the random seed to a the same number for each generation ensures experimental repeatability. + */ + random_seed?: number | null; + /** + * Temperature + * @description Control the creativity of generated text. Higher values will lead to more randomly generated outputs. + */ + temperature?: number | null; + /** + * Length penalty + * @description Can be used to exponentially increase the likelihood of the text generation terminating once a specified number of tokens have been generated. + */ + length_penalty?: ({ + /** + * Start index + * @description A number of generated tokens after which this should take effect. + */ + start_index?: number | null; + /** + * Decay factor + * @description Represents the factor of exponential decay and must be > 1.0. Larger values correspond to more aggressive decay. + */ + decay_factor?: number | null; + }) | null; + /** + * Max new tokens + * @description Define the maximum number of tokens to generate. + */ + max_new_tokens?: number | null; + /** + * Min new tokens + * @description If stop sequences are given, they are ignored until minimum tokens are generated. + */ + min_new_tokens?: number | null; + return_options?: ({ + /** + * Input text + * @description Include input text + * @default false + */ + input_text?: boolean | null; + /** + * Token ranks + * @description Include rank of each returned token + * @default false + */ + token_ranks?: boolean | null; + /** + * Input Tokens + * @description Include list of input tokens + * @default false + */ + input_tokens?: boolean | null; + /** + * Top N tokens + * @description Include top n candidate tokens at the position of each returned token + */ + top_n_tokens?: number | null; + /** + * Token logprobs + * @description Include logprob for each returned token + * @default false + */ + token_logprobs?: boolean | null; + /** + * Generated Tokens + * @description Include list of individual generated tokens + * @default false + */ + generated_tokens?: boolean | null; + input_parameters?: boolean | null; + }) | null; + /** + * Stop sequences + * @description Stop sequences are one or more strings which will cause the text generation to stop if/when they are produced as part of the output. Stop sequences encountered prior to the minimum number of tokens being generated will be ignored. + */ + stop_sequences?: string[] | null; + /** + * Decoding method + * @description Decoding method used for generation. + * @enum {string|null} + */ + decoding_method?: "greedy" | "sample" | null; + /** + * Repetition penalty + * @description The parameter for repetition penalty. 1.00 means no penalty. + */ + repetition_penalty?: number | null; + additionalProperties?: never; + include_stop_sequence?: boolean; + /** + * Truncate input tokens + * @description Truncate to this many input tokens. Can be used to avoid requests failing due to input being longer than configured limits. Zero means don't truncate. + */ + truncate_input_tokens?: number | null; + }) | null; + moderations?: { + hap?: boolean | { + /** + * @description Detects HAP (hateful, abusive, or profane language). Please see documentation for more info (API Reference -> Moderations -> HAP). + * @default true + */ + input?: boolean; + output?: boolean; + /** + * @description The higher the number, the more confidence that the sentence contains HAP. The threshold allows you to modify how much confidence is needed for the sentence to be flagged as containing HAP. + * @default 0.75 + */ + threshold?: number; + /** @default false */ + send_tokens?: boolean; + }; + stigma?: boolean | { + /** + * @description Detects Stigma. + * @default true + */ + input?: boolean; + output?: boolean; + /** + * @description The higher the number, the more confidence that the sentence contains Stigma. The threshold allows you to modify how much confidence is needed for the sentence to be flagged as containing Stigma. + * @default 0.75 + */ + threshold?: number; + /** @default false */ + send_tokens?: boolean; + }; + implicit_hate?: boolean | { + /** + * @description Detects implicit hate. + * @default true + */ + input?: boolean; + output?: boolean; + /** + * @description The higher the number, the more confidence that the sentence contains implicit hate. The threshold allows you to modify how much confidence is needed for the sentence to be flagged as containing implicit hate. + * @default 0.75 + */ + threshold?: number; + /** @default false */ + send_tokens?: boolean; + }; + }; + }; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "text/event-stream": { + id?: string | null; + model_id: string; + /** Format: date-time */ + created_at?: string; + input_parameters?: { + [key: string]: unknown; + } | null; + results?: ({ + input_text?: string | null; + generated_text: string; + generated_token_count: number; + input_token_count?: number | null; + /** @enum {string} */ + stop_reason: "not_finished" | "max_tokens" | "eos_token" | "cancelled" | "time_limit" | "stop_sequence" | "token_limit" | "error"; + stop_sequence?: string | null; + generated_tokens?: (({ + text?: string | null; + logprob?: (number | null) | (string | null); + rank?: number | null; + top_tokens?: (({ + text?: string | null; + logprob?: (number | null) | (string | null); + })[]) | null; + })[]) | null; + input_tokens?: (({ + text?: string | null; + logprob?: (number | null) | (string | null); + rank?: number | null; + top_tokens?: (({ + text?: string | null; + logprob?: (number | null) | (string | null); + })[]) | null; + })[]) | null; + seed?: number | null; + [key: string]: unknown; + })[]; + moderation?: { + hap?: { + success: boolean; + score: number; + flagged: boolean; + position: { + start: number; + end: number; + }; + tokens?: { + token?: string; + index?: number; + score?: number; + }[]; + }[]; + stigma?: { + success: boolean; + score: number; + flagged: boolean; + position: { + start: number; + end: number; + }; + tokens?: { + token?: string; + index?: number; + score?: number; + }[]; + }[]; + implicit_hate?: { + success: boolean; + score: number; + flagged: boolean; + position: { + start: number; + end: number; + }; + tokens?: { + token?: string; + index?: number; + score?: number; + }[]; + }[]; + }; + }; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["schemas"]["NotFoundResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + }; + "/v2/text/generation": { + post: { + parameters: { + query: { + version: "2024-01-10"; + }; + }; + requestBody?: { + content: { + "application/json": { + data?: { + example_file_ids?: string[]; + [key: string]: unknown; + }; + /** + * Input string + * @description The input is the prompt to generate completions, passed as a string. Note: The method tokenizes the input internally. It is recommended not to leave any trailing spaces. + */ + input?: string; + /** + * Model ID + * @description The ID of the model or tune to be used for this request. + */ + model_id?: string; + /** Saved prompt Id */ + prompt_id?: string; + parameters?: ({ + /** + * Top K + * @description Set the number of highest probability vocabulary tokens to keep for top-k-filtering. Lower values make it less likely the model will go off topic. + */ + top_k?: number | null; + /** + * Top P (nucleus sampling) + * @description If < 1.0, only the smallest set of most probable tokens with probabilities that add up to `top_p` or higher are used. + */ + top_p?: number | null; + /** + * Typical P + * @description Local typicality measures how similar the conditional probability of predicting a target token next is to the expected conditional probability of predicting a random token next, given the partial text already generated. If set to float < 1, the smallest set of the most locally typical tokens with probabilities that add up to typical_p or higher are kept for generation. 1.00 means a neutral value. + */ + typical_p?: number | null; + /** + * Beam width + * @description At each step, or token, the algorithm keeps track of the n (off=1, 2, or 3) most probable sequences (beams) and selects the one with the highest probability. This continues until the stop sequence is met. + */ + beam_width?: number | null; + /** + * Time limit + * @description Time limit in milliseconds - if not completed within this time, generation will stop. The text generated so far will be returned along with the `TIME_LIMIT` stop reason. + */ + time_limit?: number | null; + /** + * Random seed + * @description Controls the random sampling of the generated tokens when sampling is enabled. Setting the random seed to a the same number for each generation ensures experimental repeatability. + */ + random_seed?: number | null; + /** + * Temperature + * @description Control the creativity of generated text. Higher values will lead to more randomly generated outputs. + */ + temperature?: number | null; + /** + * Length penalty + * @description Can be used to exponentially increase the likelihood of the text generation terminating once a specified number of tokens have been generated. + */ + length_penalty?: ({ + /** + * Start index + * @description A number of generated tokens after which this should take effect. + */ + start_index?: number | null; + /** + * Decay factor + * @description Represents the factor of exponential decay and must be > 1.0. Larger values correspond to more aggressive decay. + */ + decay_factor?: number | null; + }) | null; + /** + * Max new tokens + * @description Define the maximum number of tokens to generate. + */ + max_new_tokens?: number | null; + /** + * Min new tokens + * @description If stop sequences are given, they are ignored until minimum tokens are generated. + */ + min_new_tokens?: number | null; + return_options?: ({ + /** + * Input text + * @description Include input text + * @default false + */ + input_text?: boolean | null; + /** + * Token ranks + * @description Include rank of each returned token + * @default false + */ + token_ranks?: boolean | null; + /** + * Input Tokens + * @description Include list of input tokens + * @default false + */ + input_tokens?: boolean | null; + /** + * Top N tokens + * @description Include top n candidate tokens at the position of each returned token + */ + top_n_tokens?: number | null; + /** + * Token logprobs + * @description Include logprob for each returned token + * @default false + */ + token_logprobs?: boolean | null; + /** + * Generated Tokens + * @description Include list of individual generated tokens + * @default false + */ + generated_tokens?: boolean | null; + input_parameters?: boolean | null; + }) | null; + /** + * Stop sequences + * @description Stop sequences are one or more strings which will cause the text generation to stop if/when they are produced as part of the output. Stop sequences encountered prior to the minimum number of tokens being generated will be ignored. + */ + stop_sequences?: string[] | null; + /** + * Decoding method + * @description Decoding method used for generation. + * @enum {string|null} + */ + decoding_method?: "greedy" | "sample" | null; + /** + * Repetition penalty + * @description The parameter for repetition penalty. 1.00 means no penalty. + */ + repetition_penalty?: number | null; + additionalProperties?: never; + include_stop_sequence?: boolean; + /** + * Truncate input tokens + * @description Truncate to this many input tokens. Can be used to avoid requests failing due to input being longer than configured limits. Zero means don't truncate. + */ + truncate_input_tokens?: number | null; + }) | null; + moderations?: { + hap?: boolean | { + /** + * @description Detects HAP (hateful, abusive, or profane language). Please see documentation for more info (API Reference -> Moderations -> HAP). + * @default true + */ + input?: boolean; + output?: boolean; + /** + * @description The higher the number, the more confidence that the sentence contains HAP. The threshold allows you to modify how much confidence is needed for the sentence to be flagged as containing HAP. + * @default 0.75 + */ + threshold?: number; + /** @default false */ + send_tokens?: boolean; + }; + stigma?: boolean | { + /** + * @description Detects Stigma. + * @default true + */ + input?: boolean; + output?: boolean; + /** + * @description The higher the number, the more confidence that the sentence contains Stigma. The threshold allows you to modify how much confidence is needed for the sentence to be flagged as containing Stigma. + * @default 0.75 + */ + threshold?: number; + /** @default false */ + send_tokens?: boolean; + }; + implicit_hate?: boolean | { + /** + * @description Detects implicit hate. + * @default true + */ + input?: boolean; + output?: boolean; + /** + * @description The higher the number, the more confidence that the sentence contains implicit hate. The threshold allows you to modify how much confidence is needed for the sentence to be flagged as containing implicit hate. + * @default 0.75 + */ + threshold?: number; + /** @default false */ + send_tokens?: boolean; + }; + }; + }; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + id: string | null; + model_id: string; + /** Format: date-time */ + created_at: string; + input_parameters?: { + [key: string]: unknown; + } | null; + results: ({ + input_text?: string | null; + generated_text: string; + generated_token_count: number; + input_token_count?: number | null; + /** @enum {string} */ + stop_reason: "not_finished" | "max_tokens" | "eos_token" | "cancelled" | "time_limit" | "stop_sequence" | "token_limit" | "error"; + stop_sequence?: string | null; + generated_tokens?: (({ + text?: string | null; + logprob?: (number | null) | (string | null); + rank?: number | null; + top_tokens?: (({ + text?: string | null; + logprob?: (number | null) | (string | null); + })[]) | null; + })[]) | null; + input_tokens?: (({ + text?: string | null; + logprob?: (number | null) | (string | null); + rank?: number | null; + top_tokens?: (({ + text?: string | null; + logprob?: (number | null) | (string | null); + })[]) | null; + })[]) | null; + seed?: number | null; + moderation?: { + [key: string]: unknown; + } | null; + [key: string]: unknown; + })[]; + }; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["schemas"]["NotFoundResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + }; + "/v2/text/chat_stream": { + post: { + parameters: { + query: { + version: "2024-01-10"; + }; + }; + requestBody?: { + content: { + "application/json": { + model_id?: string; + prompt_template_id?: string | null; + moderations?: { + hap?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + stigma?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + implicit_hate?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + }; + messages?: ({ + /** @enum {string} */ + role: "user" | "system" | "assistant"; + content: string; + file_ids?: string[]; + })[]; + conversation_id?: string | null; + parent_id?: string | null; + prompt_id?: string; + /** @enum {string} */ + trim_method?: "floating_window" | "none"; + use_conversation_parameters?: boolean; + parameters?: ({ + beam_width?: number | null; + /** @enum {string|null} */ + decoding_method?: "greedy" | "sample" | null; + max_new_tokens?: number | null; + min_new_tokens?: number | null; + random_seed?: number | null; + stop_sequences?: string[] | null; + temperature?: number | null; + time_limit?: number | null; + top_k?: number | null; + top_p?: number | null; + typical_p?: number | null; + repetition_penalty?: number | null; + truncate_input_tokens?: number | null; + include_stop_sequence?: boolean; + return_options?: ({ + generated_tokens?: boolean | null; + input_text?: boolean | null; + input_tokens?: boolean | null; + input_parameters?: boolean | null; + token_logprobs?: boolean | null; + token_ranks?: boolean | null; + top_n_tokens?: number | null; + }) | null; + length_penalty?: ({ + decay_factor?: number | null; + start_index?: number | null; + }) | null; + }) | null; + }; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "text/event-stream": { + id?: string | null; + model_id?: string; + /** Format: date-time */ + created_at?: string; + input_parameters?: { + [key: string]: unknown; + } | null; + results?: ({ + input_text?: string | null; + generated_text: string; + generated_token_count: number; + input_token_count?: number | null; + /** @enum {string} */ + stop_reason: "not_finished" | "max_tokens" | "eos_token" | "cancelled" | "time_limit" | "stop_sequence" | "token_limit" | "error"; + stop_sequence?: string | null; + generated_tokens?: (({ + text?: string | null; + logprob?: (number | null) | (string | null); + rank?: number | null; + top_tokens?: (({ + text?: string | null; + logprob?: (number | null) | (string | null); + })[]) | null; + })[]) | null; + input_tokens?: (({ + text?: string | null; + logprob?: (number | null) | (string | null); + rank?: number | null; + top_tokens?: (({ + text?: string | null; + logprob?: (number | null) | (string | null); + })[]) | null; + })[]) | null; + seed?: number | null; + [key: string]: unknown; + })[]; + moderation?: { + hap?: { + success: boolean; + score: number; + flagged: boolean; + position: { + start: number; + end: number; + }; + tokens?: { + token?: string; + index?: number; + score?: number; + }[]; + }[]; + stigma?: { + success: boolean; + score: number; + flagged: boolean; + position: { + start: number; + end: number; + }; + tokens?: { + token?: string; + index?: number; + score?: number; + }[]; + }[]; + implicit_hate?: { + success: boolean; + score: number; + flagged: boolean; + position: { + start: number; + end: number; + }; + tokens?: { + token?: string; + index?: number; + score?: number; + }[]; + }[]; + }; + conversation_id: string; + }; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["schemas"]["NotFoundResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + }; + "/v2/text/chat/output": { + post: { + parameters: { + query: { + version: "2024-01-10"; + }; + }; + requestBody?: { + content: { + "application/json": { + model_id?: string; + prompt_template_id?: string | null; + moderations?: { + hap?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + stigma?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + implicit_hate?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + }; + messages?: ({ + /** @enum {string} */ + role: "user" | "system" | "assistant"; + content: string; + file_ids?: string[]; + })[]; + conversation_id?: string | null; + parent_id?: string | null; + prompt_id?: string; + /** @enum {string} */ + trim_method?: "floating_window" | "none"; + use_conversation_parameters?: boolean; + parameters?: ({ + beam_width?: number | null; + /** @enum {string|null} */ + decoding_method?: "greedy" | "sample" | null; + max_new_tokens?: number | null; + min_new_tokens?: number | null; + random_seed?: number | null; + stop_sequences?: string[] | null; + temperature?: number | null; + time_limit?: number | null; + top_k?: number | null; + top_p?: number | null; + typical_p?: number | null; + repetition_penalty?: number | null; + truncate_input_tokens?: number | null; + include_stop_sequence?: boolean; + return_options?: ({ + generated_tokens?: boolean | null; + input_text?: boolean | null; + input_tokens?: boolean | null; + input_parameters?: boolean | null; + token_logprobs?: boolean | null; + token_ranks?: boolean | null; + top_n_tokens?: number | null; + }) | null; + length_penalty?: ({ + decay_factor?: number | null; + start_index?: number | null; + }) | null; + }) | null; + }; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + result: string; + }; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["schemas"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["schemas"]["TooManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + }; + "/v2/text/chat": { + post: { + parameters: { + query: { + version: "2024-01-10"; + }; + }; + requestBody?: { + content: { + "application/json": { + model_id?: string; + prompt_template_id?: string | null; + moderations?: { + hap?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + stigma?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + implicit_hate?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + }; + messages?: ({ + /** @enum {string} */ + role: "user" | "system" | "assistant"; + content: string; + file_ids?: string[]; + })[]; + conversation_id?: string | null; + parent_id?: string | null; + prompt_id?: string; + /** @enum {string} */ + trim_method?: "floating_window" | "none"; + use_conversation_parameters?: boolean; + parameters?: ({ + beam_width?: number | null; + /** @enum {string|null} */ + decoding_method?: "greedy" | "sample" | null; + max_new_tokens?: number | null; + min_new_tokens?: number | null; + random_seed?: number | null; + stop_sequences?: string[] | null; + temperature?: number | null; + time_limit?: number | null; + top_k?: number | null; + top_p?: number | null; + typical_p?: number | null; + repetition_penalty?: number | null; + truncate_input_tokens?: number | null; + include_stop_sequence?: boolean; + return_options?: ({ + generated_tokens?: boolean | null; + input_text?: boolean | null; + input_tokens?: boolean | null; + input_parameters?: boolean | null; + token_logprobs?: boolean | null; + token_ranks?: boolean | null; + top_n_tokens?: number | null; + }) | null; + length_penalty?: ({ + decay_factor?: number | null; + start_index?: number | null; + }) | null; + }) | null; + }; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + id?: string | null; + model_id?: string; + /** Format: date-time */ + created_at?: string; + input_parameters?: { + [key: string]: unknown; + } | null; + results: ({ + input_text?: string | null; + generated_text: string; + generated_token_count: number; + input_token_count?: number | null; + /** @enum {string} */ + stop_reason: "not_finished" | "max_tokens" | "eos_token" | "cancelled" | "time_limit" | "stop_sequence" | "token_limit" | "error"; + stop_sequence?: string | null; + generated_tokens?: (({ + text?: string | null; + logprob?: (number | null) | (string | null); + rank?: number | null; + top_tokens?: (({ + text?: string | null; + logprob?: (number | null) | (string | null); + })[]) | null; + })[]) | null; + input_tokens?: (({ + text?: string | null; + logprob?: (number | null) | (string | null); + rank?: number | null; + top_tokens?: (({ + text?: string | null; + logprob?: (number | null) | (string | null); + })[]) | null; + })[]) | null; + seed?: number | null; + moderation?: { + [key: string]: unknown; + } | null; + [key: string]: unknown; + })[]; + conversation_id: string; + }; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["schemas"]["NotFoundResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + }; + "/v2/prompts/{id}": { + get: { + parameters: { + query: { + version: "2024-01-10"; + }; + path: { + id: string; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + result: { + id: string; + name: string; + description?: string; + data?: Record; + input?: string; + output?: string; + model_id?: string; + parameters?: ({ + beam_width?: number | null; + /** @enum {string|null} */ + decoding_method?: "greedy" | "sample" | null; + max_new_tokens?: number | null; + min_new_tokens?: number | null; + random_seed?: number | null; + stop_sequences?: string[] | null; + temperature?: number | null; + time_limit?: number | null; + top_k?: number | null; + top_p?: number | null; + typical_p?: number | null; + repetition_penalty?: number | null; + truncate_input_tokens?: number | null; + include_stop_sequence?: boolean; + return_options?: ({ + generated_tokens?: boolean | null; + input_text?: boolean | null; + input_tokens?: boolean | null; + input_parameters?: boolean | null; + token_logprobs?: boolean | null; + token_ranks?: boolean | null; + top_n_tokens?: number | null; + }) | null; + length_penalty?: ({ + decay_factor?: number | null; + start_index?: number | null; + }) | null; + }) | null; + moderations?: ({ + hap?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + stigma?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + implicit_hate?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + }) | null; + /** Format: date-time */ + created_at: string; + /** @enum {string} */ + type: "private" | "public" | "community" | "example"; + public?: boolean; + prompt_id?: string | null; + metadata?: { + [key: string]: unknown; + }; + author?: { + id?: number; + first_name?: string; + last_name?: string; + }; + task?: { + id?: string; + name?: string; + icon?: string; + }; + messages?: (({ + /** @enum {string} */ + role: "user" | "system" | "assistant"; + content: string; + })[]) | null; + }; + }; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["schemas"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["schemas"]["TooManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + put: { + parameters: { + query: { + version: "2024-01-10"; + }; + path: { + id: string; + }; + }; + requestBody: { + content: { + "application/json": { + name: string; + description?: string; + prompt_id?: string; + data?: { + example_file_ids?: string[]; + [key: string]: unknown; + }; + output?: string | null; + parameters?: ({ + beam_width?: number | null; + /** @enum {string|null} */ + decoding_method?: "greedy" | "sample" | null; + max_new_tokens?: number | null; + min_new_tokens?: number | null; + random_seed?: number | null; + stop_sequences?: string[] | null; + temperature?: number | null; + time_limit?: number | null; + top_k?: number | null; + top_p?: number | null; + typical_p?: number | null; + repetition_penalty?: number | null; + truncate_input_tokens?: number | null; + include_stop_sequence?: boolean; + return_options?: ({ + generated_tokens?: boolean | null; + input_text?: boolean | null; + input_tokens?: boolean | null; + input_parameters?: boolean | null; + token_logprobs?: boolean | null; + token_ranks?: boolean | null; + top_n_tokens?: number | null; + }) | null; + length_penalty?: ({ + decay_factor?: number | null; + start_index?: number | null; + }) | null; + }) | null; + moderations?: { + hap?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + stigma?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + implicit_hate?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + }; + model_id: string; + task_id?: string; + /** @enum {string} */ + type?: "private" | "public" | "community"; + input?: string; + messages?: (({ + /** @enum {string} */ + role: "user" | "system" | "assistant"; + content: string; + file_ids?: string[]; + })[]) | null; + }; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + result: { + id: string; + name: string; + description?: string; + data?: Record; + input?: string; + output?: string; + model_id?: string; + parameters?: ({ + beam_width?: number | null; + /** @enum {string|null} */ + decoding_method?: "greedy" | "sample" | null; + max_new_tokens?: number | null; + min_new_tokens?: number | null; + random_seed?: number | null; + stop_sequences?: string[] | null; + temperature?: number | null; + time_limit?: number | null; + top_k?: number | null; + top_p?: number | null; + typical_p?: number | null; + repetition_penalty?: number | null; + truncate_input_tokens?: number | null; + include_stop_sequence?: boolean; + return_options?: ({ + generated_tokens?: boolean | null; + input_text?: boolean | null; + input_tokens?: boolean | null; + input_parameters?: boolean | null; + token_logprobs?: boolean | null; + token_ranks?: boolean | null; + top_n_tokens?: number | null; + }) | null; + length_penalty?: ({ + decay_factor?: number | null; + start_index?: number | null; + }) | null; + }) | null; + moderations?: ({ + hap?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + stigma?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + implicit_hate?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + }) | null; + /** Format: date-time */ + created_at: string; + /** @enum {string} */ + type: "private" | "public" | "community" | "example"; + public?: boolean; + prompt_id?: string | null; + metadata?: { + [key: string]: unknown; + }; + author?: { + id?: number; + first_name?: string; + last_name?: string; + }; + task?: { + id?: string; + name?: string; + icon?: string; + }; + messages?: (({ + /** @enum {string} */ + role: "user" | "system" | "assistant"; + content: string; + })[]) | null; + }; + }; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["schemas"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["schemas"]["TooManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + delete: { + parameters: { + query: { + version: "2023-11-22"; + }; + path: { + id: string; + }; + }; + responses: { + /** @description Success */ + 204: { + content: never; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["schemas"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["schemas"]["TooManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + patch: { + parameters: { + query: { + version: "2024-01-10"; + }; + path: { + id: string; + }; + }; + requestBody?: { + content: { + "application/json": { + /** @enum {string} */ + type?: "private" | "public" | "community"; + }; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + result: { + id: string; + name: string; + description?: string; + data?: Record; + input?: string; + output?: string; + model_id?: string; + parameters?: ({ + beam_width?: number | null; + /** @enum {string|null} */ + decoding_method?: "greedy" | "sample" | null; + max_new_tokens?: number | null; + min_new_tokens?: number | null; + random_seed?: number | null; + stop_sequences?: string[] | null; + temperature?: number | null; + time_limit?: number | null; + top_k?: number | null; + top_p?: number | null; + typical_p?: number | null; + repetition_penalty?: number | null; + truncate_input_tokens?: number | null; + include_stop_sequence?: boolean; + return_options?: ({ + generated_tokens?: boolean | null; + input_text?: boolean | null; + input_tokens?: boolean | null; + input_parameters?: boolean | null; + token_logprobs?: boolean | null; + token_ranks?: boolean | null; + top_n_tokens?: number | null; + }) | null; + length_penalty?: ({ + decay_factor?: number | null; + start_index?: number | null; + }) | null; + }) | null; + moderations?: ({ + hap?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + stigma?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + implicit_hate?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + }) | null; + /** Format: date-time */ + created_at: string; + /** @enum {string} */ + type: "private" | "public" | "community" | "example"; + public?: boolean; + prompt_id?: string | null; + metadata?: { + [key: string]: unknown; + }; + author?: { + id?: number; + first_name?: string; + last_name?: string; + }; + task?: { + id?: string; + name?: string; + icon?: string; + }; + messages?: (({ + /** @enum {string} */ + role: "user" | "system" | "assistant"; + content: string; + })[]) | null; + }; + }; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["schemas"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["schemas"]["TooManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + }; + "/v2/prompts": { + get: { + parameters: { + query: { + limit?: number; + offset?: number; + search?: string; + task_id?: string | string[]; + model_id?: string | string[]; + source?: ("user" | "example" | "community") | (("user" | "example" | "community")[]); + version: "2024-01-10"; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + results: ({ + id: string; + name: string; + description?: string; + data?: Record; + input?: string; + output?: string; + model_id?: string; + parameters?: ({ + beam_width?: number | null; + /** @enum {string|null} */ + decoding_method?: "greedy" | "sample" | null; + max_new_tokens?: number | null; + min_new_tokens?: number | null; + random_seed?: number | null; + stop_sequences?: string[] | null; + temperature?: number | null; + time_limit?: number | null; + top_k?: number | null; + top_p?: number | null; + typical_p?: number | null; + repetition_penalty?: number | null; + truncate_input_tokens?: number | null; + include_stop_sequence?: boolean; + return_options?: ({ + generated_tokens?: boolean | null; + input_text?: boolean | null; + input_tokens?: boolean | null; + input_parameters?: boolean | null; + token_logprobs?: boolean | null; + token_ranks?: boolean | null; + top_n_tokens?: number | null; + }) | null; + length_penalty?: ({ + decay_factor?: number | null; + start_index?: number | null; + }) | null; + }) | null; + moderations?: ({ + hap?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + stigma?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + implicit_hate?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + }) | null; + /** Format: date-time */ + created_at: string; + /** @enum {string} */ + type: "private" | "public" | "community" | "example"; + public?: boolean; + prompt_id?: string | null; + metadata?: { + [key: string]: unknown; + }; + author?: { + id?: number; + first_name?: string; + last_name?: string; + }; + task?: { + id?: string; + name?: string; + icon?: string; + }; + messages?: (({ + /** @enum {string} */ + role: "user" | "system" | "assistant"; + content: string; + })[]) | null; + })[]; + total_count: number; + }; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["schemas"]["TooManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + post: { + parameters: { + query: { + version: "2024-01-10"; + }; + }; + requestBody: { + content: { + "application/json": { + name: string; + description?: string; + prompt_id?: string; + data?: { + example_file_ids?: string[]; + [key: string]: unknown; + }; + output?: string | null; + parameters?: ({ + beam_width?: number | null; + /** @enum {string|null} */ + decoding_method?: "greedy" | "sample" | null; + max_new_tokens?: number | null; + min_new_tokens?: number | null; + random_seed?: number | null; + stop_sequences?: string[] | null; + temperature?: number | null; + time_limit?: number | null; + top_k?: number | null; + top_p?: number | null; + typical_p?: number | null; + repetition_penalty?: number | null; + truncate_input_tokens?: number | null; + include_stop_sequence?: boolean; + return_options?: ({ + generated_tokens?: boolean | null; + input_text?: boolean | null; + input_tokens?: boolean | null; + input_parameters?: boolean | null; + token_logprobs?: boolean | null; + token_ranks?: boolean | null; + top_n_tokens?: number | null; + }) | null; + length_penalty?: ({ + decay_factor?: number | null; + start_index?: number | null; + }) | null; + }) | null; + moderations?: { + hap?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + stigma?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + implicit_hate?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + }; + model_id: string; + task_id?: string; + /** @enum {string} */ + type?: "private" | "public" | "community"; + input?: string; + messages?: (({ + /** @enum {string} */ + role: "user" | "system" | "assistant"; + content: string; + file_ids?: string[]; + })[]) | null; + }; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + result: { + id: string; + name: string; + description?: string; + data?: Record; + input?: string; + output?: string; + model_id?: string; + parameters?: ({ + beam_width?: number | null; + /** @enum {string|null} */ + decoding_method?: "greedy" | "sample" | null; + max_new_tokens?: number | null; + min_new_tokens?: number | null; + random_seed?: number | null; + stop_sequences?: string[] | null; + temperature?: number | null; + time_limit?: number | null; + top_k?: number | null; + top_p?: number | null; + typical_p?: number | null; + repetition_penalty?: number | null; + truncate_input_tokens?: number | null; + include_stop_sequence?: boolean; + return_options?: ({ + generated_tokens?: boolean | null; + input_text?: boolean | null; + input_tokens?: boolean | null; + input_parameters?: boolean | null; + token_logprobs?: boolean | null; + token_ranks?: boolean | null; + top_n_tokens?: number | null; + }) | null; + length_penalty?: ({ + decay_factor?: number | null; + start_index?: number | null; + }) | null; + }) | null; + moderations?: ({ + hap?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + stigma?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + implicit_hate?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + }) | null; + /** Format: date-time */ + created_at: string; + /** @enum {string} */ + type: "private" | "public" | "community" | "example"; + public?: boolean; + prompt_id?: string | null; + metadata?: { + [key: string]: unknown; + }; + author?: { + id?: number; + first_name?: string; + last_name?: string; + }; + task?: { + id?: string; + name?: string; + icon?: string; + }; + messages?: (({ + /** @enum {string} */ + role: "user" | "system" | "assistant"; + content: string; + })[]) | null; + }; + }; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["schemas"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["schemas"]["TooManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + }; + "/v2/models/{id}": { + get: { + parameters: { + query: { + version: "2024-01-10"; + }; + path: { + id: string; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + result: { + tasks: { + id: string; + name: string; + json_example?: string; + jsonl_example?: string; + csv_example?: string; + verbalizer?: string; + file_format_id?: number; + tune: boolean; + }[]; + model_family: { + id: number; + name: string; + }; + id: string; + name: string; + developer?: string; + size: string; + label: string; + disabled: boolean; + preferred: boolean; + warning?: string; + description?: string; + tags: string[]; + source_model_id?: string | null; + is_live: boolean; + token_limits: { + beam_width: number; + token_limit: number; + }[]; + system_prompt?: string; + prompt_example?: string; + }; + }; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["schemas"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["schemas"]["TooManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + }; + "/v2/tunes/{id}/content/{type}": { + get: { + parameters: { + query: { + version: "2023-12-15"; + }; + path: { + id: string; + type: "vectors" | "logs" | "export"; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/octet-stream": string; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["schemas"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["schemas"]["TooManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + }; + "/v2/files/{id}": { + get: { + parameters: { + query: { + version: "2023-12-15"; + }; + path: { + id: string; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + result: { + id: string; + bytes: number; + file_name: string; + /** @enum {string} */ + storage_provider_location: "us-south" | "us-east"; + /** @enum {string} */ + purpose: "tune" | "template" | "tune_import" | "extraction"; + /** Format: date-time */ + created_at: string; + file_formats?: { + id: number; + name: string; + }[]; + }; + }; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["schemas"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["schemas"]["TooManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + delete: { + parameters: { + query: { + version: "2023-11-22"; + }; + path: { + id: string; + }; + }; + responses: { + /** @description Success */ + 204: { + content: never; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["schemas"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["schemas"]["TooManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + }; + "/v2/files": { + get: { + parameters: { + query: { + limit?: number; + offset?: number; + sort_by?: "name" | "created_at"; + direction?: "asc" | "desc"; + search?: string; + purpose?: "tune" | "template" | "tune_import" | "extraction"; + format_id?: number; + version: "2023-12-15"; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + results: ({ + id: string; + bytes: number; + file_name: string; + /** @enum {string} */ + storage_provider_location: "us-south" | "us-east"; + /** @enum {string} */ + purpose: "tune" | "template" | "tune_import" | "extraction"; + /** Format: date-time */ + created_at: string; + file_formats?: { + id: number; + name: string; + }[]; + })[]; + total_count: number; + }; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["schemas"]["TooManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + post: { + parameters: { + query: { + version: "2023-12-15"; + }; + }; + requestBody: { + content: { + "multipart/form-data": { + /** @enum {string} */ + purpose: "tune" | "template" | "tune_import" | "extraction"; + /** Format: binary */ + file: string; + }; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + result: { + id: string; + bytes: number; + file_name: string; + /** @enum {string} */ + storage_provider_location: "us-south" | "us-east"; + /** @enum {string} */ + purpose: "tune" | "template" | "tune_import" | "extraction"; + /** Format: date-time */ + created_at: string; + file_formats?: { + id: number; + name: string; + }[]; + }; + }; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["schemas"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["schemas"]["TooManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + }; + "/v2/user": { + get: { + parameters: { + query: { + version: "2023-11-22"; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + result: { + id: number; + first_name?: string; + last_name?: string; + tou_accepted: boolean; + tou_accepted_at?: string; + generate_default?: ({ + model_id?: string; + prompt_id?: string; + template?: { + id?: string; + value?: string; + data: { + example_file_ids?: string[]; + [key: string]: unknown; + }; + }; + parameters?: ({ + beam_width?: number | null; + /** @enum {string|null} */ + decoding_method?: "greedy" | "sample" | null; + max_new_tokens?: number | null; + min_new_tokens?: number | null; + random_seed?: number | null; + stop_sequences?: string[] | null; + temperature?: number | null; + time_limit?: number | null; + top_k?: number | null; + top_p?: number | null; + typical_p?: number | null; + repetition_penalty?: number | null; + truncate_input_tokens?: number | null; + include_stop_sequence?: boolean; + return_options?: ({ + generated_tokens?: boolean | null; + input_text?: boolean | null; + input_tokens?: boolean | null; + input_parameters?: boolean | null; + token_logprobs?: boolean | null; + token_ranks?: boolean | null; + top_n_tokens?: number | null; + }) | null; + length_penalty?: ({ + decay_factor?: number | null; + start_index?: number | null; + }) | null; + }) | null; + moderations?: { + hap?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + stigma?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + implicit_hate?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + }; + }) | null; + data_usage_consent: boolean; + }; + }; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["schemas"]["TooManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + post: { + parameters: { + query: { + version: "2023-11-22"; + }; + }; + requestBody?: { + content: { + "application/json": { + first_name?: string; + last_name?: string; + }; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + result: { + user_id: string; + api_key: { + value: string; + created_at: string; + generated_at: string; + last_used_at?: string; + }; + id: number; + first_name?: string; + last_name?: string; + tou_accepted: boolean; + tou_accepted_at?: string; + generate_default?: ({ + model_id?: string; + prompt_id?: string; + template?: { + id?: string; + value?: string; + data: { + example_file_ids?: string[]; + [key: string]: unknown; + }; + }; + parameters?: ({ + beam_width?: number | null; + /** @enum {string|null} */ + decoding_method?: "greedy" | "sample" | null; + max_new_tokens?: number | null; + min_new_tokens?: number | null; + random_seed?: number | null; + stop_sequences?: string[] | null; + temperature?: number | null; + time_limit?: number | null; + top_k?: number | null; + top_p?: number | null; + typical_p?: number | null; + repetition_penalty?: number | null; + truncate_input_tokens?: number | null; + include_stop_sequence?: boolean; + return_options?: ({ + generated_tokens?: boolean | null; + input_text?: boolean | null; + input_tokens?: boolean | null; + input_parameters?: boolean | null; + token_logprobs?: boolean | null; + token_ranks?: boolean | null; + top_n_tokens?: number | null; + }) | null; + length_penalty?: ({ + decay_factor?: number | null; + start_index?: number | null; + }) | null; + }) | null; + moderations?: { + hap?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + stigma?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + implicit_hate?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + }; + }) | null; + data_usage_consent: boolean; + [key: string]: unknown; + }; + }; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["schemas"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["schemas"]["TooManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + delete: { + parameters: { + query: { + version: "2023-11-22"; + }; + }; + responses: { + /** @description Success */ + 204: { + content: never; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["schemas"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["schemas"]["TooManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + patch: { + parameters: { + query: { + version: "2023-11-22"; + }; + }; + requestBody?: { + content: { + "application/json": { + tou_accepted?: boolean; + data_usage_consent?: boolean; + }; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + result: { + id: number; + first_name?: string; + last_name?: string; + tou_accepted: boolean; + tou_accepted_at?: string; + generate_default?: ({ + model_id?: string; + prompt_id?: string; + template?: { + id?: string; + value?: string; + data: { + example_file_ids?: string[]; + [key: string]: unknown; + }; + }; + parameters?: ({ + beam_width?: number | null; + /** @enum {string|null} */ + decoding_method?: "greedy" | "sample" | null; + max_new_tokens?: number | null; + min_new_tokens?: number | null; + random_seed?: number | null; + stop_sequences?: string[] | null; + temperature?: number | null; + time_limit?: number | null; + top_k?: number | null; + top_p?: number | null; + typical_p?: number | null; + repetition_penalty?: number | null; + truncate_input_tokens?: number | null; + include_stop_sequence?: boolean; + return_options?: ({ + generated_tokens?: boolean | null; + input_text?: boolean | null; + input_tokens?: boolean | null; + input_parameters?: boolean | null; + token_logprobs?: boolean | null; + token_ranks?: boolean | null; + top_n_tokens?: number | null; + }) | null; + length_penalty?: ({ + decay_factor?: number | null; + start_index?: number | null; + }) | null; + }) | null; + moderations?: { + hap?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + stigma?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + implicit_hate?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + }; + }) | null; + data_usage_consent: boolean; + }; + }; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["schemas"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["schemas"]["TooManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + }; + "/v2/tuning_types": { + get: { + parameters: { + query: { + version: "2023-11-22"; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + results: { + id?: string; + name?: string; + /** @description JSON Schema */ + schema?: { + [key: string]: unknown; + }; + model_ids?: string[]; + }[]; + }; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["schemas"]["TooManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + }; + "/v2/tunes/{id}": { + get: { + parameters: { + query: { + version: "2023-11-22"; + }; + path: { + id: string; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + result: { + id: string; + name: string; + model_id: string; + model_name: string | null; + /** @enum {string} */ + status: "initializing" | "not_started" | "pending" | "halted" | "running" | "queued" | "completed" | "failed"; + status_message?: string | null; + tuning_type: string; + parameters?: Record; + preferred: boolean; + task_id: string; + task_name: string; + /** Format: date-time */ + started_at?: string | null; + /** Format: date-time */ + finished_at?: string | null; + /** Format: date-time */ + created_at: string; + validation_files?: { + id: string; + file_name: string; + /** Format: date-time */ + created_at?: string; + }[] | null; + training_files?: { + id: string; + file_name: string; + /** Format: date-time */ + created_at?: string; + }[] | null; + evaluation_files?: { + id: string; + file_name: string; + /** Format: date-time */ + created_at?: string; + }[] | null; + datapoints?: { + loss: { + data: { + value: number; + epoch: number; + }; + /** Format: date-time */ + timestamp: string; + }[]; + }; + vectors?: string | null; + }; + }; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["schemas"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["schemas"]["TooManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + delete: { + parameters: { + query: { + version: "2023-11-22"; + }; + path: { + id: string; + }; + }; + responses: { + /** @description Success */ + 204: { + content: never; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["schemas"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["schemas"]["TooManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + patch: { + parameters: { + query: { + version: "2023-11-22"; + }; + path: { + id: string; + }; + }; + requestBody?: { + content: { + "application/json": { + name?: string; + preferred?: boolean; + }; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + result: { + id: string; + name: string; + model_id: string; + model_name: string | null; + /** @enum {string} */ + status: "initializing" | "not_started" | "pending" | "halted" | "running" | "queued" | "completed" | "failed"; + status_message?: string | null; + tuning_type: string; + parameters?: Record; + preferred: boolean; + task_id: string; + task_name: string; + /** Format: date-time */ + started_at?: string | null; + /** Format: date-time */ + finished_at?: string | null; + /** Format: date-time */ + created_at: string; + validation_files?: { + id: string; + file_name: string; + /** Format: date-time */ + created_at?: string; + }[] | null; + training_files?: { + id: string; + file_name: string; + /** Format: date-time */ + created_at?: string; + }[] | null; + evaluation_files?: { + id: string; + file_name: string; + /** Format: date-time */ + created_at?: string; + }[] | null; + datapoints?: { + loss: { + data: { + value: number; + epoch: number; + }; + /** Format: date-time */ + timestamp: string; + }[]; + }; + vectors?: string | null; + }; + }; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["schemas"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["schemas"]["TooManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + }; + "/v2/tunes/import": { + post: { + parameters: { + query: { + version: "2023-11-22"; + }; + }; + requestBody: { + content: { + "application/json": { + name: string; + file_id: string; + }; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + result: { + id: string; + name: string; + model_id: string; + model_name: string | null; + /** @enum {string} */ + status: "initializing" | "not_started" | "pending" | "halted" | "running" | "queued" | "completed" | "failed"; + status_message?: string | null; + tuning_type: string; + parameters?: Record; + preferred: boolean; + task_id: string; + task_name: string; + /** Format: date-time */ + started_at?: string | null; + /** Format: date-time */ + finished_at?: string | null; + /** Format: date-time */ + created_at: string; + validation_files?: { + id: string; + file_name: string; + /** Format: date-time */ + created_at?: string; + }[] | null; + training_files?: { + id: string; + file_name: string; + /** Format: date-time */ + created_at?: string; + }[] | null; + evaluation_files?: { + id: string; + file_name: string; + /** Format: date-time */ + created_at?: string; + }[] | null; + datapoints?: { + loss: { + data: { + value: number; + epoch: number; + }; + /** Format: date-time */ + timestamp: string; + }[]; + }; + vectors?: string | null; + }; + }; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["schemas"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["schemas"]["TooManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + }; + "/v2/tunes": { + get: { + parameters: { + query: { + limit?: number; + offset?: number; + status?: "initializing" | "not_started" | "pending" | "halted" | "running" | "queued" | "completed" | "failed"; + search?: string | null; + sort_by?: "status" | "created_at" | "name" | "id" | "model"; + direction?: "asc" | "desc"; + version: "2023-11-22"; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + total_count: number; + results: ({ + id: string; + name: string; + model_id: string; + model_name: string | null; + /** @enum {string} */ + status: "initializing" | "not_started" | "pending" | "halted" | "running" | "queued" | "completed" | "failed"; + status_message?: string | null; + tuning_type: string; + parameters?: Record; + preferred: boolean; + task_id: string; + task_name: string; + /** Format: date-time */ + started_at?: string | null; + /** Format: date-time */ + finished_at?: string | null; + /** Format: date-time */ + created_at: string; + validation_files?: { + id: string; + file_name: string; + /** Format: date-time */ + created_at?: string; + }[] | null; + training_files?: { + id: string; + file_name: string; + /** Format: date-time */ + created_at?: string; + }[] | null; + evaluation_files?: { + id: string; + file_name: string; + /** Format: date-time */ + created_at?: string; + }[] | null; + datapoints?: { + loss: { + data: { + value: number; + epoch: number; + }; + /** Format: date-time */ + timestamp: string; + }[]; + }; + vectors?: string | null; + })[]; + }; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["schemas"]["TooManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + post: { + parameters: { + query: { + version: "2023-11-22"; + }; + }; + requestBody: { + content: { + "application/json": { + name: string; + model_id: string; + task_id: string; + training_file_ids: string[]; + validation_file_ids?: string[] | null; + evaluation_file_ids?: string[] | null; + /** @enum {string} */ + tuning_type: "prompt_tuning" | "multitask_prompt_tuning"; + parameters?: ({ + batch_size?: number | null; + num_epochs?: number | null; + verbalizer?: string | null; + learning_rate?: number | null; + accumulate_steps?: number | null; + max_input_tokens?: number | null; + max_output_tokens?: number | null; + num_virtual_tokens?: number | null; + [key: string]: unknown; + }) | null; + }; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + result: { + id: string; + name: string; + model_id: string; + model_name: string | null; + /** @enum {string} */ + status: "initializing" | "not_started" | "pending" | "halted" | "running" | "queued" | "completed" | "failed"; + status_message?: string | null; + tuning_type: string; + parameters?: Record; + preferred: boolean; + task_id: string; + task_name: string; + /** Format: date-time */ + started_at?: string | null; + /** Format: date-time */ + finished_at?: string | null; + /** Format: date-time */ + created_at: string; + validation_files?: { + id: string; + file_name: string; + /** Format: date-time */ + created_at?: string; + }[] | null; + training_files?: { + id: string; + file_name: string; + /** Format: date-time */ + created_at?: string; + }[] | null; + evaluation_files?: { + id: string; + file_name: string; + /** Format: date-time */ + created_at?: string; + }[] | null; + datapoints?: { + loss: { + data: { + value: number; + epoch: number; + }; + /** Format: date-time */ + timestamp: string; + }[]; + }; + vectors?: string | null; + }; + }; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["schemas"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["schemas"]["TooManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + }; + "/v2/text/moderations": { + post: { + parameters: { + query: { + version: "2023-11-22"; + }; + }; + requestBody: { + content: { + "application/json": { + input: string; + hap?: { + /** @default 0.75 */ + threshold?: number; + /** @default false */ + send_tokens?: boolean; + }; + stigma?: { + /** @default 0.75 */ + threshold?: number; + /** @default false */ + send_tokens?: boolean; + }; + implicit_hate?: { + /** @default 0.75 */ + threshold?: number; + /** @default false */ + send_tokens?: boolean; + }; + }; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + results: { + hap?: { + success: boolean; + score: number; + flagged: boolean; + position: { + start: number; + end: number; + }; + tokens?: { + token?: string; + index?: number; + score?: number; + }[]; + }[]; + stigma?: { + success: boolean; + score: number; + flagged: boolean; + position: { + start: number; + end: number; + }; + tokens?: { + token?: string; + index?: number; + score?: number; + }[]; + }[]; + implicit_hate?: { + success: boolean; + score: number; + flagged: boolean; + position: { + start: number; + end: number; + }; + tokens?: { + token?: string; + index?: number; + score?: number; + }[]; + }[]; + }[]; + }; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["schemas"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["schemas"]["TooManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + }; + "/v2/text/generation/{id}/feedback": { + get: { + parameters: { + query: { + version: "2023-11-22"; + }; + path: { + id: string; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + result: { + id: number; + /** Format: date-time */ + created_at: string; + /** Format: date-time */ + updated_at: string; + comment?: string | null; + categories: string[]; + api_request: string; + }; + }; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["schemas"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["schemas"]["TooManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + put: { + parameters: { + query: { + version: "2023-11-22"; + }; + path: { + id: string; + }; + }; + requestBody?: { + content: { + "application/json": { + comment?: string; + categories?: ("no_problem" | "hap" | "pii" | "social_bias" | "not_honest_or_truthful" | "taboo_topics" | "other")[]; + }; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + result: { + id: number; + /** Format: date-time */ + created_at: string; + /** Format: date-time */ + updated_at: string; + comment?: string | null; + categories: string[]; + api_request: string; + }; + }; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["schemas"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["schemas"]["TooManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + post: { + parameters: { + query: { + version: "2023-11-22"; + }; + path: { + id: string; + }; + }; + requestBody?: { + content: { + "application/json": { + comment?: string; + categories?: ("no_problem" | "hap" | "pii" | "social_bias" | "not_honest_or_truthful" | "taboo_topics" | "other")[]; + }; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + result: { + id: number; + /** Format: date-time */ + created_at: string; + /** Format: date-time */ + updated_at: string; + comment?: string | null; + categories: string[]; + api_request: string; + }; + }; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["schemas"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["schemas"]["TooManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + }; + "/v2/text/generation/output": { + post: { + parameters: { + query: { + version: "2023-11-22"; + }; + }; + requestBody?: { + content: { + "application/json": { + model_id?: string; + prompt_id?: string; + input?: string; + data?: { + example_file_ids?: string[]; + [key: string]: unknown; + }; + parameters?: ({ + beam_width?: number | null; + /** @enum {string|null} */ + decoding_method?: "greedy" | "sample" | null; + max_new_tokens?: number | null; + min_new_tokens?: number | null; + random_seed?: number | null; + stop_sequences?: string[] | null; + temperature?: number | null; + time_limit?: number | null; + top_k?: number | null; + top_p?: number | null; + typical_p?: number | null; + repetition_penalty?: number | null; + truncate_input_tokens?: number | null; + include_stop_sequence?: boolean; + return_options?: ({ + generated_tokens?: boolean | null; + input_text?: boolean | null; + input_tokens?: boolean | null; + input_parameters?: boolean | null; + token_logprobs?: boolean | null; + token_ranks?: boolean | null; + top_n_tokens?: number | null; + }) | null; + length_penalty?: ({ + decay_factor?: number | null; + start_index?: number | null; + }) | null; + }) | null; + moderations?: { + hap?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + stigma?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + implicit_hate?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + }; + use_default?: boolean | null; + }; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + results: string[]; + }; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["schemas"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["schemas"]["TooManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + }; + "/v2/text/generation/limits": { + get: { + parameters: { + query: { + version: "2023-11-22"; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + result: { + concurrency: { + limit: number; + remaining: number; + }; + }; + }; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + }; + "/v2/text/generation/comparison": { + post: { + parameters: { + query: { + version: "2023-11-22"; + }; + }; + requestBody: { + content: { + "application/json": { + request: { + model_id?: string; + prompt_id?: string; + input: string; + data?: { + example_file_ids?: string[]; + [key: string]: unknown; + }; + parameters?: ({ + beam_width?: number | null; + /** @enum {string|null} */ + decoding_method?: "greedy" | "sample" | null; + max_new_tokens?: number | null; + min_new_tokens?: number | null; + random_seed?: number | null; + stop_sequences?: string[] | null; + temperature?: number | null; + time_limit?: number | null; + top_k?: number | null; + top_p?: number | null; + typical_p?: number | null; + repetition_penalty?: number | null; + truncate_input_tokens?: number | null; + include_stop_sequence?: boolean; + return_options?: ({ + generated_tokens?: boolean | null; + input_text?: boolean | null; + input_tokens?: boolean | null; + input_parameters?: boolean | null; + token_logprobs?: boolean | null; + token_ranks?: boolean | null; + top_n_tokens?: number | null; + }) | null; + length_penalty?: ({ + decay_factor?: number | null; + start_index?: number | null; + }) | null; + }) | null; + moderations?: { + hap?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + stigma?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + implicit_hate?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + }; + use_default?: boolean | null; + }; + name?: string; + compare_parameters: { + model_id?: string[]; + temperature?: number[]; + top_k?: number[]; + top_p?: number[]; + typical_p?: number[]; + repetition_penalty?: number[]; + length_penalty?: Record[]; + }; + }; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + results: ({ + parameters: { + model_id?: string; + temperature?: number; + top_k?: number; + top_p?: number; + typical_p?: number; + repetition_penalty?: number; + length_penalty?: Record; + }; + error?: (({ + status_code: number; + message?: string; + extensions?: { + code?: string; + reason?: string; + }; + } | null) | ({ + status_code: number; + extensions?: { + code?: string; + reason?: string; + }; + } | null)) | null; + result?: ({ + id: string | null; + model_id: string; + /** Format: date-time */ + created_at: string; + input_parameters?: { + [key: string]: unknown; + } | null; + results: ({ + input_text?: string | null; + generated_text: string; + generated_token_count: number; + input_token_count?: number | null; + /** @enum {string} */ + stop_reason: "not_finished" | "max_tokens" | "eos_token" | "cancelled" | "time_limit" | "stop_sequence" | "token_limit" | "error"; + stop_sequence?: string | null; + generated_tokens?: (({ + text?: string | null; + logprob?: (number | null) | (string | null); + rank?: number | null; + top_tokens?: (({ + text?: string | null; + logprob?: (number | null) | (string | null); + })[]) | null; + })[]) | null; + input_tokens?: (({ + text?: string | null; + logprob?: (number | null) | (string | null); + rank?: number | null; + top_tokens?: (({ + text?: string | null; + logprob?: (number | null) | (string | null); + })[]) | null; + })[]) | null; + seed?: number | null; + moderation?: { + [key: string]: unknown; + } | null; + [key: string]: unknown; + })[]; + }) | null; + })[]; + }; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["schemas"]["NotFoundResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + }; + "/v2/text/extraction/limits": { + get: { + parameters: { + query: { + version: "2023-11-22"; + }; + }; + responses: { + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + }; + "/v2/text/embeddings/limits": { + get: { + parameters: { + query: { + version: "2023-11-22"; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + result: { + concurrency: { + limit: number; + remaining: number; + }; + }; + }; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + }; + "/v2/text/embeddings": { + post: { + parameters: { + query: { + version: "2023-11-22"; + }; + }; + requestBody: { + content: { + "application/json": { + model_id: string; + input: string | string[]; + }; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + results: number[][]; + }; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["schemas"]["NotFoundResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + }; + "/v2/tasks": { + get: { + parameters: { + query: { + tune?: boolean | null; + version: "2023-11-22"; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + results: { + id: string; + name: string; + json_example?: string; + jsonl_example?: string; + csv_example?: string; + verbalizer?: string; + file_format_id?: number; + tune: boolean; + }[]; + }; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["schemas"]["TooManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + }; + "/v2/requests/{id}": { + delete: { + parameters: { + query: { + version: "2023-11-22"; + }; + path: { + id: string; + }; + }; + responses: { + /** @description Success */ + 204: { + content: never; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["schemas"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["schemas"]["TooManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + }; + "/v2/requests/chat/{conversationId}": { + get: { + parameters: { + query: { + version: "2023-11-22"; + }; + path: { + conversationId: string; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + results: ({ + id: string; + duration: number; + /** Format: date-time */ + created_at: string; + request: { + model_id?: string; + prompt_template_id?: string | null; + moderations?: { + hap?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + stigma?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + implicit_hate?: boolean | { + input?: boolean; + output?: boolean; + threshold?: number; + send_tokens?: boolean; + }; + }; + messages?: ({ + /** @enum {string} */ + role: "user" | "system" | "assistant"; + content: string; + file_ids?: string[]; + })[]; + conversation_id?: string | null; + parent_id?: string | null; + prompt_id?: string; + /** @enum {string} */ + trim_method?: "floating_window" | "none"; + use_conversation_parameters?: boolean; + parameters?: ({ + beam_width?: number | null; + /** @enum {string|null} */ + decoding_method?: "greedy" | "sample" | null; + max_new_tokens?: number | null; + min_new_tokens?: number | null; + random_seed?: number | null; + stop_sequences?: string[] | null; + temperature?: number | null; + time_limit?: number | null; + top_k?: number | null; + top_p?: number | null; + typical_p?: number | null; + repetition_penalty?: number | null; + truncate_input_tokens?: number | null; + include_stop_sequence?: boolean; + return_options?: ({ + generated_tokens?: boolean | null; + input_text?: boolean | null; + input_tokens?: boolean | null; + input_parameters?: boolean | null; + token_logprobs?: boolean | null; + token_ranks?: boolean | null; + top_n_tokens?: number | null; + }) | null; + length_penalty?: ({ + decay_factor?: number | null; + start_index?: number | null; + }) | null; + }) | null; + }; + /** @enum {string} */ + status: "success" | "error"; + response: { + id?: string | null; + model_id?: string; + /** Format: date-time */ + created_at?: string; + input_parameters?: { + [key: string]: unknown; + } | null; + results: ({ + input_text?: string | null; + generated_text: string; + generated_token_count: number; + input_token_count?: number | null; + /** @enum {string} */ + stop_reason: "not_finished" | "max_tokens" | "eos_token" | "cancelled" | "time_limit" | "stop_sequence" | "token_limit" | "error"; + stop_sequence?: string | null; + generated_tokens?: (({ + text?: string | null; + logprob?: (number | null) | (string | null); + rank?: number | null; + top_tokens?: (({ + text?: string | null; + logprob?: (number | null) | (string | null); + })[]) | null; + })[]) | null; + input_tokens?: (({ + text?: string | null; + logprob?: (number | null) | (string | null); + rank?: number | null; + top_tokens?: (({ + text?: string | null; + logprob?: (number | null) | (string | null); + })[]) | null; + })[]) | null; + seed?: number | null; + moderation?: { + [key: string]: unknown; + } | null; + [key: string]: unknown; + })[]; + conversation_id: string; + }; + version?: ({ + api?: string | null; + /** Format: date */ + date?: string | null; + }) | null; + parent_id?: string | null; + [key: string]: unknown; + })[]; + }; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["schemas"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["schemas"]["TooManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + delete: { + parameters: { + query: { + version: "2023-11-22"; + }; + path: { + conversationId: string; + }; + }; + responses: { + /** @description Success */ + 204: { + content: never; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["schemas"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["schemas"]["TooManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + }; + "/v2/requests": { + get: { + parameters: { + query: { + limit?: number; + offset?: number; + status?: "success" | "error"; + origin?: "api" | "ui"; + before?: string; + after?: string; + endpoint?: ("generate" | "compare" | "chat") | (("generate" | "compare" | "chat")[]); + api?: "v0" | "v1" | "v2"; + date?: string; + version: "2023-11-22"; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + results: ({ + id: string; + duration: number; + /** Format: date-time */ + created_at: string; + request?: { + [key: string]: unknown; + } | null; + /** @enum {string} */ + status: "success" | "error"; + response?: { + [key: string]: unknown; + } | null; + version?: ({ + api?: string | null; + /** Format: date */ + date?: string | null; + }) | null; + [key: string]: unknown; + })[]; + total_count: number; + }; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["schemas"]["TooManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + }; + "/v2/models": { + get: { + parameters: { + query: { + limit: number; + offset: number; + version: "2023-11-22"; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + results: ({ + id: string; + name: string; + size: string; + label: string; + warning?: string; + source_model_id?: string | null; + is_live: boolean; + token_limits: { + beam_width: number; + token_limit: number; + }[]; + task_ids: string[]; + })[]; + }; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["schemas"]["TooManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + }; + "/v2/files/{id}/content": { + get: { + parameters: { + query: { + version: "2023-11-22"; + }; + path: { + id: string; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/octet-stream": string; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["schemas"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["schemas"]["TooManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + }; + "/v2/api_key/regenerate": { + post: { + parameters: { + query: { + version: "2023-11-22"; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + /** @description The valid JWT token must be sent to return a valid API key. The API key always exists for each user in the System. */ + result?: { + value: string; + /** Format: date-time */ + created_at: string; + /** Format: date-time */ + last_used_at?: string; + /** Format: date-time */ + generated_at: string; + }; + }; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["schemas"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["schemas"]["TooManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + }; + "/v2/api_key": { + get: { + parameters: { + query: { + version: "2023-11-22"; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + /** @description The valid JWT token must be sent to return a valid API key. The API key always exists for each user in the System. */ + result?: { + value: string; + /** Format: date-time */ + created_at: string; + /** Format: date-time */ + last_used_at?: string; + /** Format: date-time */ + generated_at: string; + }; + }; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["schemas"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route access. */ + 401: { + content: { + "application/json": components["schemas"]["UnauthorizedResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["schemas"]["TooManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["schemas"]["InternalServerErrorResponse"]; + }; + }; + /** @description The remote server is not ready to handle the request. */ + 503: { + content: { + "application/json": components["schemas"]["UnavailableResponse"]; + }; + }; + }; + }; + }; +} + +export type webhooks = Record; + +export interface components { + schemas: { + BaseErrorExtension: { + code: string; + state?: { + [key: string]: unknown; + } | null; + }; + BaseErrorResponse: { + error: string; + message: string; + status_code: number; + extensions: components["schemas"]["BaseErrorExtension"]; + }; + UnauthorizedResponse: components["schemas"]["BaseErrorResponse"] & ({ + /** @enum {unknown} */ + status_code: 401; + extensions: components["schemas"]["BaseErrorExtension"] & { + /** @enum {unknown} */ + code: "AUTH_ERROR"; + }; + }); + NotFoundResponse: components["schemas"]["BaseErrorResponse"] & ({ + /** @enum {unknown} */ + status_code: 404; + extensions: components["schemas"]["BaseErrorExtension"] & { + /** @enum {unknown} */ + code: "NOT_FOUND"; + }; + }); + TooManyRequestsResponse: components["schemas"]["BaseErrorResponse"] & ({ + /** @enum {unknown} */ + status_code: 429; + extensions: components["schemas"]["BaseErrorExtension"] & { + /** @enum {unknown} */ + code: "TOO_MANY_REQUESTS"; + }; + }); + BadRequestResponse: components["schemas"]["BaseErrorResponse"] & ({ + /** @enum {unknown} */ + status_code: 400; + extensions: components["schemas"]["BaseErrorExtension"] & { + /** @enum {unknown} */ + code: "INVALID_INPUT"; + }; + }); + InternalServerErrorResponse: components["schemas"]["BaseErrorResponse"] & ({ + /** @enum {unknown} */ + status_code: 500; + extensions: components["schemas"]["BaseErrorExtension"] & { + /** @enum {unknown} */ + code: "INTERNAL_SERVER_ERROR"; + }; + }); + UnavailableResponse: components["schemas"]["BaseErrorResponse"] & ({ + /** @enum {unknown} */ + status_code: 503; + extensions: components["schemas"]["BaseErrorExtension"] & { + /** @enum {unknown} */ + code: "SERVICE_UNAVAILABLE"; + }; + }); + }; + responses: never; + parameters: never; + requestBodies: never; + headers: never; + pathItems: never; +} + +export type $defs = Record; + +export type external = Record; + +export type operations = Record; diff --git a/src/api/streaming-client.ts b/src/api/streaming-client.ts new file mode 100644 index 0000000..56151d8 --- /dev/null +++ b/src/api/streaming-client.ts @@ -0,0 +1,121 @@ +import { + EventStreamContentType, + fetchEventSource, +} from '@ai-zen/node-fetch-event-source'; +import { mergeHeaders } from 'openapi-fetch'; + +import { TypedReadable } from '../utils/stream.js'; +import { BaseError, HttpError, InternalError } from '../errors.js'; +import { safeParseJson } from '../helpers/common.js'; + +export interface SteamingApiClient { + stream: (opts: { + url: string; + headers?: Headers; + body?: any; + signal?: AbortSignal; + }) => TypedReadable; +} + +export function createStreamingApiClient(clientOptions: { + baseUrl?: string; + headers?: Headers; +}): SteamingApiClient { + return { + stream: function fetchSSE({ + url, + headers, + body, + signal, + }: Parameters[0]) { + const outputStream = new TypedReadable({ + autoDestroy: true, + objectMode: true, + signal: signal, + }); + + const onClose = () => { + if (outputStream.readable) { + outputStream.push(null); + } + }; + + const delegatedController = new AbortController(); + if (signal) { + signal.addEventListener( + 'abort', + () => { + delegatedController.abort(); + }, + { + once: true, + }, + ); + } + + const onError = (e: unknown) => { + const err = + e instanceof BaseError + ? e + : new InternalError('Unexpected error', { cause: e }); + + delegatedController.abort(); + if (outputStream.readable) { + outputStream.emit('error', err); + throw err; + } + onClose(); + }; + fetchEventSource(new URL(url, clientOptions.baseUrl).toString(), { + method: 'POST', + body: JSON.stringify(body), + headers: Object.fromEntries( + mergeHeaders(clientOptions.headers, headers, { + 'Content-Type': 'application/json', + }) as any, // Types are incomplete, support is there in Node 18 https://developer.mozilla.org/en-US/docs/Web/API/Headers + ), + signal: delegatedController.signal, + onclose: onClose, + async onopen(response) { + const contentType = response.headers.get('content-type') || ''; + + if (response.ok && contentType === EventStreamContentType) { + return; + } + + const responseData = contentType.startsWith('application/json') + ? await response.json().catch(() => null) + : await response.text(); + + onError(new HttpError(responseData)); + }, + onmessage(message) { + if (message.event === 'close') { + onClose(); + return; + } + if (message.data === '') { + return; + } + + const result = safeParseJson(message.data); + if (result === null) { + onError( + new InternalError( + `Failed to parse message "${JSON.stringify(message)}"`, + ), + ); + return; + } + + outputStream.push(result); + }, + onerror: onError, + }).catch(() => { + /* Prevent uncaught exception (errors are handled inside the stream) */ + }); + + return outputStream; + }, + }; +} diff --git a/src/client/client.test.ts b/src/client.test.ts similarity index 96% rename from src/client/client.test.ts rename to src/client.test.ts index e9d0140..f4149a9 100644 --- a/src/client/client.test.ts +++ b/src/client.test.ts @@ -1,5 +1,4 @@ -import { lookupApiKey, lookupEndpoint } from '../helpers/config.js'; - +import { lookupApiKey, lookupEndpoint } from './helpers/config.js'; import { Client } from './client.js'; vi.mock('../helpers/config.js'); diff --git a/src/client.ts b/src/client.ts new file mode 100644 index 0000000..833b121 --- /dev/null +++ b/src/client.ts @@ -0,0 +1,151 @@ +import { Transform, TransformCallback } from 'node:stream'; + +import fetchRetry from 'fetch-retry'; +import fetch from 'cross-fetch'; + +import { InvalidInputError } from './errors.js'; +import { version } from './buildInfo.js'; +import { lookupApiKey, lookupEndpoint } from './helpers/config.js'; +import { + ApiClient, + ApiClientOptions, + ApiClientResponse, + createApiClient, +} from './api/client.js'; +import { clientErrorWrapper } from './utils/errors.js'; +import type { OmitVersion } from './utils/types.js'; +import { + SteamingApiClient, + createStreamingApiClient, +} from './api/streaming-client.js'; +export interface Configuration { + apiKey?: string; + endpoint?: string; + headers?: Headers; +} + +export type Options = { signal?: AbortSignal }; + +export class Client { + protected readonly _client: ApiClient; + protected readonly _streamingClient: SteamingApiClient; + + constructor(config: Configuration = {}) { + const endpoint = config.endpoint ?? lookupEndpoint(); + if (!endpoint) { + throw new InvalidInputError('Configuration endpoint is missing!'); + } + + const apiKey = config.apiKey ?? lookupApiKey(); + if (!apiKey) { + throw new InvalidInputError('Configuration API key is missing!'); + } + + const agent = version ? `node-sdk/${version}` : 'node-sdk'; + + const headers = new Headers(config.headers); + headers.set('user-agent', agent); + headers.set('x-request-origin', agent); + headers.set('authorization', `Bearer ${apiKey}`); + + this._client = createApiClient({ + baseUrl: endpoint, + headers, + fetch: fetchRetry(fetch) as any, // https://github.com/jonbern/fetch-retry/issues/89 + }); + this._streamingClient = createStreamingApiClient({ + baseUrl: endpoint, + headers, + }); + } + + async models( + input: OmitVersion< + ApiClientOptions<'GET', '/v2/models'>['params']['query'] + >, + opts?: Options, + ) { + return clientErrorWrapper( + this._client.GET('/v2/models', { + ...opts, + params: { + query: { + ...input, + version: '2023-11-22', + }, + }, + }), + ); + } + + async model( + input: ApiClientOptions<'GET', '/v2/models/{id}'>['params']['path'], + opts?: Options, + ) { + return clientErrorWrapper( + this._client.GET('/v2/models/{id}', { + ...opts, + params: { + path: input, + query: { + version: '2024-01-10', + }, + }, + }), + ); + } + + generation_stream( + input: ApiClientOptions<'POST', '/v2/text/generation_stream'>['body'], + opts?: Options, + ) { + type EventMessage = Required< + ApiClientResponse<'POST', '/v2/text/generation_stream'> + >['data']; + + const stream = new Transform({ + autoDestroy: true, + objectMode: true, + transform( + chunk: EventMessage, + encoding: BufferEncoding, + callback: TransformCallback, + ) { + try { + const { + generated_text = '', + stop_reason = null, + input_token_count = 0, + generated_token_count = 0, + ...props + } = (chunk.results || [{}])[0]; + + callback(null, { + generated_text, + stop_reason, + input_token_count, + generated_token_count, + ...(chunk.moderation && { + moderation: chunk.moderation, + }), + ...props, + }); + } catch (e) { + const err = (chunk || e) as unknown as Error; + callback(err, null); + } + }, + }); + + this._streamingClient + .stream({ + url: '/v2/text/generation_stream?version=2023-11-22', + body: input, + signal: opts?.signal, + }) + .on('error', (err) => stream.emit('error', err)) + .pipe(stream); + + return stream; + } +} diff --git a/src/client/cache.ts b/src/client/cache.ts deleted file mode 100644 index 7a5424b..0000000 --- a/src/client/cache.ts +++ /dev/null @@ -1,15 +0,0 @@ -export const CacheDiscriminator = { - GENERATE_CONFIG: 'generate-config', - TUNE: 'tune', - PROMPT_TEMPLATE: 'prompt-template', - FILE: 'file', - MODEL: 'model', - MODELS: 'models', -} as const; - -export type CacheDiscriminator = - (typeof CacheDiscriminator)[keyof typeof CacheDiscriminator]; - -export function generateCacheKey(discriminator: CacheDiscriminator, id = '') { - return `${discriminator}#${id}`; -} diff --git a/src/client/client.ts b/src/client/client.ts deleted file mode 100644 index 6ce879c..0000000 --- a/src/client/client.ts +++ /dev/null @@ -1,1422 +0,0 @@ -import http, { IncomingMessage } from 'node:http'; -import https from 'node:https'; -import { Transform, TransformCallback } from 'node:stream'; - -import axios, { AxiosError } from 'axios'; -import FormData from 'form-data'; -import { - AxiosCacheInstance, - CacheRequestConfig, - setupCache, -} from 'axios-cache-interceptor'; -import promiseRetry from 'promise-retry'; -import { - EventStreamContentType, - fetchEventSource, -} from '@ai-zen/node-fetch-event-source'; -import { ZodSchema } from 'zod'; - -import * as ApiTypes from '../api-types.js'; -import { - errorTransformer, - HttpError, - InternalError, - InvalidInputError, - isRetrievableError, -} from '../errors.js'; -import type { StrictUnion } from '../types.js'; -import { version } from '../buildInfo.js'; -import { - safeParseJson, - Unwrap, - wait, - parseFunctionOverloads, - handle, - isTypeOf, - handleGenerator, - paginator, - isEmptyObject, - callbackifyStream, - callbackifyPromise, -} from '../helpers/common.js'; -import { TypedReadable } from '../utils/stream.js'; -import { lookupApiKey, lookupEndpoint } from '../helpers/config.js'; -import { RETRY_ATTEMPTS_DEFAULT } from '../constants.js'; -import { Callback } from '../helpers/types.js'; - -import { - GenerateConfigInput, - GenerateConfigOptions, - GenerateConfigOutput, - GenerateInput, - GenerateLimitsInput, - GenerateLimitsOutput, - GenerateOutput, - HttpHandlerOptions, - ModelsInput, - ModelsOutput, - HttpHandlerNoStreamOptions, - HttpHandlerStreamOptions, - TokenizeInput, - TokenizeOutput, - ModelInput, - ModelOutput, - GenerateConfigInputOptions, - TunesInput, - TunesOutput, - TuneCreateInput, - TuneOutput, - TuneOptions, - TuneInput, - TuneCreateOptions, - TuneMethodsOutput, - TuneMethodsInput, - TuneAssetType, - PromptTemplatesOutput, - PromptTemplatesInput, - PromptTemplateCreateInput, - PromptTemplateOutput, - PromptTemplateOptions, - PromptTemplateDeleteOptions, - PromptTemplateInput, - PromptTemplateExecuteInput, - PromptTemplateExecuteOptions, - PromptTemplateExecuteOutput, - PromptTemplateUpdateInput, - HistoryInput, - HistoryOptions, - HistoryOutput, - FileInput, - FileOutput, - FileOptions, - FileDeleteOptions, - FileCreateInput, - FilesOutput, - FilesInput, - FileDeleteOutput, - PromptTemplateDeleteOutput, - ChatInput, - ChatOutput, - ChatOptions, - ChatStreamOptions, - ChatStreamInput, - ChatStreamOutput, -} from './types.js'; -import { CacheDiscriminator, generateCacheKey } from './cache.js'; - -type FetchConfig = Omit, 'signal'> & { - retries?: number; - retryCondition?: (error: unknown) => boolean; - signal?: AbortSignal; -}; -type FetchConfigNoStream = FetchConfig & { - stream?: false; -}; -type FetchConfigStream = FetchConfig & { - stream: true; -}; - -export type RawHeaders = Record; - -export interface Configuration { - apiKey?: string; - endpoint?: string; - headers?: RawHeaders; - retries?: HttpHandlerOptions['retries']; -} - -export class Client { - readonly #client: AxiosCacheInstance; - readonly #options: Required; - - constructor(config: Configuration = {}) { - const endpoint = config.endpoint ?? lookupEndpoint(); - if (!endpoint) { - throw new InvalidInputError('Configuration endpoint is missing!'); - } - - const apiKey = config.apiKey ?? lookupApiKey(); - if (!apiKey) { - throw new InvalidInputError('Configuration API key is missing!'); - } - - const agent = version ? `node-sdk/${version}` : 'node-sdk'; - - this.#options = { - endpoint, - apiKey, - headers: { - 'User-Agent': agent, - 'X-Request-Origin': agent, - ...config.headers, - Accept: 'application/json', - Authorization: `Bearer ${apiKey}`, - }, - retries: config.retries ?? RETRY_ATTEMPTS_DEFAULT, - }; - - this.#client = setupCache( - axios.create({ - baseURL: this.#options.endpoint, - headers: this.#options.headers, - httpAgent: new http.Agent({ keepAlive: true }), - httpsAgent: new https.Agent({ keepAlive: true }), - maxRedirects: 0, - transitional: { - clarifyTimeoutError: true, - }, - }), - ); - } - - #fetcher( - input: FetchConfigStream, - schema?: ZodSchema, - ): TypedReadable; - #fetcher( - input: FetchConfigNoStream | FetchConfig, - schema?: ZodSchema, - ): Promise; - #fetcher( - input: - | FetchConfigNoStream - | FetchConfigStream, - schema?: ZodSchema, - ): Promise | TypedReadable { - if (input.stream) { - const outputStream = new TypedReadable({ - autoDestroy: true, - objectMode: true, - signal: input.signal, - }); - - const onClose = () => { - if (outputStream.readable) { - outputStream.push(null); - } - }; - - const delegatedController = new AbortController(); - if (input.signal) { - input.signal.addEventListener( - 'abort', - () => { - delegatedController.abort(); - }, - { - once: true, - }, - ); - } - - const onError = (e: unknown) => { - const err = errorTransformer(e); - - delegatedController.abort(); - if (outputStream.readable) { - outputStream.emit('error', err); - throw err; - } - onClose(); - }; - const url = new URL( - input.url ?? this.#options.endpoint, - this.#options.endpoint, - ); - fetchEventSource(url.toString(), { - method: 'POST', - body: JSON.stringify(input.data), - headers: { - ...this.#options.headers, - 'Content-Type': 'application/json', - }, - signal: delegatedController.signal, - onclose: onClose, - async onopen(response) { - const contentType = response.headers.get('content-type') || ''; - - if (response.ok && contentType === EventStreamContentType) { - return; - } - - const responseData = contentType.startsWith('application/json') - ? await response.json().catch(() => null) - : null; - - const headers = (() => { - const obj: Record = {}; - response.headers?.forEach((value, key) => { - obj[key] = value; - }); - return obj; - })(); - - onError( - new HttpError( - responseData?.message || 'Invalid response from server', - response.statusText, - response.status, - responseData?.extensions, - responseData, - headers, - ), - ); - }, - onmessage(message) { - if (message.event === 'close') { - onClose(); - return; - } - if (message.data === '') { - return; - } - - const result = safeParseJson(message.data); - if (result === null) { - onError( - new InternalError( - `Failed to parse message "${JSON.stringify(message)}"`, - ), - ); - return; - } - - outputStream.push(schema ? schema.parse(result) : result); - }, - onerror: onError, - }).catch(() => { - /* Prevent uncaught exception (errors are handled inside the stream) */ - }); - - return outputStream; - } - - const { retries, retryCondition, cache, ...restConfig } = input; - return promiseRetry( - (retry, attempt) => - this.#client({ - ...restConfig, - timeout: - input.timeout === undefined || input.timeout === Infinity - ? 0 // no timeout - : Math.max(1, input.timeout), - cache: { - ...cache, - override: (cache ? cache.override : false) || attempt > 1, - }, - }).catch((err) => { - const error = errorTransformer(err); - const conditionFn = retryCondition ?? isRetrievableError; - - if (conditionFn(error)) { - retry(error); - } - throw error; - }), - { retries: retries ?? this.#options.retries }, - ).then(({ data }) => (schema ? schema.parse(data) : data)); - } - - tokenize( - input: TokenizeInput, - options?: HttpHandlerOptions, - ): Promise; - tokenize( - input: TokenizeInput, - options: HttpHandlerOptions, - callback: Callback, - ): void; - tokenize(input: TokenizeInput, callback: Callback): void; - tokenize( - { input, ...restInput }: TokenizeInput, - optionsOrCallback?: HttpHandlerOptions | Callback, - callback?: Callback, - ): Promise | void { - return handle( - { - optionsOrCallback, - callback, - }, - async ({ options }) => { - const { results } = await this.#fetcher< - ApiTypes.TokenizeOutput, - ApiTypes.TokenizeInput - >({ - ...options, - method: 'POST', - url: '/v1/tokenize', - data: { - ...restInput, - use_default: true, - inputs: [input], - }, - stream: false, - }); - - if (results.length !== 1) { - throw new InvalidInputError('Unexpected number of results'); - } - - return results[0]; - }, - ); - } - - generate(input: GenerateInput, callback: Callback): void; - generate(input: GenerateInput[], callback: Callback): void; - generate( - input: GenerateInput, - options: HttpHandlerStreamOptions, - callback: Callback, - ): void; - generate( - input: GenerateInput, - options: HttpHandlerNoStreamOptions, - callback: Callback, - ): void; - generate( - input: GenerateInput, - options?: HttpHandlerNoStreamOptions, - ): Promise; - generate( - input: GenerateInput, - options: HttpHandlerStreamOptions, - ): TypedReadable; - generate( - input: GenerateInput[], - options: HttpHandlerNoStreamOptions, - callback: Callback, - ): void; - generate( - input: GenerateInput[], - options?: HttpHandlerNoStreamOptions, - ): Promise[]; - generate( - input: GenerateInput | GenerateInput[], - optionsOrCallback?: - | HttpHandlerNoStreamOptions - | HttpHandlerStreamOptions - | Callback - | Callback, - callbackOrNothing?: - | Callback - | Callback, - ): - | TypedReadable - | Promise - | Promise[] - | void { - const { callback, options } = parseFunctionOverloads( - undefined, - optionsOrCallback, - callbackOrNothing, - ); - - // Track time for timeout - const getTimeout = (() => { - const start = Date.now(); - const timeout = options?.timeout ?? this.#client.defaults.timeout; - - return () => - Math.max(0, timeout ? timeout - (Date.now() - start) : Infinity); - })(); - - // Normalize inputs - const inputs = !Array.isArray(input) ? [input] : input; - - const prepareRequest = ({ - input: inputText, - ...params - }: Unwrap) => ({ - ...options, - method: 'POST', - url: '/v1/generate', - data: { - ...params, - inputs: [inputText], - use_default: !params.prompt_id, - parameters: { - ...params.parameters, - stream: Boolean(options?.stream), - }, - }, - }); - - if (options?.stream) { - if (inputs.length > 1) { - throw new InvalidInputError( - 'Cannot do streaming for more than one input!', - ); - } - const stream = new Transform({ - autoDestroy: true, - objectMode: true, - transform( - chunk: ApiTypes.GenerateOutput, - encoding: BufferEncoding, - callback: TransformCallback, - ) { - try { - const { - generated_text = '', - stop_reason = null, - input_token_count = 0, - generated_token_count = 0, - ...props - } = (chunk.results || [{}])[0]; - - callback(null, { - generated_text, - stop_reason, - input_token_count, - generated_token_count, - ...(chunk.moderation && { - moderation: chunk.moderation, - }), - ...props, - } as GenerateOutput); - } catch (e) { - const err = (chunk || e) as unknown as Error; - callback(err, null); - } - }, - }); - - this.#fetcher({ - ...prepareRequest(inputs[0]), - timeout: getTimeout(), - stream: true, - }) - .on('error', (err) => stream.emit('error', errorTransformer(err))) - .pipe(stream); - - if (!callback) { - return stream; - } - - callbackifyStream(stream)(callback); - return; - } - - // Token calculation is considered public API - const tokenCounts = inputs.map(() => 1); - - const promises = inputs.map(async (inputData, index, arr) => { - try { - // Retry on concurrency limit error - while (getTimeout() > 0) { - // Cached limits preflight - const limits = await this.generateLimits(undefined, { - ...options, - timeout: getTimeout(), - }); - - // Check if the input fits into the capacity given previous inputs have precedence - const cumulativeTokenCount = tokenCounts - .slice(0, index + 1) - .reduce((acc, value) => acc + value, 0); // sum - const isWithinLimits = - limits.tokensUsed + cumulativeTokenCount <= limits.tokenCapacity; - - // If within concurrency limits, try to execute the request - if (isWithinLimits) { - try { - const { results } = await this.#fetcher< - ApiTypes.GenerateOutput, - ApiTypes.GenerateInput - >({ - ...prepareRequest(inputData), - timeout: getTimeout(), - }); - - if (results.length !== 1) { - throw new InternalError('Unexpected number of results'); - } - return results[0]; - } catch (err) { - if ( - err instanceof HttpError && - err.extensions?.code === 'TOO_MANY_REQUESTS' && - err.extensions?.reason === 'CONCURRENCY_LIMIT' - ) { - continue; - } - throw err; - } - } - await wait(Math.min(getTimeout(), 1000)); - } - throw new AxiosError('Timeout exceeded', AxiosError.ETIMEDOUT); - } finally { - tokenCounts[index] = 0; - await Promise.allSettled(arr.slice(0, index)); // Settle promises in-order - } - }); - - if (callback) { - promises.forEach((promise) => callbackifyPromise(promise)(callback)); - } else { - return Array.isArray(input) ? promises : promises[0]; - } - } - - generateConfig( - options: GenerateConfigOptions, - callback: Callback, - ): void; - generateConfig( - options?: GenerateConfigOptions, - ): Promise; - generateConfig( - input: GenerateConfigInput, - options?: GenerateConfigInputOptions, - ): Promise; - generateConfig( - input: GenerateConfigInput, - options?: GenerateConfigInputOptions, - ): Promise; - generateConfig( - input: GenerateConfigInput, - options: GenerateConfigInputOptions, - callback: Callback, - ): void; - generateConfig( - input: GenerateConfigInput, - callback: Callback, - ): void; - generateConfig( - inputOrResetOptions?: GenerateConfigInput | GenerateConfigOptions, - optionsOrCallback?: - | GenerateConfigInputOptions - | Callback, - callback?: Callback, - ): Promise | void { - return handle< - GenerateConfigInput | GenerateConfigOptions, - GenerateConfigInputOptions | Callback, - Callback, - GenerateConfigOutput - >( - { - inputOrOptionsOrCallback: inputOrResetOptions, - optionsOrCallback: optionsOrCallback, - callback, - }, - ({ input, options }) => { - const cacheKey = generateCacheKey(CacheDiscriminator.GENERATE_CONFIG); - if ( - isTypeOf( - input, - !input || 'reset' in input, - ) - ) { - const { reset, ...httpOptions } = input ?? {}; - if (reset) { - return this.#fetcher({ - ...httpOptions, - method: 'DELETE', - url: '/v1/generate/config', - cache: { - update: { - [cacheKey]: 'delete', - }, - }, - }); - } else { - return this.#fetcher({ - ...httpOptions, - method: 'GET', - url: '/v1/generate/config', - id: cacheKey, - }); - } - } - - const { strategy, ...httpOptions } = options ?? {}; - return this.#fetcher< - ApiTypes.GenerateConfigOutput, - ApiTypes.GenerateConfigInput - >({ - ...httpOptions, - method: strategy === 'merge' ? 'PATCH' : 'PUT', - url: '/v1/generate/config', - stream: false, - data: input, - cache: { - update: { - [cacheKey]: 'delete', - }, - }, - }); - }, - ); - } - - generateLimits( - input?: GenerateLimitsInput, - options?: HttpHandlerOptions, - ): Promise; - generateLimits(callback: Callback): void; - generateLimits( - input: GenerateLimitsInput, - callback: Callback, - ): void; - generateLimits( - input: GenerateLimitsInput, - options: HttpHandlerOptions, - callback: Callback, - ): void; - generateLimits( - inputOrCallback: GenerateLimitsInput, - optionsOrCallback?: HttpHandlerOptions | Callback, - callback?: Callback, - ): Promise | void { - return handle( - { - inputOrOptionsOrCallback: inputOrCallback, - optionsOrCallback: optionsOrCallback, - callback, - }, - ({ options }) => - this.#fetcher({ - ...options, - method: 'GET', - url: '/v1/generate/limits', - cache: { - ttl: 1_000, - }, - }), - ); - } - - models(callback: Callback): void; - models(input: ModelsInput, callback: Callback): void; - models( - input: ModelsInput, - options: HttpHandlerOptions, - callback: Callback, - ): void; - models( - input?: ModelsInput, - options?: HttpHandlerOptions, - ): Promise; - models( - inputOrCallback?: ModelsInput | Callback, - optionsOrCallback?: HttpHandlerOptions | Callback, - callback?: Callback, - ): Promise | void { - return handle( - { - inputOrOptionsOrCallback: inputOrCallback, - optionsOrCallback, - callback, - }, - async ({ options }) => { - const { results } = await this.#fetcher({ - ...options, - method: 'GET', - url: '/v1/models', - id: generateCacheKey(CacheDiscriminator.MODELS), - }); - return results; - }, - ); - } - - model(input: ModelInput, options?: HttpHandlerOptions): Promise; - model( - input: ModelInput, - options: HttpHandlerOptions, - callback: Callback, - ): void; - model(input: ModelInput, callback: Callback): void; - model( - input: ModelInput, - optionsOrCallback?: HttpHandlerOptions | Callback, - callback?: Callback, - ): Promise | void { - return handle( - { - optionsOrCallback, - callback, - }, - async ({ options }) => { - const { results } = await this.#fetcher({ - ...options, - method: 'GET', - url: `/v1/models/${encodeURIComponent(input.id)}`, - id: generateCacheKey(CacheDiscriminator.MODEL, input.id), - }); - return results; - }, - ); - } - - tunes(callback: Callback): void; - tunes(input: TunesInput, callback: Callback): void; - tunes( - input: TunesInput, - options: HttpHandlerOptions, - callback: Callback, - ): void; - tunes( - input?: TunesInput, - options?: HttpHandlerOptions, - ): AsyncGenerator; - tunes( - inputOrCallback?: TunesInput | Callback, - optionsOrCallback?: HttpHandlerOptions | Callback, - callback?: Callback, - ): AsyncGenerator | void { - return handleGenerator< - TunesInput | Callback, - HttpHandlerOptions | Callback, - Callback, - TunesOutput - >( - { - inputOrOptionsOrCallback: inputOrCallback, - optionsOrCallback, - callback, - }, - ({ input, options }) => { - const params = new URLSearchParams(); - if (input?.filters?.search) params.set('search', input.filters.search); - if (input?.filters?.status) params.set('status', input.filters.status); - return paginator( - (paginatorParams) => - this.#fetcher({ - ...options, - method: 'GET', - url: `/v1/tunes?${paginatorParams.toString()}`, - cache: false, - }), - { - offset: input?.filters?.offset ?? undefined, - count: input?.filters?.count ?? undefined, - params, - }, - ); - }, - ); - } - - tune( - input: TuneCreateInput, - options?: TuneCreateOptions, - ): Promise; - tune(input: TuneInput, options?: TuneOptions): Promise; - tune( - input: TuneCreateInput, - options: TuneCreateOptions, - callback: Callback, - ): void; - tune(input: TuneInput, options: TuneOptions, callback: Callback): void; - tune(input: TuneCreateInput, callback: Callback): void; - tune(input: TuneInput, callback: Callback): void; - tune( - input: TuneCreateInput | TuneInput, - optionsOrCallback?: - | TuneCreateOptions - | TuneOptions - | Callback - | Callback, - callback?: Callback | Callback, - ): Promise | void { - return handle({ optionsOrCallback, callback }, async ({ options }) => { - let apiOutput: ApiTypes.TuneOutput; - const isTuneInput = isTypeOf(input, 'id' in input); - if (isTuneInput) { - const cacheKey = generateCacheKey(CacheDiscriminator.TUNE, input.id); - const opts = options as TuneOptions | undefined; - if (opts?.delete) { - await this.#fetcher({ - ...options, - method: 'DELETE', - url: `/v1/tunes/${encodeURIComponent(input.id)}`, - cache: { - update: { - [cacheKey]: 'delete', - [generateCacheKey(CacheDiscriminator.MODEL, input.id)]: - 'delete', - [generateCacheKey(CacheDiscriminator.MODELS)]: 'delete', - }, - }, - }); - return; - } else { - apiOutput = await this.#fetcher({ - ...options, - method: 'GET', - url: `/v1/tunes/${encodeURIComponent(input.id)}`, - id: cacheKey, - }); - } - } else { - apiOutput = await this.#fetcher< - ApiTypes.TuneOutput, - ApiTypes.TuneInput - >({ - ...options, - method: 'POST', - url: `/v1/tunes`, - data: input, - cache: { - update: { - [generateCacheKey(CacheDiscriminator.MODELS)]: 'delete', - }, - }, - }); - } - const { status } = apiOutput.results; - switch (status) { - case 'COMPLETED': - return { - ...apiOutput.results, - status, - downloadAsset: async (type: TuneAssetType) => - this.#fetcher({ - ...options, - responseType: 'stream', - method: 'GET', - url: `/v1/tunes/${encodeURIComponent( - apiOutput.results.id, - )}/content/${type}`, - cache: false, - }), - }; - default: - return { ...apiOutput.results, status }; - } - }); - } - - tuneMethods(callback: Callback): void; - tuneMethods( - input: TuneMethodsInput, - callback: Callback, - ): void; - tuneMethods( - input: TuneMethodsInput, - options: HttpHandlerOptions, - callback: Callback, - ): void; - tuneMethods( - input?: TuneMethodsInput, - options?: HttpHandlerOptions, - ): Promise; - tuneMethods( - inputOrCallback?: TuneMethodsInput | Callback, - optionsOrCallback?: HttpHandlerOptions | Callback, - callback?: Callback, - ): Promise | void { - return handle( - { - optionsOrCallback, - callback, - }, - async ({ options }) => { - const { results } = await this.#fetcher({ - ...options, - method: 'GET', - url: `/v1/tune_methods`, - }); - return results; - }, - ); - } - - promptTemplate( - input: StrictUnion, - callback: Callback, - ): void; - promptTemplate( - input: StrictUnion, - options: PromptTemplateOptions, - callback: Callback, - ): void; - promptTemplate( - input: StrictUnion< - | PromptTemplateInput - | PromptTemplateCreateInput - | PromptTemplateUpdateInput - >, - options?: PromptTemplateOptions, - ): Promise; - promptTemplate( - input: PromptTemplateInput, - options: PromptTemplateDeleteOptions, - ): Promise; - promptTemplate( - input: PromptTemplateInput, - options: PromptTemplateDeleteOptions, - callback: Callback, - ): void; - promptTemplate( - input: PromptTemplateCreateInput, - callback: Callback, - ): void; - promptTemplate( - input: StrictUnion< - | PromptTemplateCreateInput - | PromptTemplateInput - | PromptTemplateUpdateInput - >, - optionsOrCallback?: - | PromptTemplateDeleteOptions - | PromptTemplateOptions - | Callback - | Callback, - callback?: - | Callback - | Callback, - ): Promise | void { - return handle({ optionsOrCallback, callback }, async ({ options }) => { - const isCreateInput = isTypeOf( - input, - !('id' in input), - ); - if (isCreateInput) { - const { results: result } = await this.#fetcher< - ApiTypes.PromptTemplateOutput, - ApiTypes.PromptTemplateCreateInput - >( - { - ...options, - method: 'POST', - url: `/v1/prompt_templates`, - data: input, - }, - ApiTypes.PromptTemplateOutputSchema, - ); - return result; - } - - const endpoint = `/v1/prompt_templates/${encodeURIComponent(input.id)}`; - const cacheKey = generateCacheKey( - CacheDiscriminator.PROMPT_TEMPLATE, - input.id, - ); - const opts = options as PromptTemplateDeleteOptions | undefined; - if (opts?.delete) { - await this.#fetcher({ - ...options, - method: 'DELETE', - url: endpoint, - cache: { - update: { - [cacheKey]: 'delete', - }, - }, - }); - return; - } - - const { id: _, ...body } = input; - if (isTypeOf(body, !isEmptyObject(body))) { - const { results: result } = await this.#fetcher< - ApiTypes.PromptTemplateOutput, - ApiTypes.PromptTemplateUpdate - >( - { - ...options, - method: 'PUT', - url: endpoint, - data: body, - cache: { - update: { - [cacheKey]: 'delete', - }, - }, - }, - ApiTypes.PromptTemplateOutputSchema, - ); - return result; - } - - const { results: result } = await this.#fetcher( - { - ...options, - method: 'GET', - url: endpoint, - id: cacheKey, - }, - ApiTypes.PromptTemplateOutputSchema, - ); - return result; - }); - } - - promptTemplates(callback: Callback): void; - promptTemplates( - input: PromptTemplatesInput, - callback: Callback, - ): void; - promptTemplates( - input: PromptTemplatesInput, - options: HttpHandlerOptions, - callback: Callback, - ): void; - promptTemplates( - input?: PromptTemplatesInput, - options?: HttpHandlerOptions, - ): AsyncGenerator; - promptTemplates( - inputOrCallback?: PromptTemplatesInput | Callback, - optionsOrCallback?: HttpHandlerOptions | Callback, - callback?: Callback, - ): AsyncGenerator | void { - return handleGenerator< - PromptTemplatesInput | Callback, - HttpHandlerOptions | Callback, - Callback, - PromptTemplatesOutput - >( - { - inputOrOptionsOrCallback: inputOrCallback, - optionsOrCallback, - callback, - }, - ({ input, options }) => - paginator( - async (paginatorParams) => - this.#fetcher( - { - ...options, - method: 'GET', - url: `/v1/prompt_templates?${paginatorParams.toString()}`, - cache: false, - }, - ApiTypes.PromptTemplatesOutputSchema, - ), - { - offset: input?.offset ?? undefined, - count: input?.count ?? undefined, - }, - ), - ); - } - - promptTemplateExecute( - input: PromptTemplateExecuteInput, - options?: PromptTemplateExecuteOptions, - ): Promise; - promptTemplateExecute( - input: PromptTemplateExecuteInput, - options: PromptTemplateExecuteOptions, - callback: Callback, - ): void; - promptTemplateExecute( - input: PromptTemplateExecuteInput, - callback: Callback, - ): void; - promptTemplateExecute( - input: PromptTemplateExecuteInput, - optionsOrCallback?: - | PromptTemplateExecuteOptions - | Callback, - callback?: Callback | Callback, - ): Promise | void { - return handle({ optionsOrCallback, callback }, async ({ options }) => { - const { results } = await this.#fetcher< - ApiTypes.PromptTemplateExecuteOutput, - ApiTypes.PromptTemplateExecuteInput - >({ - ...options, - method: 'POST', - url: '/v1/prompt_templates/output', - data: input, - }); - return results; - }); - } - - history(callback: Callback): void; - history(input: HistoryInput, callback: Callback): void; - history( - input: HistoryInput, - options: HistoryOptions, - callback: Callback, - ): void; - history( - input?: HistoryInput, - options?: HistoryOptions, - ): AsyncGenerator; - history( - inputOrCallback?: HistoryInput | Callback, - optionsOrCallback?: HistoryOptions | Callback, - callback?: Callback, - ): AsyncGenerator | void { - return handleGenerator< - HistoryInput | Callback, - HistoryOptions | Callback, - Callback, - HistoryOutput - >( - { - inputOrOptionsOrCallback: inputOrCallback, - optionsOrCallback, - callback, - }, - ({ input, options }) => { - const params = new URLSearchParams(); - if (input?.status) params.set('status', input.status); - if (input?.origin) params.set('origin', input.origin); - - return paginator( - (paginatorParams) => - this.#fetcher( - { - ...options, - method: 'GET', - url: `/v1/requests?${paginatorParams.toString()}`, - cache: false, - }, - ApiTypes.HistoryOutputSchema, - ), - { - offset: input?.offset ?? undefined, - count: input?.count ?? undefined, - params, - }, - ); - }, - ); - } - - files(callback: Callback): void; - files(input: FilesInput, callback: Callback): void; - files( - input: FilesInput, - options: HttpHandlerOptions, - callback: Callback, - ): void; - files( - input?: FilesInput, - options?: HttpHandlerOptions, - ): AsyncGenerator; - files( - inputOrCallback?: FilesInput | Callback, - optionsOrCallback?: HttpHandlerOptions | Callback, - callback?: Callback, - ): AsyncGenerator | void { - return handleGenerator< - FilesInput | Callback, - HttpHandlerOptions | Callback, - Callback, - FilesOutput - >( - { - inputOrOptionsOrCallback: inputOrCallback, - optionsOrCallback, - callback, - }, - ({ input, options }) => - paginator( - async (paginatorParams) => - this.#fetcher( - { - ...options, - method: 'GET', - url: `/v1/files?${paginatorParams.toString()}`, - cache: false, - }, - ApiTypes.FilesOutputSchema, - ), - { - offset: input?.offset ?? undefined, - count: input?.count ?? undefined, - }, - ), - ); - } - - file(input: FileInput, callback: Callback): void; - file( - input: FileInput, - options: FileOptions, - callback: Callback, - ): void; - file( - input: StrictUnion, - options?: FileOptions, - ): Promise; - file(input: FileInput, options: FileDeleteOptions): Promise; - file( - input: FileInput, - options: FileDeleteOptions, - callback: Callback, - ): void; - file(input: FileCreateInput, callback: Callback): void; - file( - input: StrictUnion, - optionsOrCallback?: - | FileDeleteOptions - | FileOptions - | Callback - | Callback, - callback?: Callback | Callback, - ): Promise | void { - return handle({ optionsOrCallback, callback }, async ({ options }) => { - const transformOutput = (apiOutput: ApiTypes.FileOutput['results']) => ({ - ...apiOutput, - download: () => - this.#fetcher({ - ...options, - responseType: 'stream', - method: 'GET', - url: `/v1/files/${encodeURIComponent(apiOutput.id)}/content`, - cache: false, - }), - }); - - const isCreateInput = isTypeOf(input, !('id' in input)); - if (isCreateInput) { - const { purpose, filename, file } = input; - const formData = new FormData(); - formData.append('purpose', purpose); - formData.append('file', file, { filename }); - const { results: result } = await this.#fetcher< - ApiTypes.FileOutput, - ApiTypes.FileCreateInput - >( - { - ...options, - method: 'POST', - url: `/v1/files`, - data: formData, - }, - ApiTypes.FileOutputSchema, - ); - return transformOutput(result); - } - - const endpoint = `/v1/files/${encodeURIComponent(input.id)}`; - const cacheKey = generateCacheKey(CacheDiscriminator.FILE, input.id); - const opts = options as FileDeleteOptions | undefined; - if (opts?.delete) { - await this.#fetcher({ - ...options, - method: 'DELETE', - url: endpoint, - cache: { - update: { - [cacheKey]: 'delete', - }, - }, - }); - return; - } - - const { results: result } = await this.#fetcher( - { - ...options, - method: 'GET', - url: endpoint, - id: cacheKey, - }, - ApiTypes.FileOutputSchema, - ); - return transformOutput(result); - }); - } - - chat(input: ChatInput, callback: Callback): void; - chat( - input: ChatInput, - options: ChatOptions, - callback: Callback, - ): void; - chat( - input: ChatStreamInput, - options: ChatStreamOptions, - callback: Callback, - ): void; - chat(input: ChatInput, options?: ChatOptions): Promise; - chat( - input: ChatStreamInput, - options?: ChatStreamOptions, - ): TypedReadable; - chat( - input: ChatInput | ChatStreamInput, - optionsOrCallback?: - | ChatOptions - | ChatStreamOptions - | Callback - | Callback, - callback?: Callback, - ): TypedReadable | Promise | void { - const { callback: cb, options } = parseFunctionOverloads( - undefined, - optionsOrCallback, - callback, - ); - - if (options?.stream) { - const stream = new Transform({ - autoDestroy: true, - objectMode: true, - transform( - chunk: ApiTypes.ChatStreamOutput, - encoding: BufferEncoding, - callback: TransformCallback, - ) { - const { results, ...rest } = chunk; - callback(null, { - ...rest, - result: results[0], - } as ChatStreamOutput); - }, - }); - this.#fetcher({ - ...options, - method: 'POST', - url: '/v0/generate/chat', - data: { - ...input, - parameters: { - ...input.parameters, - stream: true, - }, - }, - stream: true, - }) - .on('error', (err) => stream.emit('error', errorTransformer(err))) - .pipe(stream); - - if (cb) { - callbackifyStream(stream)(cb); - return; - } else { - return stream; - } - } else { - const promise = (async () => { - const { results, ...rest } = await this.#fetcher< - ApiTypes.ChatOutput, - ApiTypes.ChatInput - >( - { - ...options, - method: 'POST', - url: '/v0/generate/chat', - data: input, - stream: false, - }, - ApiTypes.ChatOutputSchema, - ); - if (results.length !== 1) { - throw new InternalError('Unexpected number of results'); - } - return { ...rest, result: results[0] }; - })(); - - if (cb) { - callbackifyPromise(promise)(cb); - return; - } else { - return promise; - } - } - } -} diff --git a/src/client/types.ts b/src/client/types.ts deleted file mode 100644 index 51643f2..0000000 --- a/src/client/types.ts +++ /dev/null @@ -1,288 +0,0 @@ -import { Readable } from 'node:stream'; - -import { z } from 'zod'; - -import * as ApiTypes from '../api-types.js'; -import { FlagOption } from '../helpers/types.js'; - -// COMMON - -const ListInputSchema = z.object({ - offset: z.number().int().nonnegative().nullish(), - count: z.number().int().positive().nullish(), -}); - -// GENERAL - -export interface HttpHandlerOptions { - timeout?: number; - signal?: AbortSignal; - retries?: number; -} - -export type HttpHandlerNoStreamOptions = HttpHandlerOptions & - FlagOption<'stream', false>; -export type HttpHandlerStreamOptions = HttpHandlerOptions & - FlagOption<'stream', true>; - -// GENERATE -const BaseGenerateInputSchema = ApiTypes.GenerateInputSchema.omit({ - inputs: true, - use_default: true, - prompt_id: true, - model_id: true, -}).extend({ input: z.string() }); -export const GenerateInputSchema = z.union([ - BaseGenerateInputSchema.extend({ - model_id: ApiTypes.GenerateInputSchema.shape.model_id, - prompt_id: z.never().optional(), - }), - BaseGenerateInputSchema.extend({ - prompt_id: ApiTypes.GenerateInputSchema.shape.prompt_id, - model_id: z.never().optional(), - }), -]); -export type GenerateInput = z.infer; -export type GenerateOutput = ApiTypes.GenerateOutput['results'][number] & { - moderation?: ApiTypes.GenerateOutput['moderation']; -}; - -export const GenerateConfigInputSchema = ApiTypes.GenerateConfigInputSchema; -export type GenerateConfigInput = z.input; - -export const GenerateConfigOutputSchema = ApiTypes.GenerateConfigOutputSchema; -export type GenerateConfigOutput = z.output; - -export type GenerateConfigOptions = HttpHandlerOptions & { reset?: boolean }; -export type GenerateConfigInputOptions = HttpHandlerOptions & { - strategy: 'merge' | 'replace'; -}; - -export const GenerateLimitsInputSchema = z.never(); -export type GenerateLimitsInput = z.input; - -export const GenerateLimitsOutputSchema = ApiTypes.GenerateLimitsOutputSchema; -export type GenerateLimitsOutput = z.output; - -// TOKENIZE - -export const TokenizeInputSchema = ApiTypes.TokenizeInputSchema.omit({ - inputs: true, -}).extend({ input: z.string() }); -export type TokenizeInput = z.infer; - -export const TokenizeOutputSchema = - ApiTypes.TokenizeOutputSchema.shape.results.element; -export type TokenizeOutput = z.output; - -// MODELS - -export const ModelsInputSchema = z.never(); -export type ModelsInput = z.infer; - -export const ModelsOutputSchema = ApiTypes.ModelsOutputSchema.shape.results; -export type ModelsOutput = z.output; - -export const ModelInputSchema = z.object({ id: z.string() }); -export type ModelInput = z.infer; - -export const ModelOutputSchema = ApiTypes.ModelOutputSchema.shape.results; -export type ModelOutput = z.output; - -// TUNES - -export const TunesInputSchema = z - .object({ - filters: ListInputSchema.and( - z.object({ - search: z.string().nullish(), - status: ApiTypes.TuneStatusSchema.nullish(), - }), - ), - }) - .partial(); -export type TunesInput = z.infer; -export type TunesOutput = ApiTypes.TunesOuput['results'][number]; - -export type TuneAssetType = z.infer; -export const TuneAssetTypeSchema = z.enum(['encoder', 'logs']); - -export const TuneInputSchema = z.object({ - id: z.string(), -}); -export type TuneInput = z.infer; - -export type TuneOptions = HttpHandlerOptions & { - delete?: boolean; -}; - -export const TuneCreateInputSchema = ApiTypes.TuneInputSchema; -export type TuneCreateInput = z.infer; -export type TuneCreateOptions = HttpHandlerOptions; - -export const TuneOutputSchema = z.union([ - ApiTypes.TuneOutputSchema.shape.results.extend({ - status: ApiTypes.TuneOutputSchema.shape.results.shape.status.exclude([ - 'COMPLETED', - ]), - }), - ApiTypes.TuneOutputSchema.shape.results - .extend({ - status: ApiTypes.TuneOutputSchema.shape.results.shape.status.extract([ - 'COMPLETED', - ]), - }) - .and( - z.object({ - downloadAsset: z - .function() - .args(TuneAssetTypeSchema) - .returns(z.custom>()), - }), - ), -]); -export type TuneOutput = z.output; - -export const TuneDeleteOutputSchema = z.void(); -export type TuneDeleteOutput = z.output; - -export const TuneMethodsInputSchema = z.never(); -export type TuneMethodsInput = z.infer; - -export const TuneMethodsOutputSchema = - ApiTypes.TuneMethodsOutputSchema.shape.results; -export type TuneMethodsOutput = z.output; - -// PROMPT TEMPLATES - -export const PromptTemplateInputSchema = ApiTypes.PromptTemplateInputSchema; -export type PromptTemplateInput = z.input; - -export const PromptTemplateCreateInputSchema = - ApiTypes.PromptTemplateCreateInputSchema; -export type PromptTemplateCreateInput = z.input< - typeof PromptTemplateCreateInputSchema ->; - -export const PromptTemplateUpdateInputSchema = z.intersection( - PromptTemplateInputSchema, - ApiTypes.PromptTemplateUpdateInputSchema, -); -export type PromptTemplateUpdateInput = z.input< - typeof PromptTemplateUpdateInputSchema ->; - -export const PromptTemplateOutputSchema = - ApiTypes.PromptTemplateOutputSchema.shape.results; -export type PromptTemplateOutput = z.output; - -export const PromptTemplateDeleteOutputSchema = z.void(); -export type PromptTemplateDeleteOutput = z.output< - typeof PromptTemplateDeleteOutputSchema ->; - -export const PromptTemplatesInputSchema = ListInputSchema; -export type PromptTemplatesInput = z.input; - -export const PromptTemplatesOutputSchema = - ApiTypes.PromptTemplatesOutputSchema.shape.results.element; -export type PromptTemplatesOutput = z.output< - typeof PromptTemplatesOutputSchema ->; - -export type PromptTemplateOptions = HttpHandlerOptions & - FlagOption<'delete', false>; -export type PromptTemplateDeleteOptions = HttpHandlerOptions & - FlagOption<'delete', true>; - -export const PromptTemplateExecuteInputSchema = - ApiTypes.PromptTemplateExecuteInputSchema; -export type PromptTemplateExecuteInput = z.input< - typeof PromptTemplateExecuteInputSchema ->; - -export const PromptTemplateExecuteOutputSchema = - ApiTypes.PromptTemplateExecuteOutputSchema.shape.results; -export type PromptTemplateExecuteOutput = z.output< - typeof PromptTemplateExecuteOutputSchema ->; - -export type PromptTemplateExecuteOptions = HttpHandlerOptions; - -// HISTORY - -export const HistoryInputSchema = ListInputSchema.and( - ApiTypes.HistoryInputSchema.pick({ - status: true, - origin: true, - }), -); -export type HistoryInput = z.input; - -export type HistoryOptions = HttpHandlerOptions; - -export const HistoryOutputSchema = - ApiTypes.HistoryOutputSchema.shape.results.element; -export type HistoryOutput = z.infer; - -export const HistoryStatusSchema = ApiTypes.HistoryStatusSchema; -export type HistoryStatus = z.infer; - -export const HistoryOriginSchema = ApiTypes.HistoryOriginSchema; -export type HistoryOrigin = z.infer; - -// FILES - -export const FilePurposeSchema = ApiTypes.FilePurposeSchema; -export type FilePupose = z.infer; - -export const FileInputSchema = ApiTypes.FileInputSchema; -export type FileInput = z.input; - -export const FileCreateInputSchema = z.object({ - purpose: FilePurposeSchema, - filename: z.string(), - file: z.custom(), -}); -export type FileCreateInput = z.input; - -export const FileOutputSchema = ApiTypes.FileOutputSchema.shape.results.and( - z.object({ - download: z.function().returns(z.custom>()), - }), -); -export type FileOutput = z.output; - -export const FileDeleteOutputSchema = z.void(); -export type FileDeleteOutput = z.output; - -export const FilesInputSchema = ListInputSchema; -export type FilesInput = z.input; - -export type FileOptions = HttpHandlerOptions & FlagOption<'delete', false>; -export type FileDeleteOptions = HttpHandlerOptions & FlagOption<'delete', true>; - -export const FilesOutputSchema = - ApiTypes.FilesOutputSchema.shape.results.element; -export type FilesOutput = z.output; - -// CHAT - -export const ChatInputSchema = z.union([ - ApiTypes.ChatInputSchema, - ApiTypes.ChatStreamInputSchema, -]); -export type ChatInput = z.input; -export type ChatOptions = HttpHandlerNoStreamOptions; -export const ChatOutputSchema = ApiTypes.ChatOutputSchema.omit({ - results: true, -}).extend({ result: ApiTypes.ChatOutputSchema.shape.results.element }); -export type ChatOutput = z.output; - -export const ChatStreamInputSchema = ApiTypes.ChatStreamInputSchema; -export type ChatStreamInput = z.input; -export type ChatStreamOptions = HttpHandlerStreamOptions; -export const ChatStreamOutputSchema = ApiTypes.ChatStreamOutputSchema.omit({ - results: true, -}).extend({ result: ApiTypes.ChatOutputSchema.shape.results.element }); -export type ChatStreamOutput = z.output; diff --git a/src/constants.ts b/src/constants.ts deleted file mode 100644 index 2d753b9..0000000 --- a/src/constants.ts +++ /dev/null @@ -1 +0,0 @@ -export const RETRY_ATTEMPTS_DEFAULT = 3; diff --git a/src/errors.ts b/src/errors.ts index c3ae5bc..bf47890 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -1,14 +1,4 @@ -import { IncomingHttpHeaders } from 'node:http'; - -import { - AxiosHeaders, - AxiosResponse, - HttpStatusCode, - isAxiosError, - isCancel as isAxiosCancel, -} from 'axios'; - -import { ErrorExtensions, ErrorResponse } from './api-types.js'; +import { ApiError } from './api/client.js'; export class BaseError extends Error {} @@ -16,116 +6,19 @@ export class InvalidInputError extends BaseError {} export class InternalError extends BaseError {} -export class RequestError extends BaseError { - code?: string; - cancelled: boolean; - - constructor( - message?: string, - code?: string, - cancelled = false, - options?: ErrorOptions, - ) { - super(message, options); - - this.name = new.target.name; - this.code = code; - this.cancelled = cancelled; - Object.setPrototypeOf(this, new.target.prototype); - Error.captureStackTrace(this, this.constructor); - } -} - -export class RequestCanceledError extends RequestError { - constructor(message?: string, code?: string, options?: ErrorOptions) { - super(message, code, true, options); - } -} - -export class HttpError extends RequestError { - statusCode: number; - extensions?: ErrorExtensions; - response: ErrorResponse; - headers: IncomingHttpHeaders; - - constructor( - message: string, - statusText: string, - statusCode: number, - extensions: ErrorExtensions | undefined, - response: ErrorResponse, - headers: IncomingHttpHeaders, - options?: ErrorOptions, - ) { - super(message, 'ERR_NON_2XX_3XX_RESPONSE', false, options); - this.statusCode = statusCode; - this.extensions = extensions; - this.response = response; - this.headers = headers; - } -} - -function isAbortError(err: unknown): err is DOMException { - return Boolean(err && err instanceof Error && err.name === 'AbortError'); -} - -export function errorTransformer(err: unknown) { - if (isAbortError(err)) { - return new RequestCanceledError( - err.message, - String(err.cause ?? err.name), - { - cause: err, - }, - ); - } - - if (!isAxiosError(err)) { - return err; - } - - if (err.response) { - // The request was made and the server responded with a status code - // that falls out of the range of 2xx - const response = err.response as AxiosResponse; +export abstract class RequestError extends BaseError {} - return new HttpError( - response.data.message, - response.statusText, - response.status, - response.data.extensions, - response.data, - (response.headers instanceof AxiosHeaders - ? response.headers.toJSON() - : // There's some inconsistency between node headers and axios headers types (and only types because we are not in the browser) - // eslint-disable-next-line @typescript-eslint/no-explicit-any - response.headers) as any, - { cause: err }, - ); - } +export class NetworkError extends RequestError {} - if (isAxiosCancel(err as unknown)) { - return new RequestCanceledError(err.message, err.code, { cause: err }); - } - return new RequestError(err.message, err.code, false, { cause: err }); -} +export class HttpError extends RequestError implements ApiError { + readonly error: ApiError['error']; + readonly status_code: ApiError['status_code']; + readonly extensions: ApiError['extensions']; -export function isRetrievableError(error: unknown): boolean { - if (error instanceof HttpError) { - return [ - // Client errors - HttpStatusCode.RequestTimeout, - HttpStatusCode.TooManyRequests, - // Server errors - HttpStatusCode.InternalServerError, - HttpStatusCode.BadGateway, - HttpStatusCode.ServiceUnavailable, - HttpStatusCode.GatewayTimeout, - HttpStatusCode.InsufficientStorage, - ].includes(error.statusCode); - } - if (error instanceof RequestError) { - return !error.cancelled; + constructor(error: ApiError) { + super(error.message, { cause: error }); + this.error = error.error; + this.status_code = error.status_code; + this.extensions = error.extensions; } - return false; } diff --git a/src/index.ts b/src/index.ts index 9455acd..8583a02 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,4 @@ -export * from './client/client.js'; +export * from './client.js'; export * from './errors.js'; -export * from './client/types.js'; -export * as ApiTypes from './api-types.js'; - export * from './buildInfo.js'; -export * from './constants.js'; diff --git a/src/langchain/llm.ts b/src/langchain/llm.ts index 7005a1c..3f57212 100644 --- a/src/langchain/llm.ts +++ b/src/langchain/llm.ts @@ -3,7 +3,7 @@ import { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager'; import type { LLMResult, Generation } from '@langchain/core/outputs'; import { GenerationChunk } from '@langchain/core/outputs'; -import { Client, Configuration } from '../client/client.js'; +import { Client, Configuration } from '../client.js'; import { isNotEmptyArray, concatUnique, diff --git a/src/tests/e2e/client.test.ts b/src/tests/e2e/client.test.ts index e76854f..925adf4 100644 --- a/src/tests/e2e/client.test.ts +++ b/src/tests/e2e/client.test.ts @@ -3,7 +3,7 @@ import { GenerateInput, GenerateOutput, } from '../../client/types.js'; -import { Client } from '../../client/client.js'; +import { Client } from '../../client.js'; import { RequestCanceledError } from '../../errors.js'; describe('client', () => { diff --git a/src/tests/integration/client.test.ts b/src/tests/integration/client.test.ts index 20d21f9..a0b71eb 100644 --- a/src/tests/integration/client.test.ts +++ b/src/tests/integration/client.test.ts @@ -8,7 +8,7 @@ import { promptTemplatesStore, historyStore, } from '../mocks/handlers.js'; -import { Client } from '../../client/client.js'; +import { Client } from '../../client.js'; const dummyTune = { name: 'newTune', @@ -63,57 +63,6 @@ describe('client', () => { }); describe('generate', () => { - describe('config', () => { - test('should read the config', async () => { - const config = await client.generateConfig(); - expect(config).toMatchObject({ model_id: 'foobar' }); - }); - - test('should replace the config', async () => { - const input = { - model_id: 'google/flan-ul2', - parameters: { - decoding_method: 'greedy', - random_seed: 8, - }, - }; - const config = await client.generateConfig(input, { - strategy: 'replace', - }); - expect(config).toMatchObject(input); - }); - - test('should merge the config', async () => { - const input = { - parameters: { - decoding_method: 'greedy', - random_seed: 8, - }, - }; - const config = await client.generateConfig(input, { - strategy: 'merge', - }); - expect(config).toMatchObject({ model_id: 'foobar', ...input }); - }); - - test('should set and reset the config', async () => { - const input = { - model_id: 'google/flan-ul2', - parameters: { - decoding_method: 'greedy', - random_seed: 8, - }, - }; - const replacedConfig = await client.generateConfig(input, { - strategy: 'replace', - }); - expect(replacedConfig).toMatchObject(input); - - const config = await client.generateConfig({ reset: true }); - expect(config).toMatchObject({ model_id: 'foobar' }); - }); - }); - test('should return single output for a single input', async () => { const data = await client.generate({ model_id: 'bigscience/bloom', diff --git a/src/tests/integration/errors.test.ts b/src/tests/integration/errors.test.ts new file mode 100644 index 0000000..e61d655 --- /dev/null +++ b/src/tests/integration/errors.test.ts @@ -0,0 +1,46 @@ +import fetch from 'cross-fetch'; + +import { createApiClient } from '../../api/client.js'; +import { BaseError, HttpError, NetworkError } from '../../errors.js'; +import { clientErrorWrapper } from '../../utils/errors.js'; +import { MOCK_ENDPOINT } from '../mocks/handlers.js'; + +describe('errors', () => { + test('should fail with network error', async () => { + const client = createApiClient({ baseUrl: 'http://invalidhost', fetch }); + await expect( + clientErrorWrapper( + client.GET('/v2/models', { + params: { query: { limit: 100, offset: 0, version: '2023-11-22' } }, + }), + ), + ).rejects.toBeInstanceOf(NetworkError); + }); + + test('should fail with http error', async () => { + const client = createApiClient({ baseUrl: MOCK_ENDPOINT, fetch }); + await expect( + clientErrorWrapper( + client.GET('/error' as '/v2/models', { + params: { query: { limit: 100, offset: 0, version: '2023-11-22' } }, + }), + ), + ).rejects.toBeInstanceOf(HttpError); + }); + + test('should fail with abort error', async () => { + const client = createApiClient({ baseUrl: MOCK_ENDPOINT, fetch }); + const controller = new AbortController(); + controller.abort(); + + const promise = clientErrorWrapper( + client.GET('/v2/models', { + params: { query: { limit: 100, offset: 0, version: '2023-11-22' } }, + signal: controller.signal, + }), + ); + + await expect(promise).rejects.toThrowError('The user aborted a request.'); + await expect(promise).rejects.not.toBeInstanceOf(BaseError); + }); +}); diff --git a/src/tests/mocks/handlers.ts b/src/tests/mocks/handlers.ts index e0d9fce..52b3c86 100644 --- a/src/tests/mocks/handlers.ts +++ b/src/tests/mocks/handlers.ts @@ -436,4 +436,16 @@ export const handlers: RestHandler>[] = [ }), ); }), + + // ERROR + rest.get(`${MOCK_ENDPOINT}/error`, async (req, res, ctx) => + res( + ctx.status(500), + ctx.json({ + error: 'Any error', + message: 'Any message', + status_code: 500, + }), + ), + ), ]; diff --git a/src/utils/errors.ts b/src/utils/errors.ts new file mode 100644 index 0000000..113c0b9 --- /dev/null +++ b/src/utils/errors.ts @@ -0,0 +1,38 @@ +import { FetchResponse } from 'openapi-fetch'; + +import { HttpError, InternalError, NetworkError } from '../errors.js'; + +function isAbortError(err: unknown): err is DOMException { + return Boolean(err && err instanceof Error && err.name === 'AbortError'); +} + +const ServiceUnavailableErrorCodes = new Set([ + 'ENOTFOUND', + 'ETIMEDOUT', + 'ECONNRESET', + 'EHOSTDOWN', + 'ECONNREFUSED', + 'ENETUNREACH', // macOS + 'EHOSTUNREACH', // Linux + 'UND_ERR_CONNECT_TIMEOUT', +]); +function isServiceError(err: unknown) { + const code = (err as any)?.code; + return !!code && ServiceUnavailableErrorCodes.has(code); +} + +export async function clientErrorWrapper( + request: Promise>, +) { + try { + const { data, error } = await request; + if (error) throw new HttpError(error as Exclude); + return data; + } catch (err) { + if (err instanceof HttpError) throw err; + if (isAbortError(err)) throw err; + if (isServiceError(err)) + throw new NetworkError('Unable to connect', { cause: err }); + throw new InternalError('Request failed', { cause: err }); + } +} diff --git a/src/types.ts b/src/utils/types.ts similarity index 57% rename from src/types.ts rename to src/utils/types.ts index 05a6d45..dab68cf 100644 --- a/src/types.ts +++ b/src/utils/types.ts @@ -3,3 +3,9 @@ type StrictUnionHelper = T extends any ? T & Partial, keyof T>, never>> : never; export type StrictUnion = StrictUnionHelper; + +export type FilterKeys = { + [K in keyof Obj]: K extends Matchers ? Obj[K] : never; +}[keyof Obj]; + +export type OmitVersion = Omit; diff --git a/yarn.lock b/yarn.lock index 312f561..5b671d9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -621,6 +621,13 @@ __metadata: languageName: node linkType: hard +"@fastify/busboy@npm:^2.0.0": + version: 2.0.0 + resolution: "@fastify/busboy@npm:2.0.0" + checksum: 41879937ce1dee6421ef9cd4da53239830617e1f0bb7a0e843940772cd72827205d05e518af6adabe6e1ea19301285fff432b9d11bad01a531e698bea95c781b + languageName: node + linkType: hard + "@gar/promisify@npm:^1.1.3": version: 1.1.3 resolution: "@gar/promisify@npm:1.1.3" @@ -662,28 +669,27 @@ __metadata: "@commitlint/config-conventional": ^18.0.0 "@langchain/core": ^0.1.11 "@types/lodash": ^4.14.200 - "@types/node": ^20.8.8 - "@types/promise-retry": ^1.1.5 + "@types/node": 18.18.2 "@typescript-eslint/eslint-plugin": ^6.9.0 "@typescript-eslint/parser": ^6.9.0 "@vitest/coverage-c8": ^0.31.2 - axios: ^1.5.1 - axios-cache-interceptor: ^1.3.2 compare-versions: ^6.1.0 + cross-fetch: ^4.0.0 dotenv-flow: ^4.0.0 eslint: ^8.52.0 eslint-config-prettier: ^9.0.0 eslint-import-resolver-typescript: ^3.6.1 eslint-plugin-import: ^2.29.0 - form-data: ^4.0.0 + fetch-retry: ^5.0.6 husky: ^8.0.3 jest-extended: ^4.0.2 lint-staged: ^15.0.2 lodash: ^4.17.21 msw: ^1.3.2 + openapi-fetch: ^0.8.1 + openapi-typescript: ^6.7.1 pinst: ^3.0.0 prettier: ^3.0.3 - promise-retry: ^2.0.1 ts-node: ^10.9.1 tsup: ^7.2.0 typescript: ^5.2.2 @@ -1082,6 +1088,13 @@ __metadata: languageName: node linkType: hard +"@types/node@npm:18.18.2": + version: 18.18.2 + resolution: "@types/node@npm:18.18.2" + checksum: 252a90e351e6e8631e5eb66d826b6a80fe191fcf94d8cc8ace5263afeb2da0b25d80a4bdae59fa7ec1b2dc6f03f5fb9a1f23c310dac72789b03935c7ba901853 + languageName: node + linkType: hard + "@types/node@npm:^18.11.9": version: 18.18.6 resolution: "@types/node@npm:18.18.6" @@ -1089,15 +1102,6 @@ __metadata: languageName: node linkType: hard -"@types/node@npm:^20.8.8": - version: 20.8.8 - resolution: "@types/node@npm:20.8.8" - dependencies: - undici-types: ~5.25.1 - checksum: 028a9606e4ef594a4bc7b3310596499d7ce01b2e30f4d1d906ad8ec30c24cea7ec1b3dc181dd5df8d8d2bfe8de54bf3e28ae93be174b4c7d81c0db8326e4f35c - languageName: node - linkType: hard - "@types/normalize-package-data@npm:^2.4.0": version: 2.4.1 resolution: "@types/normalize-package-data@npm:2.4.1" @@ -1105,22 +1109,6 @@ __metadata: languageName: node linkType: hard -"@types/promise-retry@npm:^1.1.5": - version: 1.1.5 - resolution: "@types/promise-retry@npm:1.1.5" - dependencies: - "@types/retry": "*" - checksum: 8da877e0db31630835ab95767decb0aa74828b7507c59c7c501e43f6fa2524d58c7b727024b343a592769f4c592e958cbb2843e8af44dffacf26a6e0ad424c30 - languageName: node - linkType: hard - -"@types/retry@npm:*": - version: 0.12.2 - resolution: "@types/retry@npm:0.12.2" - checksum: e5675035717b39ce4f42f339657cae9637cf0c0051cf54314a6a2c44d38d91f6544be9ddc0280587789b6afd056be5d99dbe3e9f4df68c286c36321579b1bf4a - languageName: node - linkType: hard - "@types/retry@npm:0.12.0": version: 0.12.0 resolution: "@types/retry@npm:0.12.0" @@ -1469,6 +1457,13 @@ __metadata: languageName: node linkType: hard +"ansi-colors@npm:^4.1.3": + version: 4.1.3 + resolution: "ansi-colors@npm:4.1.3" + checksum: a9c2ec842038a1fabc7db9ece7d3177e2fe1c5dc6f0c51ecfbf5f39911427b89c00b5dc6b8bd95f82a26e9b16aaae2e83d45f060e98070ce4d1333038edceb0e + languageName: node + linkType: hard + "ansi-escapes@npm:^4.2.1": version: 4.3.2 resolution: "ansi-escapes@npm:4.3.2" @@ -1684,13 +1679,6 @@ __metadata: languageName: node linkType: hard -"asynckit@npm:^0.4.0": - version: 0.4.0 - resolution: "asynckit@npm:0.4.0" - checksum: 7b78c451df768adba04e2d02e63e2d0bf3b07adcd6e42b4cf665cb7ce899bedd344c69a1dcbce355b5f972d597b25aaa1c1742b52cffd9caccb22f348114f6be - languageName: node - linkType: hard - "available-typed-arrays@npm:^1.0.5": version: 1.0.5 resolution: "available-typed-arrays@npm:1.0.5" @@ -1698,30 +1686,6 @@ __metadata: languageName: node linkType: hard -"axios-cache-interceptor@npm:^1.3.2": - version: 1.3.2 - resolution: "axios-cache-interceptor@npm:1.3.2" - dependencies: - cache-parser: ^1.2.4 - fast-defer: ^1.1.7 - object-code: ^1.3.0 - peerDependencies: - axios: ^1 - checksum: 1cda85f549f6471ba37938deda381e6b638c08e48363b803055b3e10bb4c854274a7bc206c2763efb23cc4b0a7f01285cb4fa7a4575fa8d9d65ded70b3512a59 - languageName: node - linkType: hard - -"axios@npm:^1.5.1": - version: 1.5.1 - resolution: "axios@npm:1.5.1" - dependencies: - follow-redirects: ^1.15.0 - form-data: ^4.0.0 - proxy-from-env: ^1.1.0 - checksum: 4444f06601f4ede154183767863d2b8e472b4a6bfc5253597ed6d21899887e1fd0ee2b3de792ac4f8459fe2e359d2aa07c216e45fd8b9e4e0688a6ebf48a5a8d - languageName: node - linkType: hard - "balanced-match@npm:^1.0.0": version: 1.0.2 resolution: "balanced-match@npm:1.0.2" @@ -1865,13 +1829,6 @@ __metadata: languageName: node linkType: hard -"cache-parser@npm:^1.2.4": - version: 1.2.4 - resolution: "cache-parser@npm:1.2.4" - checksum: de9fc4ab7af318109f1e53474e674d43997bc7b8676157e4f28e7dc60fda2f434ee138aa9d9abb9f849c55e73202ee74865afaa4e00298de70c4326510680c19 - languageName: node - linkType: hard - "call-bind@npm:^1.0.0, call-bind@npm:^1.0.2": version: 1.0.2 resolution: "call-bind@npm:1.0.2" @@ -2136,15 +2093,6 @@ __metadata: languageName: node linkType: hard -"combined-stream@npm:^1.0.8": - version: 1.0.8 - resolution: "combined-stream@npm:1.0.8" - dependencies: - delayed-stream: ~1.0.0 - checksum: 49fa4aeb4916567e33ea81d088f6584749fc90c7abec76fd516bf1c5aa5c79f3584b5ba3de6b86d26ddd64bae5329c4c7479343250cfe71c75bb366eae53bb7c - languageName: node - linkType: hard - "commander@npm:11.1.0": version: 11.1.0 resolution: "commander@npm:11.1.0" @@ -2283,6 +2231,15 @@ __metadata: languageName: node linkType: hard +"cross-fetch@npm:^4.0.0": + version: 4.0.0 + resolution: "cross-fetch@npm:4.0.0" + dependencies: + node-fetch: ^2.6.12 + checksum: ecca4f37ffa0e8283e7a8a590926b66713a7ef7892757aa36c2d20ffa27b0ac5c60dcf453119c809abe5923fc0bae3702a4d896bfb406ef1077b0d0018213e24 + languageName: node + linkType: hard + "cross-spawn@npm:^7.0.0, cross-spawn@npm:^7.0.2, cross-spawn@npm:^7.0.3": version: 7.0.3 resolution: "cross-spawn@npm:7.0.3" @@ -2397,13 +2354,6 @@ __metadata: languageName: node linkType: hard -"delayed-stream@npm:~1.0.0": - version: 1.0.0 - resolution: "delayed-stream@npm:1.0.0" - checksum: 46fe6e83e2cb1d85ba50bd52803c68be9bd953282fa7096f51fc29edd5d67ff84ff753c51966061e5ba7cb5e47ef6d36a91924eddb7f3f3483b1c560f77a0020 - languageName: node - linkType: hard - "delegates@npm:^1.0.0": version: 1.0.0 resolution: "delegates@npm:1.0.0" @@ -3067,13 +3017,6 @@ __metadata: languageName: node linkType: hard -"fast-defer@npm:^1.1.7": - version: 1.1.7 - resolution: "fast-defer@npm:1.1.7" - checksum: c0f816fe3f83ca7e12c56a6631c6819a50349fb7e1931cec88e883168b523c65605e6e0df32c8fba6c341860c29ecc6fe2531ba8a418d27d31da6544ad1bf45b - languageName: node - linkType: hard - "fast-glob@npm:^3.2.9": version: 3.2.12 resolution: "fast-glob@npm:3.2.12" @@ -3123,6 +3066,13 @@ __metadata: languageName: node linkType: hard +"fetch-retry@npm:^5.0.6": + version: 5.0.6 + resolution: "fetch-retry@npm:5.0.6" + checksum: 4ad8bca6ec7a7b1212e636bb422a9ae8bb9dce38df0b441c9eb77a29af99b368029d6248ff69427da67e3d43c53808b121135ea395e7fe4f8f383e0ad65b4f27 + languageName: node + linkType: hard + "figures@npm:^3.0.0": version: 3.2.0 resolution: "figures@npm:3.2.0" @@ -3187,16 +3137,6 @@ __metadata: languageName: node linkType: hard -"follow-redirects@npm:^1.15.0": - version: 1.15.2 - resolution: "follow-redirects@npm:1.15.2" - peerDependenciesMeta: - debug: - optional: true - checksum: faa66059b66358ba65c234c2f2a37fcec029dc22775f35d9ad6abac56003268baf41e55f9ee645957b32c7d9f62baf1f0b906e68267276f54ec4b4c597c2b190 - languageName: node - linkType: hard - "for-each@npm:^0.3.3": version: 0.3.3 resolution: "for-each@npm:0.3.3" @@ -3216,17 +3156,6 @@ __metadata: languageName: node linkType: hard -"form-data@npm:^4.0.0": - version: 4.0.0 - resolution: "form-data@npm:4.0.0" - dependencies: - asynckit: ^0.4.0 - combined-stream: ^1.0.8 - mime-types: ^2.1.12 - checksum: 01135bf8675f9d5c61ff18e2e2932f719ca4de964e3be90ef4c36aacfc7b9cb2fceb5eca0b7e0190e3383fe51c5b37f4cb80b62ca06a99aaabfcfd6ac7c9328c - languageName: node - linkType: hard - "fs-extra@npm:^11.0.0": version: 11.1.1 resolution: "fs-extra@npm:11.1.1" @@ -4712,22 +4641,6 @@ __metadata: languageName: node linkType: hard -"mime-db@npm:1.52.0": - version: 1.52.0 - resolution: "mime-db@npm:1.52.0" - checksum: 0d99a03585f8b39d68182803b12ac601d9c01abfa28ec56204fa330bc9f3d1c5e14beb049bafadb3dbdf646dfb94b87e24d4ec7b31b7279ef906a8ea9b6a513f - languageName: node - linkType: hard - -"mime-types@npm:^2.1.12": - version: 2.1.35 - resolution: "mime-types@npm:2.1.35" - dependencies: - mime-db: 1.52.0 - checksum: 89a5b7f1def9f3af5dad6496c5ed50191ae4331cc5389d7c521c8ad28d5fdad2d06fd81baf38fed813dc4e46bb55c8145bb0ff406330818c9cf712fb2e9b3836 - languageName: node - linkType: hard - "mimic-fn@npm:^2.1.0": version: 2.1.0 resolution: "mimic-fn@npm:2.1.0" @@ -5164,13 +5077,6 @@ __metadata: languageName: node linkType: hard -"object-code@npm:^1.3.0": - version: 1.3.0 - resolution: "object-code@npm:1.3.0" - checksum: bc5e3df85ac54785b3d1d9b8a9b2e168eb5f69e02be3c958fc05aebaa85ec659c55903852bf15ebf44e98053212f88eb7d3a9e28a50f10c4d60d820f80aa8d64 - languageName: node - linkType: hard - "object-inspect@npm:^1.12.3, object-inspect@npm:^1.9.0": version: 1.12.3 resolution: "object-inspect@npm:1.12.3" @@ -5258,6 +5164,38 @@ __metadata: languageName: node linkType: hard +"openapi-fetch@npm:^0.8.1": + version: 0.8.1 + resolution: "openapi-fetch@npm:0.8.1" + dependencies: + openapi-typescript-helpers: ^0.0.4 + checksum: 9bdc37b21bc662af2826c51165ea1e1b0fa5ac05076852870457800d85b0a9bbf3466f905919fba014badd7143946e86640f748dd5720d49445d32d16429ccab + languageName: node + linkType: hard + +"openapi-typescript-helpers@npm:^0.0.4": + version: 0.0.4 + resolution: "openapi-typescript-helpers@npm:0.0.4" + checksum: d06d62f9669db5110f32ef36a70f9f2698dd7ad03ba41968baf13e1091caaf23c4af6f8a79103cfd68f65046da4284dcc2f75e9950e9170115189b545e3030fc + languageName: node + linkType: hard + +"openapi-typescript@npm:^6.7.1": + version: 6.7.1 + resolution: "openapi-typescript@npm:6.7.1" + dependencies: + ansi-colors: ^4.1.3 + fast-glob: ^3.3.1 + js-yaml: ^4.1.0 + supports-color: ^9.4.0 + undici: ^5.27.2 + yargs-parser: ^21.1.1 + bin: + openapi-typescript: bin/cli.js + checksum: 2c7f47ad6e11082747fbae35304e2e26072ad317a4066a3bc934caf1a39fb7d34ca3ecb34aabf36162f89fc99ee7ca7cd3c4a9c8bcb079b527cda5ef33c2c6d7 + languageName: node + linkType: hard + "optionator@npm:^0.9.3": version: 0.9.3 resolution: "optionator@npm:0.9.3" @@ -5614,13 +5552,6 @@ __metadata: languageName: node linkType: hard -"proxy-from-env@npm:^1.1.0": - version: 1.1.0 - resolution: "proxy-from-env@npm:1.1.0" - checksum: ed7fcc2ba0a33404958e34d95d18638249a68c430e30fcb6c478497d72739ba64ce9810a24f53a7d921d0c065e5b78e3822759800698167256b04659366ca4d4 - languageName: node - linkType: hard - "punycode@npm:^2.1.0": version: 2.3.0 resolution: "punycode@npm:2.3.0" @@ -6455,6 +6386,13 @@ __metadata: languageName: node linkType: hard +"supports-color@npm:^9.4.0": + version: 9.4.0 + resolution: "supports-color@npm:9.4.0" + checksum: cb8ff8daeaf1db642156f69a9aa545b6c01dd9c4def4f90a49f46cbf24be0c245d392fcf37acd119cd1819b99dad2cc9b7e3260813f64bcfd7f5b18b5a1eefb8 + languageName: node + linkType: hard + "supports-preserve-symlinks-flag@npm:^1.0.0": version: 1.0.0 resolution: "supports-preserve-symlinks-flag@npm:1.0.0" @@ -6880,10 +6818,12 @@ __metadata: languageName: node linkType: hard -"undici-types@npm:~5.25.1": - version: 5.25.3 - resolution: "undici-types@npm:5.25.3" - checksum: ec9d2cc36520cbd9fbe3b3b6c682a87fe5be214699e1f57d1e3d9a2cb5be422e62735f06e0067dc325fd3dd7404c697e4d479f9147dc8a804e049e29f357f2ff +"undici@npm:^5.27.2": + version: 5.27.2 + resolution: "undici@npm:5.27.2" + dependencies: + "@fastify/busboy": ^2.0.0 + checksum: 22bbdd763798700979986546d70072b67223189353d2a811efa9c6e44476161a0d1781ffe24115221f69a1b344b95d5926bd39a6eb760a2cd8804781cec0c5eb languageName: node linkType: hard