forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenai.ts
More file actions
179 lines (157 loc) · 6.38 KB
/
Copy pathopenai.ts
File metadata and controls
179 lines (157 loc) · 6.38 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
import axios from "axios";
import * as FileSystem from 'expo-file-system';
import { Platform } from 'react-native';
import { keys } from "../keys";
function blobToBase64(blob: Blob | File): Promise<string> {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = () => {
const result = reader.result as string;
// Remove the data URL prefix to get just the base64 string
const base64 = result.split(',')[1];
resolve(base64);
};
reader.onerror = reject;
reader.readAsDataURL(blob);
});
}
export async function transcribeAudio(audioInput: string | File | Blob) {
let audioBase64: string;
if (Platform.OS === 'web') {
if (typeof audioInput === 'string') {
// If it's a URL, fetch it first
const response = await fetch(audioInput);
const blob = await response.blob();
audioBase64 = await blobToBase64(blob);
} else {
// If it's a File or Blob object
audioBase64 = await blobToBase64(audioInput as Blob);
}
} else {
// Mobile: expect a file path string
audioBase64 = await FileSystem.readAsStringAsync(audioInput as string, {
encoding: FileSystem.EncodingType.Base64
});
}
try {
const response = await axios.post("https://api.openai.com/v1/audio/transcriptions", {
audio: audioBase64,
}, {
headers: {
'Authorization': `Bearer ${keys.openai}`, // Replace YOUR_API_KEY with your actual OpenAI API key
'Content-Type': 'application/json'
},
});
return response.data;
} catch (error) {
console.error("Error in transcribeAudio:", error);
return null; // or handle error differently
}
}
let audioContext: AudioContext;
export async function startAudio() {
audioContext = new AudioContext();
}
export async function textToSpeech(text: string) {
try {
const response = await axios.post("https://api.openai.com/v1/audio/speech", {
input: text, // Use 'input' instead of 'text'
voice: "nova",
model: "tts-1",
}, {
headers: {
'Authorization': `Bearer ${keys.openai}`, // Replace YOUR_API_KEY with your actual OpenAI API key
'Content-Type': 'application/json'
},
responseType: 'arraybuffer' // This will handle the binary data correctly
});
// Decode the audio data asynchronously
const audioBuffer = await audioContext.decodeAudioData(response.data);
// Create an audio source
const source = audioContext.createBufferSource();
source.buffer = audioBuffer;
source.connect(audioContext.destination);
source.start(); // Play the audio immediately
return response.data;
} catch (error) {
console.error("Error in textToSpeech:", error);
return null; // or handle error differently
}
}
// Function to convert image to base64
async function imageToBase64(imageInput: string | File | Blob): Promise<string> {
let base64: string;
if (Platform.OS === 'web') {
if (typeof imageInput === 'string') {
// If it's a URL, fetch it first
const response = await fetch(imageInput);
const blob = await response.blob();
base64 = await blobToBase64(blob);
} else {
// If it's a File or Blob object
base64 = await blobToBase64(imageInput as Blob);
}
// Determine MIME type for web
const mimeType = (imageInput as File)?.type || 'image/jpeg';
return `data:${mimeType};base64,${base64}`;
} else {
// Mobile: expect a file path string
const image = await FileSystem.readAsStringAsync(imageInput as string, {
encoding: FileSystem.EncodingType.Base64
});
return `data:image/jpeg;base64,${image}`;
}
}
export async function describeImage(imageInput: string | File | Blob) {
const imageBase64 = await imageToBase64(imageInput);
try {
const response = await axios.post("https://api.openai.com/v1/images/descriptions", {
image: imageBase64,
}, {
headers: {
'Authorization': `Bearer ${keys.openai}`, // Replace YOUR_API_KEY with your actual OpenAI API key
'Content-Type': 'application/json'
},
});
return response.data;
} catch (error) {
console.error("Error in describeImage:", error);
return null; // or handle error differently
}
}
export async function gptRequest(systemPrompt: string, userPrompt: string) {
try {
const response = await axios.post("https://api.openai.com/v1/chat/completions", {
model: "gpt-4o",
messages: [
{ role: "system", content: systemPrompt },
{ role: "user", content: userPrompt },
],
}, {
headers: {
'Authorization': `Bearer ${keys.openai}`, // Replace YOUR_API_KEY with your actual OpenAI API key
'Content-Type': 'application/json'
},
});
return response.data;
} catch (error) {
console.error("Error in gptRequest:", error);
return null; // or handle error differently
}
}
textToSpeech("Hello I am an agent")
console.info(gptRequest(
`
You are a smart AI that need to read through description of a images and answer user's questions.
This are the provided images:
The image features a woman standing in an open space with a metal roof, possibly at a train station or another large building.
She is wearing a hat and appears to be looking up towards the sky.
The scene captures her attention as she gazes upwards, perhaps admiring something above her or simply enjoying the view from this elevated position.
DO NOT mention the images, scenes or descriptions in your answer, just answer the question.
DO NOT try to generalize or provide possible scenarios.
ONLY use the information in the description of the images to answer the question.
BE concise and specific.
`
,
'where is the person?'
))