emailValHandlermain.tsx22 matches
20212. Set up the required environment variables:
22- OPENAI_API_KEY: Your OpenAI API key
23- MD_API_KEY: Your API key for the markdown extraction service (optional)
2460// Main controller function
61export default async function emailValHandler(receivedEmail) {
62const openaiUrl = "https://api.openai.com/v1/chat/completions";
63const openaiKey = Deno.env.get("OPENAI_API_KEY");
64const mdApiKey = Deno.env.get("MD_API_KEY");
65const model = "o1-2024-12-17";
6667if (!openaiKey) {
68throw new Error("OPENAI_KEY environment variable is not set.");
69}
707677// Step 2: Transform the email prompt using the prompt transformer
78const transformedPrompt = await transformPrompt(receivedEmail.text, openaiUrl, openaiKey, model);
7980// Step 3: Process different types of content
81const { pdfTexts, imageAnalysis } = await processAttachments(attachments, openaiKey, transformedPrompt);
82const websiteMarkdown = await extractWebsiteMarkdown(links, mdApiKey);
8391);
9293// Step 5: Send to OpenAI and get response
94const openaiResponse = await sendRequestToOpenAI(finalPrompt, transformedPrompt, openaiUrl, openaiKey, model);
9596// Step 6: Send response email
97await sendResponseByEmail(receivedEmail.from, openaiResponse);
98console.log("Response email sent.");
99}
179async function analyzeImage(imageAttachment, apiKey, transformedPrompt) {
180try {
181const response = await fetch("https://api.openai.com/v1/chat/completions", {
182method: "POST",
183headers: {
221222// Transform the original prompt using the prompt transformer
223async function transformPrompt(emailText, openaiUrl, apiKey, model) {
224const promptTransformerText =
225`You are an AI assistant tasked with transforming user queries into structured research or information requests. Your goal is to take a simple query and expand it into a comprehensive research objective with specific formatting requirements.
290};
291292const response = await fetch(openaiUrl, {
293method: "POST",
294body: JSON.stringify(body),
301const data = await response.json();
302if (data.error) {
303throw new Error(`OpenAI API Error: ${data.error.message}`);
304}
305return data.choices[0]?.message?.content || emailText;
387}
388389// Helper function to send a request to OpenAI
390async function sendRequestToOpenAI(prompt, transformedPrompt, openaiUrl, apiKey, model) {
391try {
392// Debug logging for the prompt and transformed prompt
396console.log(prompt);
397398// Prepare the OpenAI messages payload
399const messages = [
400{
417};
418419// Send the request to OpenAI
420const response = await fetch(openaiUrl, {
421method: "POST",
422body: JSON.stringify(body),
430const data = await response.json();
431if (data.error) {
432throw new Error(`OpenAI API Error: ${data.error.message}`);
433}
434return data.choices[0]?.message?.content || "No response from OpenAI.";
435} catch (err) {
436console.error("Error in sendRequestToOpenAI:", err);
437return "Error processing your request. Please try again later.";
438}
emailValHandlerNomain.tsx22 matches
20212. Set up the required environment variable:
22- OPENAI_API_KEY: Your OpenAI API key
2324Optional environment variable:
62// Main controller function
63export default async function emailValHandler(receivedEmail) {
64const openaiUrl = "https://api.openai.com/v1/chat/completions";
65const openaiKey = Deno.env.get("OPENAI_API_KEY");
66const mdApiKey = Deno.env.get("MD_API_KEY");
67const model = "o1-2024-12-17";
6869if (!openaiKey) {
70throw new Error("OPENAI_KEY environment variable is not set.");
71}
727879// Step 2: Transform the email prompt using the prompt transformer
80const transformedPrompt = await transformPrompt(receivedEmail.text, openaiUrl, openaiKey, model);
8182// Step 3: Process different types of content
83const { pdfTexts, imageAnalysis } = await processAttachments(attachments, openaiKey, transformedPrompt);
84const websiteMarkdown = await extractWebsiteMarkdown(links, mdApiKey);
8593);
9495// Step 5: Send to OpenAI and get response
96const openaiResponse = await sendRequestToOpenAI(finalPrompt, transformedPrompt, openaiUrl, openaiKey, model);
9798// Step 6: Send response email
99await sendResponseByEmail(receivedEmail.from, openaiResponse);
100console.log("Response email sent.");
101}
172async function analyzeImage(imageAttachment, apiKey, transformedPrompt) {
173try {
174const response = await fetch("https://api.openai.com/v1/chat/completions", {
175method: "POST",
176headers: {
214215// Transform the original prompt using the prompt transformer
216async function transformPrompt(emailText, openaiUrl, apiKey, model) {
217const promptTransformerText =
218`You are an AI assistant tasked with transforming user queries into structured research or information requests. Your goal is to take a simple query and expand it into a comprehensive research objective with specific formatting requirements.
283};
284285const response = await fetch(openaiUrl, {
286method: "POST",
287body: JSON.stringify(body),
294const data = await response.json();
295if (data.error) {
296throw new Error(`OpenAI API Error: ${data.error.message}`);
297}
298return data.choices[0]?.message?.content || emailText;
382}
383384// Helper function to send a request to OpenAI
385async function sendRequestToOpenAI(prompt, transformedPrompt, openaiUrl, apiKey, model) {
386try {
387// Debug logging for the prompt and transformed prompt
391console.log(prompt);
392393// Prepare the OpenAI messages payload
394const messages = [
395{
412};
413414// Send the request to OpenAI
415const response = await fetch(openaiUrl, {
416method: "POST",
417body: JSON.stringify(body),
425const data = await response.json();
426if (data.error) {
427throw new Error(`OpenAI API Error: ${data.error.message}`);
428}
429return data.choices[0]?.message?.content || "No response from OpenAI.";
430} catch (err) {
431console.error("Error in sendRequestToOpenAI:", err);
432return "Error processing your request. Please try again later.";
433}
emailValHandlerNoREADME.md1 match
19202. Set up the required environment variables:
21- OPENAI_API_KEY: Your OpenAI API key
22- MD_API_KEY: Your API key for the markdown extraction service
23
preciseScarletHerringmain.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",
preciseScarletHerringREADME.md2 matches
35Use Cerebras for AI on the backend like so:
3637const { OpenAI } = await import("https://esm.sh/openai");
38const client = new OpenAI({
39apiKey: "YOUR_CEREBRAS_API_KEY",
40baseURL: "https://api.cerebras.ai/v1"
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",