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

Added new Multimodal AI Block utilizing openrouter's API Service. #8290

Open
wants to merge 39 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
dc61e78
Added Replicate Flux Blocks
ymrohit Sep 29, 2024
4d0ac7a
updated poetry lock file for replicate
ymrohit Sep 29, 2024
133ed10
Refactor ReplicateFluxAdvancedModelBlock to use an enum for replicate…
Torantulino Sep 29, 2024
e0cdfff
Refactor ReplicateFluxAdvancedModelBlock to use an enum for output_fo…
Torantulino Sep 29, 2024
cf67551
Refactor ReplicateFluxAdvancedModelBlock to stop requiring people to …
Torantulino Sep 29, 2024
ca54d06
Refactor ReplicateFluxAdvancedModelBlock to stop requiring people to …
Torantulino Sep 29, 2024
deb64d8
run format
Torantulino Sep 29, 2024
bf851f3
Merge branch 'toran/flux-block-tweaks' of https://github.com/Signific…
Torantulino Sep 29, 2024
75265b1
poetry run format
Torantulino Sep 29, 2024
ea6b171
Delete ReplicateFluxBasicModelBlock
Torantulino Sep 29, 2024
3a743d9
Mark model name as not advanced
Torantulino Sep 29, 2024
212e977
tweak input order
Torantulino Sep 29, 2024
a788aa2
Fix test
Torantulino Sep 29, 2024
df99c94
Merge pull request #1 from Significant-Gravitas/toran/flux-block-tweaks
ymrohit Sep 29, 2024
59b0eaa
Refactor ReplicateFluxAdvancedModelBlock to use an enum for output_fo…
Torantulino Sep 29, 2024
1ba3cdd
Refactor ReplicateFluxAdvancedModelBlock to stop requiring people to …
Torantulino Sep 29, 2024
aa82eec
poetry run format
Torantulino Sep 29, 2024
2844fca
Merge branch 'Significant-Gravitas-master'
ymrohit Oct 2, 2024
51f916f
Merge pull request #3 from Significant-Gravitas/master
ymrohit Oct 4, 2024
3c6bb99
Added Flux 1.1 pro option
ymrohit Oct 4, 2024
a295c40
Update replicate_flux_advanced.py
ymrohit Oct 4, 2024
68eeefd
Update replicate_flux_advanced.py
ymrohit Oct 4, 2024
44d902d
poetry linted
ymrohit Oct 4, 2024
ea35b9e
Merge branch 'master' into master
majdyz Oct 7, 2024
653ae5b
Merge branch 'Significant-Gravitas:master' into master
ymrohit Oct 7, 2024
f5b37ae
Merge branch 'Significant-Gravitas:master' into master
ymrohit Oct 8, 2024
6385163
Merge branch 'Significant-Gravitas:master' into master
ymrohit Oct 9, 2024
ddad41b
Merge branch 'Significant-Gravitas:master' into master
ymrohit Oct 10, 2024
6f15027
Created new Multimodal AI Block
ymrohit Oct 10, 2024
39110c7
added test mock
ymrohit Oct 10, 2024
8b31891
updated test_mock
ymrohit Oct 10, 2024
305bb0f
Merge branch 'dev' into master
ntindle Oct 16, 2024
b41f8f7
Merge branch 'dev' into master
aarushik93 Oct 16, 2024
be135ff
Merge branch 'dev' into master
aarushik93 Oct 17, 2024
0b824d3
Merge branch 'dev' into master
ntindle Oct 22, 2024
8352185
Merge branch 'Significant-Gravitas:master' into master
ymrohit Oct 24, 2024
97b9ee8
Merge branch 'dev' into master
Torantulino Oct 25, 2024
88885f1
feat(platform): Add default secrets (#8452)
aarushik93 Oct 25, 2024
be1b253
Merge branch 'Significant-Gravitas:master' into master
ymrohit Oct 30, 2024
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
3 changes: 3 additions & 0 deletions autogpt_platform/backend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ REPLICATE_API_KEY=
# Ideogram
IDEOGRAM_API_KEY=

# Openrouter
OPENROUTER_API_KEY=

# Logging Configuration
LOG_LEVEL=INFO
ENABLE_CLOUD_LOGGING=false
Expand Down
104 changes: 104 additions & 0 deletions autogpt_platform/backend/backend/blocks/multimodal_ai_block.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
from enum import Enum

from openai import OpenAI

from backend.data.block import Block, BlockCategory, BlockOutput, BlockSchema
from backend.data.model import BlockSecret, SchemaField, SecretField


# Model name enum
class MultimodalAIModelName(str, Enum):
LLAMA_VISION_11B = "meta-llama/llama-3.2-11b-vision-instruct:free"
LLAMA_VISION_90B = "meta-llama/llama-3.2-90b-vision-instruct"
PIXTRAL_12B = "mistralai/pixtral-12b"
CHATGPT_4O_LATEST = "openai/chatgpt-4o-latest"


class MultimodalAIBlock(Block):
class Input(BlockSchema):
api_key: BlockSecret = SecretField(
key="openrouter_api_key",
description="OpenRouter API Key",
advanced=False,
)
prompt: str = SchemaField(
description="Text prompt for multimodal AI response",
placeholder="e.g., 'Describe the contents of the image'",
title="Prompt",
)
image_url: str = SchemaField(
description="URL of the image to analyze",
placeholder="e.g., 'https://example.com/image.jpg'",
title="Image URL",
)
model_name: MultimodalAIModelName = SchemaField(
description="The name of the multimodal AI model",
default=MultimodalAIModelName.LLAMA_VISION_11B,
title="Multimodal AI Model",
advanced=False,
)

class Output(BlockSchema):
result: str = SchemaField(description="Generated output")
error: str = SchemaField(description="Error message if the model run failed")

def __init__(self):
super().__init__(
id="62f5be31-9896-40ed-bd71-2dda74459e20",
description="This block runs multimodal AI models on OpenRouter with advanced settings.",
categories={BlockCategory.AI},
input_schema=MultimodalAIBlock.Input,
output_schema=MultimodalAIBlock.Output,
test_input={
"api_key": "test_api_key",
"model_name": MultimodalAIModelName.LLAMA_VISION_11B,
"prompt": "Describe the contents of the image",
"image_url": "https://example.com/image.jpg",
},
test_output=[
(
"result",
"The image depicts a serene boardwalk surrounded by lush greenery.",
),
],
test_mock={
"run_model": lambda api_key, model_name, prompt, image_url: "The image depicts a serene boardwalk surrounded by lush greenery.",
},
)

def run(self, input_data: Input, **kwargs) -> BlockOutput:
try:
# Call the separated model execution logic
result = self.run_model(
api_key=input_data.api_key.get_secret_value(),
model_name=input_data.model_name,
prompt=input_data.prompt,
image_url=input_data.image_url,
)
yield "result", result
except Exception as e:
yield "error", str(e)

def run_model(self, api_key, model_name, prompt, image_url):
# Initialize OpenAI client with the API key
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key=api_key,
)

# Call the API to create a completion based on the input data
completion = client.chat.completions.create(
model=model_name,
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": prompt},
{"type": "image_url", "image_url": {"url": image_url}},
],
}
],
)

# Extract and return the content from the API response
return completion.choices[0].message.content
2 changes: 1 addition & 1 deletion autogpt_platform/backend/backend/util/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ class Secrets(UpdateTrackingModel["Secrets"], BaseSettings):
replicate_api_key: str = Field(default="", description="Replicate API Key")
unreal_speech_api_key: str = Field(default="", description="Unreal Speech API Key")
ideogram_api_key: str = Field(default="", description="Ideogram API Key")

openrouter_api_key: str = Field(default="", description="OpenRouter API Key")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please migrate to new credentials system

# Add more secret fields as needed

model_config = SettingsConfigDict(
Expand Down
Loading