|
| 1 | +import asyncio |
| 2 | +import os |
| 3 | +from datetime import datetime |
| 4 | +from math import ceil |
| 5 | +from time import perf_counter |
| 6 | +from uuid import uuid4 |
| 7 | + |
| 8 | +import pandas as pd |
| 9 | +from dotenv import load_dotenv |
| 10 | +from langchain_core.messages import AIMessage |
| 11 | +from langchain_core.tools import StructuredTool |
| 12 | +from langchain_mcp_adapters.tools import load_mcp_tools |
| 13 | +from langgraph.checkpoint.memory import InMemorySaver |
| 14 | +from langgraph.prebuilt import create_react_agent |
| 15 | +from mcp import ClientSession, StdioServerParameters |
| 16 | +from mcp.client.stdio import stdio_client |
| 17 | + |
| 18 | +from evals.models import QuestionRecord, ResponseTableRecord |
| 19 | +from prompt import get_movies_system_prompt |
| 20 | +from tools.find_movie_recommendations import find_movie_recommendations_tool |
| 21 | +from utils import get_questions_from_yaml, pre_model_hook |
| 22 | + |
| 23 | +if load_dotenv(): |
| 24 | + print("Loaded .env file") |
| 25 | +else: |
| 26 | + print("No .env file found") |
| 27 | + |
| 28 | +neo4j_cypher_mcp = StdioServerParameters( |
| 29 | + command="uvx", |
| 30 | + args=[ "[email protected]", "--transport", "stdio"], |
| 31 | + env={ |
| 32 | + "NEO4J_URI": os.getenv("NEO4J_URI"), |
| 33 | + "NEO4J_USERNAME": os.getenv("NEO4J_USERNAME"), |
| 34 | + "NEO4J_PASSWORD": os.getenv("NEO4J_PASSWORD"), |
| 35 | + "NEO4J_DATABASE": os.getenv("NEO4J_DATABASE"), |
| 36 | + }, |
| 37 | +) |
| 38 | + |
| 39 | +evals_loc = "evals/output/" |
| 40 | +eval_results = list() |
| 41 | + |
| 42 | + |
| 43 | +async def evaluate_single_question( |
| 44 | + question_dict: dict[str, str], |
| 45 | + prompt: str, |
| 46 | + tools: list[StructuredTool], |
| 47 | + model: str = "openai:gpt-4.1", |
| 48 | +) -> ResponseTableRecord: |
| 49 | + """ |
| 50 | + Initialize a fresh agent and evaluate a single question. |
| 51 | + """ |
| 52 | + try: |
| 53 | + assert question_dict.get("question") is not None, "Question not found" |
| 54 | + |
| 55 | + # create the thread id for the agent eval |
| 56 | + # use the question id if it exists, otherwise generate a random uuid |
| 57 | + thread_id = "eval-" + question_dict.get("id", str(uuid4())) |
| 58 | + config = {"configurable": {"thread_id": thread_id}} |
| 59 | + |
| 60 | + agent = create_react_agent( |
| 61 | + model=model, |
| 62 | + pre_model_hook=pre_model_hook, |
| 63 | + checkpointer=InMemorySaver(), |
| 64 | + tools=tools, |
| 65 | + prompt=prompt, |
| 66 | + ) |
| 67 | + |
| 68 | + response_time_start = perf_counter() |
| 69 | + response = await agent.ainvoke({"messages": question_dict["question"]}, config=config) |
| 70 | + response_time = perf_counter() - response_time_start |
| 71 | + |
| 72 | + tool_calls = [ |
| 73 | + tool_call |
| 74 | + for message in response["messages"] |
| 75 | + if isinstance(message, AIMessage) |
| 76 | + and hasattr(message, "tool_calls") |
| 77 | + and message.tool_calls |
| 78 | + for tool_call in message.tool_calls |
| 79 | + ] |
| 80 | + |
| 81 | + # capture all text2cypher queries |
| 82 | + cyphers = [c.get("args") for c in tool_calls if c.get("name") == "read_neo4j_cypher"] |
| 83 | + |
| 84 | + return ResponseTableRecord( |
| 85 | + question_id=question_dict.get("id"), |
| 86 | + question=question_dict.get("question"), |
| 87 | + expected_answer=question_dict.get("answer"), |
| 88 | + agent_final_answer=response["messages"][-1].content, |
| 89 | + generated_cypher=cyphers, |
| 90 | + model=model, |
| 91 | + available_tools=[t.name for t in tools], |
| 92 | + called_tools=tool_calls, |
| 93 | + num_messages=len(response["messages"]), |
| 94 | + num_llm_calls=len([m for m in response["messages"] if isinstance(m, AIMessage)]), |
| 95 | + num_tool_calls=len(tool_calls), |
| 96 | + response_time=response_time, |
| 97 | + error=None, |
| 98 | + ) |
| 99 | + |
| 100 | + except Exception as e: |
| 101 | + print(f"Error: {e}") |
| 102 | + return ResponseTableRecord( |
| 103 | + question_id=question_dict.get("id"), |
| 104 | + question=question_dict.get("question"), |
| 105 | + expected_answer=question_dict.get("answer"), |
| 106 | + agent_final_answer=None, |
| 107 | + generated_cypher=list(), |
| 108 | + model=model, |
| 109 | + available_tools=[t.name for t in tools], |
| 110 | + called_tools=list(), |
| 111 | + num_messages=None, |
| 112 | + num_llm_calls=None, |
| 113 | + num_tool_calls=None, |
| 114 | + response_time=None, |
| 115 | + error=str(e), |
| 116 | + ) |
| 117 | + |
| 118 | + |
| 119 | +async def _evaluate_single_batch( |
| 120 | + batch: list[QuestionRecord], |
| 121 | + prompt: str, |
| 122 | + tools: list[StructuredTool], |
| 123 | + model: str = "openai:gpt-4.1", |
| 124 | +) -> list[ResponseTableRecord]: |
| 125 | + """ |
| 126 | + Evaluate a batch of questions asynchronously. |
| 127 | +
|
| 128 | + Parameters |
| 129 | + ---------- |
| 130 | + batch : list[QuestionRecord] |
| 131 | + A list of question records containing the question, expected answer and the question id. |
| 132 | +
|
| 133 | + Returns |
| 134 | + ------- |
| 135 | + list[ResponseTableRecord] |
| 136 | + A list of response table records containing the agent response and associated metadata. |
| 137 | + """ |
| 138 | + |
| 139 | + tasks = [ |
| 140 | + evaluate_single_question(question_dict, prompt, tools, model) for question_dict in batch |
| 141 | + ] |
| 142 | + return await asyncio.gather(*tasks) |
| 143 | + |
| 144 | + |
| 145 | +async def _evaluate_batches( |
| 146 | + questions: list[QuestionRecord], |
| 147 | + prompt: str, |
| 148 | + tools: list[StructuredTool], |
| 149 | + model: str = "openai:gpt-4.1", |
| 150 | + batch_size: int = 10, |
| 151 | +) -> list[ResponseTableRecord]: |
| 152 | + """ |
| 153 | + Create embeddings for a Pandas DataFrame of text chunks in batches. |
| 154 | +
|
| 155 | + Parameters |
| 156 | + ---------- |
| 157 | + questions : list[QuestionRecord] |
| 158 | + A list of question records containing the question, expected answer and the question id. |
| 159 | + prompt : str |
| 160 | + The system prompt to use. |
| 161 | + tools : list[StructuredTool] |
| 162 | + The tools to use. |
| 163 | + model : str |
| 164 | + The model to use. |
| 165 | + batch_size : int |
| 166 | + The number of questions to process in each batch. |
| 167 | +
|
| 168 | + Returns |
| 169 | + ------- |
| 170 | + list[ResponseTableRecord] |
| 171 | + A list of response table records containing the agent response and associated metadata. |
| 172 | + """ |
| 173 | + |
| 174 | + results = list() |
| 175 | + for batch_idx, i in enumerate(range(0, len(questions), batch_size)): |
| 176 | + print( |
| 177 | + f"Processing batch {batch_idx + 1} of {ceil(len(questions) / (batch_size))} \n", |
| 178 | + end="\r", |
| 179 | + ) |
| 180 | + if i + batch_size >= len(questions): |
| 181 | + batch = questions[i:] |
| 182 | + else: |
| 183 | + batch = questions[i : i + batch_size] |
| 184 | + batch_results = await _evaluate_single_batch(batch, prompt, tools, model) |
| 185 | + |
| 186 | + # Add extracted records to the results list |
| 187 | + results.extend(batch_results) |
| 188 | + |
| 189 | + return results |
| 190 | + |
| 191 | + |
| 192 | +async def main(): |
| 193 | + """ |
| 194 | + Main function to run the agent. |
| 195 | +
|
| 196 | + Based on the documentation: |
| 197 | + https://github.com/langchain-ai/langchain-mcp-adapters?tab=readme-ov-file#client |
| 198 | + """ |
| 199 | + |
| 200 | + questions = get_questions_from_yaml("questions.yaml") |
| 201 | + print(f"Retrieved {len(questions)} questions for evaluation.") |
| 202 | + |
| 203 | + async with stdio_client(neo4j_cypher_mcp) as (read, write): |
| 204 | + async with ClientSession(read, write) as session: |
| 205 | + # Initialize the connection |
| 206 | + await session.initialize() |
| 207 | + |
| 208 | + # Get tools |
| 209 | + mcp_tools = await load_mcp_tools(session) |
| 210 | + |
| 211 | + # We only need to get schema and execute read queries from the Cypher MCP server |
| 212 | + allowed_tools = [ |
| 213 | + tool for tool in mcp_tools if tool.name in {"get_neo4j_schema", "read_neo4j_cypher"} |
| 214 | + ] |
| 215 | + |
| 216 | + # We can also add non-mcp tools for our agent to use |
| 217 | + allowed_tools.append(find_movie_recommendations_tool) |
| 218 | + |
| 219 | + prompt = get_movies_system_prompt() |
| 220 | + |
| 221 | + model = "openai:gpt-4.1" |
| 222 | + batch_size = 10 |
| 223 | + |
| 224 | + eval_results = await _evaluate_batches( |
| 225 | + questions, prompt, allowed_tools, model, batch_size |
| 226 | + ) |
| 227 | + |
| 228 | + df = pd.DataFrame(eval_results) |
| 229 | + |
| 230 | + df.to_csv( |
| 231 | + f"{evals_loc}eval_benchmark_results_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.csv", |
| 232 | + index=False, |
| 233 | + ) |
| 234 | + |
| 235 | + |
| 236 | +if __name__ == "__main__": |
| 237 | + asyncio.run(main()) |
0 commit comments