|

A Coding Implementation for an Agentic AI Framework that Performs Literature Analysis, Hypothesis Generation, Experimental Planning, Simulation, and Scientific Reporting

In this tutorial, we construct an entire scientific discovery agent step-by-step and expertise how every part works collectively to type a coherent analysis workflow. We start by loading our literature corpus, setting up retrieval and LLM modules, and then assembling brokers that search papers, generate hypotheses, design experiments, and produce structured reviews. Through snippets talked about beneath, we see how an agentic pipeline emerges naturally, permitting us to discover a scientific query from preliminary curiosity to a full evaluation inside a single, built-in system. Check out the FULL CODES here.

import sys, subprocess


def install_deps():
   pkgs = ["transformers", "scikit-learn", "numpy"]
   subprocess.check_call([sys.executable, "-m", "pip", "install", "-q"] + pkgs)


strive:
   from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
   from sklearn.feature_extraction.textual content import TfidfVectorizer
   from sklearn.metrics.pairwise import cosine_similarity
   import numpy as np
besides ImportError:
   install_deps()
   from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
   from sklearn.feature_extraction.textual content import TfidfVectorizer
   from sklearn.metrics.pairwise import cosine_similarity
   import numpy as np


from dataclasses import dataclass
from typing import List, Dict, Any


np.random.seed(42)


LITERATURE = [
   {"id": "P1","title": "Self-Supervised Protein Language Models for Structure Prediction","field": "computational biology",
    "abstract": "We explore transformer-based protein language models trained on millions of sequences. The models learn residue-level embeddings that improve secondary structure prediction and stability estimation."},
   {"id": "P2","title": "CRISPR Off-Target Detection Using Deep Learning","field": "genome editing",
    "abstract": "We propose a convolutional neural network architecture for predicting CRISPR-Cas9 off-target effects directly from genomic sequences, achieving state-of-the-art accuracy on GUIDE-seq datasets."},
   {"id": "P3","title": "Foundation Models for Scientific Equation Discovery","field": "scientific ML",
    "abstract": "Large language models are combined with symbolic regression to recover governing equations from noisy experimental observations in physics and fluid dynamics."},
   {"id": "P4","title": "Active Learning for Materials Property Optimization","field": "materials science",
    "abstract": "We integrate Bayesian optimization with graph neural networks to actively select candidate materials that maximize target properties while reducing experimental cost."},
   {"id": "P5","title": "Graph-Based Retrieval for Cross-Domain Literature Review","field": "NLP for science",
    "abstract": "We construct a heterogeneous citation and concept graph over multi-domain scientific papers and show that graph-aware retrieval improves cross-domain literature exploration."},
]


corpus_texts = [p["abstract"] + " " + p["title"] for p in LITERATURE]
vectorizer = TfidfVectorizer(stop_words="english")
corpus_matrix = vectorizer.fit_transform(corpus_texts)


MODEL_NAME = "google/flan-t5-small"
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
mannequin = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)


def generate_text(immediate: str, max_new_tokens: int = 256) -> str:
   inputs = tokenizer(immediate, return_tensors="pt", truncation=True)
   outputs = mannequin.generate(**inputs, max_new_tokens=max_new_tokens, num_beams=4, early_stopping=True)
   return tokenizer.decode(outputs[0], skip_special_tokens=True)

We laid the inspiration for our scientific agent by loading libraries, getting ready the literature corpus, and initializing our language mannequin. We construct the TF-IDF vectorizer and embed all abstracts to later retrieve related papers. With the mannequin loaded and knowledge structured, we create the computational spine for all the pieces that follows. Check out the FULL CODES here.

@dataclass
class PaperHit:
   paper: Dict[str, Any]
   rating: float


class LiteratureAgent:
   def __init__(self, vectorizer, corpus_matrix, papers: List[Dict[str, Any]]):
       self.vectorizer = vectorizer
       self.corpus_matrix = corpus_matrix
       self.papers = papers


   def search(self, question: str, okay: int = 3) -> List[PaperHit]:
       q_vec = self.vectorizer.remodel([query])
       sims = cosine_similarity(q_vec, self.corpus_matrix)[0]
       idxs = np.argsort(-sims)[:k]
       hits = [PaperHit(self.papers[i], float(sims[i])) for i in idxs]
       return hits

We implement the literature-search part of our agent. We convert person queries right into a vector area and determine probably the most related scientific papers utilizing cosine similarity. Through this, we give our system the flexibility to floor its reasoning within the closest-matching prior work. Check out the FULL CODES here.

@dataclass
class ExperimentPlan:
   system: str
   speculation: str
   variables: Dict[str, Any]
   protocol: List[str]


@dataclass
class ExperimentResult:
   plan: ExperimentPlan
   metrics: Dict[str, float]


