cloudflare/cloudflare-typescript

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v4.0.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/resources/ai/ai.ts

812lines · 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 export namespace UnionMember7 {
198 export interface ToolCall {
199 /**
200 * The arguments passed to be passed to the tool call request
201 */
202 arguments?: unknown;
203
204 /**
205 * The name of the tool to be called
206 */
207 name?: string;
208 }
209 }
210
211 export interface Translation {
212 /**
213 * The translated text in the target language
214 */
215 translated_text?: string;
216 }
217
218 export interface Summarization {
219 /**
220 * The summarized version of the input text
221 */
222 summary?: string;
223 }
224
225 export interface ImageToText {
226 description?: string;
227 }
228}
229
230export type AIRunParams =
231 | AIRunParams.TextClassification
232 | AIRunParams.TextToImage
233 | AIRunParams.TextToSpeech
234 | AIRunParams.TextEmbeddings
235 | AIRunParams.AutomaticSpeechRecognition
236 | AIRunParams.ImageClassification
237 | AIRunParams.ObjectDetection
238 | AIRunParams.Prompt
239 | AIRunParams.Messages
240 | AIRunParams.Translation
241 | AIRunParams.Summarization
242 | AIRunParams.ImageToText;
243
244export declare namespace AIRunParams {
245 export interface TextClassification {
246 /**
247 * Path param:
248 */
249 account_id: string;
250
251 /**
252 * Body param: The text that you want to classify
253 */
254 text: string;
255 }
256
257 export interface TextToImage {
258 /**
259 * Path param:
260 */
261 account_id: string;
262
263 /**
264 * Body param: A text description of the image you want to generate
265 */
266 prompt: string;
267
268 /**
269 * Body param: Controls how closely the generated image should adhere to the
270 * prompt; higher values make the image more aligned with the prompt
271 */
272 guidance?: number;
273
274 /**
275 * Body param: The height of the generated image in pixels
276 */
277 height?: number;
278
279 /**
280 * Body param: For use with img2img tasks. An array of integers that represent the
281 * image data constrained to 8-bit unsigned integer values
282 */
283 image?: Array<number>;
284
285 /**
286 * Body param: For use with img2img tasks. A base64-encoded string of the input
287 * image
288 */
289 image_b64?: string;
290
291 /**
292 * Body param: An array representing An array of integers that represent mask image
293 * data for inpainting constrained to 8-bit unsigned integer values
294 */
295 mask?: Array<number>;
296
297 /**
298 * Body param: Text describing elements to avoid in the generated image
299 */
300 negative_prompt?: string;
301
302 /**
303 * Body param: The number of diffusion steps; higher values can improve quality but
304 * take longer
305 */
306 num_steps?: number;
307
308 /**
309 * Body param: Random seed for reproducibility of the image generation
310 */
311 seed?: number;
312
313 /**
314 * Body param: A value between 0 and 1 indicating how strongly to apply the
315 * transformation during img2img tasks; lower values make the output closer to the
316 * input image
317 */
318 strength?: number;
319
320 /**
321 * Body param: The width of the generated image in pixels
322 */
323 width?: number;
324 }
325
326 export interface TextToSpeech {
327 /**
328 * Path param:
329 */
330 account_id: string;
331
332 /**
333 * Body param: A text description of the image you want to generate
334 */
335 prompt: string;
336
337 /**
338 * Body param: The speech language (e.g., 'en' for English, 'fr' for French).
339 * Defaults to 'en' if not specified
340 */
341 lang?: string;
342 }
343
344 export interface TextEmbeddings {
345 /**
346 * Path param:
347 */
348 account_id: string;
349
350 /**
351 * Body param: The text to embed
352 */
353 text: string | Array<string>;
354 }
355
356 export interface AutomaticSpeechRecognition {
357 /**
358 * Path param:
359 */
360 account_id: string;
361
362 /**
363 * Body param: An array of integers that represent the audio data constrained to
364 * 8-bit unsigned integer values
365 */
366 audio: Array<number>;
367
368 /**
369 * Body param: The language of the recorded audio
370 */
371 source_lang?: string;
372
373 /**
374 * Body param: The language to translate the transcription into. Currently only
375 * English is supported.
376 */
377 target_lang?: string;
378 }
379
380 export interface ImageClassification {
381 /**
382 * Path param:
383 */
384 account_id: string;
385
386 /**
387 * Body param: An array of integers that represent the image data constrained to
388 * 8-bit unsigned integer values
389 */
390 image: Array<number>;
391 }
392
393 export interface ObjectDetection {
394 /**
395 * Path param:
396 */
397 account_id: string;
398
399 /**
400 * Body param: An array of integers that represent the image data constrained to
401 * 8-bit unsigned integer values
402 */
403 image?: Array<number>;
404 }
405
406 export interface Prompt {
407 /**
408 * Path param:
409 */
410 account_id: string;
411
412 /**
413 * Body param: The input text prompt for the model to generate a response.
414 */
415 prompt: string;
416
417 /**
418 * Body param: Decreases the likelihood of the model repeating the same lines
419 * verbatim.
420 */
421 frequency_penalty?: number;
422
423 /**
424 * Body param: Name of the LoRA (Low-Rank Adaptation) model to fine-tune the base
425 * model.
426 */
427 lora?: string;
428
429 /**
430 * Body param: The maximum number of tokens to generate in the response.
431 */
432 max_tokens?: number;
433
434 /**
435 * Body param: Increases the likelihood of the model introducing new topics.
436 */
437 presence_penalty?: number;
438
439 /**
440 * Body param: If true, a chat template is not applied and you must adhere to the
441 * specific model's expected formatting.
442 */
443 raw?: boolean;
444
445 /**
446 * Body param: Penalty for repeated tokens; higher values discourage repetition.
447 */
448 repetition_penalty?: number;
449
450 /**
451 * Body param: Random seed for reproducibility of the generation.
452 */
453 seed?: number;
454
455 /**
456 * Body param: If true, the response will be streamed back incrementally using SSE,
457 * Server Sent Events.
458 */
459 stream?: boolean;
460
461 /**
462 * Body param: Controls the randomness of the output; higher values produce more
463 * random results.
464 */
465 temperature?: number;
466
467 /**
468 * Body param: Limits the AI to choose from the top 'k' most probable words. Lower
469 * values make responses more focused; higher values introduce more variety and
470 * potential surprises.
471 */
472 top_k?: number;
473
474 /**
475 * Body param: Adjusts the creativity of the AI's responses by controlling how many
476 * possible words it considers. Lower values make outputs more predictable; higher
477 * values allow for more varied and creative responses.
478 */
479 top_p?: number;
480 }
481
482 export interface Messages {
483 /**
484 * Path param:
485 */
486 account_id: string;
487
488 /**
489 * Body param: An array of message objects representing the conversation history.
490 */
491 messages: Array<AIRunParams.Messages.Message>;
492
493 /**
494 * Body param: Decreases the likelihood of the model repeating the same lines
495 * verbatim.
496 */
497 frequency_penalty?: number;
498
499 /**
500 * Body param:
501 */
502 functions?: Array<AIRunParams.Messages.Function>;
503
504 /**
505 * Body param: The maximum number of tokens to generate in the response.
506 */
507 max_tokens?: number;
508
509 /**
510 * Body param: Increases the likelihood of the model introducing new topics.
511 */
512 presence_penalty?: number;
513
514 /**
515 * Body param: Penalty for repeated tokens; higher values discourage repetition.
516 */
517 repetition_penalty?: number;
518
519 /**
520 * Body param: Random seed for reproducibility of the generation.
521 */
522 seed?: number;
523
524 /**
525 * Body param: If true, the response will be streamed back incrementally.
526 */
527 stream?: boolean;
528
529 /**
530 * Body param: Controls the randomness of the output; higher values produce more
531 * random results.
532 */
533 temperature?: number;
534
535 /**
536 * Body param: A list of tools available for the assistant to use.
537 */
538 tools?: Array<AIRunParams.Messages.UnionMember0 | AIRunParams.Messages.UnionMember1>;
539
540 /**
541 * Body param: Limits the AI to choose from the top 'k' most probable words. Lower
542 * values make responses more focused; higher values introduce more variety and
543 * potential surprises.
544 */
545 top_k?: number;
546
547 /**
548 * Body param: Controls the creativity of the AI's responses by adjusting how many
549 * possible words it considers. Lower values make outputs more predictable; higher
550 * values allow for more varied and creative responses.
551 */
552 top_p?: number;
553 }
554
555 export namespace Messages {
556 export interface Message {
557 /**
558 * The content of the message as a string.
559 */
560 content: string;
561
562 /**
563 * The role of the message sender (e.g., 'user', 'assistant', 'system', 'tool').
564 */
565 role: string;
566 }
567
568 export interface Function {
569 code: string;
570
571 name: string;
572 }
573
574 export interface UnionMember0 {
575 /**
576 * A brief description of what the tool does.
577 */
578 description: string;
579
580 /**
581 * The name of the tool. More descriptive the better.
582 */
583 name: string;
584
585 /**
586 * Schema defining the parameters accepted by the tool.
587 */
588 parameters: UnionMember0.Parameters;
589 }
590
591 export namespace UnionMember0 {
592 /**
593 * Schema defining the parameters accepted by the tool.
594 */
595 export interface Parameters {
596 /**
597 * Definitions of each parameter.
598 */
599 properties: Record<string, Parameters.Properties>;
600
601 /**
602 * The type of the parameters object (usually 'object').
603 */
604 type: string;
605
606 /**
607 * List of required parameter names.
608 */
609 required?: Array<string>;
610 }
611
612 export namespace Parameters {
613 export interface Properties {
614 /**
615 * A description of the expected parameter.
616 */
617 description: string;
618
619 /**
620 * The data type of the parameter.
621 */
622 type: string;
623 }
624 }
625 }
626
627 export interface UnionMember1 {
628 /**
629 * Details of the function tool.
630 */
631 function: UnionMember1.Function;
632
633 /**
634 * Specifies the type of tool (e.g., 'function').
635 */
636 type: string;
637 }
638
639 export namespace UnionMember1 {
640 /**
641 * Details of the function tool.
642 */
643 export interface Function {
644 /**
645 * A brief description of what the function does.
646 */
647 description: string;
648
649 /**
650 * The name of the function.
651 */
652 name: string;
653
654 /**
655 * Schema defining the parameters accepted by the function.
656 */
657 parameters: Function.Parameters;
658 }
659
660 export namespace Function {
661 /**
662 * Schema defining the parameters accepted by the function.
663 */
664 export interface Parameters {
665 /**
666 * Definitions of each parameter.
667 */
668 properties: Record<string, Parameters.Properties>;
669
670 /**
671 * The type of the parameters object (usually 'object').
672 */
673 type: string;
674
675 /**
676 * List of required parameter names.
677 */
678 required?: Array<string>;
679 }
680
681 export namespace Parameters {
682 export interface Properties {
683 /**
684 * A description of the expected parameter.
685 */
686 description: string;
687
688 /**
689 * The data type of the parameter.
690 */
691 type: string;
692 }
693 }
694 }
695 }
696 }
697
698 export interface Translation {
699 /**
700 * Path param:
701 */
702 account_id: string;
703
704 /**
705 * Body param: The language code to translate the text into (e.g., 'es' for
706 * Spanish)
707 */
708 target_lang: string;
709
710 /**
711 * Body param: The text to be translated
712 */
713 text: string;
714
715 /**
716 * Body param: The language code of the source text (e.g., 'en' for English).
717 * Defaults to 'en' if not specified
718 */
719 source_lang?: string;
720 }
721
722 export interface Summarization {
723 /**
724 * Path param:
725 */
726 account_id: string;
727
728 /**
729 * Body param: The text that you want the model to summarize
730 */
731 input_text: string;
732
733 /**
734 * Body param: The maximum length of the generated summary in tokens
735 */
736 max_length?: number;
737 }
738
739 export interface ImageToText {
740 /**
741 * Path param:
742 */
743 account_id: string;
744
745 /**
746 * Body param: An array of integers that represent the image data constrained to
747 * 8-bit unsigned integer values
748 */
749 image: Array<number>;
750
751 /**
752 * Body param: The maximum number of tokens to generate in the response.
753 */
754 max_tokens?: number;
755
756 /**
757 * Body param: The input text prompt for the model to generate a response.
758 */
759 prompt?: string;
760
761 /**
762 * Body param: If true, a chat template is not applied and you must adhere to the
763 * specific model's expected formatting.
764 */
765 raw?: boolean;
766
767 /**
768 * Body param: Controls the randomness of the output; higher values produce more
769 * random results.
770 */
771 temperature?: number;
772 }
773}
774
775AI.Finetunes = Finetunes;
776AI.Authors = Authors;
777AI.AuthorListResponsesSinglePage = AuthorListResponsesSinglePage;
778AI.Tasks = Tasks;
779AI.TaskListResponsesSinglePage = TaskListResponsesSinglePage;
780AI.Models = Models;
781AI.ModelListResponsesV4PagePaginationArray = ModelListResponsesV4PagePaginationArray;
782
783export declare namespace AI {
784 export {
785 Finetunes as Finetunes,
786 type FinetuneCreateResponse as FinetuneCreateResponse,
787 type FinetuneListResponse as FinetuneListResponse,
788 type FinetuneCreateParams as FinetuneCreateParams,
789 type FinetuneListParams as FinetuneListParams,
790 };
791
792 export {
793 Authors as Authors,
794 type AuthorListResponse as AuthorListResponse,
795 AuthorListResponsesSinglePage as AuthorListResponsesSinglePage,
796 type AuthorListParams as AuthorListParams,
797 };
798
799 export {
800 Tasks as Tasks,
801 type TaskListResponse as TaskListResponse,
802 TaskListResponsesSinglePage as TaskListResponsesSinglePage,
803 type TaskListParams as TaskListParams,
804 };
805
806 export {
807 Models as Models,
808 type ModelListResponse as ModelListResponse,
809 ModelListResponsesV4PagePaginationArray as ModelListResponsesV4PagePaginationArray,
810 type ModelListParams as ModelListParams,
811 };
812}
813