-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolved merge conflicts between feat/added-anthropic-vision-api and …
…main
- Loading branch information
Showing
7 changed files
with
125 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
NUM_WORKERS_PER_QUEUE=8 | ||
PORT= | ||
HOST= | ||
SUPABASE_ANON_TOKEN= | ||
SUPABASE_URL= | ||
SUPABASE_SERVICE_TOKEN= | ||
REDIS_URL= | ||
SCRAPING_BEE_API_KEY= | ||
OPENAI_API_KEY= | ||
ANTHROPIC_API_KEY= | ||
BULL_AUTH_KEY= | ||
LOGTAIL_KEY= | ||
PLAYWRIGHT_MICROSERVICE_URL= | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import Anthropic from '@anthropic-ai/sdk'; | ||
import axios from 'axios'; | ||
|
||
export async function getImageDescription( | ||
imageUrl: string, | ||
backText: string, | ||
frontText: string, | ||
model: string = "gpt-4-turbo" | ||
): Promise<string> { | ||
try { | ||
const prompt = "What's in the image? You need to answer with the content for the alt tag of the image. To help you with the context, the image is in the following text: " + | ||
backText + | ||
" and the following text: " + | ||
frontText + | ||
". Be super concise." | ||
|
||
switch (model) { | ||
case 'claude-3-opus': { | ||
if (!process.env.ANTHROPIC_API_KEY) { | ||
throw new Error("No Anthropic API key provided"); | ||
} | ||
const imageRequest = await axios.get(imageUrl, { responseType: 'arraybuffer' }); | ||
const imageMediaType = 'image/png'; | ||
const imageData = Buffer.from(imageRequest.data, 'binary').toString('base64'); | ||
|
||
const anthropic = new Anthropic(); | ||
const response = await anthropic.messages.create({ | ||
model: "claude-3-opus-20240229", | ||
max_tokens: 1024, | ||
messages: [ | ||
{ | ||
role: "user", | ||
content: [ | ||
{ | ||
type: "image", | ||
source: { | ||
type: "base64", | ||
media_type: imageMediaType, | ||
data: imageData, | ||
}, | ||
}, | ||
{ | ||
type: "text", | ||
text: prompt | ||
} | ||
], | ||
} | ||
] | ||
}); | ||
|
||
return response.content[0].text; | ||
} | ||
default: { | ||
if (!process.env.OPENAI_API_KEY) { | ||
throw new Error("No OpenAI API key provided"); | ||
} | ||
|
||
const { OpenAI } = require("openai"); | ||
const openai = new OpenAI(); | ||
|
||
const response = await openai.chat.completions.create({ | ||
model: "gpt-4-turbo", | ||
messages: [ | ||
{ | ||
role: "user", | ||
content: [ | ||
{ | ||
type: "text", | ||
text: prompt, | ||
}, | ||
{ | ||
type: "image_url", | ||
image_url: { | ||
url: imageUrl, | ||
}, | ||
}, | ||
], | ||
}, | ||
], | ||
}); | ||
return response.choices[0].message.content; | ||
} | ||
} | ||
} catch (error) { | ||
console.error("Error generating image alt text:", error?.message); | ||
return ""; | ||
} | ||
} |