Search

3,375 results found for openai (5503ms)

Code
3,280

// @ts-ignore
import { 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) th
*/
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 in
lls are sent to `gpt-4o-mini`](https://www.val.town/v/std/openaiproxy?v=12#L85).
## Basic Usage
```ts title="Example" val
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" },
## Limits
While our wrapper simplifies the integration of OpenAI, there are a few limitations to keep in m
* **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 o
1. Create your own API key on [OpenAI's website](https://platform.openai.com/api-keys)
l.town/settings/environment-variables?adding=true) named `OPENAI_API_KEY`
3. Use the `OpenAI` client from `npm:openai`:
```ts title="Example" val
import { OpenAI } from "npm:openai";
const openai = new OpenAI();
```
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) th
*/
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,
};
}
join/text/main.tsx
6 matches
// @ts-ignore
import { 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 Proxy
This OpenAI API proxy injects Val Town's API keys. For usage documentation, check out https://ww
Migrated 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-ignore
import { 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: [