79export default async function server(request: Request): Promise<Response> {
80 if (request.method === 'POST' && new URL(request.url).pathname === '/chat') {
81 const { OpenAI } = await import("https://esm.town/v/std/openai");
82 const openai = new OpenAI();
83
84 const body = await request.json();
85 const completion = await openai.chat.completions.create({
86 messages: [
87 {
2import { email } from "https://esm.town/v/std/email";
3import { extractValInfo } from "https://esm.town/v/stevekrouse/extractValInfo";
4import { OpenAI } from "npm:openai";
5
6function stripHtmlBackticks(html: string): string {
9
10export default async function(e: Email) {
11 const openai = new OpenAI();
12 console.log(`from: ${e.from} to: ${e.to} subject: ${e.subject}, cc: ${e.cc}, bcc: ${e.bcc}`);
13
25 }
26
27 const summary = await openai.chat.completions.create({
28 messages: [
29 {
61 },
62 "ChatGPT-User": {
63 "operator": "[OpenAI](https://openai.com)",
64 "respect": "Yes",
65 "function": "Takes action based on user prompts.",
169 },
170 "GPTBot": {
171 "operator": "[OpenAI](https://openai.com)",
172 "respect": "Yes",
173 "function": "Scrapes data to train OpenAI's products.",
174 "frequency": "No information.",
175 "description":
239 },
240 "OAI-SearchBot": {
241 "operator": "[OpenAI](https://openai.com)",
242 "respect": "[Yes](https://platform.openai.com/docs/bots)",
243 "function": "Search result generation.",
244 "frequency": "No information.",
300 { getRandomRegexExplanation, saveRegexExplanation, getRegexExplanationById },
301 ReactMarkdown,
302 { OpenAI },
303 { renderToString },
304 { jsx, jsxs, Fragment },
306 import("https://esm.town/v/stainless_em/brainrotdb"),
307 import("npm:react-markdown@7"),
308 import("https://esm.town/v/std/openai"),
309 import("npm:react-dom@19/server.browser"),
310 import("npm:react@19/jsx-runtime"),
336 }
337
338 const openai = new OpenAI();
339
340 const abortController = new AbortController();
341 const completion = await openai.chat.completions.create({
342 messages: [
343 {
86 />
87 <FeatureItem
88 href="https://www.val.town/v/std/openaiproxy"
89 title="OpenAI Proxy"
90 description="Provide a secure and efficient proxy for OpenAI API calls"
91 />
92 <FeatureItem
1# OpenAI - [Docs ↗](https://docs.val.town/std/openai)
2
3Use OpenAI's chat completion API with [`std/openai`](https://www.val.town/v/std/openai). This integration enables access to OpenAI's language models without needing to acquire API keys.
4
5For free Val Town users, [all calls are sent to `gpt-4o-mini`](https://www.val.town/v/std/openaiproxy?v=12#L85).
6
7## Basic Usage
8
9```ts title="Example" val
10import { OpenAI } from "https://esm.town/v/std/openai";
11
12const openai = new OpenAI();
13
14const completion = await openai.chat.completions.create({
15 messages: [
16 { role: "user", content: "Say hello in a creative way" },
58## Limits
59
60While our wrapper simplifies the integration of OpenAI, there are a few limitations to keep in mind:
61
62* **Usage Quota**: We limit each user to 10 requests per minute.
65If these limits are too low, let us know! You can also get around the limitation by using your own keys:
66
671. Create your own API key on [OpenAI's website](https://platform.openai.com/api-keys)
682. Create an [environment variable](https://www.val.town/settings/environment-variables?adding=true) named `OPENAI_API_KEY`
693. Use the `OpenAI` client from `npm:openai`:
70
71```ts title="Example" val
72import { OpenAI } from "npm:openai";
73
74const openai = new OpenAI();
75```
76
77
78[📝 Edit docs](https://github.com/val-town/val-town-docs/edit/main/src/content/docs/std/openai.mdx)
1import { type ClientOptions, OpenAI as RawOpenAI } from "npm:openai";
2
3/**
4 * API Client for interfacing with the OpenAI API. Uses Val Town credentials.
5 */
6export class OpenAI {
7 private rawOpenAIClient: RawOpenAI;
8
9 /**
10 * API Client for interfacing with the OpenAI API. Uses Val Town credentials.
11 *
12 * @param {number} [opts.timeout=10 minutes] - The maximum amount of time (in milliseconds) the client will wait for a response before timing out.
19 */
20 constructor(options: Omit<ClientOptions, "baseURL" | "apiKey" | "organization"> = {}) {
21 this.rawOpenAIClient = new RawOpenAI({
22 ...options,
23 baseURL: "https://std-openaiproxy.web.val.run/v1",
24 apiKey: Deno.env.get("valtown"),
25 organization: null,
28
29 get chat() {
30 return this.rawOpenAIClient.chat;
31 }
32
33 get beta() {
34 return {
35 chat: this.rawOpenAIClient.beta.chat,
36 };
37 }
434export default async function server(request: Request): Promise<Response> {
435 if (request.method === 'POST' && request.url.includes('/recommendations')) {
436 const { OpenAI } = await import("https://esm.town/v/std/openai");
437 const openai = new OpenAI();
438
439 const { latitude, longitude } = await request.json();
459 `;
460
461 const completion = await openai.chat.completions.create({
462 messages: [{ role: "user", content: prompt }],
463 model: "gpt-4o-mini",
35Use Cerebras for AI on the backend like so:
36
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"
83 const { messages } = await request.json();
84
85 const { 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
89 const client = new OpenAI({
90 apiKey: Deno.env.get("CEREBRAS_API_KEY"),
91 baseURL: "https://api.cerebras.ai/v1",