diff options
Diffstat (limited to 'src/client/apis/gpt/GPT.ts')
-rw-r--r-- | src/client/apis/gpt/GPT.ts | 76 |
1 files changed, 49 insertions, 27 deletions
diff --git a/src/client/apis/gpt/GPT.ts b/src/client/apis/gpt/GPT.ts index 2fa92373f..9efe4ec39 100644 --- a/src/client/apis/gpt/GPT.ts +++ b/src/client/apis/gpt/GPT.ts @@ -94,8 +94,51 @@ const gptAPICall = async (inputTextIn: string, callType: GPTCallType, prompt?: a return 'Error connecting with API.'; } }; +const gptImageCall = async (prompt: string, n?: number) => { + try { + const configuration: ClientOptions = { + apiKey: process.env.OPENAI_KEY, + dangerouslyAllowBrowser: true, + }; + + const openai = new OpenAI(configuration); + const response = await openai.images.generate({ + prompt: prompt, + n: n ?? 1, + size: '1024x1024', + }); + return response.data.map((data: any) => data.url); + // return response.data.data[0].url; + } catch (err) { + console.error(err); + } + return undefined; +}; + +const gptGetEmbedding = async (src: string): Promise<number[]> => { + try { + const configuration: ClientOptions = { + apiKey: process.env.OPENAI_KEY, + dangerouslyAllowBrowser: true, + }; + const openai = new OpenAI(configuration); + const embeddingResponse = await openai.embeddings.create({ + model: 'text-embedding-3-large', + input: [src], + encoding_format: 'float', + dimensions: 256, + }); -const gptImageLabel = async (imgUrl: string): Promise<string> => { + // Assume the embeddingResponse structure is correct; adjust based on actual API response + const { embedding } = embeddingResponse.data[0]; + return embedding; + } catch (err) { + console.log(err); + return []; + } +}; + +const gptImageLabel = async (src: string): Promise<string> => { try { const configuration: ClientOptions = { apiKey: process.env.OPENAI_KEY, @@ -109,11 +152,12 @@ const gptImageLabel = async (imgUrl: string): Promise<string> => { { role: 'user', content: [ - { type: 'text', text: 'Describe this image in 3-5 words' }, + { type: 'text', text: 'Give three to five labels to describe this image.' }, { type: 'image_url', image_url: { - url: `${imgUrl}`, + url: `${src}`, + detail: 'low', }, }, ], @@ -122,34 +166,12 @@ const gptImageLabel = async (imgUrl: string): Promise<string> => { }); if (response.choices[0].message.content) { return response.choices[0].message.content; - } else { - return ':('; } + return 'Missing labels'; } catch (err) { console.log(err); return 'Error connecting with API'; } }; -const gptImageCall = async (prompt: string, n?: number) => { - try { - const configuration: ClientOptions = { - apiKey: process.env.OPENAI_KEY, - dangerouslyAllowBrowser: true, - }; - - const openai = new OpenAI(configuration); - const response = await openai.images.generate({ - prompt: prompt, - n: n ?? 1, - size: '1024x1024', - }); - return response.data.map((data: any) => data.url); - // return response.data.data[0].url; - } catch (err) { - console.error(err); - } - return undefined; -}; - -export { gptAPICall, gptImageCall, gptImageLabel, GPTCallType }; +export { gptAPICall, gptImageCall, GPTCallType, gptImageLabel, gptGetEmbedding }; |