Public
Edited
Apr 30, 2023
1 fork
Insert cell
Insert cell
viewof DOCUMENT = Inputs.textarea({
rows: 24,
submit: true,
});
Insert cell
RESPONSES
Insert cell
viewof RESPONSES = Inputs_abutton(`Execute ${REQUESTS.length} Requests`, {
value: null,
async reduce() {
const responses = [];
for (const request of REQUESTS) {
let response = await fetch(`https://llama.is.mediocreatbest.xyz/v1/chat/completions`, {
method: 'POST',
credentials: 'include',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(request),
});

response = await response.json();

responses.push(response);
}

return responses;
},
});
Insert cell
REQUESTS = GPTetry.evaluate.tag`
local fragments = gpt.Split(gpt.Input, 1024, 64);
local request(i, fragment) =
gpt.Request({
fragment: fragment,
}, [
gpt.System(|||
You are a helpful AI assistant.
|||),
gpt.User(|||
This is some helpful context.

%(fragment)s,
|||),
gpt.User(|||
What is the capital of France?
|||),
]);

local requests = std.mapWithIndex(request, fragments);
requests
``${DOCUMENT}`
Insert cell
GPTetry = {
const GPT3Tokenizer = (await import("https://cdn.skypack.dev/gpt3-tokenizer@1.1.5/dist-browser/gpt3-tokenizer.js")).default;

return {
evaluate: Object.defineProperties(evaluate, {
tag: {
enumerable: true,
value: tag,
},
}),
};

function tag(strings, ...values) {
const filename = `<GPTetry.tag>`;
const code = String.raw(strings, ...values);
return async function(strings, ...values) {
const input = String.raw(strings, ...values);
return await evaluate({
filename,
code,
input,
});
};
}

async function evaluate({
filename,
code,
input='',
...extra
}) {
if (Object.keys(extra).length) throw `Unexpected options: ${Object.keys(extra)}`;

const extStrs = {
__CHAT_INPUT: input,
};

code = `\
local gpt = {
local gpt = self,


_Trim(x)::
local strip(x) = std.stripChars(x, " \n\t");
strip(x),


Input::
std.extVar('__CHAT_INPUT'),


_Split:: std.native('split'),

Split(document, new_tokens, old_tokens=0)::
gpt._Split(document, new_tokens, old_tokens),


_Request2(context, messages):: {
local each(message) = {
role: message.role,
content: gpt._Trim(message.content % (context + message.context)),
},

messages: std.map(each, messages),
},

_Request1(messages)::
gpt.Request({}, messages),

Request::
std.dispatch(gpt, '_Request'),


_Fetch:: std.native('fetch'),

Fetch(request)::
local stringify(x) = std.manifestJsonMinified(x);
gpt._Fetch(stringify(request)),


_Message3(role, context, content):: {
role: role,
content: gpt._Trim(content),
context:: context,
},

_Message2(role, content)::
gpt.Message(role, {}, content),

_Message1(content)::
gpt.Message('user', content),

Message::
std.dispatch(gpt, '_Message'),


_User2(context, content)::
gpt.Message('user', context, content),

_User1(content)::
gpt.Message('user', content),

User::
std.dispatch(gpt, '_User'),


_System2(context, content)::
gpt.Message('system', context, content),

_System1(content)::
gpt.Message('system', content),

System::
std.dispatch(gpt, '_System'),


_Assistant2(context, content)::
gpt.Message('assistant', context, content),

_Assistant1(content)::
gpt.Message('assistant', content),

Assistant::
std.dispatch(gpt, '_Assistant'),
};

${code}`;

return jsonnet.evaluate({
filename,
code,
extStrs,
nativeCallbacks: {
split(document, newTokens, oldTokens) {
newTokens = Number.parseInt(newTokens, 10);
oldTokens = Number.parseInt(oldTokens, 10);

return split(document, { newTokens, oldTokens });
},
},
});
}

function split(document, {
newTokens,
oldTokens=0,
type='gpt3',
}={}) {
const tokenizer = new GPT3Tokenizer({ type });
const { text } = tokenizer.encode(document);

const fragments = [];
for (let i=0, n=text.length; i<n; i += newTokens) {
fragments.push(text.slice(i, i + newTokens + oldTokens).join(''));
}

return fragments;
}
}
Insert cell
jsonnet = {
const { evaluate_snippet: __evaluate } = await LOAD_NATIVE_LIBRARY();

return Object.defineProperties({}, {
__evaluate: {
value: __evaluate,
},
_evaluate: {
value: _evaluate,
},
evaluate: {
enumerable: true,
value: evaluate,
},
});

async function evaluate({
filename,
code,
files={},
extStrs={},
extCodes={},
tlaStrs={},
tlaCodes={},
nativeCallbacks={},
maxIterations=1024,
...extra
}) {
const UNIQUE_SEPARATOR = ':--UNIQUE_SEPARATOR--:';
// code = code.replaceAll(/\bstd\.native\./, `std_native_ex`);
// code = `\
// local std_native_ex(name) =
// local sep = std.extVar('STD_NATIVE_EX_SEPARATOR');
// local func(
extStrs = { ...extStrs, '__STD_UNIQUE_SEPARATOR': UNIQUE_SEPARATOR };
code = `\
local __std = std + {
local std = self,
_unique_separator:: std.extVar('__STD_UNIQUE_SEPARATOR'),
_unset:: 'd209d4c8-2672-4c03-93e5-d084245597a9',
__native(name, args)::
local each(x) = std.toString(x);
local parts = ['', name] + args + [''];
local key = std.join(std._unique_separator, std.map(each, parts));
std.extVar(key),
_native(name, a, b, c, d, e, f)::
if false then false
else if a == std._unset then std.__native(name, [])
else if b == std._unset then std.__native(name, [a])
else if c == std._unset then std.__native(name, [a, b])
else if d == std._unset then std.__native(name, [a, b, c])
else if e == std._unset then std.__native(name, [a, b, c, d])
else if f == std._unset then std.__native(name, [a, b, c, d, e])
else std.__native(name, [a, b, c, d, e, f]),
native(name)::
function(a=std._unset, b=std._unset, c=std._unset, d=std._unset, e=std._unset, f=std._unset)
std._native(name, a, b, c, d, e, f),

dispatch(object, prefix)::
function(a=std._unset, b=std._unset, c=std._unset, d=std._unset, e=std._unset, f=std._unset)
if false then false
else if a == std._unset then object[prefix + '0']()
else if b == std._unset then object[prefix + '1'](a)
else if c == std._unset then object[prefix + '2'](a, b)
else if d == std._unset then object[prefix + '3'](a, b, c)
else if e == std._unset then object[prefix + '4'](a, b, c, d)
else if f == std._unset then object[prefix + '5'](a, b, c, d, e)
else object[prefix + '6'](a, b, c, d, e, f),

};
local std = __std;
${code}`;
if (Object.keys(extra).length) throw `Error: jsonnet.evaluate: Unexpected options: ${Object.keys(extra)}`;
let i, n;
for (i=0, n=maxIterations; i<n; ++i) {
try {
const promise = _evaluate({
filename,
code,
files,
extStrs,
extCodes,
tlaStrs,
tlaCodes,
});
let result = await promise;
try {
result = JSON.parse(result);
} catch (e) {}

return result;
} catch (e) {
const error = `${e}`;
let lo = error.indexOf(UNIQUE_SEPARATOR);
if (lo === -1) {
throw e;
}
let hi = error.lastIndexOf(UNIQUE_SEPARATOR);
hi += UNIQUE_SEPARATOR.length;
const key = error.substring(lo, hi);
const parts = key.split(UNIQUE_SEPARATOR);
parts.shift(); // remove beginning
parts.pop(); // remove end
const [name, ...args] = parts;
const callback = nativeCallbacks[name];
if (callback === undefined) {
throw `Error: jsonnet.evaluate: callback "${name}" does not exist`;
}
const promise = Promise.resolve(callback(...args));
const result = await promise;
extCodes = { ...extCodes, [key]: JSON.stringify(result) };
// console.log({ error, key });
}

await new Promise((resolve) => setTimeout(resolve, 0));
}
throw `Error: jsonnet.evaluate: max recursive iterations exceeded: ${i}`;
}

async function _evaluate({
filename,
code,
files={},
extStrs={},
extCodes={},
tlaStrs={},
tlaCodes={},
...extra
}) {
if (Object.keys(extra).length) throw `Error: jsonnet._evaluate: Unexpected options: ${Object.keys(extra)}`;
const result = await __evaluate(filename, code, files, extStrs, extCodes, tlaStrs, tlaCodes);
// return { result };
// if (result.startsWith('Error: ')) throw result;
return result;
}

async function LOAD_NATIVE_LIBRARY(identifier='_jsonnet') {
if (window[identifier] !== undefined) {
return window[identifier];
}

const before = new Set();
for (const key of Object.keys(window)) {
before.add(key);
}

const Go = await require('https://jsonnet.org/js/wasm_exec.js').catch(() => {
const go = window.Go;
delete window.Go;
return go;
});

const go = new Go();
const request = fetch("https://jsonnet.org/js/libjsonnet.wasm");
const response = await WebAssembly.instantiateStreaming(request, go.importObject);
go.run(response.instance);

const jsonnet = {};
for (let key of Object.keys(window)) {
if (before.has(key)) continue;
if (!key.startsWith('jsonnet_')) continue;
const value = window[key];
delete window[key];
key = key.substring('jsonnet_'.length);
jsonnet[key] = value;
}

window[identifier] = jsonnet;
return jsonnet;
}
}
Insert cell
Inputs_abutton = {
return abutton;

function abutton(content, { value, reduce }) {
const form = htl.html`<form class=__ns__>`;
form.addEventListener('submit', (e) => e.preventDefault());

const button = htl.html`<button onclick=${(event) => {
event.stopPropagation();
const { currentTarget } = event;
Promise.resolve(reduce(form.value)).then((value) => {
form.value = value;
dispatchInput({ currentTarget });
});
}}>${content}`;

form.appendChild(button);
form.value = value;
return form;
}

function dispatchInput({currentTarget}) {
const bubbles = {bubbles: true};
(currentTarget.form || currentTarget).dispatchEvent(new Event("input", bubbles));
}
}
Insert cell
Inputs_code = {
const indentTextarea = await import("https://cdn.skypack.dev/indent-textarea");
return code;
function code({ ...options }) {
const textarea = Inputs.textarea({ ...options });
indentTextarea.enableTabToIndent(textarea);
return textarea;
}
}
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