-
Notifications
You must be signed in to change notification settings - Fork 1
/
experiments.test.ts
184 lines (178 loc) · 4.8 KB
/
experiments.test.ts
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { suite, it, beforeAll, afterAll, expect } from 'vitest'
import { Florence2ForConditionalGeneration, WhisperForConditionalGeneration } from '@huggingface/transformers'
import { ModelServer } from '#package/server.js'
import { ChatMessage, ToolDefinition } from '#package/types/index.js'
import { ChatWithVisionEngine } from '#package/experiments/ChatWithVision.js'
import { VoiceFunctionCallEngine } from '#package/experiments/VoiceFunctionCall.js'
import { createChatCompletion } from '../util/completions.js'
import { loadImageFromUrl } from '#package/lib/loadImage.js'
import { loadAudioFromFile } from '#package/lib/loadAudio.js'
suite('chat with vision', () => {
// florence2 generates a description of the image and passes it to phi3
const modelServer = new ModelServer({
// log: 'debug',
concurrency: 2,
engines: {
'chat-with-vision': new ChatWithVisionEngine({
chatModel: 'phi3',
imageToTextModel: 'florence2',
}),
},
models: {
phi3: {
url: 'https://gpt4all.io/models/gguf/Phi-3-mini-4k-instruct.Q4_0.gguf',
md5: 'f8347badde9bfc2efbe89124d78ddaf5',
engine: 'gpt4all',
task: 'text-completion',
},
florence2: {
url: 'https://huggingface.co/onnx-community/Florence-2-large-ft',
engine: 'transformers-js',
task: 'image-to-text',
modelClass: Florence2ForConditionalGeneration,
dtype: {
embed_tokens: 'fp16',
vision_encoder: 'fp32',
encoder_model: 'fp16',
decoder_model_merged: 'q4',
},
device: {
gpu: false,
},
},
'vision-at-home': {
engine: 'chat-with-vision',
task: 'text-completion',
},
},
})
beforeAll(async () => {
await modelServer.start()
})
afterAll(async () => {
await modelServer.stop()
})
it('can see', async () => {
const image = await loadImageFromUrl(
'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg?download=true',
)
const messages: ChatMessage[] = [
{
role: 'user',
content: [
{
type: 'image',
image,
},
{
type: 'text',
text: 'WHAT DO YOUR ELF EYES SEE?',
},
],
},
]
const response = await createChatCompletion(modelServer, {
model: 'vision-at-home',
temperature: 0,
messages,
})
// Based on the description provided, my elf eyes would see ...
console.debug({ response: response.result.message.content })
expect(response.result.message.content).toContain('car')
})
})
suite('voice functions', () => {
type SearchParams = { query: string, sources: string }
const searchCalls: SearchParams[] = []
const searchTool: ToolDefinition<SearchParams> = {
description: 'Search',
parameters: {
type: 'object',
properties: {
query: {
type: 'string',
},
sources: {
type: 'string',
enum: ['web', 'all databases', 'local files'],
},
test: {
type: 'number',
multipleOf: 2,
}
},
required: ['query', 'sources'],
},
handler: async (params) => {
// console.debug('called', { params })
searchCalls.push(params)
return (
`Searching for: ${params.query}` +
'1. A dessert on Darmok\n' +
'2. A continent on Etobicoke\n' +
'3. A city on Risa'
)
},
}
const modelServer = new ModelServer({
// log: 'debug',
engines: {
'voice-function-calling': new VoiceFunctionCallEngine({
speechToTextModel: 'whisper-base',
chatModel: 'functionary',
tools: {
search: searchTool,
},
}),
},
models: {
'voice-function-calling': {
engine: 'voice-function-calling',
task: 'speech-to-text',
},
'whisper-base': {
url: 'https://huggingface.co/onnx-community/whisper-base',
engine: 'transformers-js',
task: 'speech-to-text',
prepare: 'async',
minInstances: 1,
modelClass: WhisperForConditionalGeneration,
dtype: {
encoder_model: 'fp16', // 'fp16' works too
decoder_model_merged: 'q4', // or 'fp32' ('fp16' is broken)
},
device: {
gpu: false,
},
},
functionary: {
task: 'text-completion',
engine: 'node-llama-cpp',
url: 'https://huggingface.co/meetkai/functionary-small-v3.2-GGUF/blob/main/functionary-small-v3.2.Q4_0.gguf',
sha256: 'c0afdbbffa498a8490dea3401e34034ac0f2c6e337646513a7dbc04fcef1c3a4',
// device: {
// gpu: 'vulkan',
// }
},
},
})
beforeAll(async () => {
await modelServer.start()
})
afterAll(async () => {
await modelServer.stop()
})
it('can hear', async () => {
const audio = await loadAudioFromFile('tests/fixtures/tenagra.mp3', {
sampleRate: 16000,
})
const result = await modelServer.processSpeechToTextTask({
model: 'voice-function-calling',
audio,
})
expect(result.text).toContain('Risa')
expect(searchCalls).toHaveLength(1)
expect(searchCalls[0].sources).toMatch('all databases')
expect(searchCalls[0].query).toMatch(/tenagra/i)
}, 120000)
})