cloudflare/cloudflare-typescript

Public

mirrored fromhttps://github.com/cloudflare/cloudflare-typescriptAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v4.2.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/resources/ai/ai.ts

896lines · modecode

1// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
3import { APIResource } from '../../resource';
4import * as Core from '../../core';
5import * as AuthorsAPI from './authors';
6import { AuthorListParams, AuthorListResponse, AuthorListResponsesSinglePage, Authors } from './authors';
7import * as TasksAPI from './tasks';
8import { TaskListParams, TaskListResponse, TaskListResponsesSinglePage, Tasks } from './tasks';
9import * as FinetunesAPI from './finetunes/finetunes';
10import {
11 FinetuneCreateParams,
12 FinetuneCreateResponse,
13 FinetuneListParams,
14 FinetuneListResponse,
15 Finetunes,
16} from './finetunes/finetunes';
17import * as ModelsAPI from './models/models';
18import {
19 ModelListParams,
20 ModelListResponse,
21 ModelListResponsesV4PagePaginationArray,
22 Models,
23} from './models/models';
24
25export class AI extends APIResource {
26 finetunes: FinetunesAPI.Finetunes = new FinetunesAPI.Finetunes(this._client);
27 authors: AuthorsAPI.Authors = new AuthorsAPI.Authors(this._client);
28 tasks: TasksAPI.Tasks = new TasksAPI.Tasks(this._client);
29 models: ModelsAPI.Models = new ModelsAPI.Models(this._client);
30
31 /**
32 * This endpoint provides users with the capability to run specific AI models
33 * on-demand.
34 *
35 * By submitting the required input data, users can receive real-time predictions
36 * or results generated by the chosen AI model. The endpoint supports various AI
37 * model types, ensuring flexibility and adaptability for diverse use cases.
38 *
39 * Model specific inputs available in
40 * [Cloudflare Docs](https://developers.cloudflare.com/workers-ai/models/).
41 */
42 run(modelName: string, params: AIRunParams, options?: Core.RequestOptions): Core.APIPromise<AIRunResponse> {
43 const { account_id, ...body } = params;
44 return (
45 this._client.post(`/accounts/${account_id}/ai/run/${modelName}`, {
46 body,
47 ...options,
48 }) as Core.APIPromise<{ result: AIRunResponse }>
49 )._thenUnwrap((obj) => obj.result);
50 }
51}
52
53/**
54 * An array of classification results for the input text
55 */
56export type AIRunResponse =
57 | Array<AIRunResponse.TextClassification>
58 | Core.Uploadable
59 | AIRunResponse.Audio
60 | AIRunResponse.TextEmbeddings
61 | AIRunResponse.AutomaticSpeechRecognition
62 | Array<AIRunResponse.ImageClassification>
63 | Array<AIRunResponse.ObjectDetection>
64 | AIRunResponse.UnionMember7
65 | AIRunResponse.Translation
66 | AIRunResponse.Summarization
67 | AIRunResponse.ImageToText;
68
69export namespace AIRunResponse {
70 export interface TextClassification {
71 /**
72 * The classification label assigned to the text (e.g., 'POSITIVE' or 'NEGATIVE')
73 */
74 label?: string;
75
76 /**
77 * Confidence score indicating the likelihood that the text belongs to the
78 * specified label
79 */
80 score?: number;
81 }
82
83 export interface Audio {
84 /**
85 * The generated audio in MP3 format, base64-encoded
86 */
87 audio?: string;
88 }
89
90 export interface TextEmbeddings {
91 /**
92 * Embeddings of the requested text values
93 */
94 data?: Array<Array<number>>;
95
96 shape?: Array<number>;
97 }
98
99 export interface AutomaticSpeechRecognition {
100 /**
101 * The transcription
102 */
103 text: string;
104
105 vtt?: string;
106
107 word_count?: number;
108
109 words?: Array<AutomaticSpeechRecognition.Word>;
110 }
111
112 export namespace AutomaticSpeechRecognition {
113 export interface Word {
114 /**
115 * The ending second when the word completes
116 */
117 end?: number;
118
119 /**
120 * The second this word begins in the recording
121 */
122 start?: number;
123
124 word?: string;
125 }
126 }
127
128 export interface ImageClassification {
129 /**
130 * The predicted category or class for the input image based on analysis
131 */
132 label?: string;
133
134 /**
135 * A confidence value, between 0 and 1, indicating how certain the model is about
136 * the predicted label
137 */
138 score?: number;
139 }
140
141 export interface ObjectDetection {
142 /**
143 * Coordinates defining the bounding box around the detected object
144 */
145 box?: ObjectDetection.Box;
146
147 /**
148 * The class label or name of the detected object
149 */
150 label?: string;
151
152 /**
153 * Confidence score indicating the likelihood that the detection is correct
154 */
155 score?: number;
156 }
157
158 export namespace ObjectDetection {
159 /**
160 * Coordinates defining the bounding box around the detected object
161 */
162 export interface Box {
163 /**
164 * The x-coordinate of the bottom-right corner of the bounding box
165 */
166 xmax?: number;
167
168 /**
169 * The x-coordinate of the top-left corner of the bounding box
170 */
171 xmin?: number;
172
173 /**
174 * The y-coordinate of the bottom-right corner of the bounding box
175 */
176 ymax?: number;
177
178 /**
179 * The y-coordinate of the top-left corner of the bounding box
180 */
181 ymin?: number;
182 }
183 }
184
185 export interface UnionMember7 {
186 /**
187 * The generated text response from the model
188 */
189 response?: string;
190
191 /**
192 * An array of tool calls requests made during the response generation
193 */
194 tool_calls?: Array<UnionMember7.ToolCall>;
195
196 /**
197 * Usage statistics for the inference request
198 */
199 usage?: UnionMember7.Usage;
200 }
201
202 export namespace UnionMember7 {
203 export interface ToolCall {
204 /**
205 * The arguments passed to be passed to the tool call request
206 */
207 arguments?: unknown;
208
209 /**
210 * The name of the tool to be called
211 */
212 name?: string;
213 }
214
215 /**
216 * Usage statistics for the inference request
217 */
218 export interface Usage {
219 /**
220 * Total number of tokens in output
221 */
222 completion_tokens?: number;
223
224 /**
225 * Total number of tokens in input
226 */
227 prompt_tokens?: number;
228
229 /**
230 * Total number of input and output tokens
231 */
232 total_tokens?: number;
233 }
234 }
235
236 export interface Translation {
237 /**
238 * The translated text in the target language
239 */
240 translated_text?: string;
241 }
242
243 export interface Summarization {
244 /**
245 * The summarized version of the input text
246 */
247 summary?: string;
248 }
249
250 export interface ImageToText {
251 description?: string;
252 }
253}
254
255export type AIRunParams =
256 | AIRunParams.TextClassification
257 | AIRunParams.TextToImage
258 | AIRunParams.TextToSpeech
259 | AIRunParams.TextEmbeddings
260 | AIRunParams.AutomaticSpeechRecognition
261 | AIRunParams.ImageClassification
262 | AIRunParams.ObjectDetection
263 | AIRunParams.Prompt
264 | AIRunParams.Messages
265 | AIRunParams.Translation
266 | AIRunParams.Summarization
267 | AIRunParams.ImageToText;
268
269export declare namespace AIRunParams {
270 export interface TextClassification {
271 /**
272 * Path param:
273 */
274 account_id: string;
275
276 /**
277 * Body param: The text that you want to classify
278 */
279 text: string;
280 }
281
282 export interface TextToImage {
283 /**
284 * Path param:
285 */
286 account_id: string;
287
288 /**
289 * Body param: A text description of the image you want to generate
290 */
291 prompt: string;
292
293 /**
294 * Body param: Controls how closely the generated image should adhere to the
295 * prompt; higher values make the image more aligned with the prompt
296 */
297 guidance?: number;
298
299 /**
300 * Body param: The height of the generated image in pixels
301 */
302 height?: number;
303
304 /**
305 * Body param: For use with img2img tasks. An array of integers that represent the
306 * image data constrained to 8-bit unsigned integer values
307 */
308 image?: Array<number>;
309
310 /**
311 * Body param: For use with img2img tasks. A base64-encoded string of the input
312 * image
313 */
314 image_b64?: string;
315
316 /**
317 * Body param: An array representing An array of integers that represent mask image
318 * data for inpainting constrained to 8-bit unsigned integer values
319 */
320 mask?: Array<number>;
321
322 /**
323 * Body param: Text describing elements to avoid in the generated image
324 */
325 negative_prompt?: string;
326
327 /**
328 * Body param: The number of diffusion steps; higher values can improve quality but
329 * take longer
330 */
331 num_steps?: number;
332
333 /**
334 * Body param: Random seed for reproducibility of the image generation
335 */
336 seed?: number;
337
338 /**
339 * Body param: A value between 0 and 1 indicating how strongly to apply the
340 * transformation during img2img tasks; lower values make the output closer to the
341 * input image
342 */
343 strength?: number;
344
345 /**
346 * Body param: The width of the generated image in pixels
347 */
348 width?: number;
349 }
350
351 export interface TextToSpeech {
352 /**
353 * Path param:
354 */
355 account_id: string;
356
357 /**
358 * Body param: A text description of the image you want to generate
359 */
360 prompt: string;
361
362 /**
363 * Body param: The speech language (e.g., 'en' for English, 'fr' for French).
364 * Defaults to 'en' if not specified
365 */
366 lang?: string;
367 }
368
369 export interface TextEmbeddings {
370 /**
371 * Path param:
372 */
373 account_id: string;
374
375 /**
376 * Body param: The text to embed
377 */
378 text: string | Array<string>;
379 }
380
381 export interface AutomaticSpeechRecognition {
382 /**
383 * Path param:
384 */
385 account_id: string;
386
387 /**
388 * Body param: An array of integers that represent the audio data constrained to
389 * 8-bit unsigned integer values
390 */
391 audio: Array<number>;
392
393 /**
394 * Body param: The language of the recorded audio
395 */
396 source_lang?: string;
397
398 /**
399 * Body param: The language to translate the transcription into. Currently only
400 * English is supported.
401 */
402 target_lang?: string;
403 }
404
405 export interface ImageClassification {
406 /**
407 * Path param:
408 */
409 account_id: string;
410
411 /**
412 * Body param: An array of integers that represent the image data constrained to
413 * 8-bit unsigned integer values
414 */
415 image: Array<number>;
416 }
417
418 export interface ObjectDetection {
419 /**
420 * Path param:
421 */
422 account_id: string;
423
424 /**
425 * Body param: An array of integers that represent the image data constrained to
426 * 8-bit unsigned integer values
427 */
428 image?: Array<number>;
429 }
430
431 export interface Prompt {
432 /**
433 * Path param:
434 */
435 account_id: string;
436
437 /**
438 * Body param: The input text prompt for the model to generate a response.
439 */
440 prompt: string;
441
442 /**
443 * Body param: Decreases the likelihood of the model repeating the same lines
444 * verbatim.
445 */
446 frequency_penalty?: number;
447
448 /**
449 * Body param: Name of the LoRA (Low-Rank Adaptation) model to fine-tune the base
450 * model.
451 */
452 lora?: string;
453
454 /**
455 * Body param: The maximum number of tokens to generate in the response.
456 */
457 max_tokens?: number;
458
459 /**
460 * Body param: Increases the likelihood of the model introducing new topics.
461 */
462 presence_penalty?: number;
463
464 /**
465 * Body param: If true, a chat template is not applied and you must adhere to the
466 * specific model's expected formatting.
467 */
468 raw?: boolean;
469
470 /**
471 * Body param: Penalty for repeated tokens; higher values discourage repetition.
472 */
473 repetition_penalty?: number;
474
475 /**
476 * Body param:
477 */
478 response_format?: Prompt.ResponseFormat;
479
480 /**
481 * Body param: Random seed for reproducibility of the generation.
482 */
483 seed?: number;
484
485 /**
486 * Body param: If true, the response will be streamed back incrementally using SSE,
487 * Server Sent Events.
488 */
489 stream?: boolean;
490
491 /**
492 * Body param: Controls the randomness of the output; higher values produce more
493 * random results.
494 */
495 temperature?: number;
496
497 /**
498 * Body param: Limits the AI to choose from the top 'k' most probable words. Lower
499 * values make responses more focused; higher values introduce more variety and
500 * potential surprises.
501 */
502 top_k?: number;
503
504 /**
505 * Body param: Adjusts the creativity of the AI's responses by controlling how many
506 * possible words it considers. Lower values make outputs more predictable; higher
507 * values allow for more varied and creative responses.
508 */
509 top_p?: number;
510 }
511
512 export namespace Prompt {
513 export interface ResponseFormat {
514 json_schema?: unknown;
515
516 type?: 'json_object' | 'json_schema';
517 }
518 }
519
520 export interface Messages {
521 /**
522 * Path param:
523 */
524 account_id: string;
525
526 /**
527 * Body param: An array of message objects representing the conversation history.
528 */
529 messages: Array<Messages.Message>;
530
531 /**
532 * Body param: Decreases the likelihood of the model repeating the same lines
533 * verbatim.
534 */
535 frequency_penalty?: number;
536
537 /**
538 * Body param:
539 */
540 functions?: Array<Messages.Function>;
541
542 /**
543 * Body param: The maximum number of tokens to generate in the response.
544 */
545 max_tokens?: number;
546
547 /**
548 * Body param: Increases the likelihood of the model introducing new topics.
549 */
550 presence_penalty?: number;
551
552 /**
553 * Body param: Penalty for repeated tokens; higher values discourage repetition.
554 */
555 repetition_penalty?: number;
556
557 /**
558 * Body param:
559 */
560 response_format?: Messages.ResponseFormat;
561
562 /**
563 * Body param: Random seed for reproducibility of the generation.
564 */
565 seed?: number;
566
567 /**
568 * Body param: If true, the response will be streamed back incrementally.
569 */
570 stream?: boolean;
571
572 /**
573 * Body param: Controls the randomness of the output; higher values produce more
574 * random results.
575 */
576 temperature?: number;
577
578 /**
579 * Body param: A list of tools available for the assistant to use.
580 */
581 tools?: Array<Messages.UnionMember0 | Messages.UnionMember1>;
582
583 /**
584 * Body param: Limits the AI to choose from the top 'k' most probable words. Lower
585 * values make responses more focused; higher values introduce more variety and
586 * potential surprises.
587 */
588 top_k?: number;
589
590 /**
591 * Body param: Controls the creativity of the AI's responses by adjusting how many
592 * possible words it considers. Lower values make outputs more predictable; higher
593 * values allow for more varied and creative responses.
594 */
595 top_p?: number;
596 }
597
598 export namespace Messages {
599 export interface Message {
600 /**
601 * The content of the message as a string.
602 */
603 content: string;
604
605 /**
606 * The role of the message sender (e.g., 'user', 'assistant', 'system', 'tool').
607 */
608 role: string;
609 }
610
611 export interface Function {
612 code: string;
613
614 name: string;
615 }
616
617 export interface ResponseFormat {
618 json_schema?: unknown;
619
620 type?: 'json_object' | 'json_schema';
621 }
622
623 export interface UnionMember0 {
624 /**
625 * A brief description of what the tool does.
626 */
627 description: string;
628
629 /**
630 * The name of the tool. More descriptive the better.
631 */
632 name: string;
633
634 /**
635 * Schema defining the parameters accepted by the tool.
636 */
637 parameters: UnionMember0.Parameters;
638 }
639
640 export namespace UnionMember0 {
641 /**
642 * Schema defining the parameters accepted by the tool.
643 */
644 export interface Parameters {
645 /**
646 * Definitions of each parameter.
647 */
648 properties: Record<string, Parameters.Properties>;
649
650 /**
651 * The type of the parameters object (usually 'object').
652 */
653 type: string;
654
655 /**
656 * List of required parameter names.
657 */
658 required?: Array<string>;
659 }
660
661 export namespace Parameters {
662 export interface Properties {
663 /**
664 * A description of the expected parameter.
665 */
666 description: string;
667
668 /**
669 * The data type of the parameter.
670 */
671 type: string;
672 }
673 }
674 }
675
676 export interface UnionMember1 {
677 /**
678 * Details of the function tool.
679 */
680 function: UnionMember1.Function;
681
682 /**
683 * Specifies the type of tool (e.g., 'function').
684 */
685 type: string;
686 }
687
688 export namespace UnionMember1 {
689 /**
690 * Details of the function tool.
691 */
692 export interface Function {
693 /**
694 * A brief description of what the function does.
695 */
696 description: string;
697
698 /**
699 * The name of the function.
700 */
701 name: string;
702
703 /**
704 * Schema defining the parameters accepted by the function.
705 */
706 parameters: Function.Parameters;
707 }
708
709 export namespace Function {
710 /**
711 * Schema defining the parameters accepted by the function.
712 */
713 export interface Parameters {
714 /**
715 * Definitions of each parameter.
716 */
717 properties: Record<string, Parameters.Properties>;
718
719 /**
720 * The type of the parameters object (usually 'object').
721 */
722 type: string;
723
724 /**
725 * List of required parameter names.
726 */
727 required?: Array<string>;
728 }
729
730 export namespace Parameters {
731 export interface Properties {
732 /**
733 * A description of the expected parameter.
734 */
735 description: string;
736
737 /**
738 * The data type of the parameter.
739 */
740 type: string;
741 }
742 }
743 }
744 }
745 }
746
747 export interface Translation {
748 /**
749 * Path param:
750 */
751 account_id: string;
752
753 /**
754 * Body param: The language code to translate the text into (e.g., 'es' for
755 * Spanish)
756 */
757 target_lang: string;
758
759 /**
760 * Body param: The text to be translated
761 */
762 text: string;
763
764 /**
765 * Body param: The language code of the source text (e.g., 'en' for English).
766 * Defaults to 'en' if not specified
767 */
768 source_lang?: string;
769 }
770
771 export interface Summarization {
772 /**
773 * Path param:
774 */
775 account_id: string;
776
777 /**
778 * Body param: The text that you want the model to summarize
779 */
780 input_text: string;
781
782 /**
783 * Body param: The maximum length of the generated summary in tokens
784 */
785 max_length?: number;
786 }
787
788 export interface ImageToText {
789 /**
790 * Path param:
791 */
792 account_id: string;
793
794 /**
795 * Body param: An array of integers that represent the image data constrained to
796 * 8-bit unsigned integer values
797 */
798 image: Array<number>;
799
800 /**
801 * Body param: Decreases the likelihood of the model repeating the same lines
802 * verbatim.
803 */
804 frequency_penalty?: number;
805
806 /**
807 * Body param: The maximum number of tokens to generate in the response.
808 */
809 max_tokens?: number;
810
811 /**
812 * Body param: Increases the likelihood of the model introducing new topics.
813 */
814 presence_penalty?: number;
815
816 /**
817 * Body param: The input text prompt for the model to generate a response.
818 */
819 prompt?: string;
820
821 /**
822 * Body param: If true, a chat template is not applied and you must adhere to the
823 * specific model's expected formatting.
824 */
825 raw?: boolean;
826
827 /**
828 * Body param: Penalty for repeated tokens; higher values discourage repetition.
829 */
830 repetition_penalty?: number;
831
832 /**
833 * Body param: Random seed for reproducibility of the generation.
834 */
835 seed?: number;
836
837 /**
838 * Body param: Controls the randomness of the output; higher values produce more
839 * random results.
840 */
841 temperature?: number;
842
843 /**
844 * Body param: Limits the AI to choose from the top 'k' most probable words. Lower
845 * values make responses more focused; higher values introduce more variety and
846 * potential surprises.
847 */
848 top_k?: number;
849
850 /**
851 * Body param: Controls the creativity of the AI's responses by adjusting how many
852 * possible words it considers. Lower values make outputs more predictable; higher
853 * values allow for more varied and creative responses.
854 */
855 top_p?: number;
856 }
857}
858
859AI.Finetunes = Finetunes;
860AI.Authors = Authors;
861AI.AuthorListResponsesSinglePage = AuthorListResponsesSinglePage;
862AI.Tasks = Tasks;
863AI.TaskListResponsesSinglePage = TaskListResponsesSinglePage;
864AI.Models = Models;
865AI.ModelListResponsesV4PagePaginationArray = ModelListResponsesV4PagePaginationArray;
866
867export declare namespace AI {
868 export {
869 Finetunes as Finetunes,
870 type FinetuneCreateResponse as FinetuneCreateResponse,
871 type FinetuneListResponse as FinetuneListResponse,
872 type FinetuneCreateParams as FinetuneCreateParams,
873 type FinetuneListParams as FinetuneListParams,
874 };
875
876 export {
877 Authors as Authors,
878 type AuthorListResponse as AuthorListResponse,
879 AuthorListResponsesSinglePage as AuthorListResponsesSinglePage,
880 type AuthorListParams as AuthorListParams,
881 };
882
883 export {
884 Tasks as Tasks,
885 type TaskListResponse as TaskListResponse,
886 TaskListResponsesSinglePage as TaskListResponsesSinglePage,
887 type TaskListParams as TaskListParams,
888 };
889
890 export {
891 Models as Models,
892 type ModelListResponse as ModelListResponse,
893 ModelListResponsesV4PagePaginationArray as ModelListResponsesV4PagePaginationArray,
894 type ModelListParams as ModelListParams,
895 };
896}
897