Skip to content

Commit

Permalink
Load message history to core chat from schema chat (#429)
Browse files Browse the repository at this point in the history
Co-authored-by: Philip Meier <[email protected]>
  • Loading branch information
blakerosenthal and pmeier authored Jun 13, 2024
1 parent 8ea62a3 commit 45d5f94
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
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
26 changes: 26 additions & 0 deletions ragna/deploy/_api/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ def from_core(cls, document: ragna.core.Document) -> Document:
name=document.name,
)

def to_core(self) -> ragna.core.Document:
return ragna.core.LocalDocument(
id=self.id,
name=self.name,
# TEMP: setting an empty metadata dict for now.
# Will be resolved as part of the "managed ragna" work:
# https://github.com/Quansight/ragna/issues/256
metadata={},
)


class DocumentUpload(BaseModel):
parameters: ragna.core.DocumentUploadParameters
Expand All @@ -50,6 +60,15 @@ def from_core(cls, source: ragna.core.Source) -> Source:
num_tokens=source.num_tokens,
)

def to_core(self) -> ragna.core.Source:
return ragna.core.Source(
id=self.id,
document=self.document.to_core(),
location=self.location,
content=self.content,
num_tokens=self.num_tokens,
)


class Message(BaseModel):
id: uuid.UUID = Field(default_factory=uuid.uuid4)
Expand All @@ -66,6 +85,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=[source.to_core() for source in self.sources],
)


class ChatMetadata(BaseModel):
name: str
Expand Down

0 comments on commit 45d5f94

Please sign in to comment.