Created
December 17, 2023 23:11
-
-
Save mizchi/d4e629c86eaf3abe4cbc16f5e7c4aeb4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env -S deno run --allow-net --allow-read --allow-env --unstable --ext=ts | |
// USAGE: $ deno run cfai-text.ts "Hello Cloudflare AI!" | |
type RequestType = { | |
messages: { | |
role: string; | |
content: string; | |
}[]; | |
}; | |
type ResponseType = { | |
success: true, | |
result: { | |
response: string; | |
}; | |
errors: string[]; | |
messages: string[]; | |
} | { | |
success: false, | |
errors: string[]; | |
messages: string[]; | |
} | |
function createRequest(opts: { token: string, accountId: string }) { | |
return async (model: string, payload: RequestType): Promise<ResponseType> => { | |
const endpoint = `https://api.cloudflare.com/client/v4/accounts/${opts.accountId}/ai/run/${model}`; | |
return fetch(endpoint, | |
{ | |
headers: { | |
'Authorization': `Bearer ${opts.token}`, | |
'Content-Type': 'application/json', | |
}, | |
method: "POST", | |
body: JSON.stringify(payload), | |
} | |
).then((res) => res.json()); | |
} | |
} | |
// Run | |
const CF_API_TOKEN = Deno.env.get('CF_API_TOKEN'); | |
const CF_ACCOUNT_ID = Deno.env.get('CF_ACCOUNT_ID'); | |
if (!CF_ACCOUNT_ID || !CF_API_TOKEN) { | |
throw new Error('CF_ACCOUNT_ID and CF_API_TOKEN must be set'); | |
} | |
if (import.meta.main) { | |
const input = Deno.args.join(' '); | |
const request = createRequest({ token: CF_API_TOKEN, accountId: CF_ACCOUNT_ID }); | |
const res = await request('@cf/meta/llama-2-7b-chat-int8', { | |
messages: [ | |
{ role: 'user', content: input } | |
], | |
}); | |
if (res.success) { | |
console.log(res.result.response); | |
} else { | |
console.error('Error', res); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment