Skip to content
File

Blob: types/test/types/ai.ts

typescript68 lines
1// Copyright (c) 2025 Cloudflare, Inc.
2// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3// https://opensource.org/licenses/Apache-2.0
4 
5function expectType<T>(_value: T) {}
6 
7export const handler: ExportedHandler<{ AI: Ai }> = {
8 async fetch(_request, env) {
9 // Known model -- normal response
10 {
11 const result = await env.AI.run('@cf/meta/llama-3.1-8b-instruct-fp8', {
12 prompt: 'hello',
13 });
14 expectType<AiTextGenerationOutput>(result);
15 }
16 
17 // Known model -- streaming
18 {
19 const result = await env.AI.run('@cf/meta/llama-3.1-8b-instruct-fp8', {
20 prompt: 'hello',
21 stream: true as const,
22 });
23 expectType<ReadableStream>(result);
24 }
25 
26 // Known model -- raw response
27 {
28 const result = await env.AI.run(
29 '@cf/meta/llama-3.1-8b-instruct-fp8',
30 { prompt: 'hello' },
31 { returnRawResponse: true as const }
32 );
33 expectType<Response>(result);
34 }
35 
36 // Known model -- batch request
37 {
38 const result = await env.AI.run(
39 '@cf/meta/llama-3.1-8b-instruct-fp8',
40 { requests: [{ prompt: 'hello' }, { prompt: 'world' }] },
41 { queueRequest: true as const }
42 );
43 expectType<AiAsyncBatchResponse>(result);
44 }
45 
46 // Gateway model -- unknown model name, permissive types
47 {
48 const result = await env.AI.run('google/nano-banana', {
49 prompt: 'hello',
50 aspect_ratio: '16:9',
51 });
52 expectType<Record<string, unknown>>(result);
53 }
54 
55 // Gateway model with gateway options
56 {
57 const result = await env.AI.run(
58 'google/nano-banana',
59 { prompt: 'hello' },
60 { gateway: { id: 'my-gateway' } }
61 );
62 expectType<Record<string, unknown>>(result);
63 }
64 
65 return new Response();
66 },
67};