Search
Code3,282
from openai import OpenAIimport osclient = OpenAI( api_key=os.environ.get( "OPENAI_API_KEY", "<your OpenAI API key if not set as env var>" ))
import { type ClientOptions, OpenAI as RawOpenAI } from "npm:openai";/** * API Client for interfacing with the OpenAI API. Uses Val Town credentials. */export class OpenAI { private rawOpenAIClient: RawOpenAI; /** * API Client for interfacing with the OpenAI API. Uses Val Town credentials. * * @param {number} [opts.timeout=10 minutes] - The maximum amount of time (in milliseconds) the client will wait for a response before timing out. */ constructor(options: Omit<ClientOptions, "baseURL" | "apiKey" | "organization"> = {}) { this.rawOpenAIClient = new RawOpenAI({ ...options, baseURL: "https://std-openaiproxy.web.val.run/v1", apiKey: Deno.env.get("valtown"), organization: null, get chat() { return this.rawOpenAIClient.chat; } get beta() { return { chat: this.rawOpenAIClient.beta.chat, }; }
# OpenAI - [Docs ↗](https://docs.val.town/std/openai)Use 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.For free Val Town users, [all calls are sent to `gpt-4o-mini`](https://www.val.town/v/std/openaiproxy?v=12#L85).## Basic Usage```ts title="Example" valimport { OpenAI } from "https://esm.town/v/std/openai";const openai = new OpenAI();const completion = await openai.chat.completions.create({ messages: [ { role: "user", content: "Say hello in a creative way" },## LimitsWhile our wrapper simplifies the integration of OpenAI, there are a few limitations to keep in mind:* **Usage Quota**: We limit each user to 10 requests per minute.If these limits are too low, let us know! You can also get around the limitation by using your own keys:1. Create your own API key on [OpenAI's website](https://platform.openai.com/api-keys)2. Create an [environment variable](https://www.val.town/settings/environment-variables?adding=true) named `OPENAI_API_KEY`3. Use the `OpenAI` client from `npm:openai`:```ts title="Example" valimport { OpenAI } from "npm:openai";const openai = new OpenAI();```[📝 Edit docs](https://github.com/val-town/val-town-docs/edit/main/src/content/docs/std/openai.mdx)Migrated from folder: openai/openai
# OpenAI - [Docs ↗](https://docs.val.town/std/openai)Use 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.For free Val Town users, [all calls are sent to `gpt-4o-mini`](https://www.val.town/v/std/openaiproxy?v=12#L85).## Basic Usage```ts title="Example" valimport { OpenAI } from "https://esm.town/v/std/openai";const openai = new OpenAI();const completion = await openai.chat.completions.create({ messages: [ { role: "user", content: "Say hello in a creative way" },## LimitsWhile our wrapper simplifies the integration of OpenAI, there are a few limitations to keep in mind:* **Usage Quota**: We limit each user to 10 requests per minute.If these limits are too low, let us know! You can also get around the limitation by using your own keys:1. Create your own API key on [OpenAI's website](https://platform.openai.com/api-keys)2. Create an [environment variable](https://www.val.town/settings/environment-variables?adding=true) named `OPENAI_API_KEY`3. Use the `OpenAI` client from `npm:openai`:```ts title="Example" valimport { OpenAI } from "npm:openai";const openai = new OpenAI();```[📝 Edit docs](https://github.com/val-town/val-town-docs/edit/main/src/content/docs/std/openai.mdx)Migrated from folder: openai/openai
import { type ClientOptions, OpenAI as RawOpenAI } from "npm:openai";/** * API Client for interfacing with the OpenAI API. Uses Val Town credentials. */export class OpenAI { private rawOpenAIClient: RawOpenAI; /** * API Client for interfacing with the OpenAI API. Uses Val Town credentials. * * @param {number} [opts.timeout=10 minutes] - The maximum amount of time (in milliseconds) the client will wait for a response before timing out. */ constructor(options: Omit<ClientOptions, "baseURL" | "apiKey" | "organization"> = {}) { this.rawOpenAIClient = new RawOpenAI({ ...options, baseURL: "https://std-openaiproxy.web.val.run/v1", apiKey: Deno.env.get("valtown"), organization: null, get chat() { return this.rawOpenAIClient.chat; } get beta() { return { chat: this.rawOpenAIClient.beta.chat, }; }
import { type ClientOptions, OpenAI as RawOpenAI } from "npm:openai";/** * API Client for interfacing with the OpenAI API. Uses Val Town credentials. */export class OpenAI { private rawOpenAIClient: RawOpenAI; /** * API Client for interfacing with the OpenAI API. Uses Val Town credentials. * * @param {number} [opts.timeout=10 minutes] - The maximum amount of time (in milliseconds) the client will wait for a response before timing out. */ constructor(options: Omit<ClientOptions, "baseURL" | "apiKey" | "organization"> = {}) { this.rawOpenAIClient = new RawOpenAI({ ...options, baseURL: "https://std-openaiproxy.web.val.run/v1", apiKey: Deno.env.get("valtown"), organization: null, get chat() { return this.rawOpenAIClient.chat; } get beta() { return { chat: this.rawOpenAIClient.beta.chat, }; }
import { type ClientOptions, OpenAI as RawOpenAI } from "npm:openai";/** * API Client for interfacing with the OpenAI API. Uses Val Town credentials. */export class OpenAI { private rawOpenAIClient: RawOpenAI; /** * API Client for interfacing with the OpenAI API. Uses Val Town credentials. * * @param {number} [opts.timeout=10 minutes] - The maximum amount of time (in milliseconds) the client will wait for a response before timing out. */ constructor(options: Omit<ClientOptions, "baseURL" | "apiKey" | "organization"> = {}) { this.rawOpenAIClient = new RawOpenAI({ ...options, baseURL: "https://std-openaiproxy.web.val.run/v1", apiKey: Deno.env.get("valtown"), organization: null, get chat() { return this.rawOpenAIClient.chat; } get beta() { return { chat: this.rawOpenAIClient.beta.chat, }; }
import { fetch } from "https://esm.town/v/std/fetch";import { OpenAI } from "https://esm.town/v/std/openai";import { z } from "npm:zod";import { Logger } from "./logger.ts";const llm = async (sysPrompt, userPrompt, log, tid, model = "gpt-4o") => { log("DEBUG", "LLM", `Calling OpenAI for TID ${tid}`); try { const oa = new OpenAI(); const completion = await oa.chat.completions.create({ model, }); const content = completion.choices[0]?.message?.content; if (!content) throw new Error("OpenAI returned no content."); return JSON.parse(content); } catch (err) { log("ERROR", "LLM", `OpenAI API call failed for TID ${tid}`, { error: err.message }); throw new Error(`AI model error: ${err.message}`); }
import { fetch } from "https://esm.town/v/std/fetch";import { OpenAI } from "https://esm.town/v/std/openai";import { z } from "npm:zod";// --- CORE BACKEND LOGIC ---const llm = async (sysPrompt, userPrompt, log, tid, model = "gpt-4o") => { log("DEBUG", "LLM", `Calling OpenAI for TID ${tid}`); try { const oa = new OpenAI(); const completion = await oa.chat.completions.create({ model, }); const content = completion.choices[0]?.message?.content; if (!content) throw new Error("OpenAI returned no content."); return JSON.parse(content); } catch (err) { log("ERROR", "LLM", `OpenAI API call failed for TID ${tid}`, { error: err.message }); throw new Error(`AI model error: ${err.message}`); }
Note: When changing a SQLite table's schema, change the table's name (e.g., add _2 or _3) to create a fresh table.### OpenAI```tsimport { OpenAI } from "https://esm.town/v/std/openai";const openai = new OpenAI();const completion = await openai.chat.completions.create({ messages: [ { role: "user", content: "Say hello in a creative way" },
reconsumeralization
import { OpenAI } from "https://esm.town/v/std/openai";
import { sqlite } from "https://esm.town/v/stevekrouse/sqlite";
/**
* Practical Implementation of Collective Content Intelligence
* Bridging advanced AI with collaborative content creation
*/
exp
kwhinnery_openai
lost1991
import { OpenAI } from "https://esm.town/v/std/openai";
export default async function(req: Request): Promise<Response> {
if (req.method === "OPTIONS") {
return new Response(null, {
headers: {
"Access-Control-Allow-Origin": "*",
No docs found