31const { OpenAI } = await import("https://esm.sh/openai");
32const client = new OpenAI({
33 apiKey: "YOUR_CEREBRAS_API_KEY",
34 baseURL: "https://api.cerebras.ai/v1"
35});
36const response = await client.chat.completions.create({
48## Sample apps
49
50* **[Cerebras Searcher](https://www.val.town/v/stevekrouse/cerebras_searcher)** - a Perplexity clone that uses the SerpAPI to do RAG
51and summaries with Cerebras (*requires a SerpAPI key*)
52* **[Cerebras Coder](https://www.val.town/v/stevekrouse/cerebras_coder)** - an app that
53generates websites in a second with Cerebras
93 const { OpenAI } = await import("https://esm.sh/openai");
94 const client = new OpenAI({
95 apiKey: Deno.env.get("CEREBRAS_API_KEY"),
96 baseURL: "https://api.cerebras.ai/v1"
97 });
98
106 return Response.json({ message: generatedMessage });
107 } catch (error) {
108 console.error("Error calling Cerebras API:", error);
109
110 if (error.status === 429) {
111 return Response.json({ error: "Cerebras API rate limit reached. Please try again later." }, { status: 429 });
112 } else {
113 return Response.json({ error: `API Error: ${error.message}` }, { status: 500 });
114 }
115 }
217 } catch (error) {
218 Toastify({
219 text: "We may have hit our Cerebras Usage limits. Try again later or fork this and use your own API key.",
220 position: "center",
221 duration: 3000,
970 };
971 } else {
972 const client = new Cerebras({ apiKey: Deno.env.get("CEREBRAS_API_KEY") });
973 const completion = await client.chat.completions.create({
974 messages: [
1095 <meta name="viewport" content="width=device-width, initial-scale=1.0">
1096 <title>CerebrasCoder</title>
1097 <link rel="preconnect" href="https://fonts.googleapis.com" />
1098 <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
1099 <link
1100 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"
1101 rel="stylesheet"
1102 />
1111 <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."">
1112 <meta property="og:type" content="website">
1113 <meta property="og:image" content="https://stevekrouse-blob_admin.web.val.run/api/public/CerebrasCoderOG.jpg">
1114
1115
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`
10
11# Todos
4// import sgMail from "npm:@sendgrid/mail";
5
6// sgMail.setApiKey(Deno.env.get("sendgrid"));
7
8const KNOWN_IRRELEVANT_DOMAINS = [
42
43 try {
44 const response = await fetch(`https://www.shovel.report/api/domains/${domain}`);
45 if (!response.ok) {
46 throw new Error(`HTTP error! status: ${response.status}`);
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`
10
11# Todos
217 } catch (error) {
218 Toastify({
219 text: "We may have hit our Cerebras Usage limits. Try again later or fork this and use your own API key.",
220 position: "center",
221 duration: 3000,
970 };
971 } else {
972 const client = new Cerebras({ apiKey: Deno.env.get("CEREBRAS_API_KEY") });
973 const completion = await client.chat.completions.create({
974 messages: [
1095 <meta name="viewport" content="width=device-width, initial-scale=1.0">
1096 <title>CerebrasCoder</title>
1097 <link rel="preconnect" href="https://fonts.googleapis.com" />
1098 <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
1099 <link
1100 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"
1101 rel="stylesheet"
1102 />
1111 <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."">
1112 <meta property="og:type" content="website">
1113 <meta property="og:image" content="https://stevekrouse-blob_admin.web.val.run/api/public/CerebrasCoderOG.jpg">
1114
1115
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;
5// main controller function
6export async function emailValHandler(receivedEmail) {
7 const openaiUrl = "https://api.openai.com/v1/chat/completions";
8 const apiKey = Deno.env.get("OPENAI_API_KEY"); // replace this entire line with your OpenAI API key as a string, e.g., "sk-123..." or use environment variable: https://docs.val.town/reference/environment-variables/
9 const model = "gpt-4o-mini";
10
11 if (!apiKey) {
12 throw new Error(
13 "OPENAI_KEY environment variable is not set. Please set it or replace this line with your API key.",
14 );
15 }
36
37 // step 4: send prompt to openai
38 const openaiResponse = await sendRequestToOpenAI(prompt, openaiUrl, apiKey, model);
39
40 // log the openai response
122
123// helper function to send a request to openai
124async function sendRequestToOpenAI(prompt, openaiUrl, apiKey, model) {
125 try {
126 // prepare the openai messages payload
148 headers: {
149 "Content-Type": "application/json",
150 Authorization: `Bearer ${apiKey}`,
151 },
152 });
141. Copy this Val and save it as an Email Val (choose Val type in top-right corner of editor)
15
162. Add your OpenAI API key to line 8 (or use an environment variable: https://docs.val.town/reference/environment-variables/)
17
183. Copy the email address of the Val (click 3 dots in top-right > Copy > Copy email address)