class ExperimentAgent:
   def design_experiment(self, query: str, speculation: str, hits: List[PaperHit]) -> ExperimentPlan:
       top_field = hits[0].paper["field"] if hits else "computational science"
       protocol = [
           f"Construct dataset combining ideas from: {', '.join(h.paper['id'] for h in hits)}.",
           "Split knowledge into prepare/validation/take a look at.",
           "Compare baseline mannequin vs. augmented mannequin implementing the speculation.",
           "Evaluate utilizing acceptable metrics and carry out ablation evaluation.",
       ]
       variables = {
           "baseline_model": "sequence CNN",
           "augmented_model": "protein language mannequin + CNN",
           "n_train_samples": 5000,
           "n_validation_samples": 1000,
           "metric": "AUROC",
       }
       system = f"{top_field} system associated to: {query}"
       return ExperimentPlan(system=system, speculation=speculation, variables=variables, protocol=protocol)


   def run_experiment(self, plan: ExperimentPlan) -> ExperimentResult:
       base = 0.78 + 0.02 * np.random.randn()
       achieve = abs(0.05 + 0.01 * np.random.randn())
       metrics = {
           "baseline_AUROC": spherical(base, 3),
           "augmented_AUROC": spherical(base + achieve, 3),
           "estimated_gain": spherical(achieve, 3),
       }
       return ExperimentResult(plan=plan, metrics=metrics)

We design and simulate experiments based mostly on the retrieved literature and the generated speculation. We mechanically outline variables, construct a protocol, and generate artificial metrics that imitate the dynamics of an actual scientific analysis. This lets us transfer from theoretical concepts to an actionable experimental plan. Check out the FULL CODES here.

class ReportAgent:
   def write_report(self, query: str, hits: List[PaperHit], plan: ExperimentPlan, end result: ExperimentResult) -> str:
       related_work = "n".be part of(f"- {h.paper['title']} ({h.paper['field']})" for h in hits)
       protocol_str = "n".be part of(f"- {step}" for step in plan.protocol)
       immediate = f"""
You are an AI analysis assistant writing a concise research-style report.


Research query:
{query}


Hypothesis:
{plan.speculation}


Relevant prior work:
{related_work}


Planned experiment:
System: {plan.system}
Variables: {plan.variables}
Protocol:
{protocol_str}


Simulated outcomes:
{end result.metrics}


Write a transparent report with the next sections:
1. Background
2. Proposed Approach
3. Experimental Setup
4. Results and Discussion
5. Limitations and Future Work
"""
       return generate_text(immediate.strip(), max_new_tokens=320)

We generate a full research-style report utilizing the LLM. We assemble the speculation, protocol, outcomes, and associated work right into a structured doc with clearly outlined sections. This permits us to show the pipeline’s uncooked outputs into polished scientific communication. Check out the FULL CODES here.

class ScientificAgent:
   def __init__(self):
       self.lit_agent = LiteratureAgent(vectorizer, corpus_matrix, LITERATURE)
       self.exp_agent = ExperimentAgent()
       self.report_agent = ReportAgent()


   def propose_hypothesis(self, query: str, hits: List[PaperHit]) -> str:
       context = " ".be part of(h.paper["abstract"] for h in hits)
       immediate = f"""
You are an AI scientist. Given a analysis query and associated abstracts,
suggest a single, testable speculation in 2-3 sentences.


Research query:
{query}


Related abstracts:
{context}
"""
       return generate_text(immediate.strip(), max_new_tokens=96)


   def run_pipeline(self, query: str) -> str:
       hits = self.lit_agent.search(query, okay=3)
       speculation = self.propose_hypothesis(query, hits)
       plan = self.exp_agent.design_experiment(query, speculation, hits)
       end result = self.exp_agent.run_experiment(plan)
       report = self.report_agent.write_report(query, hits, plan, end result)
       return report


if __name__ == "__main__":
   research_question = (
       "How can protein language mannequin embeddings enhance CRISPR off-target "
       "prediction in comparison with sequence-only CNN baselines?"
   )
   agent = ScientificAgent()
   final_report = agent.run_pipeline(research_question)
   print(final_report)

We orchestrate your entire pipeline, looking out the literature, producing a speculation, designing the experiment, operating the simulation, and writing the report. We then execute the system on an actual analysis query and observe the whole workflow in motion. This step brings all of the modules collectively right into a unified scientific agent.

In conclusion, we see how a compact codebase can evolve right into a functioning AI co-researcher able to looking out, reasoning, simulating, and summarizing. We perceive how every snippet contributes to the complete pipeline and how agentic parts amplify each other when mixed. Also, we place ourselves in a powerful place to increase the agent with richer literature sources, extra sensible fashions, and extra refined experimental logic, pushing our scientific exploration additional with each iteration.


Check out the FULL CODES here. Feel free to take a look at our GitHub Page for Tutorials, Codes and Notebooks. Also, be happy to observe us on Twitter and don’t neglect to hitch our 100k+ ML SubReddit and Subscribe to our Newsletter. Wait! are you on telegram? now you can join us on telegram as well.

The put up A Coding Implementation for an Agentic AI Framework that Performs Literature Analysis, Hypothesis Generation, Experimental Planning, Simulation, and Scientific Reporting appeared first on MarkTechPost.

Similar Posts