Skip to content
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

Load message history to core chat from schema chat #429

Merged
merged 6 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions ragna/deploy/_api/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,7 @@ def schema_to_core_chat(
chat_name=chat.metadata.name,
**chat.metadata.params,
)
# FIXME: We need to reconstruct the previous messages here. Right now this is
# not needed, because the chat itself never accesses past messages. However,
# if we implement a chat history feature, i.e. passing past messages to
# the assistant, this becomes crucial.
core_chat._messages = []
core_chat._messages = [message.to_core() for message in chat.messages]
core_chat._prepared = chat.prepared

return core_chat
Expand Down
7 changes: 7 additions & 0 deletions ragna/deploy/_api/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ def from_core(cls, message: ragna.core.Message) -> Message:
sources=[Source.from_core(source) for source in message.sources],
)

def to_core(self) -> ragna.core.Message:
return ragna.core.Message(
content=self.content,
role=self.role,
sources=[],
)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pmeier Tell me if this makes sense:
For the purpose of chat history, we don't need Sources for each individual message since they already exist on the chat object. So we don't need to rebuild them here (?)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since they already exist on the chat object

They don't. Source's are returned by the SourceStorage and are never saved on the Chat but only ever on the Message

ragna/ragna/core/_rag.py

Lines 221 to 227 in 8ea62a3

sources = await self._run(self.source_storage.retrieve, self.documents, prompt)
answer = Message(
content=self._run_gen(self.assistant.answer, prompt, sources),
role=MessageRole.ASSISTANT,
sources=sources,
)

If you have a look at the ORM model for the chat, we also have no connection to the sources table, but only the messages

class Chat(Base):
__tablename__ = "chats"
id = Column(types.Uuid, primary_key=True) # type: ignore[attr-defined]
user_id = Column(ForeignKey("users.id"))
name = Column(types.String, nullable=False)
documents = relationship(
"Document",
secondary=document_chat_association_table,
back_populates="chats",
)
source_storage = Column(types.String, nullable=False)
assistant = Column(types.String, nullable=False)
params = Column(Json, nullable=False)
messages = relationship(
"Message", cascade="all, delete", order_by="Message.timestamp"
)
prepared = Column(types.Boolean, nullable=False)

The messages table however is connected to the sources table

source_message_association_table = Table(
"source_message_association_table",
Base.metadata,
Column("source_id", ForeignKey("sources.id"), primary_key=True),
Column("message_id", ForeignKey("messages.id"), primary_key=True),
)



class ChatMetadata(BaseModel):
name: str
Expand Down