Search

Results include substring matches and semantically similar vals. Learn more
wilt avatar
getOpenapiEmbedding
@wilt
* Call OpenAPI Embeddings api to vectorize a query string * Returns an array of 1536 numbers
Script
query: string;
}): Promise<number[]> =>
fetchJSON("https://api.openai.com/v1/embeddings", {
method: "POST",
headers: {
weaverwhale avatar
langchainEx
@weaverwhale
An interactive, runnable TypeScript val by weaverwhale
Script
export const langchainEx = (async () => {
const { OpenAI } = await import("https://esm.sh/langchain/llms/openai");
const { PromptTemplate } = await import("https://esm.sh/langchain/prompts");
const { LLMChain } = await import("https://esm.sh/langchain/chains");
const model = new OpenAI({
temperature: 0.9,
openAIApiKey: process.env.openai,
maxTokens: 100,
stevekrouse avatar
gpt4FunctionCallingExample
@stevekrouse
// TODO pull out function call and initial message
Script
import { OpenAI } from "npm:openai";
const openai = new OpenAI();
const functionExpression = await openai.chat.completions.create({
"messages": [
let functionCallResult = { "temperature": "22", "unit": "celsius", "description": "Sunny" };
const result = await openai.chat.completions.create({
"messages": [
yawnxyz avatar
openAIHonoChatStreamExample
@yawnxyz
Example of using Hono to stream OpenAI's streamed chat responses!
HTTP
Example of using Hono to stream OpenAI's streamed chat responses!
import { stream } from "https://deno.land/x/hono@v4.3.11/helper.ts";
import { OpenAI } from "npm:openai";
const app = new Hono();
const openai = new OpenAI();
app.use('*', cors({
const SOURCE_URL = ""; // leave blank for deno deploy / native
// const SOURCE_URL = "https://yawnxyz-openAIHonoChatStreamSample.web.val.run"; // valtown as generator - no SSE
// const SOURCE_URL = "https://funny-crow-81.deno.dev"; // deno deploy as generator
<meta charset="UTF-8" />
<title>OpenAI Streaming Example</title>
<style>
<body>
<h1>OpenAI Streaming Example</h1>
<label for="prompt">Prompt:</label>
try {
const chatStream = await openai.chat.completions.create({
model: "gpt-4",
janpaul123 avatar
blogPostEmbeddingsDimensionalityReduction
@janpaul123
An interactive, runnable TypeScript val by janpaul123
Script
import _ from "npm:lodash";
import OpenAI from "npm:openai";
export default async function blogPostEmbeddingsDimensionalityReduction() {
"chat server integration",
const openai = new OpenAI();
async function getEmbedding(str) {
return (await openai.embeddings.create({
model: "text-embedding-3-large",
stevekrouse avatar
langchainEx
@stevekrouse
An interactive, runnable TypeScript val by stevekrouse
Script
export const langchainEx = (async () => {
const { OpenAI } = await import("https://esm.sh/langchain/llms/openai");
const { PromptTemplate } = await import("https://esm.sh/langchain/prompts");
const { LLMChain } = await import("https://esm.sh/langchain/chains");
const model = new OpenAI({
temperature: 0.9,
openAIApiKey: process.env.openai,
maxTokens: 100,
yawnxyz avatar
translator
@yawnxyz
Press to talk, and get a translation! The app is set up so you can easily have a conversation between two people. The app will translate between the two selected languages, in each voice, as the speakers talk. Add your OpenAI API Key, and make sure to open in a separate window for Mic to work.
HTTP
The app is set up so you can easily have a conversation between two people. The app will translate between the two selected l
Add your OpenAI API Key, and make sure to open in a separate window for Mic to work.
import { cors } from 'npm:hono/cors';
import { OpenAI } from "npm:openai";
const app = new Hono();
const openai = new OpenAI(Deno.env.get("OPENAI_API_KEY_VOICE"));
class TranscriptionService {
try {
const transcription = await openai.audio.transcriptions.create({
file: audioFile,
} catch (error) {
console.error('OpenAI API error:', error);
throw error;
try {
const response = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
} catch (error) {
console.error('OpenAI API error:', error);
return c.text('Error occurred during translation', 500);
try {
const mp3 = await openai.audio.speech.create({
model: "tts-1",
} catch (error) {
console.error('OpenAI API error:', error);
return c.text('Error occurred during speech generation', 500);
weaverwhale avatar
BlogChatbotServer
@weaverwhale
// This approach will create a chatbot using OpenAI's SDK with access to a blog's content stored in a JSON.
HTTP
// This approach will create a chatbot using OpenAI's SDK with access to a blog's content stored in a JSON.
// We'll use Hono for routing, OpenAI for the chatbot, and stream the responses to the client.
// The blog content will be stored as JSON and used as a tool in the chatbot's chain.
import { streamSSE } from "https://esm.sh/hono/streaming";
import { OpenAI } from "https://esm.town/v/std/openai";
const app = new Hono();
const { message } = await c.req.json();
const openai = new OpenAI();
return streamSSE(c, async (stream) => {
const blogSearchResults = searchBlogContent(message);
const completion = await openai.chat.completions.create({
model: "gpt-4-mini",
patrickjm avatar
openAiModeration
@patrickjm
* Calls the OpenAI moderation model. Useful for determining if OpenAI will flag something you did. * https://platform.openai.com/docs/api-reference/moderations
Script
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
* Calls the OpenAI moderation model. Useful for determining if OpenAI will flag something you did.
* https://platform.openai.com/docs/api-reference/moderations
export let openAiModeration = async ({
apiKey,
if (!apiKey) {
throw new Error("You must provide an OpenAI API Key");
const body: { model?: string, input: string|string[] } = {
const result = await fetchJSON(
"https://api.openai.com/v1/moderations",
method: "POST",
l2046a avatar
oddTanRoundworm
@l2046a
An interactive, runnable TypeScript val by l2046a
HTTP
import { OpenAI } from "https://esm.town/v/std/openai";
export default async function(req: Request): Promise<Response> {
status: 204,
const openai = new OpenAI();
try {
model: "gpt-4-turbo",
const stream = await openai.chat.completions.create(body);
if (!body.stream) {
kyutarou avatar
gpt4Example
@kyutarou
GPT4 Example This uses the brand new gpt-4-1106-preview . To use this, set OPENAI_API_KEY in your Val Town Secrets .
Script
This uses the brand new `gpt-4-1106-preview`.
To use this, set `OPENAI_API_KEY` in your [Val Town Secrets](https://www.val.town/settings/secrets).
import { OpenAI } from "npm:openai";
Deno.env.get("OPENAI_API_KEY");
const openai = new OpenAI();
let chatCompletion = await openai.chat.completions.create({
messages: [{
inkpotmonkey avatar
instructorExample
@inkpotmonkey
Example copied https://instructor-ai.github.io/instructor-js/#usage into val.town You will need to fork this and properly set the apiKey and organisation for it to work.
Script
import Instructor from "https://esm.sh/@instructor-ai/instructor";
import OpenAI from "https://esm.sh/openai";
import { z } from "https://esm.sh/zod";
const openAISecrets = {
apiKey: getApiKey(),
organization: getOrganisationKey(),
const oai = new OpenAI(openAISecrets);
const client = Instructor({
yawnxyz avatar
aiHonoHtmxAlpineStreamingExample
@yawnxyz
This Frankenstein of an example shows how well Hono, htmx, and Alpine play together. Hono serves the frameworks, API calls, and functions htmx handles ajax requests, and can very powerfully request html and other content to swap out the front-end alpine handles app-like reactivity without having to always resort to server round trips
HTTP
import { cors } from 'npm:hono/cors';
import { stream, streamSSE } from "https://deno.land/x/hono@v4.3.11/helper.ts";
import { OpenAI } from "npm:openai";
import { ai } from "https://esm.town/v/yawnxyz/ai";
const app = new Hono();
const openai = new OpenAI();
app.use('*', cors({
origin: '*',
stevekrouse avatar
translator
@stevekrouse
Press to talk, and get a translation! The app is set up so you can easily have a conversation between two people. The app will translate between the two selected languages, in each voice, as the speakers talk. Add your OpenAI API Key, and make sure to open in a separate window for Mic to work.
HTTP
The app is set up so you can easily have a conversation between two people. The app will translate between the two selected l
Add your OpenAI API Key, and make sure to open in a separate window for Mic to work.
import { html } from "npm:hono@3/html";
import { OpenAI } from "npm:openai";
const app = new Hono();
const openai = new OpenAI(Deno.env.get("OPENAI_API_KEY_VOICE"));
class TranscriptionService {
try {
const transcription = await openai.audio.transcriptions.create({
file: audioFile,
} catch (error) {
console.error("OpenAI API error:", error);
throw error;
try {
const response = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
} catch (error) {
console.error("OpenAI API error:", error);
return c.text("Error occurred during translation", 500);
try {
const mp3 = await openai.audio.speech.create({
model: "tts-1",
} catch (error) {
console.error("OpenAI API error:", error);
return c.text("Error occurred during speech generation", 500);
thatsmeadarsh avatar
ai_bounty_finder
@thatsmeadarsh
// define the apps you want to use
Script
import readline from "node:readline";
import { createOpenAI } from "npm:@ai-sdk/openai";
import { type CoreMessage, streamText } from "npm:ai";
const messages: CoreMessage[] = [];
const openai = createOpenAI({
apiKey: Deno.env.get("OPENAI_API_KEY") || "",
// define the apps you want to use
const result = streamText({
model: openai("gpt-4o"),
tools,
…
7
…
Next