RobotecAI / rai

RAI is a multi-vendor agent framework for robotics, utilizing Langchain and ROS 2 tools to perform complex actions, defined scenarios, free interface execution, log summaries, voice interaction and more.
Apache License 2.0
84 stars 8 forks source link

Artifact Database #164

Open maciejmajek opened 3 weeks ago

maciejmajek commented 3 weeks ago

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.

maciejmajek commented 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