Spaces:
Running
Running
interface Message { | |
incoming: boolean; | |
content: string; | |
} | |
interface MessageOutput { | |
text: string; | |
attention: [string, number][][]; /// <- [persona, context, user message] | |
} | |
class Api { | |
static ENDPOINT = `https://convai-proxy.huggingface.co`; | |
static shared = new Api(); | |
path(p: string): string { | |
return `${Api.ENDPOINT}/${p}`; | |
} | |
async getShuffle(): Promise<{slug: string, text: string}> { | |
const response = await fetch('/shuffle'); | |
return response.json(); | |
} | |
private async postMessage( | |
user: string, | |
query: { text: string } | { completion: string }, | |
params: { | |
context?: Message[]; | |
persona?: string; | |
top_k?: number; /// int between 0 and 1k | |
top_p?: number; /// float between 0 and 1 | |
temperature?: number; /// float between 0 and 100 | |
} | |
): Promise<MessageOutput> { | |
const qs = new URLSearchParams(query); | |
const path = this.path(`messages/${user}?${ qs.toString() }`); | |
const response = await fetch(path, { | |
method: 'POST', | |
headers: { 'Content-Type': 'application/json' }, | |
body: JSON.stringify(params), | |
}); | |
return await response.json() as MessageOutput; | |
} | |
/** | |
* Demo-specific helpers | |
*/ | |
async postWithSettings( | |
query: { text: string } | { completion: string }, | |
params: { | |
context?: Message[]; | |
persona?: string; | |
} | |
): Promise<MessageOutput> { | |
/// Retrieve all settings params then launch the request. | |
const top_k = Number( | |
document.querySelector('.decoder-settings .setting:nth-child(2) .js-val')!.textContent | |
); | |
const top_p = Number( | |
document.querySelector('.decoder-settings .setting:nth-child(3) .js-val')!.textContent | |
); | |
const temperature = Number( | |
document.querySelector('.decoder-settings .setting:nth-child(4) .js-val')!.textContent | |
); | |
return this.postMessage(`toto`, query, { ...params, top_k, top_p, temperature }); | |
} | |
} | |