~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
working on benchmarks
LearningCircuitfe9cec036dc0
diff --git a/.gitignore b/.gitignoreindex c2523c4d54..98c6c63a8e 100644--- a/.gitignore+++ b/.gitignore@@ -56,6 +56,7 @@ research_history.db# Data.cache+*.jsonl# Toolspython-db/diff --git a/src/benchmark-simple-qa.py b/src/benchmark-simple-qa.pynew file mode 100644index 0000000000..e695895584--- /dev/null+++ b/src/benchmark-simple-qa.py@@ -0,0 +1,127 @@+import json+import os+import random+from typing import Optional++import pandas as pd++from local_deep_research.api.research_functions import quick_summary+++def run_simpleqa_evaluation(+ dataset_path: str = "https://openaipublic.blob.core.windows.net/simple-evals/simple_qa_test_set.csv",+ output_file: str = "ldr_simpleqa_results.jsonl",+ num_examples: Optional[int] = None,+ seed: int = 42,+):+ """+ Run the SimpleQA evaluation using Local Deep Research.+ """+ # Load SimpleQA dataset+ print(f"Loading dataset from {dataset_path}")+ df = pd.read_csv(dataset_path)++ examples = [row.to_dict() for _, row in df.iterrows()]++ # Sample examples if specified+ if num_examples and num_examples < len(examples):+ random.seed(seed)+ examples = random.sample(examples, num_examples)++ # Remove output file if it exists to avoid appending+ if os.path.exists(output_file):+ os.remove(output_file)++ results = []++ # Process each question+ for i, example in enumerate(examples):+ question = example.get("problem", "")+ gold_answer = example.get("answer", "")++ print(f"Processing {i + 1}/{len(examples)}: {question}")++ try:+ # Using only the query parameter to avoid errors+ summary = quick_summary(+ query=question,+ iterations=3,+ questions_per_iteration=9,+ search_tool="searxng",+ )++ # Extract the answer+ answer = summary.get("summary", "")++ # Clean up the answer for better evaluation+ answer = answer.replace("[1]", "").replace("[2]", "").replace("[3]", "")+ answer = " ".join(+ [line for line in answer.split("\n") if not line.startswith("[")]+ )++ # Format result for output+ result = {+ "id": example.get("id", f"q{i}"),+ "problem": question,+ "answer": gold_answer,+ "predicted_answer": answer,+ }++ # Write incrementally to output file+ with open(output_file, "a") as f:+ f.write(json.dumps(result) + "\n")++ results.append(result)++ except Exception as e:+ print(f"Error processing question {i + 1}: {str(e)}")+ # In case of error, write a placeholder result+ result = {+ "id": example.get("id", f"q{i}"),+ "problem": question,+ "answer": gold_answer,+ "predicted_answer": "I don't have enough information to answer this question accurately.",+ }+ with open(output_file, "a") as f:+ f.write(json.dumps(result) + "\n")++ results.append(result)++ print(f"Evaluation complete. Results saved to {output_file}")+ return results+++def summarize_results(results_file: str):+ """+ Provide a basic summary of results without running the actual grader.+ """+ results = []+ with open(results_file, "r") as f:+ for line in f:+ if line.strip():+ results.append(json.loads(line))++ total = len(results)+ answer_lengths = [len(r.get("predicted_answer", "")) for r in results]++ print(f"Total questions: {total}")+ print(f"Average answer length: {sum(answer_lengths) / total:.1f} characters")+ print(f"Min answer length: {min(answer_lengths)} characters")+ print(f"Max answer length: {max(answer_lengths)} characters")++ # Sample a few results+ print("\nSample questions and answers:")+ for i in range(min(3, total)):+ print(f"\nQ: {results[i].get('problem', '')}")+ print(f"Gold: {results[i].get('answer', '')}")+ print(f"Predicted: {results[i].get('predicted_answer', '')}")+++# Main execution+if __name__ == "__main__":+ print("Starting SimpleQA benchmark...")+ # Start with a small sample for testing+ run_simpleqa_evaluation(num_examples=500)++ # Summarize results+ summarize_results("ldr_simpleqa_results.jsonl")diff --git a/src/benchmark_browser_comp.py b/src/benchmark_browser_comp.pynew file mode 100644index 0000000000..b2e1d24ca8--- /dev/null+++ b/src/benchmark_browser_comp.py@@ -0,0 +1,356 @@+import base64+import hashlib+import json+import os+import random+import re+import sys+from typing import Optional++import pandas as pd++# Import LangChain components for LLM access+from langchain.chat_models import ChatOpenAI+from langchain.schema import HumanMessage+from langchain_community.llms import Ollama++try:+ from local_deep_research.api import quick_summary+ from local_deep_research.config.llm_config import get_llm+except ImportError as e:+ print(f"Error importing from api: {e}")+ sys.exit(1)++# The template for formatting queries to the model+QUERY_TEMPLATE = """+{Question}++Your response should be in the following format:+Explanation: {{your explanation for your final answer}}+Exact Answer: {{your succinct, final answer}}+Confidence: {{your confidence score between 0% and 100% for your answer}}+""".strip()++# Grader template from BrowseComp+GRADER_TEMPLATE = """+Judge whether the following [response] to [question] is correct or not based on the precise and unambiguous [correct_answer] below.++[question]: {question}++[response]: {response}++Your judgement must be in the format and criteria specified below:++extracted_final_answer: The final exact answer extracted from the [response]. Put the extracted answer as 'None' if there is no exact, final answer to extract from the response.++[correct_answer]: {correct_answer}++reasoning: Explain why the extracted_final_answer is correct or incorrect based on [correct_answer], focusing only on if there are meaningful differences between [correct_answer] and the extracted_final_answer. Do not comment on any background to the problem, do not attempt to solve the problem, do not argue for any answer different than [correct_answer], focus only on whether the answers match.++correct: Answer 'yes' if extracted_final_answer matches the [correct_answer] given above, or is within a small margin of error for numerical problems. Answer 'no' otherwise, i.e. if there if there is any inconsistency, ambiguity, non-equivalency, or if the extracted answer is incorrect.++confidence: The extracted confidence score between 0% and 100% from [response]. Put 100 if there is no confidence score available.+""".strip()+++def derive_key(password: str, length: int) -> bytes:+ """Derive a fixed-length key from the password using SHA256."""+ hasher = hashlib.sha256()+ hasher.update(password.encode())+ key = hasher.digest()+ return key * (length // len(key)) + key[: length % len(key)]+++def decrypt(ciphertext_b64: str, password: str) -> str:+ """Decrypt base64-encoded ciphertext with XOR."""+ encrypted = base64.b64decode(ciphertext_b64)+ key = derive_key(password, len(encrypted))+ decrypted = bytes(a ^ b for a, b in zip(encrypted, key))+ return decrypted.decode()+++def run_browsecomp_evaluation(+ dataset_path: str = "https://openaipublic.blob.core.windows.net/simple-evals/browse_comp_test_set.csv",+ output_file: str = "ldr_browsecomp_results.jsonl",+ num_examples: Optional[int] = None,+ seed: int = 42,+):+ """+ Run the BrowseComp evaluation using Local Deep Research.+ """+ # Load BrowseComp dataset+ print(f"Loading dataset from {dataset_path}")+ df = pd.read_csv(dataset_path)++ examples = [row.to_dict() for _, row in df.iterrows()]++ # Sample examples if specified+ if num_examples and num_examples < len(examples):+ random.seed(seed)+ examples = random.sample(examples, num_examples)++ # Remove output file if it exists to avoid appending+ if os.path.exists(output_file):+ os.remove(output_file)++ results = []++ # Process each question+ for i, example in enumerate(examples):+ # Decrypt the problem and answer using the canary+ try:+ problem = decrypt(example.get("problem", ""), example.get("canary", ""))+ correct_answer = decrypt(+ example.get("answer", ""), example.get("canary", "")+ )+ except Exception as e:+ print(f"Error decrypting problem/answer: {e}")+ continue++ # Format the question using the QUERY_TEMPLATE+ formatted_question = QUERY_TEMPLATE.format(Question=problem)++ print(f"Processing {i + 1}/{len(examples)}: {problem[:50]}...")++ try:+ # Query using quick_summary+ # summary = quick_summary(query=formatted_question)+ summary = quick_summary(+ query=formatted_question,+ iterations=5,+ questions_per_iteration=9,+ search_tool="searxng",+ )+ # Extract the response+ response = summary.get("summary", "")++ # Clean up the response for better evaluation+ response = response.replace("[1]", "").replace("[2]", "").replace("[3]", "")+ response = " ".join(+ [line for line in response.split("\n") if not line.startswith("[")]+ )++ # Extract the final answer from the response+ answer_match = re.search(r"Exact Answer:\s*(.*?)(?:\n|$)", response)+ exact_answer = answer_match.group(1).strip() if answer_match else "None"++ # Extract confidence from the response+ confidence_match = re.search(r"Confidence:\s*(\d+)%", response)+ confidence = confidence_match.group(1) if confidence_match else "100"++ # Format result for output+ result = {+ "id": example.get("id", f"q{i}"),+ "problem": problem,+ "correct_answer": correct_answer,+ "response": response,+ "extracted_answer": exact_answer,+ "confidence": confidence,+ }++ # Write incrementally to output file+ with open(output_file, "a") as f:+ f.write(json.dumps(result) + "\n")++ results.append(result)++ except Exception as e:+ print(f"Error processing question {i + 1}: {str(e)}")+ # In case of error, write a placeholder result+ result = {+ "id": example.get("id", f"q{i}"),+ "problem": problem,+ "correct_answer": correct_answer,+ "response": "Error processing this question",+ "extracted_answer": "None",+ "confidence": "0",+ }+ with open(output_file, "a") as f:+ f.write(json.dumps(result) + "\n")++ results.append(result)++ print(f"Evaluation complete. Results saved to {output_file}")+ return results+++def langchain_grader(+ results_file: str,+ output_file: str = "graded_results.jsonl",+ model_name: str = "gpt-3.5-turbo",+ api_key: Optional[str] = None,+ use_ollama: bool = True,+ ollama_model: str = "gemma:latest",+):+ """+ Grade BrowseComp results using a LangChain LLM.++ Args:+ results_file: Path to results file to grade+ output_file: Path to save graded results+ model_name: OpenAI model name to use (if not using Ollama)+ api_key: OpenAI API key (if not using Ollama)+ use_ollama: Whether to use Ollama instead of OpenAI+ ollama_model: Ollama model to use if use_ollama is True+ """+ # Initialize LLM+ if use_ollama:+ try:+ print(f"Using Ollama with model {ollama_model}")+ llm = Ollama(model=ollama_model)+ except Exception as e:+ print(f"Error initializing Ollama: {e}")+ print("Falling back to LDR's LLM")+ llm = get_llm()+ else:+ if not api_key:+ print("No API key provided. Using LDR's LLM.")+ llm = get_llm()+ else:+ try:+ print(f"Using OpenAI with model {model_name}")+ llm = ChatOpenAI(model=model_name, api_key=api_key, temperature=0)+ except Exception as e:+ print(f"Error initializing ChatOpenAI: {e}")+ print("Falling back to LDR's LLM")+ llm = get_llm()++ # Load results to grade+ results = []+ with open(results_file, "r") as f:+ for line in f:+ if line.strip():+ results.append(json.loads(line))++ # Remove output file if it exists+ if os.path.exists(output_file):+ os.remove(output_file)++ graded_results = []+ correct_count = 0++ for idx, result in enumerate(results):+ question = result.get("problem", "")+ correct_answer = result.get("correct_answer", "")+ response = result.get("response", "")++ print(f"Grading {idx + 1}/{len(results)}...")++ # Format grading prompt+ grading_prompt = GRADER_TEMPLATE.format(+ question=question, correct_answer=correct_answer, response=response+ )++ try:+ # Grade using LLM+ if isinstance(llm, ChatOpenAI):+ grading_response = llm.invoke(+ [HumanMessage(content=grading_prompt)]+ ).content+ else:+ # For other LLM types+ grading_response = llm.invoke(grading_prompt)+ if hasattr(grading_response, "content"):+ grading_response = grading_response.content++ # Extract grading information using regex+ extracted_answer_match = re.search(+ r"extracted_final_answer:\s*(.*?)(?:\n|$)", grading_response+ )+ extracted_answer = (+ extracted_answer_match.group(1).strip()+ if extracted_answer_match+ else "None"+ )++ reasoning_match = re.search(+ r"reasoning:\s*(.*?)(?:\n\n|\Z)", grading_response, re.DOTALL+ )+ reasoning = reasoning_match.group(1).strip() if reasoning_match else ""++ correct_match = re.search(+ r"correct:\s*(yes|no)", grading_response, re.IGNORECASE+ )+ is_correct = (+ (correct_match.group(1).lower() == "yes") if correct_match else False+ )++ confidence_match = re.search(r"confidence:\s*(\d+)", grading_response)+ confidence = confidence_match.group(1) if confidence_match else "100"++ if is_correct:+ correct_count += 1++ # Format graded result+ graded_result = result.copy()+ graded_result.update(+ {+ "extracted_by_grader": extracted_answer,+ "reasoning": reasoning,+ "is_correct": is_correct,+ "graded_confidence": confidence,+ "grader_response": grading_response,+ }+ )++ graded_results.append(graded_result)++ # Write incrementally to output file+ with open(output_file, "a") as f:+ f.write(json.dumps(graded_result) + "\n")++ except Exception as e:+ print(f"Error grading result {idx + 1}: {str(e)}")+ # In case of error, write ungraded result+ result["grading_error"] = str(e)+ with open(output_file, "a") as f:+ f.write(json.dumps(result) + "\n")++ accuracy = correct_count / len(results) if results else 0+ print(f"Grading complete. Accuracy: {accuracy:.3f}")+ print(f"Correct: {correct_count}/{len(results)}")++ return graded_results+++def summarize_graded_results(results_file: str):+ """+ Provide a summary of graded results.+ """+ results = []+ with open(results_file, "r") as f:+ for line in f:+ if line.strip():+ results.append(json.loads(line))++ total = len(results)+ correct_count = sum(1 for r in results if r.get("is_correct", False))+ accuracy = correct_count / total if total else 0++ print(f"Total questions: {total}")+ print(f"Correct answers: {correct_count}")+ print(f"Accuracy: {accuracy:.3f}")++ # Sample a few results with grading+ print("\nSample questions and grading:")+ for i in range(min(3, total)):+ print(f"\nQ: {results[i].get('problem', '')}")+ print(f"Correct answer: {results[i].get('correct_answer', '')}")+ print(f"Extracted answer: {results[i].get('extracted_answer', '')}")+ print(f"Extracted by grader: {results[i].get('extracted_by_grader', '')}")+ print(f"Correct: {results[i].get('is_correct', False)}")+ print(f"Reasoning: {results[i].get('reasoning', '')}")+++# Main execution+if __name__ == "__main__":+ print("Starting BrowseComp benchmark...")++ # Uncomment the line below to run the evaluation+ run_browsecomp_evaluation(num_examples=100)++ # Use LangChain-based grader with Ollama (if available)+ # langchain_grader("ldr_browsecomp_results.jsonl", use_ollama=True)++ # Summarize graded results+ # summarize_graded_results("graded_results.jsonl")