Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: groq image vision provider #2342

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/core/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,9 @@ export class AgentRuntime implements IAgentRuntime {
this.imageVisionModelProvider =
this.character.imageVisionModelProvider ?? this.modelProvider;

elizaLogger.info("Selected model provider:", this.modelProvider);
// elizaLogger.info("Selected model provider:", this.modelProvider); duplicated log ln: 343
elizaLogger.info(
"Selected image model provider:",
"Selected image vision model provider:",
this.imageVisionModelProvider
);

Expand Down
54 changes: 54 additions & 0 deletions packages/plugin-node/src/services/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,51 @@ class OpenAIImageProvider implements ImageProvider {
}
}


class GroqImageProvider implements ImageProvider {
constructor(private runtime: IAgentRuntime) {}

async initialize(): Promise<void> {}

async describeImage(
imageData: Buffer,
mimeType: string
): Promise<{ title: string; description: string }> {
const imageUrl = convertToBase64DataUrl(imageData, mimeType);

const content = [
{ type: "text", text: IMAGE_DESCRIPTION_PROMPT },
{ type: "image_url", image_url: { url: imageUrl } },
];

const endpoint =
this.runtime.imageVisionModelProvider === ModelProviderName.GROQ
? getEndpoint(this.runtime.imageVisionModelProvider)
: "https://api.groq.com/openai/v1/";

const response = await fetch(endpoint + "/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${this.runtime.getSetting("GROQ_API_KEY")}`,
},
body: JSON.stringify({
model: /*this.runtime.imageVisionModelName ||*/ "llama-3.2-90b-vision-preview",
messages: [{ role: "user", content }],
max_tokens: 1024,
}),
});

if (!response.ok) {
await handleApiError(response, "Groq");
}

const data = await response.json();
return parseImageResponse(data.choices[0].message.content);
}
}


class GoogleImageProvider implements ImageProvider {
constructor(private runtime: IAgentRuntime) {}

Expand Down Expand Up @@ -280,6 +325,12 @@ export class ImageDescriptionService
) {
this.provider = new OpenAIImageProvider(this.runtime);
elizaLogger.debug("Using openai for vision model");
} else if (
this.runtime.imageVisionModelProvider ===
ModelProviderName.GROQ
) {
this.provider = new GroqImageProvider(this.runtime);
elizaLogger.debug("Using Groq for vision model");
} else {
elizaLogger.error(
`Unsupported image vision model provider: ${this.runtime.imageVisionModelProvider}`
Expand All @@ -291,6 +342,9 @@ export class ImageDescriptionService
} else if (model === models[ModelProviderName.GOOGLE]) {
this.provider = new GoogleImageProvider(this.runtime);
elizaLogger.debug("Using google for vision model");
} else if (model === models[ModelProviderName.GROQ]) {
this.provider = new GroqImageProvider(this.runtime);
elizaLogger.debug("Using groq for vision model");
} else {
elizaLogger.debug("Using default openai for vision model");
this.provider = new OpenAIImageProvider(this.runtime);
Expand Down
Loading