Skip to content

What is the point of "EmbeddedJsonModel" ? #696

Open
@hieubnt235

Description

@hieubnt235

Hello guys, I try to understand what is the different of EmbeddedJsonModel and JsonModel and when to use it, i try some example to clarify but cannot find anything.

from redis.asyncio import Redis
from uuid_extensions import uuid7
from pydantic import AnyUrl, Field, BaseModel
from aredis_om import JsonModel, EmbeddedJsonModel, Migrator, Field as RField
from aredis_om.model.model import model_registry

from typing import Literal
from uuid import UUID

redis = Redis(host='localhost',port=3333,password="ssssss")

class Message(EmbeddedJsonModel):
	role: Literal['user', 'system', 'assistant']
	content: str
	class Meta:
		database = redis

class ChatSessionData(EmbeddedJsonModel): #  change here
	id: str = RField(index=True,primary_key=True)

	messages: list[Message] = Field(default_factory=list)
	summaries: list[str] = Field(default_factory=list)
	urls: list[AnyUrl] = Field(default_factory=list,description="Addition data should be store in object storage and provide url.")
	class Meta:
		database = redis

class UserData(JsonModel):
	username:str = RField(index=True,primary_key=True)
	password:str
	chat_sessions:list[ChatSessionData] = Field(default_factory=list)
	class Meta:
		database = redis

await Migrator().run() # create all indexes

chat_id = uuid7()
chat_id_str = str(chat_id)
chat_session_data = ChatSessionData(id = chat_id_str,
                                    messages=[Message(role = "user",content = "this is content")],
                                    summaries=["sum1","sum2"],
                                    urls = ["http://example.com"])
userdata = UserData(username="xxx", password="def",token = uuid7(),chat_sessions = [chat_session_data])

await userdata.save()

In the above code, i change EmbeddedJsonModel parent class to both JsonModel and EmbeddedJsonModel. And they store object completely the same in server (monitor in RedisInsight).

Thank you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions