Add from_lc_message method to Record class in schema.py
This commit is contained in:
parent
0d75e2905f
commit
dd7e5a7ab5
1 changed files with 16 additions and 0 deletions
|
|
@ -2,6 +2,7 @@ import copy
|
||||||
from typing import Literal, Optional
|
from typing import Literal, Optional
|
||||||
|
|
||||||
from langchain_core.documents import Document
|
from langchain_core.documents import Document
|
||||||
|
from langchain_core.messages import BaseMessage
|
||||||
from pydantic import BaseModel, model_validator
|
from pydantic import BaseModel, model_validator
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -54,6 +55,21 @@ class Record(BaseModel):
|
||||||
data["text"] = document.page_content
|
data["text"] = document.page_content
|
||||||
return cls(data=data, text_key="text")
|
return cls(data=data, text_key="text")
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_lc_message(cls, message: BaseMessage) -> "Record":
|
||||||
|
"""
|
||||||
|
Converts a BaseMessage to a Record.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
message (BaseMessage): The BaseMessage to convert.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Record: The converted Record.
|
||||||
|
"""
|
||||||
|
data = {"text": message.content}
|
||||||
|
data["metadata"] = message.to_json()
|
||||||
|
return cls(data=data, text_key="text")
|
||||||
|
|
||||||
def __add__(self, other: "Record") -> "Record":
|
def __add__(self, other: "Record") -> "Record":
|
||||||
"""
|
"""
|
||||||
Combines the data of two records by attempting to add values for overlapping keys
|
Combines the data of two records by attempting to add values for overlapping keys
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue