// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

import * as Core from 'cloudflare/core';
import { APIResource } from 'cloudflare/resource';
import * as AIAPI from 'cloudflare/resources/workers/ai';
import { type Uploadable } from 'cloudflare/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;

    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.Variant4
  | AIRunParams.Variant5
  | AIRunParams.Variant6
  | AIRunParams.Variant7
  | AIRunParams.Variant8
  | AIRunParams.Variant9
  | AIRunParams.Variant10
  | AIRunParams.Variant11
  | AIRunParams.Translation
  | AIRunParams.Summarization
  | AIRunParams.Variant14
  | AIRunParams.Variant15;

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 Variant4 {
    /**
     * Path param:
     */
    account_id: string;

    /**
     * Body param:
     */
    body: Uploadable;
  }

  export interface Variant5 {
    /**
     * Path param:
     */
    account_id: string;

    /**
     * Body param:
     */
    audio?: Array<number>;
  }

  export interface Variant6 {
    /**
     * Path param:
     */
    account_id: string;

    /**
     * Body param:
     */
    body: Uploadable;
  }

  export interface Variant7 {
    /**
     * Path param:
     */
    account_id: string;

    /**
     * Body param:
     */
    image?: Array<number>;
  }

  export interface Variant8 {
    /**
     * Path param:
     */
    account_id: string;

    /**
     * Body param:
     */
    body: Uploadable;
  }

  export interface Variant9 {
    /**
     * Path param:
     */
    account_id: string;

    /**
     * Body param:
     */
    image?: Array<number>;
  }

  export interface Variant10 {
    /**
     * Path param:
     */
    account_id: string;

    /**
     * Body param:
     */
    prompt: string;

    /**
     * Body param:
     */
    lora?: string;

    /**
     * Body param:
     */
    max_tokens?: number;

    /**
     * Body param:
     */
    raw?: boolean;

    /**
     * Body param:
     */
    stream?: boolean;
  }

  export interface Variant11 {
    /**
     * Path param:
     */
    account_id: string;

    /**
     * Body param:
     */
    messages: Array<AIRunParams.Variant11.Message>;

    /**
     * Body param:
     */
    max_tokens?: number;

    /**
     * Body param:
     */
    stream?: boolean;
  }

  export namespace Variant11 {
    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 Variant14 {
    /**
     * Path param:
     */
    account_id: string;

    /**
     * Body param:
     */
    body: Uploadable;
  }

  export interface Variant15 {
    /**
     * Path param:
     */
    account_id: string;

    /**
     * Body param:
     */
    image?: Array<number>;

    /**
     * Body param:
     */
    max_tokens?: number;

    /**
     * Body param:
     */
    prompt?: string;
  }
}

export namespace AI {
  export import AIRunResponse = AIAPI.AIRunResponse;
  export import AIRunParams = AIAPI.AIRunParams;
}
