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)
cerebrasTemplateREADME.md2 matches
35Use Cerebras for AI on the backend like so:
3637const { OpenAI } = await import("https://esm.sh/openai");
38const client = new OpenAI({
39apiKey: Deno.env.get("CEREBRAS_API_KEY"),
40baseURL: "https://api.cerebras.ai/v1"
emailValHandlerREADME.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 (optional)
23
ALPHANUMERIC_TEXT_TOO_LONGmain.tsx3 matches
71if (request.method === "POST" && new URL(request.url).pathname === "/chat") {
72try {
73const { OpenAI } = await import("https://esm.town/v/std/openai");
74const openai = new OpenAI();
7576const { messages } = await request.json();
7778const completion = await openai.chat.completions.create({
79messages: messages,
80model: "gpt-4o-mini",
aiEmailAssistantmain.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_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}
aiEmailAssistantREADME.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)
openaiproxyREADME.md2 matches
1# OpenAI Proxy
23This OpenAI API proxy injects Val Town's API keys. For usage documentation, check out https://www.val.town/v/std/openai
45# Usage Note
openaiproxymain.tsx8 matches
1import { parseBearerString } from "https://esm.town/v/andreterron/parseBearerString";
2import { API_URL } from "https://esm.town/v/std/API_URL?v=5";
3import { OpenAIUsage } from "https://esm.town/v/std/OpenAIUsage";
4import { RateLimit } from "npm:@rlimit/http";
5const client = new OpenAIUsage();
67const allowedPathnames = [
4243// Proxy the request
44const url = new URL("." + pathname, "https://api.openai.com");
45url.search = search;
4647const headers = new Headers(req.headers);
48headers.set("Host", url.hostname);
49headers.set("Authorization", `Bearer ${Deno.env.get("OPENAI_API_KEY")}`);
50headers.set("OpenAI-Organization", Deno.env.get("OPENAI_API_ORG"));
5152const modifiedBody = await limitFreeModel(req, user);
63});
6465const openAIRes = await fetch(url, {
66method: req.method,
67headers,
7172// Remove internal header
73const res = new Response(openAIRes.body, openAIRes);
74res.headers.delete("openai-organization");
75return res;
76}