Skip to content

Commit

Permalink
Merge pull request #939 from andehr/thread-images
Browse files Browse the repository at this point in the history
add image files to thread via add method (supports Assistant API vision)
  • Loading branch information
zzstoatzz authored Jun 26, 2024
2 parents 1735b7d + 2e5ecfc commit 072bf6d
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/marvin/beta/assistants/threads.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ async def add_async(
role: str = "user",
code_interpreter_files: Optional[list[str]] = None,
file_search_files: Optional[list[str]] = None,
image_files: Optional[list[str]] = None,
) -> Message:
"""
Add a user message to the thread.
Expand All @@ -76,6 +77,8 @@ async def add_async(
if self.id is None:
await self.create_async()

content = [dict(text=message, type="text")]

# Upload files and collect their IDs
attachments = []
for fp in code_interpreter_files or []:
Expand All @@ -90,10 +93,16 @@ async def add_async(
attachments.append(
dict(file_id=response.id, tools=[dict(type="file_search")])
)
for fp in image_files or []:
with open(fp, mode="rb") as file:
response = await client.files.create(file=file, purpose="vision")
content.append(
dict(image_file=dict(file_id=response.id), type="image_file")
)

# Create the message with the attached files
response = await client.beta.threads.messages.create(
thread_id=self.id, role=role, content=message, attachments=attachments
thread_id=self.id, role=role, content=content, attachments=attachments
)
return response

Expand Down

0 comments on commit 072bf6d

Please sign in to comment.