Skip to content

Instantly share code, notes, and snippets.

@mizchi
Created December 17, 2023 23:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mizchi/d4e629c86eaf3abe4cbc16f5e7c4aeb4 to your computer and use it in GitHub Desktop.
Save mizchi/d4e629c86eaf3abe4cbc16f5e7c4aeb4 to your computer and use it in GitHub Desktop.
#!/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