Search
Code3,280
// @ts-ignoreimport { OpenAI } from "https://esm.town/v/std/openai?v=4";// --- AI BEHAVIORAL GUIDELINES --- if (req.method === "POST") { try { const openai = new OpenAI(); const body = await req.json(); const completion = await openai.chat.completions.create({ model: "gpt-4o", messages: [
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
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/v3", apiKey: Deno.env.get("OPENAI_API_KEY"), organization: null, }); get chat() { return this.rawOpenAIClient.chat; } get beta() { return { chat: this.rawOpenAIClient.beta.chat, }; }
// @ts-ignoreimport { blob } from "https://esm.town/v/std/blob?v=11";import { OpenAI } from "https://esm.town/v/std/openai?v=4";// --- AI PERSONA & GAME RULES ---// --- BACKEND LOGIC ---async function callOpenAI(history: any[]) { const openai = new OpenAI(); const completion = await openai.chat.completions.create({ model: "gpt-4o", messages: [ sessionId = crypto.randomUUID(); const userMessage = "The Operator has connected. Begin the story."; const assistantResponse = await callOpenAI([{ role: "user", content: userMessage }]); gameState = { sessionId, gameState.history.push({ role: "user", content: userMessage }); const assistantResponse = await callOpenAI(gameState.history); gameState.history.push({ role: "assistant", content: JSON.stringify(assistantResponse) }); gameState.lastUpdated = new Date().toISOString();
import { 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" },
# OpenAI ProxyThis OpenAI API proxy injects Val Town's API keys. For usage documentation, check out https://www.val.town/v/std/openaiMigrated from folder: openai/openaiproxy
import { parseBearerString } from "https://esm.town/v/andreterron/parseBearerString";import { API_URL } from "https://esm.town/v/std/API_URL?v=5";import { OpenAIUsage } from "https://esm.town/v/std/OpenAIUsage";import { RateLimit } from "npm:@rlimit/http";const client = new OpenAIUsage();const allowedPathnames = [ // Proxy the request const url = new URL("." + pathname, "https://api.openai.com"); url.search = search; const headers = new Headers(req.headers); headers.set("Host", url.hostname); headers.set("Authorization", `Bearer ${Deno.env.get("OPENAI_API_KEY")}`); headers.set("OpenAI-Organization", Deno.env.get("OPENAI_API_ORG")); const modifiedBody = await limitFreeModel(req, user); }); const openAIRes = await fetch(url, { method: req.method, headers, // Remove internal header const res = new Response(openAIRes.body, openAIRes); res.headers.delete("openai-organization"); return res;}
// @ts-ignoreimport { OpenAI } from "https://esm.town/v/std/openai?v=4";const AI_PROMPT_STYLE_GUIDE = if (req.method === "POST") { try { const openai = new OpenAI(); const { prompt } = await req.json(); const stream = await openai.chat.completions.create({ model: "gpt-4o", messages: [
import { OpenAI } from "https://esm.town/v/std/openai";import { Hono } from "npm:hono@4.4.12";app.post("/ai", async (c) => { try { const openai = new OpenAI(); const body = await c.req.json(); if (body.action === "getIdeas") { const completion = await openai.chat.completions.create({ model: "gpt-4o", messages: [ if (body.action === "explainIdea") { const completion = await openai.chat.completions.create({ model: "gpt-4o-mini", messages: [
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