Spaces:
Sleeping
Sleeping
File size: 1,505 Bytes
0a518ff |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
from typing import Any, List
from langchain_core.language_models.chat_models import BaseChatModel
from langchain_core.messages import AIMessage
from src.basicchatbot.state.state import BasicChatBotState
class ChatbotWithToolsNode:
"""Handles chatbot interactions using an LLM and associated tools."""
def __init__(self, model: BaseChatModel, tools: List[Any]) -> None:
"""
Initialize the chatbot node with a model and tools.
Args:
model (BaseChatModel): The language model used for processing messages.
tools (List[Any]): A list of tools that can be used with the chatbot.
"""
self.llm = model
self.tools = tools
def node(self, state: BasicChatBotState) -> dict:
"""
Processes the chatbot state and generates a response.
Args:
state (BasicChatBotState): The current chatbot state containing messages.
Returns:
dict: A dictionary containing the chatbot's response messages.
"""
try:
messages = state.get("messages", [])
if not messages:
return {"messages": [AIMessage(content="ERROR: `messages` key is missing in the state. Contact developer to fix.")]}
response = self.llm.bind_tools(self.tools).invoke(input=messages)
return {"messages": [response] }
except Exception as e:
return {"messages": [AIMessage(content=f"Error processing request: {str(e)}")]}
|