141. Copy this Val and save it as an Email Val (choose Val type in top-right corner of editor)
15162. Add your Fireworks (or openai API compatible) API key to line 8 (or use an environment variable: https://docs.val.town/reference/environment-variables/)
17183. Copy the email address of the Val (click 3 dots in top-right > Copy > Copy email address)
cerebrasTemplatemain.tsx2 matches
83const { messages } = await request.json();
8485const { OpenAI } = await import("https://esm.sh/openai");
86// Cerebras Inference
87// Very important: do not change this
88// Keep these comments so we remember not to change this
89const client = new OpenAI({
90apiKey: Deno.env.get("CEREBRAS_API_KEY"),
91baseURL: "https://api.cerebras.ai/v1",
victoriousGreenLynxmain.tsx4 matches
93const { messages } = await request.json();
9495const { OpenAI } = await import("https://esm.town/v/std/openai");
96const openai = new OpenAI();
9798try {
99const response = await openai.chat.completions.create({
100model: "gpt-4o-mini",
101messages: [
109return Response.json({ message: generatedMessage });
110} catch (error) {
111console.error("Error calling OpenAI API:", error);
112113if (error.status === 429) {
victoriousGreenLynxREADME.md2 matches
29Use Cerebras for AI on the backend like so:
3031const { OpenAI } = await import("https://esm.sh/openai");
32const client = new OpenAI({
33apiKey: "YOUR_CEREBRAS_API_KEY",
34baseURL: "https://api.cerebras.ai/v1"
masterfulPeachHookwormmain.tsx2 matches
83const { messages, sassy } = await request.json();
8485const { OpenAI } = await import("https://esm.sh/openai");
86const client = new OpenAI({
87apiKey: Deno.env.get("CEREBRAS_API_KEY"),
88baseURL: "https://api.cerebras.ai/v1",
masterfulPeachHookwormREADME.md2 matches
29Use Cerebras for AI on the backend like so:
3031const { OpenAI } = await import("https://esm.sh/openai");
32const client = new OpenAI({
33apiKey: "YOUR_CEREBRAS_API_KEY",
34baseURL: "https://api.cerebras.ai/v1"
knowledgeableTomatoCrawdadREADME.md2 matches
29Use Cerebras for AI on the backend like so:
3031const { OpenAI } = await import("https://esm.sh/openai");
32const client = new OpenAI({
33apiKey: "YOUR_CEREBRAS_API_KEY",
34baseURL: "https://api.cerebras.ai/v1"
knowledgeableTomatoCrawdadmain.tsx2 matches
91const { messages } = await request.json();
9293const { OpenAI } = await import("https://esm.sh/openai");
94const client = new OpenAI({
95apiKey: Deno.env.get("CEREBRAS_API_KEY"),
96baseURL: "https://api.cerebras.ai/v1"
emailValHandlermain.tsx16 matches
5// main controller function
6export async function emailValHandler(receivedEmail) {
7const openaiUrl = "https://api.openai.com/v1/chat/completions";
8const 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/
9const model = "gpt-4o-mini";
1011if (!apiKey) {
12throw new Error(
13"OPENAI_KEY environment variable is not set. Please set it or replace this line with your API key.",
14);
15}
35const prompt = generatePrompt(receivedEmail, pdfTexts, emailText);
3637// step 4: send prompt to openai
38const openaiResponse = await sendRequestToOpenAI(prompt, openaiUrl, apiKey, model);
3940// log the openai response
41console.log("openai response:", openaiResponse);
4243// step 5: send the response back via email
44await sendResponseByEmail(receivedEmail.from, openaiResponse);
4546console.log("response email sent.");
97}
9899// helper function to generate a prompt for openai
100function generatePrompt(email, pdfTexts, emailText) {
101// extract the first name from the 'from' field if it exists
121}
122123// helper function to send a request to openai
124async function sendRequestToOpenAI(prompt, openaiUrl, apiKey, model) {
125try {
126// prepare the openai messages payload
127const messages = [
128{
142};
143144// send the request to openai
145const response = await fetch(openaiUrl, {
146method: "POST",
147body: JSON.stringify(body),
154// parse the response
155const data = await response.json();
156return data.choices[0]?.message?.content || "no response from openai.";
157} catch (err) {
158console.error("error in sendRequestToOpenAI:", err);
159return "error processing your request.";
160}
emailValHandlerREADME.md2 matches
67This script allows you to:
8- Send emails to OpenAI. The text will be treated as the prompt
9- Parse PDF attachments and include their contents in the AI's analysis.
10- Get response directly to your inbox.
141. Copy this Val and save it as an Email Val (choose Val type in top-right corner of editor)
15162. Add your OpenAI API key to line 8 (or use an environment variable: https://docs.val.town/reference/environment-variables/)
17183. Copy the email address of the Val (click 3 dots in top-right > Copy > Copy email address)