File size: 1,868 Bytes
2d2e027
 
 
 
 
 
 
 
 
 
 
 
 
1909611
2d2e027
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

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 });
	}
}