forked from lobehub/lobe-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextToImage.ts
38 lines (29 loc) · 1.01 KB
/
textToImage.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
import { ModelProvider } from '@/libs/agent-runtime';
import { createHeaderWithAuth } from '@/services/_auth';
import { OpenAIImagePayload } from '@/types/openai/image';
import { API_ENDPOINTS } from './_url';
interface FetchOptions {
signal?: AbortSignal | undefined;
}
class ImageGenerationService {
async generateImage(params: Omit<OpenAIImagePayload, 'model' | 'n'>, options?: FetchOptions) {
const payload: OpenAIImagePayload = { ...params, model: 'dall-e-3', n: 1 };
const provider = ModelProvider.OpenAI;
const headers = await createHeaderWithAuth({
headers: { 'Content-Type': 'application/json' },
provider,
});
const res = await fetch(API_ENDPOINTS.images(provider), {
body: JSON.stringify(payload),
headers: headers,
method: 'POST',
signal: options?.signal,
});
if (!res.ok) {
throw await res.json();
}
const urls = await res.json();
return urls[0] as string;
}
}
export const imageGenerationService = new ImageGenerationService();