From 03110b4a80677377b41eda008aa45ef9b4fb077b Mon Sep 17 00:00:00 2001 From: retoor Date: Fri, 7 Mar 2025 17:28:58 +0100 Subject: [PATCH] New example, new feature. --- Makefile | 6 ++++ setup.cfg | 3 ++ src/ragent/__init__.py | 74 ++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 81 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 2f151ee..85ccd6b 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,7 @@ DEMO_REPLIKA = .venv/bin/ragent.demo_replika DEMO_DISCUSS = .venv/bin/ragent.demo_discuss DEMO_BATCH_MESSAGES = .venv/bin/ragent.demo_batch_messages DEMO_TOOLS = .venv/bin/ragent.demo_tools +DEMO_VISION = .venv/bin/ragent.demo_vision PIP = .venv/bin/pip default: @@ -29,3 +30,8 @@ demo_batch_messages: demo_tools: @echo "Executing Tools Demo." $(DEMO_TOOLS) + +demo_vision: + @echo "Executing Vision Demo." + $(DEMO_VISION) + diff --git a/setup.cfg b/setup.cfg index b410ae5..ba28441 100644 --- a/setup.cfg +++ b/setup.cfg @@ -25,4 +25,7 @@ console_scripts = ragent.demo_replika = ragent.demo_replika:main ragent.demo_batch_messages = ragent.demo_batch_messages:main ragent.demo_tools = ragent.demo_tools:main + ragent.demo_vision = ragent.demo_vision:main + + diff --git a/src/ragent/__init__.py b/src/ragent/__init__.py index 781a716..8c8a00e 100644 --- a/src/ragent/__init__.py +++ b/src/ragent/__init__.py @@ -37,6 +37,7 @@ import pathlib import logging import sys import os +import re from typing import Any, Dict import json import inspect @@ -221,6 +222,33 @@ class Agent: self.thread = self.client.beta.threads.create() log.debug(f"Created thread with name {self.thread.id} for assistant {self.assistant.id}.") + + def get_file_by_name(self, name: str): + for file in self.client.files.list().data: + if file.filename == str(name): + log.debug(f"Found file with name: {name} and id: {file.id}.") + return file + log.debug(f"File with name: {name} not found.") + return None + + + + def get_or_create_file(self, path: str): + path = pathlib.Path(path) + files = self.client.files.list().data + file = self.get_file_by_name(path) + if not file: + log.debug(f"File with name: {path} not found. Creating...") + file = self.client.files.create( + file=open(path, "rb"), + purpose="assistants" + ) + log.debug(f"Created file with name: {path} and id: {file.id}.") + return file.id + else: + log.debug(f"Found file with name: {path} and id: {file.id}.") + return file.id + @property def _assistants(self): @@ -334,8 +362,50 @@ class Agent: return function_schema - def communicate(self, message: str, role: str = "user"): + def extract_image_urls(self,text): + image_url_pattern = r'https?://\S+\.(?:png|jpg|jpeg|gif|bmp|webp|svg)(?:\?\S*)?' + + return re.findall(image_url_pattern, text, re.IGNORECASE) + + def communicate_images(self, message:str): + messages = message + if not isinstance(messages, list): + messages = [message] + + for message in messages: + if isinstance(message, dict): + message_role = message["role"] + message = message["content"] + else: + message_role = "user" + + for image_path in self.extract_image_urls(message): + response = self.client.chat.completions.create( + model="gpt-4o-mini", + messages=[ + { + "role": "user", + "content": [ + {"type": "text", "text": "Describe the image in full detail, in a way that a human could understand."}, + { + "type": "image_url", + "image_url": { + "url": image_path, + }, + }, + ], + } + ], + ) + response = response.choices[0].message.content + response = f"This is the description of the image from the url {image_path} i have mentioned: " + response + self.communicate(response,role="user",ignore_image_urls=True) + + + def communicate(self, message: str, role: str = "user", ignore_image_urls:bool=False): log.debug(f"Sending message: {message} to assistant {self.assistant.id} in thread {self.thread.id}.") + if not ignore_image_urls: + self.communicate_images(message) messages = isinstance(message, list) and message or [message] for message in messages: if isinstance(message, dict): @@ -415,4 +485,4 @@ def main(): if __name__ == "__main__": - main() \ No newline at end of file + main()