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

LLM: Add image recognition and generation support #89

Merged
merged 18 commits into from
Apr 29, 2024

Conversation

yassinsws
Copy link
Contributor

@yassinsws yassinsws commented Mar 31, 2024

Motivation

In the future we want to support images in our LLM subsystem. This PR aims to introduce support for this.

Description

To add support for Images we added a new domain model PyrisImage to represent images in a unified way.
We also added a basic wrapper for OpenAIs Dall-E to showcase how images can be created.
We also added support to pass images to Ollama models via normal and chat completion.
We also added support for OpenAI GPT4-Vision models

TEST Instructions

Copy the code provided in this Description in the Tutor chat pipeline class,
Add a vision model if you don't have one to your config and add it to llm_vision initialisation
Choose an image and call interpret_image(file_path) with the path to your image
run Artemis and Pyris v2, ask iris anything and wait for the interpretation of the image to be printed on the console of Pyris

def image_to_base64(file_path):
with open(file_path, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
return encoded_string

def interpret_image(img_base64: str ):
"""
Interpret the image passed
"""
img_base64 = image_to_base64(file_path)
image_interpretation_prompt = "Interpret the image passed"
image =ImageMessageContentDTO(base64=[img_base64], prompt=image_interpretation_prompt)
iris_message = PyrisMessage(
sender=IrisMessageRole.SYSTEM,
contents=[image]
)
llm_vision = BasicRequestHandler("")
response = llm_vision.chat(
[iris_message], CompletionArguments(temperature=0.2, max_tokens=1000)
)
print(response.contents[0].text_content)
return response.contents[0].text_content

coderabbitai[bot]
coderabbitai bot previously requested changes Mar 31, 2024
app/domain/pyris_image.py Outdated Show resolved Hide resolved
app/llm/external/openai_chat.py Outdated Show resolved Hide resolved
@yassinsws yassinsws self-assigned this Apr 1, 2024
@ls1intum ls1intum deleted a comment from coderabbitai bot Apr 1, 2024
@yassinsws yassinsws requested review from Hialus and kaancayli April 1, 2024 21:02
@Hialus Hialus changed the title Add Image support to llm LLM: Add image recognition and generation support Apr 2, 2024
@Hialus Hialus self-assigned this Apr 2, 2024
app/llm/external/model.py Outdated Show resolved Hide resolved
@Hialus Hialus added this to the 1.1.0 milestone Apr 24, 2024
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

app/llm/external/openai_completion.py Outdated Show resolved Hide resolved
app/llm/external/model.py Outdated Show resolved Hide resolved
@Hialus Hialus self-requested a review April 25, 2024 20:31
Copy link
Member

@Hialus Hialus left a comment

Choose a reason for hiding this comment

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

When starting I get this warning:

pyris-app | /usr/local/lib/python3.12/site-packages/pydantic/_internal/_config.py:334: UserWarning: Valid config keys have changed in V2:
pyris-app | * 'schema_extra' has been renamed to 'json_schema_extra'
pyris-app | warnings.warn(message, UserWarning)

I also tested this on pyris-test and TS5 and the tutor chat still works.
Please fix the warning and merge main, then this can get merged.

Copy link
Contributor

@kaancayli kaancayli left a comment

Choose a reason for hiding this comment

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

You need to solve the merge conflicts. We renamed the IrisMessage to PyrisMessage and also adjusted the llms accordingly. If you have question ask me anytime.

coderabbitai[bot]

This comment was marked as off-topic.

app/domain/iris_message.py Outdated Show resolved Hide resolved
@yassinsws yassinsws requested review from kaancayli and Hialus April 25, 2024 22:05
@yassinsws yassinsws requested a review from Hialus April 26, 2024 18:49
Copy link
Contributor

coderabbitai bot commented Apr 27, 2024

Walkthrough

The recent updates across the application focus on enhancing type safety, expanding functionality, and improving message handling components. These changes include stricter type hints, additions of new fields to data transfer objects, and advancements in image data support. Abstract classes and methods for image generation have been introduced to cater to diverse content handling requirements within the system.

Changes

File Path Change Summary
app/common/message_converters.py Updated map_role_to_str to use Literal for role parameter.
app/domain/.../__init__.py,
app/domain/data/image_message_content_dto.py
Added import for image_message_content_dto and updated ImageMessageContentDTO class.
app/domain/data/json_message_content_dto.py,
app/domain/data/text_message_content_dto.py,
app/domain/pyris_message.py
Enhanced DTOs and PyrisMessage class with model_config attribute and adjusted or added fields.
app/llm/external/model.py,
app/llm/external/openai_dalle.py
Introduced abstract class for image generation and a function for generating images.
app/llm/external/ollama.py,
app/llm/external/openai_chat.py
Enhanced message conversion functions to handle various content types and added image handling capabilities.
app/llm/request_handler/.../basic_request_handler.py,
app/llm/request_handler/.../request_handler_interface.py
Updated complete method to accept an optional image parameter.
app/pipeline/chat/tutor_chat_pipeline.py Added a comment for setting up the initial prompt in TutorChatPipelineExecutionDTO.

The changes reflect a comprehensive effort to improve type safety, extend functionality, and enhance content handling capabilities throughout the application, especially focusing on image data support and message conversions.


Recent Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 2c4e2ea and 272b01c.
Files selected for processing (1)
  • app/llm/external/openai_chat.py (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • app/llm/external/openai_chat.py

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

coderabbitai[bot]
coderabbitai bot previously approved these changes Apr 27, 2024
Copy link
Member

@Hialus Hialus left a comment

Choose a reason for hiding this comment

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

Went through the code and there were a couple of issues (Ollama did not work, only the first content of a message was used, ...). I already fixed most of them in 001b99d

Though I do have some more smaller questions.



class ImageMessageContentDTO(BaseModel):
image_data: Optional[str] = Field(alias="imageData", default=None)
base64: List[str] = Field(..., alias="base64") # List of base64-encoded strings
Copy link
Member

Choose a reason for hiding this comment

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

  1. The alias is redundant
  2. What is the ... for?
  3. What is the reason to have this as a list? If you want more than one image in a message you could simply add more than one ImageMessageContent

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You can have one message with multiple images. That's how it works in the openAI API call at least, so I think it makes more sense to have the list there.
Other that that I will erase the alias, thanks

Copy link
Member

Choose a reason for hiding this comment

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

You actually do not use this for the OpenAI API in any way. The only place where this would make sense is the Dalle model, as that returns multiple images, but even there you simple create separate ImageMessageContentDTO.
As it currently stands you could simply make this a single string and it would not really change anything behavior wise and would make things easier to handle.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nope this would be used for image interpretation(This can be useful for example for future interpretation of the lecture, if you have multiple pages where they have diagrams and you want them to be interpreted together)

Copy link
Member

@Hialus Hialus Apr 27, 2024

Choose a reason for hiding this comment

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

Yes, but you could simply use multiple message contents for that. That way (at least with GPT4V) you also can have text in between the images, instead of just having a bunch of images after one another.

And from a conceptual standpoint each message content should only include a single datapoint (e.g. a single text block, a single json object, and a single image)

Also from an Artemis database standpoint it would make more sense to have a separate content for each image.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think both versions make sense but let's do it in a simple text as it does not hinder the work for now.

app/domain/data/image_message_content_dto.py Outdated Show resolved Hide resolved
kaancayli
kaancayli previously approved these changes Apr 27, 2024
@yassinsws yassinsws dismissed stale reviews from kaancayli and coderabbitai[bot] via 6cf2f4e April 27, 2024 15:24
coderabbitai[bot]
coderabbitai bot previously approved these changes Apr 27, 2024
@yassinsws yassinsws requested a review from Hialus April 27, 2024 15:25
kaancayli
kaancayli previously approved these changes Apr 27, 2024
Copy link
Contributor

@kaancayli kaancayli left a comment

Choose a reason for hiding this comment

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

code

Comment on lines +52 to +57
iris_images.append(
ImageMessageContentDTO(
prompt=revised_prompt,
base64=base64_data,
)
)
Copy link
Member

Choose a reason for hiding this comment

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

This does not work, because ImageMessageContentDTO.base64 is an array.

Copy link
Member

@Hialus Hialus left a comment

Choose a reason for hiding this comment

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

The black linter is failing

@yassinsws yassinsws dismissed stale reviews from kaancayli and coderabbitai[bot] via 6fd7d26 April 27, 2024 20:11
@yassinsws yassinsws requested a review from Hialus April 27, 2024 20:11
coderabbitai[bot]
coderabbitai bot previously approved these changes Apr 27, 2024
coderabbitai[bot]
coderabbitai bot previously approved these changes Apr 28, 2024
@Hialus Hialus merged commit 6fa4c05 into main Apr 29, 2024
4 checks passed
@Hialus Hialus deleted the feature/llm/image_support branch April 29, 2024 10:55
isabellagessl pushed a commit that referenced this pull request Nov 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants