pluckyGreenHyenamain.tsx16 matches
6function App() {
7const [messages, setMessages] = useState([]);
8const [apiKey, setApiKey] = useState("");
9const [generatedHtml, setGeneratedHtml] = useState("");
10const [isLoading, setIsLoading] = useState(false);
1213useEffect(() => {
14const savedApiKey = localStorage.getItem("cerebras_api_key");
15if (savedApiKey) {
16setApiKey(savedApiKey);
17}
18}, []);
1920const handleApiKeyChange = (e) => {
21const newApiKey = e.target.value;
22setApiKey(newApiKey);
23if (newApiKey) {
24localStorage.setItem("cerebras_api_key", newApiKey);
25} else {
26localStorage.removeItem("cerebras_api_key");
27}
28};
41const startTime = Date.now();
42try {
43const client = new Cerebras({ apiKey });
44const completionCreateResponse = await client.chat.completions.create({
45messages: [
83<input
84type="password"
85value={apiKey}
86onChange={handleApiKeyChange}
87className="w-full border border-gray-300 rounded-lg p-2"
88placeholder="Enter your Cerebras API key"
89/>
90</div>
111placeholder="Type your instruction..."
112required
113disabled={!apiKey || isLoading}
114/>
115117type="submit"
118className="bg-blue-500 text-white px-4 py-2 rounded-lg hover:bg-blue-600 flex-shrink-0 disabled:opacity-50"
119disabled={!apiKey || isLoading}
120>
121{isLoading ? "Loading..." : "Send"}
runescapeWoodCuttingmain.tsx1 match
155<head>
156<title>RuneVal</title>
157<link href="https://fonts.googleapis.com/css2?family=VT323&display=swap" rel="stylesheet">
158<style>${css}</style>
159</head>
anthropicCachingmain.tsx30 matches
1/**
2* This val creates an interactive webpage that demonstrates the functionality of the Anthropic API.
3* It uses a React frontend with an input for the API key and buttons to trigger different operations.
4* The Anthropic API key is stored in the frontend state and sent with each API request.
5*/
61011function App() {
12const [apiKey, setApiKey] = useState("");
13const [outputs, setOutputs] = useState({
14nonCachedCall: "",
2324const runOperation = async (operation: string) => {
25if (!apiKey) {
26alert("Please enter your Anthropic API key first.");
27return;
28}
35"Content-Type": "application/json",
36},
37body: JSON.stringify({ apiKey }),
38});
39const result = await response.text();
52<a href="https://github.com/anthropics/anthropic-cookbook/blob/7786b9f39db8ba65202792f564c59697a5222531/misc/prompt_caching.ipynb#L402">
53this python notebook
54</a>. Enter in your Anthropic API key (which is not saved) and click the buttons to see the results.
55</p>
56<p>
60<input
61type="password"
62placeholder="Enter Anthropic API Key"
63value={apiKey}
64onChange={(e) => setApiKey(e.target.value)}
65/>
66</div>
67<div>
68<button onClick={() => runOperation("nonCachedCall")} disabled={loading.nonCachedCall}>
69Non-cached API Call
70</button>
71<button onClick={() => runOperation("cachedCall")} disabled={loading.cachedCall}>Cached API Call</button>
72<button onClick={() => runOperation("multiTurn")} disabled={loading.multiTurn}>Multi-turn Conversation</button>
73</div>
74<h2>Non-cached API Call Output:</h2>
75<pre>{loading.nonCachedCall ? "Loading..." : outputs.nonCachedCall}</pre>
76<h2>Cached API Call Output:</h2>
77<pre>{loading.cachedCall ? "Loading..." : outputs.cachedCall}</pre>
78<h2>Multi-turn Conversation Output:</h2>
95if (url.pathname === "/run") {
96const operation = url.searchParams.get("operation");
97const { apiKey } = await request.json();
9899if (!apiKey) {
100return new Response("API key is required", { status: 400 });
101}
102104105if (operation === "nonCachedCall") {
106result = await runNonCachedCall(apiKey);
107} else if (operation === "cachedCall") {
108result = "Making two calls, first one to cache...\n\n";
109result += await runCachedCall(apiKey);
110result += "\n\nNow the cached call...\n\n";
111result += await runCachedCall(apiKey);
112} else if (operation === "multiTurn") {
113result = await runMultiTurnConversation(apiKey);
114} else {
115return new Response("Invalid operation", { status: 400 });
146}
147148async function runNonCachedCall(apiKey: string): Promise<string> {
149const { default: anthropic } = await import("npm:@anthropic-ai/sdk@0.26.1");
150const client = new anthropic.Anthropic({ apiKey });
151const MODEL_NAME = "claude-3-5-sonnet-20240620";
152175const elapsedTime = (endTime - startTime) / 1000;
176177return `Non-cached API call time: ${elapsedTime.toFixed(2)} seconds
178Input tokens: ${response.usage.input_tokens}
179Output tokens: ${response.usage.output_tokens}
182}
183184async function runCachedCall(apiKey: string): Promise<string> {
185const { default: anthropic } = await import("npm:@anthropic-ai/sdk@0.26.1");
186const client = new anthropic.Anthropic({ apiKey });
187const MODEL_NAME = "claude-3-5-sonnet-20240620";
188212const elapsedTime = (endTime - startTime) / 1000;
213214return `Cached API call time: ${elapsedTime.toFixed(2)} seconds
215Input tokens: ${response.usage.input_tokens}
216Output tokens: ${response.usage.output_tokens}
221}
222223async function runMultiTurnConversation(apiKey: string): Promise<string> {
224const { default: anthropic } = await import("npm:@anthropic-ai/sdk@0.26.1");
225const client = new anthropic.Anthropic({ apiKey });
226const MODEL_NAME = "claude-3-5-sonnet-20240620";
227
19```
2021You can use this library with https://www.val.town/v/maxm/transformEvalCode to return the last value without needing to export it. This is how the /eval api endpoint used to work and makes the library preform similarly to a repl.
2223```ts
mlbScoreFetchermain.tsx4 matches
14const fetchScores = async () => {
15try {
16const response = await fetch('/api/scores');
17if (!response.ok) {
18throw new Error('Failed to fetch scores');
47<p>
48<small>
49Data source: <a href="http://statsapi.mlb.com">MLB Stats API</a> |{" "}
50<a href={import.meta.url.replace("esm.town", "val.town")}>View source</a>
51</small>
66const url = new URL(request.url);
67
68if (url.pathname === '/api/scores') {
69try {
70const mlbResponse = await fetch('https://statsapi.mlb.com/api/v1/schedule/games/?sportId=1');
71const mlbData = await mlbResponse.json();
72
1// This val fetches recent tweets about @SnapAR or Lens Studio
2// Updated to use Social Data instead of Twitter API
34import { socialDataSearch } from "https://esm.town/v/stevekrouse/socialDataSearch";
socialDataUpdatemain.tsx1 match
1// This val fetches recent tweets about @SnapAR or Lens Studio, removes duplicates,
2// and displays them as embedded tweets with preview images on a dark background.
3// Updated to use Social Data instead of Twitter API
45import { socialDataSearch } from "https://esm.town/v/stevekrouse/socialDataSearch?v=5";
27for (var i = 0; i < TV_SHOWS_WATCHED.length; ++i) {
28const show_id = TV_SHOWS_WATCHED[i]
29const url = `https://api.themoviedb.org/3/tv/${show_id}?api_key=${process.env.tmdb_api_key}`
30const resp = await fetch(url)
31const show = await resp.json()
43title: "My TV Shows",
44link: "https://www.val.town/Glench.tvshows",
45description: "Personal shows from tmdb api and val.town",
46item: {
47title: (x) =>
generateRAdioDjRssmain.tsx2 matches
5export const generateRAdioDjRss = async () => {
6const rssItems = previousDjs.map((dj) => {
7const djImgSrc = `https://r-a-d.io/api/dj-image/${
8encodeURIComponent(
9dj.djimage,
33link: "https://r-a-d.io/",
34description: "Informing you of when the DJ changes on r/a/dio with only a 15 minute delay!",
35rssLink: "https://api.val.town/v1/express/pettan.generateRAdioDjRss",
36})
37return new Response(rssBody, { headers: { "Content-Type": "application/rss+xml" } })
createGitHubContribGraphmain.tsx2 matches
380<title>Code Symphony</title>
381<style>${css}</style>
382<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap" rel="stylesheet">
383</head>
384<body>
417`
418419const response = await fetch("https://api.github.com/graphql", {
420method: "POST",
421headers: {