"vscode:/vscode.git/clone" did not exist on "8daf4500e94e645b10cfcec6744c6e70242f8f4d"
Models.svelte 4.53 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
<script lang="ts">
Timothy J. Baek's avatar
Timothy J. Baek committed
2
3
	import { createEventDispatcher } from 'svelte';

Timothy J. Baek's avatar
Timothy J. Baek committed
4
5
6
	import { generatePrompt } from '$lib/apis/ollama';
	import { models } from '$lib/stores';
	import { splitStream } from '$lib/utils';
7
	import { tick, getContext } from 'svelte';
Jannik Streidl's avatar
Jannik Streidl committed
8
	import { toast } from 'svelte-sonner';
Timothy J. Baek's avatar
Timothy J. Baek committed
9

10
11
	const i18n = getContext('i18n');

Timothy J. Baek's avatar
Timothy J. Baek committed
12
13
	const dispatch = createEventDispatcher();

Timothy J. Baek's avatar
Timothy J. Baek committed
14
15
16
17
18
19
20
21
22
23
	export let prompt = '';
	export let user = null;

	export let chatInputPlaceholder = '';
	export let messages = [];

	let selectedIdx = 0;
	let filteredModels = [];

	$: filteredModels = $models
24
25
26
27
		.filter(
			(p) =>
				p.name.toLowerCase().includes(prompt.toLowerCase().split(' ')?.at(0)?.substring(1) ?? '') &&
				p?.info?.meta?.hidden
Timothy J. Baek's avatar
Timothy J. Baek committed
28
		)
Timothy J. Baek's avatar
Timothy J. Baek committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
		.sort((a, b) => a.name.localeCompare(b.name));

	$: if (prompt) {
		selectedIdx = 0;
	}

	export const selectUp = () => {
		selectedIdx = Math.max(0, selectedIdx - 1);
	};

	export const selectDown = () => {
		selectedIdx = Math.min(selectedIdx + 1, filteredModels.length - 1);
	};

	const confirmSelect = async (model) => {
Timothy J. Baek's avatar
Timothy J. Baek committed
44
		prompt = '';
Timothy J. Baek's avatar
Timothy J. Baek committed
45
		dispatch('select', model);
Timothy J. Baek's avatar
Timothy J. Baek committed
46
47
48
	};

	const confirmSelectCollaborativeChat = async (model) => {
Timothy J. Baek's avatar
Timothy J. Baek committed
49
50
51
52
53
		// dispatch('select', model);
		prompt = '';
		user = JSON.parse(JSON.stringify(model.name));
		await tick();

54
		chatInputPlaceholder = $i18n.t('{{modelName}} is thinking...', { modelName: model.name });
Timothy J. Baek's avatar
Timothy J. Baek committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91

		const chatInputElement = document.getElementById('chat-textarea');

		await tick();
		chatInputElement?.focus();
		await tick();

		const convoText = messages.reduce((a, message, i, arr) => {
			return `${a}### ${message.role.toUpperCase()}\n${message.content}\n\n`;
		}, '');

		const res = await generatePrompt(localStorage.token, model.name, convoText);

		if (res && res.ok) {
			const reader = res.body
				.pipeThrough(new TextDecoderStream())
				.pipeThrough(splitStream('\n'))
				.getReader();

			while (true) {
				const { value, done } = await reader.read();
				if (done) {
					break;
				}

				try {
					let lines = value.split('\n');

					for (const line of lines) {
						if (line !== '') {
							console.log(line);
							let data = JSON.parse(line);

							if ('detail' in data) {
								throw data;
							}

92
93
94
95
96
97
98
99
100
101
102
103
							if ('id' in data) {
								console.log(data);
							} else {
								if (data.done == false) {
									if (prompt == '' && data.response == '\n') {
										continue;
									} else {
										prompt += data.response;
										console.log(data.response);
										chatInputElement.scrollTop = chatInputElement.scrollHeight;
										await tick();
									}
Timothy J. Baek's avatar
Timothy J. Baek committed
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
								}
							}
						}
					}
				} catch (error) {
					console.log(error);
					if ('detail' in error) {
						toast.error(error.detail);
					}
					break;
				}
			}
		} else {
			if (res !== null) {
				const error = await res.json();
				console.log(error);
				if ('detail' in error) {
					toast.error(error.detail);
				} else {
					toast.error(error.error);
				}
			} else {
Ased Mammad's avatar
Ased Mammad committed
126
127
128
				toast.error(
					$i18n.t('Uh-oh! There was an issue connecting to {{provider}}.', { provider: 'llama' })
				);
Timothy J. Baek's avatar
Timothy J. Baek committed
129
130
131
132
133
134
135
136
137
			}
		}

		chatInputPlaceholder = '';

		console.log(user);
	};
</script>

Timothy J. Baek's avatar
Timothy J. Baek committed
138
139
{#if prompt.charAt(0) === '@'}
	{#if filteredModels.length > 0}
Timothy J. Baek's avatar
Timothy J. Baek committed
140
		<div class="pl-1 pr-12 mb-3 text-left w-full absolute bottom-0 left-0 right-0 z-10">
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
141
142
			<div class="flex w-full dark:border dark:border-gray-850 rounded-lg">
				<div class=" bg-gray-50 dark:bg-gray-850 w-10 rounded-l-lg text-center">
Timothy J. Baek's avatar
Timothy J. Baek committed
143
144
					<div class=" text-lg font-semibold mt-2">@</div>
				</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
145

Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
146
147
148
149
				<div
					class="max-h-60 flex flex-col w-full rounded-r-lg bg-white dark:bg-gray-900 dark:text-gray-100"
				>
					<div class="m-1 overflow-y-auto p-1 rounded-r-lg space-y-0.5 scrollbar-hidden">
Timothy J. Baek's avatar
Timothy J. Baek committed
150
151
152
						{#each filteredModels as model, modelIdx}
							<button
								class=" px-3 py-1.5 rounded-xl w-full text-left {modelIdx === selectedIdx
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
153
									? '  bg-gray-50 dark:bg-gray-850  selected-command-option-button'
Timothy J. Baek's avatar
Timothy J. Baek committed
154
155
156
157
158
159
160
161
162
163
									: ''}"
								type="button"
								on:click={() => {
									confirmSelect(model);
								}}
								on:mousemove={() => {
									selectedIdx = modelIdx;
								}}
								on:focus={() => {}}
							>
164
165
166
167
168
169
								<div class="flex font-medium text-black dark:text-gray-100 line-clamp-1">
									<img
										src={model?.info?.meta?.profile_image_url ?? '/static/favicon.png'}
										alt="Model"
										class="rounded-full size-6 items-center mr-2"
									/>
Timothy J. Baek's avatar
Timothy J. Baek committed
170
171
172
173
									{model.name}
								</div>

								<!-- <div class=" text-xs text-gray-600 line-clamp-1">
Timothy J. Baek's avatar
Timothy J. Baek committed
174
175
								{doc.title}
							</div> -->
Timothy J. Baek's avatar
Timothy J. Baek committed
176
177
178
							</button>
						{/each}
					</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
179
180
181
				</div>
			</div>
		</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
182
183
	{/if}
{/if}