107 // checkManualProfanityList is optional and defaults to false; it checks for the words in lang.ts (if under 50 words) before hitting the AI model. Note that this affects performance.
108 checkManualProfanityList: false,
109 // provider defaults to "google-perspective-api" (Google's Perspective API); it can also be "openai" (OpenAI Moderation API) or "google-natural-language-api" (Google's Natural Language API)
110 provider: "google-perspective-api",
111 };
1import { embed, embedMany } from "npm:ai";
2import { openai } from "npm:@ai-sdk/openai";
3import lunr from "https://cdn.skypack.dev/lunr";
4
8 console.log(`Getting embedding for: ${text}`);
9 const { embedding } = await embed({
10 model: openai.embedding('text-embedding-3-small'),
11 value: text,
12 });
19 console.log(`Getting embeddings for texts: ${texts}`);
20 const { embeddings } = await embedMany({
21 model: openai.embedding('text-embedding-3-small'),
22 values: texts,
23 });
1In-memory semantic search; load it up with valtown KV.
2
3This is a "dumb" version of vector search, for prototyping RAG responses and UIs — with both regular search (w/ Lunr) and vector search (with OpenAI embeddings + cosine similarity)
4
5Usage:
1This is an example of in-memory search, using a combination of lunr, OpenAI embeddings, and cosine similarity
2
3Migrated from folder: Libraries/SemanticSearch/embeddingsSearchExample
57 const characterModel = new ModelProvider({
58 id: `${characterName}`,
59 // for web search / tool use, use openai
60 provider: 'openai',
61 model: 'gpt-4o',
62
3import { modifyImage } from "https://esm.town/v/stevekrouse/modifyImage";
4import { Hono } from "npm:hono@3";
5import { OpenAI } from "npm:openai";
6
7const openai = new OpenAI();
8
9function esmTown(url) {
95 const dataURL = await fileToDataURL(file);
96 try {
97 const response = await openai.chat.completions.create({
98 messages: [
99 {
6
7```
8<a href="https://www.val.town/settings/environment-variables?name=OpenAI&value=sk-123...">
9 Add OpenAI key to Val Town
10</a>
11```
1import { email } from "https://esm.town/v/std/email?v=11";
2import { OpenAI } from "npm:openai";
3import { createHeaders, PORTKEY_GATEWAY_URL } from "npm:portkey-ai";
4
9).then(r => r.json());
10
11const openai = new OpenAI({
12 baseURL: PORTKEY_GATEWAY_URL,
13 defaultHeaders: createHeaders({
14 apiKey: Deno.env.get("PORTKEY_API_KEY"),
15 virtualKey: Deno.env.get("PORTKEY_OPENAI_VIRTUAL_KEY"),
16 }),
17});
18let chatCompletion = await openai.chat.completions.create({
19 messages: [{
20 role: "user",
2import { z } from "npm:zod";
3
4// Function to handle image and text input using OpenAI's GPT-4-turbo
5async function handleImageChat() {
6 const initialMessages = [
13 const response = await modelProvider.gen({
14 model: "gpt-4-turbo",
15 provider: "openai",
16 messages: [
17 ...initialMessages,
1import { OpenAI } from "https://esm.town/v/std/openai";
2
3const openai = new OpenAI();
4const completion = await openai.chat.completions.create({
5 "messages": [
6 {