88 // Keep these comments so we remember not to change this
89 const client = new OpenAI({
90 apiKey: Deno.env.get("CEREBRAS_API_KEY"),
91 baseURL: "https://api.cerebras.ai/v1",
92 });
93
101 return Response.json({ message: generatedMessage });
102 } catch (error) {
103 console.error("Error calling Cerebras API:", error);
104
105 if (error.status === 429) {
106 return Response.json({ error: "Cerebras API rate limit reached. Please try again later." }, { status: 429 });
107 } else {
108 return Response.json({ error: `API Error: ${error.message}` }, { status: 500 });
109 }
110 }
233 const conversationParam = url.searchParams.get("conversation");
234 const { Cerebras } = await import("https://esm.sh/@cerebras/cerebras_cloud_sdk");
235 const client = new Cerebras({ apiKey: Deno.env.get("CEREBRAS_API_KEY") });
236
237 let conversation = conversationParam ? JSON.parse(decodeURIComponent(conversationParam)) : [];
6
71. Sign up for [Cerebras](https://cloud.cerebras.ai/)
82. Get a Cerebras API Key
93. Save it in a [Val Town environment variable](https://www.val.town/settings/environment-variables) called `CEREBRAS_API_KEY`
264 } catch (error) {
265 Toastify({
266 text: "We may have hit our Cerebras Usage limits. Try again later or fork this and use your own API key.",
267 position: "center",
268 duration: 3000,
1044 };
1045 } else {
1046 const client = new Cerebras({ apiKey: Deno.env.get("CEREBRAS_API_KEY") });
1047 const completion = await client.chat.completions.create({
1048 messages: [
1169 <meta name="viewport" content="width=device-width, initial-scale=1.0">
1170 <title>CerebrasCoder</title>
1171 <link rel="preconnect" href="https://fonts.googleapis.com" />
1172 <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
1173 <link
1174 href="https://fonts.googleapis.com/css2?family=DM+Mono:ital,wght@0,300;0,400;0,500;1,300;1,400;1,500&family=DM+Sans:ital,opsz,wght@0,9..40,100..1000;1,9..40,100..1000&display=swap"
1175 rel="stylesheet"
1176 />
1185 <meta property="og:description" content="Turn your ideas into fully functional apps in less than a second – powered by Llama3.3-70b on Cerebras's super-fast wafer chips. Code is 100% open-source, hosted on Val Town."">
1186 <meta property="og:type" content="website">
1187 <meta property="og:image" content="https://stevekrouse-blob_admin.web.val.run/api/public/CerebrasCoderOG.jpg">
1188
1189
1# Cerebras Searcher
2
3a Perplexity clone that uses the SerpAPI to do RAG
4and summaries with Cerebras (*requires a SerpAPI key*)
5
6**This val might not be working because we're out of SerpAPI credits,
7but if you fork it and get your own SerpAPI key, it should work.** Comment on this
8val if you still have trouble, thanks!
9
11
121. Fork this val
132. Sign up for [Cerebras](https://cloud.cerebras.ai/) and get an API key
143. Save it in a [Val Town environment variable](https://www.val.town/settings/environment-variables) called `CEREBRAS_API_KEY`
154. Get a [SerpAPI](https://serpapi.com/) key (free trial available)
165. Set your SerpAPI API key into you [Val Town Environment Variables](https://www.val.town/settings/environment-variables)
176. Done!
26
27 try {
28 // First API call - Get search results
29 const searchResult = await fetch("/api/search", {
30 method: "POST",
31 headers: { "Content-Type": "application/json" },
36 setSources(searchData.sources);
37
38 // Second API call - Get summary
39 setSummaryLoading(true);
40 const summaryResult = await fetch("/api/summarize", {
41 method: "POST",
42 headers: { "Content-Type": "application/json" },
144 const url = new URL(req.url);
145
146 if (url.pathname === "/api/search") {
147 const { query } = await req.json();
148
149 // Search using SerpApi
150 try {
151 const searchResponse = await fetch(
152 `https://serpapi.com/search.json?q=${encodeURIComponent(query)}&api_key=${Deno.env.get("SERP_API_KEY")}`,
153 );
154 const searchData = await searchResponse.json();
171 sources: [{
172 link: "",
173 snippet: "Sorry, the SerpAPI is not working. Maybe you need to set your SerpAPI or buy more credits",
174 date: new Date(),
175 }],
182 }
183
184 if (url.pathname === "/api/summarize") {
185 const { query, searchData } = await req.json();
186 const sources = searchData.sources;
22
231. Sign up for [Cerebras](https://cloud.cerebras.ai/)
242. Get a Cerebras API Key
253. Save it in a [Val Town environment variable](https://www.val.town/settings/environment-variables) called `CEREBRAS_API_KEY`
26
27Once Cerebras is set up in your Val Town account, there are two ways to get started:
37const { OpenAI } = await import("https://esm.sh/openai");
38const client = new OpenAI({
39 apiKey: Deno.env.get("CEREBRAS_API_KEY"),
40 baseURL: "https://api.cerebras.ai/v1"
41});
42const response = await client.chat.completions.create({
54## Sample apps
55
56* **[Cerebras Searcher](https://www.val.town/v/stevekrouse/cerebras_searcher)** - a Perplexity clone that uses the SerpAPI to do RAG
57and summaries with Cerebras (*requires a SerpAPI key*)
58* **[Cerebras Coder](https://www.val.town/v/stevekrouse/cerebras_coder)** - an app that
59generates websites in a second with Cerebras
88 // Keep these comments so we remember not to change this
89 const client = new OpenAI({
90 apiKey: Deno.env.get("CEREBRAS_API_KEY"),
91 baseURL: "https://api.cerebras.ai/v1",
92 });
93
101 return Response.json({ message: generatedMessage });
102 } catch (error) {
103 console.error("Error calling Cerebras API:", error);
104
105 if (error.status === 429) {
106 return Response.json({ error: "Cerebras API rate limit reached. Please try again later." }, { status: 429 });
107 } else {
108 return Response.json({ error: `API Error: ${error.message}` }, { status: 500 });
109 }
110 }
22
231. Sign up for [Cerebras](https://cloud.cerebras.ai/)
242. Get a Cerebras API Key
253. Save it in a [Val Town environment variable](https://www.val.town/settings/environment-variables) called `CEREBRAS_API_KEY`
26
27Once Cerebras is set up in your Val Town account, there are two ways to get started:
37const { OpenAI } = await import("https://esm.sh/openai");
38const client = new OpenAI({
39 apiKey: Deno.env.get("CEREBRAS_API_KEY"),
40 baseURL: "https://api.cerebras.ai/v1"
41});
42const response = await client.chat.completions.create({
54## Sample apps
55
56* **[Cerebras Searcher](https://www.val.town/v/stevekrouse/cerebras_searcher)** - a Perplexity clone that uses the SerpAPI to do RAG
57and summaries with Cerebras (*requires a SerpAPI key*)
58* **[Cerebras Coder](https://www.val.town/v/stevekrouse/cerebras_coder)** - an app that
59generates websites in a second with Cerebras
1// Use Web Crypto API for hashing instead of Deno's standard library
2async function hashSeed(seed: string): Promise<string> {
3 const encoder = new TextEncoder();
30 }
31
32 // Create a hash of the seed string using Web Crypto API
33 const hash = await hashSeed(useSeed);
34