I am getting this error: TypeError: Object of type Member is not serializable
when trying to have memory with a chain.
My program is a discord bot that answers with AI on_message, The discord part completely works and I have removed some functions in the shown code.
This works without the memory.
class WhiteTiger(discord.Client):
def __init__(self, intents):
super().__init__(intents=intents)
self.target_channel = None
self.llm = ChatOllama(
model="llama3.2",
temperature=0,
)
self.prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"<context>",
),
MessagesPlaceholder(variable_name="messages"),
]
)
self.chain = self.prompt | self.llm
self.init_memory()
def init_memory(self):
workflow = StateGraph(state_schema=DiscordMessagesState)
def call_model(state: DiscordMessagesState):
response = self.chain.invoke(state)
return {"messages": [response]}
workflow.add_edge(START, "model")
workflow.add_node("model", call_model)
self.memory = MemorySaver()
self.app = workflow.compile(checkpointer=self.memory)
self.config = {"configurable": {"thread_id": "abc1234"}}
async def on_message(self, message):
if (
self.target_channel
and message.channel.id == self.target_channel.id
and message.author.id != self.user.id
):
print(f"Message from {message.author}: {message.content}")
print(f"{message.author}: {message.content}")
reply = self.app.invoke(
{
"messages": [HumanMessage(f"{message.author}: {message.content}")],
"author": message.author,
},
self.config
)
await message.reply(
reply["messages"][-1].pretty_print(), mention_author=True
)
Thanks in advance!
This error occurs because you are passing a Member
object to a function which later tries to convert it to JSON and fails.
To fix that, you need to pass a member's name, ID, or other string or int which is serializable. It seems name
is the thing you are looking for:
reply = self.app.invoke(
{
"messages": [HumanMessage(f"{message.author}: {message.content}")],
"author": message.author.name,
},
self.config
)