-
Notifications
You must be signed in to change notification settings - Fork 44.4k
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
ymrohit
wants to merge
39
commits into
Significant-Gravitas:dev
Choose a base branch
from
ymrohit:master
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+124
−13
Open
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 4d0ac7a
updated poetry lock file for replicate
ymrohit 133ed10
Refactor ReplicateFluxAdvancedModelBlock to use an enum for replicate…
Torantulino e0cdfff
Refactor ReplicateFluxAdvancedModelBlock to use an enum for output_fo…
Torantulino cf67551
Refactor ReplicateFluxAdvancedModelBlock to stop requiring people to …
Torantulino ca54d06
Refactor ReplicateFluxAdvancedModelBlock to stop requiring people to …
Torantulino deb64d8
run format
Torantulino bf851f3
Merge branch 'toran/flux-block-tweaks' of https://github.com/Signific…
Torantulino 75265b1
poetry run format
Torantulino ea6b171
Delete ReplicateFluxBasicModelBlock
Torantulino 3a743d9
Mark model name as not advanced
Torantulino 212e977
tweak input order
Torantulino a788aa2
Fix test
Torantulino df99c94
Merge pull request #1 from Significant-Gravitas/toran/flux-block-tweaks
ymrohit 59b0eaa
Refactor ReplicateFluxAdvancedModelBlock to use an enum for output_fo…
Torantulino 1ba3cdd
Refactor ReplicateFluxAdvancedModelBlock to stop requiring people to …
Torantulino aa82eec
poetry run format
Torantulino 2844fca
Merge branch 'Significant-Gravitas-master'
ymrohit 51f916f
Merge pull request #3 from Significant-Gravitas/master
ymrohit 3c6bb99
Added Flux 1.1 pro option
ymrohit a295c40
Update replicate_flux_advanced.py
ymrohit 68eeefd
Update replicate_flux_advanced.py
ymrohit 44d902d
poetry linted
ymrohit ea35b9e
Merge branch 'master' into master
majdyz 653ae5b
Merge branch 'Significant-Gravitas:master' into master
ymrohit f5b37ae
Merge branch 'Significant-Gravitas:master' into master
ymrohit 6385163
Merge branch 'Significant-Gravitas:master' into master
ymrohit ddad41b
Merge branch 'Significant-Gravitas:master' into master
ymrohit 6f15027
Created new Multimodal AI Block
ymrohit 39110c7
added test mock
ymrohit 8b31891
updated test_mock
ymrohit 305bb0f
Merge branch 'dev' into master
ntindle b41f8f7
Merge branch 'dev' into master
aarushik93 be135ff
Merge branch 'dev' into master
aarushik93 0b824d3
Merge branch 'dev' into master
ntindle 8352185
Merge branch 'Significant-Gravitas:master' into master
ymrohit 97b9ee8
Merge branch 'dev' into master
Torantulino 88885f1
feat(platform): Add default secrets (#8452)
aarushik93 be1b253
Merge branch 'Significant-Gravitas:master' into master
ymrohit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
104 changes: 104 additions & 0 deletions
104
autogpt_platform/backend/backend/blocks/multimodal_ai_block.py
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,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 |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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