Design a testing and evaluation framework for RAG application using Pydantic AI. Main requirements:
code
Main goal are testing different llm performance in pdf file processing. I provide current model, prompt and pdf file. Llm return a json with values collected from pdf
I need a tool for convenient comparing output jsons
I want to change the model without changing the code
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
To design a testing and evaluation framework for a RAG (Retrieval-Augmented Generation) application using Pydantic AI, focusing on PDF processing and model comparison, we can leverage several features provided by Pydantic AI. Below is a structured approach to achieve your requirements:
1. Setup the Agent Framework
First, define an agent that can handle PDF processing and JSON output. The agent will be designed to accept different models and prompts dynamically.
python
from pydantic_ai import Agent, RunContext
from pydantic import BaseModel, Field
from typing import Dict, Any
import json
class PDFProcessingResult(BaseModel):
extracted_values: Dict[str, Any] = Field(description="Values extracted from the PDF")
class PDFProcessingAgent(Agent):
def __init__(self, model_name: str, system_prompt: str):
super().__init__(model_name, system_prompt=system_prompt, result_type=PDFProcessingResult)
async def process_pdf(self, pdf_file_path: str) -> PDFProcessingResult:
# Here you would add logic to process the PDF and extract information
# For now, we'll simulate this with a placeholder
extracted_values = {"example_key": "example_value"} # Replace with actual extraction logic
return PDFProcessingResult(extracted_values=extracted_values)
2. Dynamic Model and Prompt Switching
To change the model and prompt without altering the code, use dependency injection and configuration files or environment variables.
python
import os
def get_agent() -> PDFProcessingAgent:
model_name = os.getenv("MODEL_NAME", "default-model")
system_prompt = os.getenv("SYSTEM_PROMPT", "Default system prompt")
return PDFProcessingAgent(model_name, system_prompt)
3. Testing Framework
Create a testing framework that allows you to compare JSON outputs from different models. This framework can use Pydantic's validation to ensure the outputs are structured correctly.
python
from typing import List
from pydantic import ValidationError
def compare_results(results: List[PDFProcessingResult]):
for result in results:
try:
PDFProcessingResult(**result.dict())
print("Result is valid.")
except ValidationError as e:
print(f"Validation error: {e}")
def test_models(pdf_file_path: str, models: List[str]):
results = []
for model in models:
agent = PDFProcessingAgent(model, "Process the PDF and extract values.")
result = agent.process_pdf(pdf_file_path)
results.append(result)
compare_results(results)
4. Running Tests
Finally, run the tests by specifying different models and comparing their outputs.
Integrate this testing framework into your CI/CD pipeline to automatically test new models and prompts whenever they are updated. Use environment variables to control which models and prompts are tested.
Summary
This framework allows you to:
Dynamically switch models and prompts without code changes.
Validate and compare JSON outputs using Pydantic models.
Integrate testing into your development workflow easily.
This approach ensures that your RAG application remains flexible and maintainable, with a clear focus on evaluating the performance of different models in processing PDF files.