Open maciejmajek opened 3 weeks ago
docker run -p 6379:6379 -d redis:latest
import time
from tempfile import NamedTemporaryFile
from typing import List
import numpy as np
import redis
from PIL import Image
from pydantic import BaseModel
from rai.messages import preprocess_image
class MultimodalArtifact(
BaseModel
): # currently implemented as TypedDict, to be refactored
images: List[str] # base64 encoded images
audios: List[str]
with NamedTemporaryFile(suffix=".png") as f:
img = np.random.randint(0, 255, (3840, 2160, 3), dtype=np.uint8)
img = Image.fromarray(img)
img.save(f, format="PNG")
f.seek(0)
img = preprocess_image(f.name)
artifact = MultimodalArtifact(images=[img], audios=[])
db = redis.StrictRedis(host="localhost", port=6379, db=0)
db.set("artifact", artifact.json())
start = time.perf_counter()
x = db.get("artifact")
artifact = MultimodalArtifact.parse_raw(x)
print(f"Retrieved artifact in: {time.perf_counter()-start:.3f}s") # Retrieved artifact in: 0.092s
Feature RAI uses an artifact database for saving artifacts such as images, audios, videos etc. returned by ToolCalls.
Current solution Currently, a simple pickle file is used, which creates a bunch of problems due to tool calling concurrency (concurrent io ops). https://github.com/RobotecAI/rai/blob/99ff0a9774ab4f4376a3d7a991fb51518851f001/src/rai/rai/agents/state_based.py#L84-L100 Better alternative An alternative would be to use a database like redis with a fallback method if the server is not accessible.