1import { fetch } from "https://esm.town/v/std/fetch";
2
3export let gpt3 = async (prompt: string, openAiApiKey: string): Promise<string> => {
4 if (!prompt || !openAiApiKey) {
5 let cat = await fetch("https://catfact.ninja/fact");
6 let { fact } = await cat.json();
9 );
10 }
11 const content = await fetch("https://api.openai.com/v1/chat/completions", {
12 method: "POST",
13 body: JSON.stringify({
17 }),
18 headers: {
19 "Authorization": `Bearer ${openAiApiKey}`,
20 "Content-Type": "application/json",
21 },
2
3export const streamingTest = (async () => {
4 const { OpenAI } = await import("https://esm.sh/langchain/llms/openai");
5 // To enable streaming, we pass in `streaming: true` to the LLM constructor.
6 // Additionally, we pass in a handler for the `handleLLMNewToken` event.
7 const chat = new OpenAI({
8 maxTokens: 25,
9 streaming: true,
10 openAIApiKey: process.env.OPENAI_API_KEY,
11 });
12 const response = await chat.call("Tell me a joke.", undefined, [
50 `;
51 console.log({ prompt });
52 const response = await fetch("https://api.openai.com/v1/completions", {
53 method: "POST",
54 headers: {
55 "Content-Type": "application/json",
56 "Authorization": "Bearer " + process.env.OPENAI_API_KEY, // Replace with your OpenAI API Key
57 },
58 body: JSON.stringify({
1import { openAiFreeUsage } from "https://esm.town/v/patrickjm/openAiFreeUsage";
2
3export let openAiFreeQuotaExceeded = () =>
4 openAiFreeUsage.exceeded;
2import { runVal } from "https://esm.town/v/std/runVal";
3
4export let demoOpenAIGPT4Summary = await runVal(
5 "zzz.OpenAISummary",
6 confession,
7);
1import { Tokenizer } from "https://esm.town/v/zzz/Tokenizer";
2
3// Demo of tokenizer to mimic behavior of https://platform.openai.com/tokenizer
4// Tokenizer uses "gpt-3.5-turbo" model by default but this demo uses davinci to match the playground
5export const TokenizerDemo = (async () => {
4export let weatherTomorrowGpt3Example = weatherTomorrowGpt3({
5 city: "New York City",
6 openAiKey: process.env.openai_key,
7});
2
3/**
4 * Calls the OpenAI moderation model. Useful for determining if OpenAI will flag something you did.
5 * https://platform.openai.com/docs/api-reference/moderations
6 */
7export let openAiModeration = async ({
8 apiKey,
9 input,
15}) => {
16 if (!apiKey) {
17 throw new Error("You must provide an OpenAI API Key");
18 }
19 const body: { model?: string, input: string|string[] } = {
24 }
25 const result = await fetchJSON(
26 "https://api.openai.com/v1/moderations",
27 {
28 method: "POST",
2import { simpleWeather } from "https://esm.town/v/patrickjm/simpleWeather";
3
4export let weatherTomorrowGpt3 = (params: { openAiKey: string, city: string }) =>
5 simpleWeather(params.city).then((weather) =>
6 gpt3({
7 openAiKey: params.openAiKey,
8 prompt: `
9 Given a JSON sequence, give a short, plain-English summary about the weather tomorrow.
6 options = {},
7) => {
8 // Initialize OpenAI API stub
9 const { Configuration, OpenAIApi } = await import("https://esm.sh/openai");
10 const configuration = new Configuration({
11 apiKey: process.env.openAIAPI,
12 });
13 const openai = new OpenAIApi(configuration);
14 // Request chat completion
15 const messages = typeof prompt === "string"
16 ? [{ role: "user", content: prompt }]
17 : prompt;
18 const { data } = await openai.createChatCompletion({
19 model: "gpt-3.5-turbo-0613",
20 messages,