cloudflare
Version:
The official TypeScript library for the Cloudflare API
383 lines (308 loc) • 6.42 kB
text/typescript
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
import * as Core from "../../core";
import { APIResource } from "../../resource";
import * as AIAPI from "./ai";
import { type Uploadable } from "../../core";
export class AI extends APIResource {
/**
* This endpoint provides users with the capability to run specific AI models
* on-demand.
*
* By submitting the required input data, users can receive real-time predictions
* or results generated by the chosen AI model. The endpoint supports various AI
* model types, ensuring flexibility and adaptability for diverse use cases.
*
* Model specific inputs available in
* [Cloudflare Docs](https://developers.cloudflare.com/workers-ai/models/).
*/
run(modelName: string, params: AIRunParams, options?: Core.RequestOptions): Core.APIPromise<AIRunResponse> {
const { account_id, ...body } = params;
return (
this._client.post(`/accounts/${account_id}/ai/run/${modelName}`, {
body,
...options,
}) as Core.APIPromise<{ result: AIRunResponse }>
)._thenUnwrap((obj) => obj.result);
}
}
export type AIRunResponse =
| Array<AIRunResponse.TextClassification>
| Uploadable
| Array<number>
| AIRunResponse.TextEmbeddings
| AIRunResponse.SpeechRecognition
| Array<AIRunResponse.ImageClassification>
| Array<AIRunResponse.ObjectDetection>
| AIRunResponse.Response
| Uploadable
| AIRunResponse.Translation
| AIRunResponse.Summarization
| AIRunResponse.ImageToText;
export namespace AIRunResponse {
export interface TextClassification {
label?: string;
score?: number;
}
export interface TextEmbeddings {
data?: Array<Array<number>>;
shape?: Array<number>;
}
export interface SpeechRecognition {
text: string;
vtt?: string;
word_count?: number;
words?: Array<SpeechRecognition.Word>;
}
export namespace SpeechRecognition {
export interface Word {
end?: number;
start?: number;
word?: string;
}
}
export interface ImageClassification {
label?: string;
score?: number;
}
export interface ObjectDetection {
box?: ObjectDetection.Box;
label?: string;
score?: number;
}
export namespace ObjectDetection {
export interface Box {
xmax?: number;
xmin?: number;
ymax?: number;
ymin?: number;
}
}
export interface Response {
response?: string;
}
export interface Translation {
translated_text?: string;
}
export interface Summarization {
summary?: string;
}
export interface ImageToText {
description?: string;
}
}
export type AIRunParams =
| AIRunParams.TextClassification
| AIRunParams.TextToImage
| AIRunParams.SentenceSimilarity
| AIRunParams.TextEmbeddings
| AIRunParams.SpeechRecognition
| AIRunParams.ImageClassification
| AIRunParams.ObjectDetection
| AIRunParams.TextGeneration
| AIRunParams.Translation
| AIRunParams.Summarization
| AIRunParams.ImageToText;
export namespace AIRunParams {
export interface TextClassification {
/**
* Path param:
*/
account_id: string;
/**
* Body param:
*/
text: string;
}
export interface TextToImage {
/**
* Path param:
*/
account_id: string;
/**
* Body param:
*/
prompt: string;
/**
* Body param:
*/
guidance?: number;
/**
* Body param:
*/
image?: Array<number>;
/**
* Body param:
*/
mask?: Array<number>;
/**
* Body param:
*/
num_steps?: number;
/**
* Body param:
*/
strength?: number;
}
export interface SentenceSimilarity {
/**
* Path param:
*/
account_id: string;
/**
* Body param:
*/
sentences: Array<string>;
/**
* Body param:
*/
source: string;
}
export interface TextEmbeddings {
/**
* Path param:
*/
account_id: string;
/**
* Body param:
*/
text: string | Array<string>;
}
export interface SpeechRecognition {
/**
* Path param:
*/
account_id: string;
/**
* Body param:
*/
audio?: Array<number>;
}
export interface ImageClassification {
/**
* Path param:
*/
account_id: string;
/**
* Body param:
*/
image?: Array<number>;
}
export interface ObjectDetection {
/**
* Path param:
*/
account_id: string;
/**
* Body param:
*/
image?: Array<number>;
}
export interface TextGeneration {
/**
* Path param:
*/
account_id: string;
/**
* Body param:
*/
lora?: string;
/**
* Body param:
*/
max_tokens?: number;
/**
* Body param:
*/
messages?: Array<AIRunParams.TextGeneration.Message>;
/**
* Body param:
*/
prompt?: string;
/**
* Body param:
*/
raw?: boolean;
/**
* Body param:
*/
stream?: boolean;
}
export namespace TextGeneration {
export interface Message {
content: string;
role: string;
}
}
export interface Translation {
/**
* Path param:
*/
account_id: string;
/**
* Body param:
*/
target_lang: string;
/**
* Body param:
*/
text: string;
/**
* Body param:
*/
source_lang?: string;
}
export interface Summarization {
/**
* Path param:
*/
account_id: string;
/**
* Body param:
*/
input_text: string;
/**
* Body param:
*/
max_length?: number;
}
export interface ImageToText {
/**
* Path param:
*/
account_id: string;
/**
* Body param:
*/
image: Array<number>;
/**
* Body param:
*/
max_tokens?: number;
/**
* Body param:
*/
messages?: Array<AIRunParams.ImageToText.Message>;
/**
* Body param:
*/
prompt?: string;
/**
* Body param:
*/
raw?: boolean;
/**
* Body param:
*/
temperature?: number;
}
export namespace ImageToText {
export interface Message {
content: string;
role: string;
}
}
}
export namespace AI {
export import AIRunResponse = AIAPI.AIRunResponse;
export import AIRunParams = AIAPI.AIRunParams;
}