Skip to content

What is the point of "EmbeddedJsonModel" ? #696

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
hieubnt235 opened this issue May 8, 2025 · 0 comments
Open

What is the point of "EmbeddedJsonModel" ? #696

hieubnt235 opened this issue May 8, 2025 · 0 comments

Comments

@hieubnt235
Copy link

hieubnt235 commented May 8, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant