Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance langgraph integration to preserve metadata (explodinggradient…
…s#1878) # Enhance langgraph integration to preserve AI metadata ## Description This PR updates the `langgraph.py` integration to ensure that metadata are preserved. This enhancement is crucial for multi-agent scenarios where identifying the source AI is important for evaluation. ## Changes - Updated `langgraph.py` to ensure AI names and metadata are preserved. ## Motivation and Context In the current implementation, metadata such as the name assigned to an AI is not saved. In the era of multi-agent systems, it is essential to have information about which AI made a particular statement for accurate evaluation. This update addresses this issue by preserving the necessary metadata. ### Example Code and Output ```python import json from typing import List, Union from langchain_core.messages import AIMessage, HumanMessage, SystemMessage, ToolMessage import ragas.messages as r from ragas.integrations.langgraph import convert_message_with_metadata def test_convert_message_with_metadata(): from langchain_core.messages import HumanMessage, AIMessage human_message = HumanMessage(content="Hello", name="me", additional_kwargs={"key1": "value1"}) ai_message = AIMessage(content="Hi", name="ai_1", additional_kwargs={"tool_calls": [{"function": {"name": "tool1", "arguments": '{"arg1": "val1"}'}}]}) converted_messages = convert_message_with_metadata([human_message, ai_message]) for msg in converted_messages: print(f"Content: {msg.content}, Metadata: {msg.metadata}") if __name__ == "__main__": test_convert_message_with_metadata() ``` ``` Output Content: Hello, Metadata: {'additional_kwargs': {'key1': 'value1'}, 'response_metadata': {}, 'type': 'human', 'name': 'me', 'id': None, 'example': False} Content: Hi, Metadata: {'additional_kwargs': {'tool_calls': [{'function': {'name': 'tool1', 'arguments': '{"arg1": "val1"}'}}]}, 'response_metadata': {}, 'type': 'ai', 'name': 'ai_1', 'id': None, 'example': False, 'tool_calls': [{'name': 'tool1', 'args': {'arg1': 'val1'}, 'id': None, 'type': 'tool_call'}], 'invalid_tool_calls': [], 'usage_metadata': None} ```
- Loading branch information