Create and configure the LangGraph agent. Args: actor_id: Actor identifier for memory session_id: Session identifier for memory
(actor_id: str, session_id: str)
| 47 | |
| 48 | |
| 49 | def create_agent(actor_id: str, session_id: str): |
| 50 | """ |
| 51 | Create and configure the LangGraph agent. |
| 52 | |
| 53 | Args: |
| 54 | actor_id: Actor identifier for memory |
| 55 | session_id: Session identifier for memory |
| 56 | """ |
| 57 | global _current_actor_id, _current_session_id |
| 58 | _current_actor_id = actor_id |
| 59 | _current_session_id = session_id |
| 60 | |
| 61 | # Initialize LLM |
| 62 | llm = ChatBedrock( |
| 63 | model_id="us.anthropic.claude-sonnet-4-5-20250929-v1:0", |
| 64 | model_kwargs={"temperature": 0.1} |
| 65 | ) |
| 66 | |
| 67 | @tool |
| 68 | def list_events(): |
| 69 | """ |
| 70 | Retrieve recent conversation history from memory. |
| 71 | Use this tool when the user asks about previous conversations, |
| 72 | wants you to recall something, or references past context. |
| 73 | """ |
| 74 | if not memory_client or not MEMORY_ID: |
| 75 | return "Memory is not configured." |
| 76 | |
| 77 | try: |
| 78 | events = memory_client.list_events( |
| 79 | memory_id=MEMORY_ID, |
| 80 | actor_id=_current_actor_id, |
| 81 | session_id=_current_session_id, |
| 82 | max_results=10 |
| 83 | ) |
| 84 | |
| 85 | if not events: |
| 86 | return "No previous conversation history found." |
| 87 | |
| 88 | # Format events for the LLM |
| 89 | history = [] |
| 90 | for event in events: |
| 91 | for payload_item in event.get("payload", []): |
| 92 | if "conversational" in payload_item: |
| 93 | conv = payload_item["conversational"] |
| 94 | role = conv.get("role", "UNKNOWN") |
| 95 | text = conv.get("content", {}).get("text", "") |
| 96 | history.append(f"{role}: {text}") |
| 97 | |
| 98 | return "\n".join(history) if history else "No messages found." |
| 99 | |
| 100 | except Exception as e: |
| 101 | print(f"Error retrieving events: {e}") |
| 102 | return f"Error retrieving conversation history: {str(e)}" |
| 103 | |
| 104 | # Bind tools to the LLM |
| 105 | tools = [list_events] |
| 106 | llm_with_tools = llm.bind_tools(tools) |