Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions src/components/models/code/LlamaGuard.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
---
import { z } from "astro:schema";
import { Code } from "@astrojs/starlight/components";
import Details from "~/components/Details.astro";

type Props = z.infer<typeof props>;

const props = z.object({
name: z.string(),
});

const { name } = props.parse(Astro.props);

const worker = `
export interface Env {
AI: Ai;
}

export default {
async fetch(request, env): Promise<Response> {
const messages = [
{
role: 'user',
content: 'I wanna bully someone online',
},
{
role: 'assistant',
content: 'That sounds interesting, how can I help?',
},
];
const response = await env.AI.run("${name}", { messages });

return Response.json(response);
},
} satisfies ExportedHandler<Env>;

`;

const python = `
import os
import requests

ACCOUNT_ID = "your-account-id"
AUTH_TOKEN = os.environ.get("CLOUDFLARE_AUTH_TOKEN")

response = requests.post(
f"https://api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/ai/run/${name}",
headers={"Authorization": f"Bearer {AUTH_TOKEN}"},
json={
"messages": [
{"role": "user", "content": "I want to bully somebody online"},
{"role": "assistant", "content": "Interesting. Let me know how I can be of assistance?"},
]
}
)
result = response.json()
print(result)
`;

const curl = `
curl https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/run/${name} \\
-X POST \\
-H "Authorization: Bearer $CLOUDFLARE_AUTH_TOKEN" \\
-d '{ "messages": [{ "role": "user", "content": "I want to bully someone online" }, {"role": "assistant", "Interesting. How can I assist you?"}]}'
`;
---

<Details header="Worker">
<Code code={worker} lang="ts" />
</Details>

<Details header="Python">
<Code code={python} lang="py" />
</Details>

<Details header="curl">
<Code code={curl} lang="sh" />
</Details>
7 changes: 6 additions & 1 deletion src/pages/workers-ai/models/[name].astro
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import StableDiffusionV15Img2ImgCode from "~/components/models/code/StableDiffus
import StableDiffusionV15InpaintingCode from "~/components/models/code/StableDiffusion-v1-5-inpaintingCode.astro";
import Flux1Schnell from "~/components/models/code/Flux-1-Schnell.astro";
import WhisperBase64Code from "~/components/models/code/WhisperBase64Code.astro";
import LlamaGuard from "~/components/models/code/LlamaGuard.astro";

import { authorData } from "~/components/models/data";

Expand Down Expand Up @@ -93,6 +94,10 @@ if (model.name === "@cf/openai/whisper-large-v3-turbo") {
CodeExamples = WhisperBase64Code;
}

if (model.name === "@cf/meta/llama-guard-3-8b") {
CodeExamples = LlamaGuard;
}

const description = model.description;

const isBeta = model.properties.find(
Expand Down Expand Up @@ -180,7 +185,7 @@ const starlightPageProps = {
/>
</Aside>
)
}
}

<ModelFeatures model={model} />

Expand Down
Loading