Public
Edited
Feb 14, 2023
Paused
1 fork
Importers
6 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
import {secret} from "@tmcw/secret"
Insert cell
// The importing notebook can import this notebook with a `with` statement that initializes this value. See the instructions at the top of this notebook.
API_KEY = undefined
Insert cell
viewof openaiApiKey = secret("OPENAI_API_KEY", {
description:
"This is an OpenAI API token whose value is only available to your notebooks.",
submit: "Save OpenAI API key"
})
Insert cell
viewof openaiApiKey
Insert cell
Insert cell
async function completions(prompt, options = {}) {
const defaults = {
extract_singleton: true,
// these override the default API parameters to match the Playground settings:
max_tokens: 256,
temperature: 0.7,
};
const { extract_singleton, ...params } = { ...defaults, ...options };
const data = await fetchOpenAI("completions", {
model: "text-davinci-003",
prompt,
...params
});
const choices = data.choices.map(c => c.text.trim());
return extract_singleton && choices.length === 1
? choices[0]
: choices;
}
Insert cell
async function text_edits(input, options={}) {
const defaults = {extract_singleton: true};
const { extract_singleton, ...params } = { ...defaults, ...options };
const data = await fetchOpenAI("edits", {
input,
model: "text-davinci-edit-001",
instruction: "Fix the spelling mistakes",
...params
});
const choices = data.choices.map(c => c.text.trim());
return extract_singleton && choices.length === 1
? choices[0]
: choices;
}
Insert cell
async function code_edits(instruction, options) {
const defaults = {extract_singleton: true};
const { extract_singleton, ...params } = { ...defaults, ...options };
const data = await fetchOpenAI("edits", {
instruction,
model: "code-davinci-edit-001",
...params
});
const choices = data.choices.map(c => c.text.trim());
return extract_singleton && choices.length === 1
? choices[0]
: choices;
}
Insert cell
async function images(prompt, options) {
const defaults = {extract_singleton: true};
const {extract_singleton, size, ...params} = {...defaults, ...options};
const data = await fetchOpenAI("images/generations", {
prompt,
size: typeof size === 'number' ? `${size}x${size}` : size,
...params
});
const urls = data.data.map(d => d.url);
return extract_singleton && urls.length === 1
? urls[0]
: urls;
}
Insert cell
Insert cell
async function fetchOpenAI(endpoint, parameters) {
const { api_key, ...params } = {
...parameters,
api_key: API_KEY ?? openaiApiKey
};
if (!api_key) {
throw new Error(
"The @osteele/openai-api module must be imported with a `with` statement. See that notebook for usage instructions."
);
}
const response = await fetch(`https://api.openai.com/v1/${endpoint}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${api_key}`
},
body: JSON.stringify(params)
});
const data = await response.json();
if (response.status >= 400) {
throw new Error(data.error.message);
}
return data;
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more