-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvoice-example.ts
More file actions
215 lines (177 loc) · 6.08 KB
/
Copy pathvoice-example.ts
File metadata and controls
215 lines (177 loc) · 6.08 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import { initAIHooks, wrap } from 'npm-ai-hooks';
// Initialize with your API key
initAIHooks({
providers: [
{
provider: 'openai',
key: process.env.OPENAI_KEY || 'your-openai-key'
}
]
});
/**
* Voice Input with npm-ai-hooks
*
* Note: Voice recording is primarily designed for browser environments
* using the Web Speech API. For Node.js, you would need additional
* libraries like 'node-record-lpcm16' or 'sox-audio' for recording,
* and a speech-to-text service.
*
* This example shows how to use transcribed text with the library.
*/
// Example 1: Process transcribed voice input
async function processVoiceInput(transcribedText: string) {
const explainFn = wrap((text: string) => text, {
provider: 'openai',
model: 'gpt-4o',
task: 'explain'
});
const result = await explainFn(transcribedText);
console.log('Explanation:', result.output);
}
// Example 2: Voice-to-voice (text-to-speech not included)
async function voiceConversation(transcribedText: string) {
const converseFn = wrap((text: string) => text, {
provider: 'openai',
model: 'gpt-4o',
customPrompt: 'You are a helpful voice assistant. Respond in a natural, conversational way.'
});
const result = await converseFn(transcribedText);
console.log('Response:', result.output);
// Note: To convert this to speech, you would use a TTS library
// like 'say.js', 'google-tts-api', or OpenAI's TTS endpoint
return result.output;
}
// Example 3: Voice command processing
async function processVoiceCommand(command: string) {
const commandFn = wrap((cmd: string) => cmd, {
provider: 'openai',
model: 'gpt-4o',
customPrompt: `You are a voice command processor. Given a voice command,
identify the intent and extract relevant parameters.
Respond in JSON format with: { intent, parameters, confirmation }.`
});
const result = await commandFn(command);
console.log('Command Analysis:', result.output);
try {
const parsed = JSON.parse(result.output);
return parsed;
} catch {
return { error: 'Could not parse command', raw: result.output };
}
}
// Example 4: Multi-language voice support
async function translateVoiceInput(transcribedText: string, targetLanguage: string = 'Spanish') {
const translateFn = wrap((text: string) => text, {
provider: 'openai',
model: 'gpt-4o',
task: 'translate',
targetLanguage
});
const result = await translateFn(transcribedText);
console.log(`Translated to ${targetLanguage}:`, result.output);
}
// Example 5: Voice-based code review
async function voiceCodeReview(spokenCodeDescription: string) {
const reviewFn = wrap((text: string) => text, {
provider: 'openai',
model: 'gpt-4o',
customPrompt: `The user is describing code verbally.
Based on their description, provide code review insights and suggestions.`
});
const result = await reviewFn(spokenCodeDescription);
console.log('Code Review from Voice:', result.output);
}
// Browser-specific: Web Speech API integration
export const browserVoiceRecording = `
// This code works in browsers that support the Web Speech API
class VoiceRecorder {
private recognition: any;
private isRecording = false;
constructor() {
const SpeechRecognition = (window as any).webkitSpeechRecognition || (window as any).SpeechRecognition;
if (SpeechRecognition) {
this.recognition = new SpeechRecognition();
this.recognition.continuous = false;
this.recognition.interimResults = false;
this.recognition.lang = 'en-US';
} else {
throw new Error('Speech recognition not supported in this browser');
}
}
async startRecording(): Promise<string> {
return new Promise((resolve, reject) => {
this.recognition.onresult = (event: any) => {
const transcript = event.results[0][0].transcript;
resolve(transcript);
};
this.recognition.onerror = (event: any) => {
reject(new Error('Speech recognition error: ' + event.error));
};
this.recognition.start();
this.isRecording = true;
});
}
stopRecording() {
if (this.isRecording) {
this.recognition.stop();
this.isRecording = false;
}
}
}
// Usage in browser:
async function browserExample() {
import { wrap, initAIHooks } from 'npm-ai-hooks';
initAIHooks({
providers: [
{ provider: 'openai', key: 'your-api-key' }
]
});
const recorder = new VoiceRecorder();
try {
console.log('Listening...');
const transcript = await recorder.startRecording();
console.log('You said:', transcript);
// Process with AI
const explainFn = wrap((text: string) => text, {
provider: 'openai',
model: 'gpt-4o',
task: 'explain'
});
const result = await explainFn(transcript);
console.log('AI Response:', result.output);
} catch (error) {
console.error('Error:', error);
}
}
`;
// Run examples with sample transcribed text
async function main() {
try {
console.log('=== Voice Input Processing Examples ===\n');
console.log('1. Explaining transcribed voice input:');
await processVoiceInput('What is the difference between let and const in JavaScript?');
console.log('\n2. Voice conversation:');
await voiceConversation('Tell me a quick tip about TypeScript');
console.log('\n3. Voice command processing:');
await processVoiceCommand('Set a reminder for tomorrow at 3 PM to call John');
console.log('\n4. Multi-language translation:');
await translateVoiceInput('Hello, how are you today?', 'French');
console.log('\n5. Voice-based code review:');
await voiceCodeReview('I have a function that loops through an array and pushes items to another array inside the loop');
console.log('\n=== Browser Integration ===');
console.log('For browser-based voice recording, see the browserVoiceRecording export');
console.log('or check the React example in examples/react/');
} catch (error) {
console.error('Error:', error);
}
}
// Uncomment to run
// main();
export {
processVoiceInput,
voiceConversation,
processVoiceCommand,
translateVoiceInput,
voiceCodeReview,
main
};