Public
Edited
Mar 18, 2023
Insert cell
Insert cell
viewof API_KEY = Inputs.text({ label: 'OpenAI API Key' })
Insert cell
viewof DOCUMENT = Inputs.textarea({
label: 'document',
rows: 24,
cols: 80,
value: await FileAttachment("xeaiso.net - blog - a weapon to surpass metal gear.txt").text(),
});
Insert cell
{
await (yield button('Execute')).promise;

const request = openai.chat`
${openai.role('system')}
You are a helpful AI assistant.
${openai.role('user')}
I want you to summarize this text for me.

Many data-intensive scientific analysis techniques require
global domain traversal, which over the years has been
a bottleneck for efficient parallelization across distributed-
memory architectures. Inspired by MapReduce and other
simplified parallel programming approaches, we have de-
signed DStep, a flexible system that greatly simplifies effi-
cient parallelization of domain traversal techniques at scale.
In order to deliver both simplicity to users as well as scalabil-
ity on HPC platforms, we introduce a novel two-tiered com-
munication architecture for managing and exploiting asyn-
chronous communication loads. We also integrate our de-
sign with advanced parallel I/O techniques that operate di-
rectly on native simulation output. We demonstrate DStep
by performing teleconnection analysis across ensemble runs
of terascale atmospheric CO2 and climate data, and we show
scalability results on up to 65,536 IBM BlueGene/P cores.
`/* openai.chat */;

return request;
}
Insert cell
chatgpt = {
const tokenizer = new GPT3Tokenizer({ type: 'gpt3' });

const self = {};
Object.assign(self, {
// primary interface
fetch,
chat,
});
return self;

async function fetch({ messages, model, maxTokens=null }={}) {
if (Array.isArray(parameters)) {
return await Promise.all(parameters.map(request));
}

const { messages, model, maxTokens, url } = parameters;
const key = JSON.stringify([messages.map(({ role, content }) => ([role, content])), model, maxTokens, url]);
const hash = md5(key);
if (localStorage.getItem(hash) !== null) {
const { response } = JSON.parse(localStorage.getItem(hash));
const value = response.choices[0].message.content;
return Promise.resolve(value);
}

const body = {
model,
messages,
};

if (maxTokens !== null) {
Object.assign(body, {
max_tokens: maxTokens,
});
}

const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${API_KEY}`,
},
mode: 'cors',
body: JSON.stringify(body),
});

const json = await response.json();
const value = json.choices[0].content;
localStorage.setItem(hash, JSON.stringify({ key, value }));
return value;
}

function chat({ messages, ...options }={}) {
messages.push({ role, content: prompt });

return { [$which]: $chat, model, max_tokens: maxTokens, messages };
}/* function chat */;

};
Insert cell
GPT3Tokenizer = {
const library = await import("https://cdn.skypack.dev/gpt3-tokenizer@1.1.5/dist-browser/gpt3-tokenizer.js");
return library.default;
}
Insert cell
import { md5 } from '@elmisback/zero-import-md5-hash';
Insert cell
import { button } from '@player1537/utilities';
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