New example, new feature.

This commit is contained in:
retoor 2025-03-07 17:28:58 +01:00
parent 1b3200c6da
commit 03110b4a80
3 changed files with 81 additions and 2 deletions

View File

@ -2,6 +2,7 @@ DEMO_REPLIKA = .venv/bin/ragent.demo_replika
DEMO_DISCUSS = .venv/bin/ragent.demo_discuss DEMO_DISCUSS = .venv/bin/ragent.demo_discuss
DEMO_BATCH_MESSAGES = .venv/bin/ragent.demo_batch_messages DEMO_BATCH_MESSAGES = .venv/bin/ragent.demo_batch_messages
DEMO_TOOLS = .venv/bin/ragent.demo_tools DEMO_TOOLS = .venv/bin/ragent.demo_tools
DEMO_VISION = .venv/bin/ragent.demo_vision
PIP = .venv/bin/pip PIP = .venv/bin/pip
default: default:
@ -29,3 +30,8 @@ demo_batch_messages:
demo_tools: demo_tools:
@echo "Executing Tools Demo." @echo "Executing Tools Demo."
$(DEMO_TOOLS) $(DEMO_TOOLS)
demo_vision:
@echo "Executing Vision Demo."
$(DEMO_VISION)

View File

@ -25,4 +25,7 @@ console_scripts =
ragent.demo_replika = ragent.demo_replika:main ragent.demo_replika = ragent.demo_replika:main
ragent.demo_batch_messages = ragent.demo_batch_messages:main ragent.demo_batch_messages = ragent.demo_batch_messages:main
ragent.demo_tools = ragent.demo_tools:main ragent.demo_tools = ragent.demo_tools:main
ragent.demo_vision = ragent.demo_vision:main

View File

@ -37,6 +37,7 @@ import pathlib
import logging import logging
import sys import sys
import os import os
import re
from typing import Any, Dict from typing import Any, Dict
import json import json
import inspect import inspect
@ -222,6 +223,33 @@ class Agent:
self.thread = self.client.beta.threads.create() self.thread = self.client.beta.threads.create()
log.debug(f"Created thread with name {self.thread.id} for assistant {self.assistant.id}.") 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 @property
def _assistants(self): def _assistants(self):
return self.client.beta.assistants.list(order="desc", limit=100).data return self.client.beta.assistants.list(order="desc", limit=100).data
@ -334,8 +362,50 @@ class Agent:
return function_schema 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}.") 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] messages = isinstance(message, list) and message or [message]
for message in messages: for message in messages:
if isinstance(message, dict): if isinstance(message, dict):