From 80effcc0d455944e5b9955d0f34afab74e6c1400 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 --- .env | 2 +- .eslintignore | 3 + .nvmrc | 2 +- .prettierignore | 2 + examples/generate.ts | 112 +- examples/load_input.ts | 12 +- examples/models.ts | 8 + package.json | 14 +- src/api-types.ts | 458 +--- src/api/client.ts | 32 + src/api/schema.d.ts | 3751 ++++++++++++++++++++++++++ src/{client => }/client.test.ts | 3 +- src/client.ts | 140 + src/client/cache.ts | 15 - src/client/client.ts | 1422 ---------- src/client/types.ts | 288 -- src/errors.ts | 149 +- src/index.ts | 5 +- src/langchain/llm.ts | 2 +- src/tests/e2e/client.test.ts | 2 +- src/tests/integration/client.test.ts | 2 +- src/types.ts | 5 + src/utils/stream.ts | 109 + yarn.lock | 164 +- 24 files changed, 4241 insertions(+), 2461 deletions(-) create mode 100644 examples/models.ts create mode 100644 src/api/client.ts create mode 100644 src/api/schema.d.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 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..9dfef47 100644 --- a/.nvmrc +++ b/.nvmrc @@ -1 +1 @@ -lts/* +v18.12.0 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 c95709d..48bea28 100644 --- a/package.json +++ b/package.json @@ -56,12 +56,14 @@ "prepack": "yarn build && pinst --disable", "postinstall": "husky install", "postpack": "pinst --enable", + "generate": "npx openapi-typescript https://bam-api.res.ibm.com/docs/json -o ./src/api/schema.d.ts", "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": ">=0.0.155" @@ -71,7 +73,6 @@ "@commitlint/config-conventional": "^18.0.0", "@types/lodash": "^4.14.200", "@types/node": "^20.8.8", - "@types/promise-retry": "^1.1.5", "@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,15 +98,15 @@ }, "dependencies": { "@ai-zen/node-fetch-event-source": "^2.1.2", - "axios": "^1.5.1", - "axios-cache-interceptor": "^1.3.2", + "cross-fetch": "^4.0.0", + "fetch-retry": "^5.0.6", "form-data": "^4.0.0", - "promise-retry": "^2.0.1", + "openapi-fetch": "^0.8.1", "yaml": "^2.3.3", "zod": "^3.22.4" }, "engines": { - "node": ">=16.10.0" + "node": ">=18.12.0" }, "lint-staged": { "*.{cjs,js,jsx,ts,tsx}": [ diff --git a/src/api-types.ts b/src/api-types.ts index dca8c4b..3572b7a 100644 --- a/src/api-types.ts +++ b/src/api-types.ts @@ -1,460 +1,12 @@ -/* 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 { +export interface APIError { 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; + extension?: { + code?: string; + reason?: string; + state?: Record; }; } - -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({ - conversation_id: z.string(), - results: z.array( - z - .object({ - generated_text: z.string(), - }) - .partial(), - ), -}); -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..48dc0fb --- /dev/null +++ b/src/api/client.ts @@ -0,0 +1,32 @@ +import createClient, { FetchOptions } from 'openapi-fetch'; + +import { paths } from './schema.js'; + +export type ApiClient = ReturnType>; + +export function createApiClient( + ...params: Parameters> +): ApiClient { + return createClient(...params); +} + +type FilterKeys = { + [K in keyof Obj]: K extends Matchers ? Obj[K] : never; +}[keyof Obj]; + +type NestedOmit = { + [P in keyof T as P extends K ? never : P]: NestedOmit< + T[P], + K extends `${Exclude}.${infer R}` ? R : never + >; +}; + +type RawClientOptions< + METHOD extends keyof ApiClient, + PATH extends Parameters[0], +> = FetchOptions>>; + +export type ClientOptions< + METHOD extends keyof ApiClient, + PATH extends Parameters[0], +> = NestedOmit, 'params.query.version'>; diff --git a/src/api/schema.d.ts b/src/api/schema.d.ts new file mode 100644 index 0000000..39d49d5 --- /dev/null +++ b/src/api/schema.d.ts @@ -0,0 +1,3751 @@ +/** + * This file was auto-generated by openapi-typescript. + * Do not make direct changes to the file. + */ + + +export interface paths { + "/v2/user": { + get: { + parameters: { + query: { + version: string; + }; + }; + 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; + use_default?: boolean | null; + template?: { + id?: string; + value?: string; + data: { + example_file_ids?: string[]; + [key: string]: unknown; + }; + }; + parameters?: ({ + beam_width?: number | null; + decoding_method?: string | 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["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + post: { + parameters: { + query: { + version: string; + }; + }; + 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; + use_default?: boolean | null; + template?: { + id?: string; + value?: string; + data: { + example_file_ids?: string[]; + [key: string]: unknown; + }; + }; + parameters?: ({ + beam_width?: number | null; + decoding_method?: string | 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["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["responses"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + delete: { + parameters: { + query: { + version: string; + }; + }; + responses: { + /** @description Default Response */ + 204: { + content: { + "application/json": Record; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["responses"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + patch: { + parameters: { + query: { + version: string; + }; + }; + 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; + use_default?: boolean | null; + template?: { + id?: string; + value?: string; + data: { + example_file_ids?: string[]; + [key: string]: unknown; + }; + }; + parameters?: ({ + beam_width?: number | null; + decoding_method?: string | 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["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["responses"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + }; + "/v2/tuning_types": { + get: { + parameters: { + query: { + version: string; + }; + }; + 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["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + }; + "/v2/tunes/{id}/content/{type}": { + get: { + parameters: { + query: { + version: string; + }; + path: { + id: string; + type: "encoder" | "logs" | "export"; + }; + }; + 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["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["responses"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + }; + "/v2/tunes/{id}": { + get: { + parameters: { + query: { + version: string; + }; + path: { + id: string; + }; + }; + 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["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["responses"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + delete: { + parameters: { + query: { + version: string; + }; + path: { + id: string; + }; + }; + 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["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["responses"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + patch: { + parameters: { + query: { + version: string; + }; + path: { + id: string; + }; + }; + requestBody?: { + content: { + "application/json": { + name?: string; + preferred?: boolean; + }; + }; + }; + 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["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["responses"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + }; + "/v2/tunes/import": { + post: { + parameters: { + query: { + version: string; + }; + }; + requestBody: { + content: { + "application/json": { + name: string; + file_id: string; + }; + }; + }; + 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["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["responses"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + }; + "/v2/tunes": { + get: { + parameters: { + query: { + limit?: number; + offset?: number; + status?: "initializing" | "not_started" | "pending" | "halted" | "running" | "queued" | "completed" | "failed"; + search?: string | null; + version: string; + }; + }; + 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["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + post: { + parameters: { + query: { + version: string; + }; + }; + 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 Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["responses"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + }; + "/v2/text/tokenization": { + post: { + parameters: { + query: { + version: string; + }; + }; + requestBody: { + content: { + "application/json": { + model_id?: string; + prompt_id?: string; + use_default?: boolean | null; + input: string; + template?: { + id?: string; + value?: string; + data: { + example_file_ids?: string[]; + [key: string]: unknown; + }; + }; + parameters?: { + return_tokens?: boolean; + } | null; + }; + }; + }; + 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["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["responses"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + }; + "/v2/text/moderations": { + post: { + parameters: { + query: { + version: string; + }; + }; + 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 Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["responses"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + }; + "/v2/text/generation_stream": { + post: { + parameters: { + query: { + version: string; + }; + }; + requestBody: { + content: { + "application/json": { + model_id?: string; + prompt_id?: string; + input: string; + use_default?: boolean | null; + template?: { + id?: string; + value?: string; + data: { + example_file_ids?: string[]; + [key: string]: unknown; + }; + }; + parameters?: ({ + beam_width?: number | null; + decoding_method?: string | 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; + }; + }; + }; + }; + }; + 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["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["responses"]["NotFoundResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + }; + "/v2/text/generation/{id}/feedback": { + get: { + parameters: { + query: { + version: string; + }; + path: { + id: string; + }; + }; + 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["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["responses"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + put: { + parameters: { + query: { + version: string; + }; + 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 Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["responses"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + post: { + parameters: { + query: { + version: string; + }; + 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 Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["responses"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + }; + "/v2/text/generation/limits": { + get: { + parameters: { + query: { + version: string; + }; + }; + 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["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + }; + "/v2/text/generation/comparison": { + post: { + parameters: { + query: { + version: string; + }; + }; + requestBody: { + content: { + "application/json": { + request: { + model_id?: string; + prompt_id?: string; + input: string; + use_default?: boolean | null; + template?: { + id?: string; + value?: string; + data: { + example_file_ids?: string[]; + [key: string]: unknown; + }; + }; + parameters?: ({ + beam_width?: number | null; + decoding_method?: string | 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; + }; + }; + }; + 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 Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["responses"]["NotFoundResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + }; + "/v2/text/generation": { + post: { + parameters: { + query: { + version: string; + }; + }; + requestBody?: { + content: { + "application/json": { + required?: never; + }; + }; + }; + 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["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["responses"]["NotFoundResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + }; + "/v2/text/embeddings/limits": { + get: { + parameters: { + query: { + version: string; + }; + }; + 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["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + }; + "/v2/text/embeddings": { + post: { + parameters: { + query: { + version: string; + }; + }; + requestBody: { + content: { + "application/json": { + model_id: string; + input: 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["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["responses"]["NotFoundResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + }; + "/v2/text/chat_stream": { + post: { + parameters: { + query: { + version: string; + }; + }; + requestBody?: { + content: { + "application/json": { + model_id?: string; + 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; + })[]; + 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; + decoding_method?: string | 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 Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["responses"]["NotFoundResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + }; + "/v2/text/chat/output": { + post: { + parameters: { + query: { + version: string; + }; + }; + requestBody?: { + content: { + "application/json": { + model_id?: string; + 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; + })[]; + 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; + decoding_method?: string | 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 Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["responses"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + }; + "/v2/text/chat": { + post: { + parameters: { + query: { + version: string; + }; + }; + requestBody?: { + content: { + "application/json": { + model_id?: string; + 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; + })[]; + 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; + decoding_method?: string | 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 Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["responses"]["NotFoundResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + }; + "/v2/tasks": { + get: { + parameters: { + query: { + tune?: boolean | null; + version: string; + }; + }; + 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["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + }; + "/v2/requests/{id}": { + delete: { + parameters: { + query: { + version: string; + }; + path: { + id: string; + }; + }; + 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["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["responses"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + }; + "/v2/requests/chat/{conversationId}": { + get: { + parameters: { + query: { + version: string; + }; + path: { + conversationId: string; + }; + }; + 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; + 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["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["responses"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + delete: { + parameters: { + query: { + version: string; + }; + path: { + conversationId: string; + }; + }; + 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["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["responses"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + }; + "/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: string; + }; + }; + 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["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + }; + "/v2/prompts/{id}": { + get: { + parameters: { + query: { + version: string; + }; + 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; + decoding_method?: string | 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; + template?: string; + template_id?: string; + /** Format: date-time */ + created_at: string; + public: boolean; + created_by_id?: number; + 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["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["responses"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + put: { + parameters: { + query: { + version: string; + }; + path: { + id: string; + }; + }; + requestBody: { + content: { + "application/json": { + name: string; + description?: string; + template?: { + id?: string; + value?: string; + data: { + example_file_ids?: string[]; + [key: string]: unknown; + }; + }; + output?: string | null; + parameters?: ({ + beam_width?: number | null; + decoding_method?: string | 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; + input?: string; + messages?: (({ + /** @enum {string} */ + role: "user" | "system" | "assistant"; + content: 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; + decoding_method?: string | 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; + template?: string; + template_id?: string; + /** Format: date-time */ + created_at: string; + public: boolean; + created_by_id?: number; + 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["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["responses"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + delete: { + parameters: { + query: { + version: string; + }; + path: { + id: string; + }; + }; + responses: { + /** @description Default Response */ + 204: { + content: { + "application/json": Record; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["responses"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + patch: { + parameters: { + query: { + version: string; + }; + path: { + id: string; + }; + }; + requestBody?: { + content: { + "application/json": { + public?: boolean; + }; + }; + }; + 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; + decoding_method?: string | 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; + template?: string; + template_id?: string; + /** Format: date-time */ + created_at: string; + public: boolean; + created_by_id?: number; + 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["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["responses"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + }; + "/v2/prompts": { + get: { + parameters: { + query: { + limit?: number; + offset?: number; + view?: "ui" | null; + version: string; + }; + }; + 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; + decoding_method?: string | 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; + template?: string; + template_id?: string; + /** Format: date-time */ + created_at: string; + public: boolean; + created_by_id?: number; + 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["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + post: { + parameters: { + query: { + version: string; + }; + }; + requestBody: { + content: { + "application/json": { + name: string; + description?: string; + template?: { + id?: string; + value?: string; + data: { + example_file_ids?: string[]; + [key: string]: unknown; + }; + }; + output?: string | null; + parameters?: ({ + beam_width?: number | null; + decoding_method?: string | 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; + input?: string; + messages?: (({ + /** @enum {string} */ + role: "user" | "system" | "assistant"; + content: 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; + decoding_method?: string | 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; + template?: string; + template_id?: string; + /** Format: date-time */ + created_at: string; + public: boolean; + created_by_id?: number; + 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["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["responses"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + }; + "/v2/prompt_templates/{id}": { + get: { + parameters: { + query: { + version: string; + }; + path: { + id: string; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + result: { + id: string; + name: string; + value: string; + created_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["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["responses"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + put: { + parameters: { + query: { + version: string; + }; + path: { + id: string; + }; + }; + requestBody: { + content: { + "application/json": { + name: string; + value: string; + }; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + result: { + id: string; + name: string; + value: string; + created_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["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["responses"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + delete: { + parameters: { + query: { + version: string; + }; + path: { + id: string; + }; + }; + responses: { + /** @description Default Response */ + 204: { + content: { + "application/json": Record; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["responses"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + }; + "/v2/prompt_templates/output": { + post: { + parameters: { + query: { + version: string; + }; + }; + requestBody: { + content: { + "application/json": { + template: { + id?: string; + value?: string; + data: { + example_file_ids?: string[]; + [key: string]: unknown; + }; + }; + input: string; + model_id?: string; + }; + }; + }; + 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["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["responses"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + }; + "/v2/prompt_templates": { + get: { + parameters: { + query: { + limit?: number; + offset?: number; + version: string; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + total_count: number; + results: { + id: string; + name: string; + value: string; + created_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["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + post: { + parameters: { + query: { + version: string; + }; + }; + requestBody: { + content: { + "application/json": { + name: string; + value: string; + }; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + result: { + id: string; + name: string; + value: string; + created_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["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["responses"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + }; + "/v2/models/{id}": { + get: { + parameters: { + query: { + version: string; + }; + 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; + short_description?: string; + description?: string; + system_prompt?: string; + prompt_example?: string; + }; + schema_generate?: { + id?: number; + value?: { + [key: string]: unknown; + }; + }; + schema_tokenize?: { + id?: number; + value?: { + [key: string]: unknown; + }; + }; + id?: string; + name?: string; + developer?: string; + size?: string; + label?: string; + disabled?: boolean; + preferred?: boolean; + description?: string; + tags?: string[]; + source_model_id?: string | null; + is_live?: boolean; + token_limits?: { + beam_width?: number; + token_limit?: 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["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["responses"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + }; + "/v2/models": { + get: { + parameters: { + query: { + limit: number; + offset: number; + version: string; + }; + }; + responses: { + /** @description Default Response */ + 200: { + content: { + "application/json": { + results: ({ + id: string; + name: string; + size: string; + label: 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["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + }; + "/v2/files/{id}/content": { + get: { + parameters: { + query: { + version: string; + }; + 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["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["responses"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + }; + "/v2/files/{id}": { + get: { + parameters: { + query: { + version: string; + }; + 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"; + /** 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["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["responses"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + delete: { + parameters: { + query: { + version: string; + }; + path: { + id: string; + }; + }; + responses: { + /** @description Default Response */ + 204: { + content: { + "application/json": Record; + }; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + 400: { + content: { + "application/json": components["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["responses"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + }; + "/v2/files": { + get: { + parameters: { + query: { + limit?: number; + offset?: number; + sort_by?: "name" | "created_at"; + direction?: "asc" | "desc"; + search?: string; + purpose?: "tune" | "template" | "tune_import"; + format_id?: number; + version: string; + }; + }; + 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"; + /** 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["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + post: { + parameters: { + query: { + version: 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"; + /** 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["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["responses"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + }; + "/v2/api_key/regenerate": { + post: { + parameters: { + query: { + version: string; + }; + }; + 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["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The server can not find requested resource. */ + 404: { + content: { + "application/json": components["responses"]["NotFoundResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + }; + "/v2/api_key": { + get: { + parameters: { + query: { + version: string; + }; + }; + 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["responses"]["BadRequestResponse"]; + }; + }; + /** @description Unauthorized route accesss. */ + 401: { + content: { + "application/json": components["responses"]["UnauthorizedResponse"]; + }; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + 429: { + content: { + "application/json": components["responses"]["ToManyRequestsResponse"]; + }; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + 500: { + content: { + "application/json": components["responses"]["InternalServerErrorResponse"]; + }; + }; + }; + }; + }; +} + +export type webhooks = Record; + +export interface components { + schemas: { + }; + responses: { + /** @description Unauthorized route accesss. */ + UnauthorizedResponse: { + content: never; + }; + /** @description The server can not find requested resource. */ + NotFoundResponse: { + content: never; + }; + /** @description The user has sent too many requests in a given amount of time ("rate limiting").. */ + ToManyRequestsResponse: { + content: never; + }; + /** @description Server could not understand the request due to invalid syntax. In most cases relates with the schema validation. */ + BadRequestResponse: { + content: never; + }; + /** @description The server encountered an unexpected condition that prevented it from fulfilling the request. */ + InternalServerErrorResponse: { + content: 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/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..c9dff24 --- /dev/null +++ b/src/client.ts @@ -0,0 +1,140 @@ +import { Transform, TransformCallback } from 'node:stream'; + +import fetchRetry from 'fetch-retry'; +import fetch from 'cross-fetch'; + +import { InvalidInputError, clientErrorWrapper } from './errors.js'; +import { version } from './buildInfo.js'; +import { lookupApiKey, lookupEndpoint } from './helpers/config.js'; +import { ApiClient, ClientOptions, createApiClient } from './api/client.js'; +import { fetchSSE } from './utils/stream.js'; + +export type RawHeaders = Record; + +export interface Configuration { + apiKey?: string; + endpoint?: string; + headers?: RawHeaders; +} + +export class Client { + readonly #client: ApiClient; + readonly #endpoint: string; + readonly #headers: RawHeaders; + + 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.#endpoint = endpoint; + this.#headers = { + 'User-Agent': agent, + 'X-Request-Origin': agent, + ...config.headers, + Accept: 'application/json', + Authorization: `Bearer ${apiKey}`, + }; + this.#client = createApiClient({ + baseUrl: this.#endpoint, + headers: this.#headers, + fetch: fetchRetry(fetch) as any, // https://github.com/jonbern/fetch-retry/issues/89 + }); + } + + async models( + input: ClientOptions<'GET', '/v2/models'>['params']['query'], + opts?: { signal?: AbortSignal }, + ) { + return clientErrorWrapper( + this.#client.GET('/v2/models', { + ...opts, + params: { + query: { + ...input, + version: '2023-11-22', + }, + }, + }), + ); + } + + async model( + input: ClientOptions<'GET', '/v2/models/{id}'>['params']['path'], + opts?: { signal?: AbortSignal }, + ) { + return clientErrorWrapper( + this.#client.GET('/v2/models/{id}', { + ...opts, + params: { + path: input, + query: { + version: '2023-11-22', + }, + }, + }), + ); + } + + generation_stream( + input: ClientOptions<'POST', '/v2/text/generation_stream'>['body'], + opts?: { signal?: AbortSignal }, + ) { + const stream = new Transform({ + autoDestroy: true, + objectMode: true, + transform( + chunk: any, // TODO use soutput type from generated types + 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); + } + }, + }); + + // TODO use generated output type + fetchSSE({ + url: new URL( + `/v2/text/generation_stream?version=2023-11-22`, + this.#endpoint, + ), + headers: this.#headers, + 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/errors.ts b/src/errors.ts index c3ae5bc..6c99690 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -1,14 +1,6 @@ -import { IncomingHttpHeaders } from 'node:http'; +import { FetchResponse } from 'openapi-fetch'; -import { - AxiosHeaders, - AxiosResponse, - HttpStatusCode, - isAxiosError, - isCancel as isAxiosCancel, -} from 'axios'; - -import { ErrorExtensions, ErrorResponse } from './api-types.js'; +import { APIError } from './api-types.js'; export class BaseError extends Error {} @@ -16,116 +8,37 @@ 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 NetworkError extends BaseError {} + +export class HttpError extends BaseError { + status_code: number; + error: string; + extension?: + | { + code?: string | undefined; + reason?: string | undefined; + state?: Record | undefined; + } + | undefined; + + constructor(error: APIError) { + super(error.message, { cause: error }); + this.status_code = error.status_code; + this.error = error.error; + this.extension = error.extension; } } -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; - - 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 }, - ); - } - - 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 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; +export async function clientErrorWrapper( + request: Promise>, +) { + try { + const { data, error } = await request; + if (error) throw new HttpError(error as unknown as APIError); // TODO avoid typecast + return data; + } catch (err) { + if (err instanceof TypeError && err.name) + throw new NetworkError('Unable to connect', { cause: err }); + throw new InternalError('Request failed', { cause: err }); } - return false; } diff --git a/src/index.ts b/src/index.ts index 9455acd..3268530 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,5 @@ -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 a5d585a..922b8c2 100644 --- a/src/langchain/llm.ts +++ b/src/langchain/llm.ts @@ -3,7 +3,7 @@ import { CallbackManagerForLLMRun } from 'langchain/callbacks'; import type { LLMResult, Generation } from 'langchain/schema'; import { GenerationChunk } from 'langchain/schema'; -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 44637b6..7d00d5b 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 93ec1b6..c413547 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', diff --git a/src/types.ts b/src/types.ts index 05a6d45..f6a2daf 100644 --- a/src/types.ts +++ b/src/types.ts @@ -3,3 +3,8 @@ type StrictUnionHelper = T extends any ? T & Partial, keyof T>, never>> : never; export type StrictUnion = StrictUnionHelper; + +export type AtLeastOne }> = Partial & + U[keyof U]; + +export type ExcludeEmpty = T extends AtLeastOne ? T : never; diff --git a/src/utils/stream.ts b/src/utils/stream.ts index 25972d3..7da6e13 100644 --- a/src/utils/stream.ts +++ b/src/utils/stream.ts @@ -1,5 +1,14 @@ import { Readable } from 'stream'; +import { + EventStreamContentType, + fetchEventSource, +} from '@ai-zen/node-fetch-event-source'; + +import { BaseError, HttpError, InternalError } from '../errors.js'; +import { safeParseJson } from '../helpers/common.js'; +import { RawHeaders } from '../client.js'; + export class TypedReadable extends Readable { // eslint-disable-next-line @typescript-eslint/no-unused-vars _read(size: number) { @@ -35,3 +44,103 @@ export class TypedReadable extends Readable { return super[Symbol.asyncIterator](); } } + +export function fetchSSE({ + url, + headers, + body, + signal, +}: { + url: URL; + headers: RawHeaders; + body: Input; + signal?: AbortSignal; +}) { + 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(url.toString(), { + method: 'POST', + body: JSON.stringify(body), + headers: { + ...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; + + 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/yarn.lock b/yarn.lock index 242b3b6..39845be 100644 --- a/yarn.lock +++ b/yarn.lock @@ -637,6 +637,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" @@ -678,18 +685,17 @@ __metadata: "@commitlint/config-conventional": ^18.0.0 "@types/lodash": ^4.14.200 "@types/node": ^20.8.8 - "@types/promise-retry": ^1.1.5 "@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 + fetch-retry: ^5.0.6 form-data: ^4.0.0 husky: ^8.0.3 jest-extended: ^4.0.2 @@ -697,9 +703,10 @@ __metadata: 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 @@ -1120,22 +1127,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" @@ -1493,6 +1484,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" @@ -1722,30 +1720,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" @@ -1896,13 +1870,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" @@ -2321,6 +2288,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" @@ -3136,13 +3112,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" @@ -3192,6 +3161,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" @@ -3265,16 +3241,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" @@ -5626,13 +5592,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-hash@npm:^3.0.0": version: 3.0.0 resolution: "object-hash@npm:3.0.0" @@ -5745,6 +5704,15 @@ __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-types@npm:^12.1.3": version: 12.1.3 resolution: "openapi-types@npm:12.1.3" @@ -5752,6 +5720,29 @@ __metadata: 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" @@ -6108,13 +6099,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" @@ -6949,6 +6933,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" @@ -7381,6 +7372,15 @@ __metadata: languageName: node linkType: hard +"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 + "unique-filename@npm:^2.0.0": version: 2.0.1 resolution: "unique-filename@npm:2.0.1"