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

Image link support #25

Merged
merged 5 commits into from
Oct 24, 2024
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
1 change: 1 addition & 0 deletions app/adapters/openai/gpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from app import settings

VALID_IMAGE_EXTENSIONS: set[str] = {".png", ".jpg", ".jpeg", ".gif"}

openai_client = openai.AsyncOpenAI(
api_key=settings.OPENAI_API_KEY,
Expand Down
56 changes: 36 additions & 20 deletions app/usecases/ai_conversations.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from app import discord_message_utils
from app import openai_functions
from app.adapters.openai import gpt
from app.adapters.openai.gpt import MessageContent
from app.errors import Error
from app.errors import ErrorCode
from app.models import DiscordBot
Expand Down Expand Up @@ -93,31 +94,46 @@ async def send_message_to_thread(
for m in thread_history[-tracked_thread.context_length :]
]

# Append this new message (along w/ any attachments) to the history
## Build the new message's prompt
image_urls: list[str] = []

# Extract image URLs from the prompt and replace them with {IMAGE}
prompt_parts = prompt.split()
images_seen = 0
for i, part in enumerate(prompt_parts):
if (part.startswith("http://") or part.startswith("https://")) and any(
part.endswith(ext) for ext in gpt.VALID_IMAGE_EXTENSIONS
):
image_urls.append(part)
prompt_parts[i] = f"{{IMAGE #{images_seen + 1}}}"
images_seen += 1
prompt = " ".join(prompt_parts)

# Add the new prompt and attachments to the message history
new_message_content: list[MessageContent] = []
new_message_content.append(
{
"type": "text",
"text": prompt,
}
)
for image_url in image_urls:
new_message_content.append(
{
"type": "image_url",
"image_url": {"url": image_url},
}
)
for attachment in message.attachments:
# TODO: should these be included as {IMAGE #N} at the end of the message?
image_urls.append(attachment.url)
cmyui marked this conversation as resolved.
Show resolved Hide resolved

message_history.append(
{
"role": "user",
"content": [
{
"type": "text",
"text": prompt,
}
],
"content": new_message_content,
}
)
if message.attachments:
for attachment in message.attachments:
message_history.append(
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {"url": attachment.url},
}
],
}
)

functions = openai_functions.get_full_openai_functions_schema()
try:
Expand Down