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
Merged
Show file tree
Hide file tree
Changes from 9 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
1 change: 1 addition & 0 deletions app/domain/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
TutorChatPipelineExecutionDTO,
)
from .pyris_message import PyrisMessage, IrisMessageRole
from .pyris_image import PyrisImage
19 changes: 19 additions & 0 deletions app/domain/iris_message.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from enum import Enum
from pydantic import BaseModel
from typing import List, Optional
from .pyris_image import PyrisImage


class IrisMessageRole(str, Enum):
USER = "user"
ASSISTANT = "assistant"
SYSTEM = "system"


class IrisMessage(BaseModel):
text: str = ""
role: IrisMessageRole
images: Optional[List[PyrisImage]] = None

def __str__(self):
return f"{self.role.lower()}: {self.text}"
17 changes: 17 additions & 0 deletions app/domain/pyris_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from pydantic import BaseModel
from typing import Optional


class PyrisImage(BaseModel):
base64: str
prompt: Optional[str] = None
mime_type: Optional[str] = "jpeg"

class Config:
schema_extra = {
"example": {
"prompt": "Example prompt",
"base64": "base64EncodedString==",
"mime_type": "jpeg",
}
}
5 changes: 4 additions & 1 deletion app/llm/external/openai_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from openai import OpenAI
from openai.lib.azure import AzureOpenAI

from ...domain import PyrisImage
from ...llm import CompletionArguments
from ...llm.external.model import CompletionModel

Expand All @@ -11,7 +12,9 @@ class OpenAICompletionModel(CompletionModel):
api_key: str
_client: OpenAI

def complete(self, prompt: str, arguments: CompletionArguments) -> any:
def complete(
self, prompt: str, arguments: CompletionArguments, images: [PyrisImage] = None
) -> any:
response = self._client.completions.create(
model=self.model,
prompt=prompt,
Expand Down
60 changes: 60 additions & 0 deletions app/llm/external/openai_dalle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import base64
from datetime import datetime
from typing import Literal, Any

import requests
from openai import OpenAI

from ...domain.pyris_image import PyrisImage
from ...llm.external.model import ImageGenerationModel


class OpenAIDalleWrapper(ImageGenerationModel):
type: Literal["openai_dalle"]
model: str
_client: OpenAI

def model_post_init(self, __context: Any) -> None:
self._client = OpenAI(api_key=self.api_key)

def generate_images(
self,
prompt: str,
n: int = 1,
size: Literal[
"256x256", "512x512", "1024x1024", "1792x1024", "1024x1792"
] = "256x256",
quality: Literal["standard", "hd"] = "standard",
**kwargs
) -> [PyrisImage]:
response = self._client.images.generate(
model=self.model,
prompt=prompt,
size=size,
quality=quality,
n=n,
response_format="url",
**kwargs
)

images = response.data
iris_images = []
for image in images:
if image.revised_prompt is None:
image.revised_prompt = prompt
if image.b64_json is None:
image_response = requests.get(image.url)
image.b64_json = base64.b64encode(image_response.content).decode(
"utf-8"
)

iris_images.append(
PyrisImage(
prompt=image.revised_prompt,
base64=image.b64_json,
timestamp=datetime.fromtimestamp(response.created),
raw_data=image,
)
)

return iris_images
Loading