Search
openaiproxy
@std
OpenAI Proxy This OpenAI API proxy injects Val Town's API keys. For usage documentation, check out https://www.val.town/v/std/openai
HTTP
# OpenAI Proxy
This OpenAI API proxy injects Val Town's API keys. For usage documentation, check out https://www.val.town/v/std/openai
import { OpenAIUsage } from "https://esm.town/v/std/OpenAIUsage";
const client = new OpenAIUsage();
const url = new URL("." + pathname, "https://api.openai.com");
headers.set("Authorization", `Bearer ${Deno.env.get("OPENAI_API_KEY")}`);
headers.set("OpenAI-Organization", Deno.env.get("OPENAI_API_ORG"));
const openAIRes = await fetch(url, {
const res = new Response(openAIRes.body, openAIRes);
res.headers.delete("openai-organization");
openAIStreamingExample
@peterqliu
An interactive, runnable TypeScript val by peterqliu
HTTP
import { OpenAI } from "https://esm.town/v/std/openai";
export default async function(req: Request): Promise<Response> {
const openai = new OpenAI();
const stream = await openai.chat.completions.create({
stream: true,
openai_svg
@stevekrouse
OpenAI SVG Generates an SVG based on the ?text input pararm
HTTP
# OpenAI SVG
Generates an SVG based on the ?text input pararm
import { OpenAI } from "https://esm.town/v/std/openai";
export default async function (req: Request): Promise<Response> {
"Content-Type": "text/html",
// Generate SVG using OpenAI
const openai = new OpenAI();
const completion = await openai.chat.completions.create({
messages: [
openai
@std
OpenAI - Docs ↗ Use OpenAI's chat completion API with 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 . Basic Usage 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" },
],
model: "gpt-4",
max_tokens: 30,
});
console.log(completion.choices[0].message.content); Images To send an image to ChatGPT, the easiest way is by converting it to a
data URL, which is easiest to do with @stevekrouse/fileToDataURL . import { fileToDataURL } from "https://esm.town/v/stevekrouse/fileToDataURL";
const dataURL = await fileToDataURL(file);
const response = await chat([
{
role: "system",
content: `You are an nutritionist.
Estimate the calories.
We only need a VERY ROUGH estimate.
Respond ONLY in a JSON array with values conforming to: {ingredient: string, calories: number}
`,
},
{
role: "user",
content: [{
type: "image_url",
image_url: {
url: dataURL,
},
}],
},
], {
model: "gpt-4o",
max_tokens: 200,
}); Limits While 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. Features : Chat completions is the only endpoint available. If these limits are too low, let us know! You can also get around the limitation by using your own keys: Create your own API key on OpenAI's website Create an environment variable named OPENAI_API_KEY Use the OpenAI client from npm:openai : import { OpenAI } from "npm:openai";
const openai = new OpenAI(); 📝 Edit docs
Script
# 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).
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.
this.rawOpenAIClient = new RawOpenAI({
baseURL: "https://std-openaiproxy.web.val.run/v1",
return this.rawOpenAIClient.chat;
chat: this.rawOpenAIClient.beta.chat,
openai
@stevekrouse
OpenAI ChatGPT helper function This val uses your OpenAI token if you have one, and the @std/openai if not, so it provides limited OpenAI usage for free. import { chat } from "https://esm.town/v/stevekrouse/openai";
const { content } = await chat("Hello, GPT!");
console.log(content); import { chat } from "https://esm.town/v/stevekrouse/openai";
const { content } = await chat(
[
{ role: "system", content: "You are Alan Kay" },
{ role: "user", content: "What is the real computer revolution?"}
],
{ max_tokens: 50, model: "gpt-4o" }
);
console.log(content);
Script
# OpenAI ChatGPT helper function
This val uses your OpenAI token if you have one, and the @std/openai if not, so it provides limited OpenAI usage for free.
import { chat } from "https://esm.town/v/stevekrouse/openai";
import type { ChatCompletion, ChatCompletionCreateParamsNonStreaming, Message } from "npm:@types/openai";
async function getOpenAI() {
if (Deno.env.get("OPENAI_API_KEY") === undefined) {
const { OpenAI } = await import("https://esm.town/v/std/openai");
return new OpenAI();
const { OpenAI } = await import("npm:openai");
return new OpenAI();
* Initiates a chat conversation with OpenAI's GPT model and retrieves the content of the first response.
const openai = await getOpenAI();
const completion = await openai.chat.completions.create(createParams);
OpenAI
@pomdtr
OpenAI Get started using OpenAI's chat completion without the need to set your own API keys. Usage Here's a quick example to get you started with the Val Town OpenAI wrapper: import { OpenAI } from "https://esm.town/v/std/openai";
const openai = new OpenAI();
const functionExpression = await openai.chat.completions.create({
"messages": [
{ "role": "user", "content": "Say hello in a creative way" },
],
model: "gpt-4",
max_tokens: 30,
});
console.log(functionExpression.choices[0].message.content);
Script
# OpenAI
Get started using OpenAI's chat completion without the need to set your own API keys.
Here's a quick example to get you started with the Val Town OpenAI wrapper:
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.
this.rawOpenAIClient = new RawOpenAI({
baseURL: "https://std-openaiproxy.web.val.run/v1",
return this.rawOpenAIClient.chat;
return this.rawOpenAIClient.beta;
openAiHelloWorld
@yawnxyz
// Create a secret named OPENAI_API_KEY at https://www.val.town/settings/environment-variables
Script
import { OpenAI } from "npm:openai";
// Create a secret named OPENAI_API_KEY at https://www.val.town/settings/environment-variables
const openai = new OpenAI();
const functionExpression = await openai.chat.completions.create({
"messages": [
OpenAI
@hash0000ff
OpenAI - Docs ↗ Use OpenAI's chat completion API with 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 . Usage 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" },
],
model: "gpt-4",
max_tokens: 30,
});
console.log(completion.choices[0].message.content); Limits While 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. Features : Chat completions is the only endpoint available. If these limits are too low, let us know! You can also get around the limitation by using your own keys: Create your own API key on OpenAI's website Create an environment variable named OPENAI_API_KEY Use the OpenAI client from npm:openai : import { OpenAI } from "npm:openai";
const openai = new OpenAI(); 📝 Edit docs
Script
# 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).
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.
this.rawOpenAIClient = new RawOpenAI({
baseURL: "https://std-openaiproxy.web.val.run/v1",
return this.rawOpenAIClient.chat;
get chat(): RawOpenAI["beta"]["chat"] {
return this.rawOpenAIClient.beta.chat;
openaiCompletion
@fgeierst
An interactive, runnable TypeScript val by fgeierst
Script
import process from "node:process";
export const openaiCompletion = async (prompt) => {
const { OpenAI } = await import("https://deno.land/x/openai/mod.ts");
const openAI = new OpenAI(process.env.OPENAI_API_KEY);
const completion = openAI.createCompletion({
model: "text-davinci-003",
OpenAI
@pattysi
OpenAI - Docs ↗ Use OpenAI's chat completion API with std/openai . This integration enables access to OpenAI's language models without needing to acquire API keys. Streaming is not yet supported. Upvote the HTTP response streaming feature request if you need it! Usage 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" },
],
model: "gpt-4",
max_tokens: 30,
});
console.log(completion.choices[0].message.content); Limits While 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. Features : Chat completions is the only endpoint available. If these limits are too low, let us know! You can also get around the limitation by using your own keys: Create your own API key on OpenAI's website Create an environment variable named OPENAI_API_KEY Use the OpenAI client from npm:openai : import { OpenAI } from "npm:openai";
const openai = new OpenAI(); 📝 Edit docs
Script
# 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.
import { OpenAI } from "https://esm.town/v/std/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.
this.rawOpenAIClient = new RawOpenAI({
baseURL: "https://std-openaiproxy.web.val.run/v1",
return this.rawOpenAIClient.chat;
get chat(): RawOpenAI["beta"]["chat"] {
return this.rawOpenAIClient.beta.chat;
OpenAI
@wangqiao1234
OpenAI - Docs ↗ Use OpenAI's chat completion API with 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-3.5-turbo . Streaming is not yet supported. Upvote the HTTP response streaming feature request if you need it! Usage 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" },
],
model: "gpt-4",
max_tokens: 30,
});
console.log(completion.choices[0].message.content); Limits While 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. Features : Chat completions is the only endpoint available. If these limits are too low, let us know! You can also get around the limitation by using your own keys: Create your own API key on OpenAI's website Create an environment variable named OPENAI_API_KEY Use the OpenAI client from npm:openai : import { OpenAI } from "npm:openai";
const openai = new OpenAI(); 📝 Edit docs
HTTP
# 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-3.5-turbo`](https://www.val.town/v/std/openaiproxy?v=5#L69).
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.
this.rawOpenAIClient = new RawOpenAI({
baseURL: "https://std-openaiproxy.web.val.run/v1",
return this.rawOpenAIClient.chat;
get chat(): RawOpenAI["beta"]["chat"] {
return this.rawOpenAIClient.beta.chat;
openAiExample
@yawnxyz
// Server-side rendering
HTTP
import { cors } from 'npm:hono/cors';
import { OpenAI } from "npm:openai";
const app = new Hono();
const openai = new OpenAI();
app.use('*', cors({
<head>
<title>OpenAI Prompt Example</title>
<script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
<div class="container mx-auto py-8">
<h1 class="text-4xl font-bold mb-4">OpenAI Prompt Example</h1>
<form action="/prompt" method="GET">
try {
const response = await openai.chat.completions.create({
model: "gpt-4",
} catch (error) {
console.error('OpenAI API error:', error);
return c.redirect('/?response=Error%20occurred.');
openAiProxy
@ashryanio
openAiProxy Overview This val is a proxy server that interacts with the OpenAI API to generate responses based on prompts in the request body. The function handles incoming HTTP POST requests, processes the prompt, and returns a response generated by the LLM. Prerequisites Server-side: (Optional) An active OpenAI API key Client-side: Something that can make POST requests (browser code, Postman, cURL, another Val, etc) Usage Endpoint The primary endpoint for this function is designed to handle HTTP POST requests. Request Method : POST Content-Type : application/json Body : JSON object containing a prompt field (e.g. {"prompt": "Help me make a boat."} ) Example Request curl -X POST https://ashryanio-openaiproxy.web.val.run -H "Content-Type: application/json" -d '{"prompt": "Hello, OpenAI!"}' Response Content-Type : application/json Body : JSON object containing the response from the OpenAI language model. Example Response {
"llmResponse": "Hi there! How can I assist you today?"
} Error Handling 400 Bad Request : Returned if the prompt field is missing in the request body. 405 Method Not Allowed : Returned if any method other than POST or OPTIONS is used. 500 Internal Server Error : Returned if there is an error processing the request.
HTTP
# openAiProxy
## Overview
This val is a proxy server that interacts with the OpenAI API to generate responses based on prompts in the request body. The function handles incoming HTTP POST requests, processes the prompt, and returns a response generated by the LLM.
## Prerequisites
- Server-side: (Optional) An active OpenAI API key
- Client-side: Something that can make POST requests (browser code, Postman, cURL, another Val, etc)
```sh
curl -X POST https://ashryanio-openaiproxy.web.val.run -H "Content-Type: application/json" -d '{"prompt": "Hello, OpenAI!"}'
### Response
- **Content-Type**: application/json
- **Body**: JSON object containing the response from the OpenAI language model.
#### Example Response
import { OpenAI } from "https://esm.town/v/std/openai";
const openai = new OpenAI();
* Handles the incoming request and returns a response.
statusText: "Method Not Allowed",
* Gets the response from the OpenAI language model.
* @param {string} prompt - The prompt for the language model.
async function getLlmResponse(prompt: string) {
const completion = await openai.chat.completions.create({
"messages": [
openaiFineTune
@stevekrouse
An interactive, runnable TypeScript val by stevekrouse
Script
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON";
export function openaiFineTune({ key, model, trainingFile }: {
key: string;
model?: string;
trainingFile: string;
return fetchJSON(
"https://api.openai.com/v1/fine_tuning/jobs",
method: "POST",
body: JSON.stringify({
askAI
@DFB
An interactive, runnable TypeScript val by DFB
Script
import { OpenAI } from "https://deno.land/x/openai@v4.54.0/mod.ts";
const apiKey = Deno.env.get("OPENAI_API_KEY");
const openai = new OpenAI({ apiKey });
export async function askAI(msg: string) {
max_tokens: 3000,
} satisfies OpenAI.ChatCompletionCreateParamsNonStreaming;
const chat = await openai.chat.completions.create(cfg);
return chat.choices?.[0].message.content;