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

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more