Skip to content

Commit b2611e5

Browse files
MervinPraisonpraisonai-bot
andauthored
feat(examples): add scientific writing examples (CAJAL re-implementation) (#1644)
Re-implements the CAJAL / scientific-paper functionality that was reverted from the core SDK (PR #1611 / revert #1641) as proper examples per AGENTS.md: - examples/scientific_writing/scientific_writer.py Single-agent pattern: regular Agent + @tool functions, no subclass. Uses the CAJAL-4B HuggingFace model via litellm. - examples/scientific_writing/multi_agent_paper.py Multi-agent workflow: Literature Reviewer -> Methodologist -> Writer composed with AgentTeam + sequential Tasks (no custom Agent classes). - examples/scientific_writing/agents.yaml YAML parity for the multi-agent workflow (AGENTS.md §7 '3-way' rule). Why this location: - AGENTS.md §4.1: core SDK has no heavy domain implementations. - AGENTS.md §4.4: compose specialised Agents + tools, don't subclass Agent. - AGENTS.md §6.1: domain-specific behaviour belongs in @tool functions. - Wrapper / praisonai-tools were alternatives; examples/ keeps it discoverable and avoids bloating either package. Co-authored-by: praisonai-bot <bot@praisonai.com>
1 parent 78f0a71 commit b2611e5

3 files changed

Lines changed: 198 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Scientific paper workflow — YAML equivalent of multi_agent_paper.py
2+
# Run with:
3+
# praisonai agents run --file examples/scientific_writing/agents.yaml
4+
framework: praisonai
5+
topic: "Climate change effects on coral reef biodiversity"
6+
7+
roles:
8+
literature_reviewer:
9+
role: "Literature Reviewer"
10+
goal: "Survey recent academic work on {topic} and produce an APA review."
11+
backstory: "Expert in academic literature surveying and citation hygiene."
12+
llm: "gpt-4o-mini"
13+
tasks:
14+
review_literature:
15+
description: "Produce a concise literature review with 5-8 APA citations."
16+
expected_output: "APA-formatted literature review."
17+
18+
methodology_designer:
19+
role: "Methodology Designer"
20+
goal: "Design a reproducible research methodology for {topic}."
21+
backstory: "Specialises in rigorous experimental design."
22+
llm: "gpt-4o-mini"
23+
tasks:
24+
design_methodology:
25+
description: "Write a reproducible methods section in LaTeX."
26+
expected_output: "LaTeX methods section."
27+
28+
scientific_writer:
29+
role: "Scientific Writer"
30+
goal: "Assemble the final LaTeX paper on {topic}."
31+
backstory: >-
32+
Specialised in LaTeX-formatted academic papers, fine-tuned on
33+
scientific literature (CAJAL-4B checkpoint).
34+
llm: "huggingface/Agnuxo/CAJAL-4B-P2PCLAW"
35+
tasks:
36+
write_paper:
37+
description: >-
38+
Combine the literature review and methodology into a full
39+
scientific paper on {topic}. Use LaTeX formatting.
40+
expected_output: "Complete LaTeX paper."
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
"""Multi-agent scientific paper workflow (Researcher -> Methodologist -> Writer).
2+
3+
Demonstrates the agent-centric pattern from AGENTS.md: compose specialised
4+
Agents with ``AgentTeam`` + sequential ``Task``s instead of creating custom
5+
Agent subclasses. The writer uses the CAJAL-4B HuggingFace model; the
6+
reviewer and methodologist use general-purpose LLMs.
7+
8+
Usage:
9+
python examples/scientific_writing/multi_agent_paper.py
10+
"""
11+
from praisonaiagents import Agent, AgentTeam, Task
12+
13+
from scientific_writer import ( # noqa: E402 — example imports a sibling file
14+
build_scientific_writer,
15+
format_citation,
16+
format_latex_section,
17+
)
18+
19+
20+
literature_reviewer = Agent(
21+
name="Literature Reviewer",
22+
instructions=(
23+
"You are an expert at surveying academic literature. "
24+
"Produce a concise literature review with 5-8 key citations in APA."
25+
),
26+
tools=[format_citation],
27+
)
28+
29+
methodology_designer = Agent(
30+
name="Methodology Designer",
31+
instructions=(
32+
"You design rigorous research methodologies. "
33+
"Output a clear, reproducible methods section."
34+
),
35+
tools=[format_latex_section],
36+
)
37+
38+
scientific_writer = build_scientific_writer()
39+
40+
41+
def run(topic: str) -> str:
42+
"""Run the 3-agent workflow for a given research ``topic``."""
43+
t1 = Task(
44+
name="review_literature",
45+
description=f"Review literature on: {topic}",
46+
agent=literature_reviewer,
47+
expected_output="APA literature review.",
48+
)
49+
t2 = Task(
50+
name="design_methodology",
51+
description=f"Design research methodology for: {topic}",
52+
agent=methodology_designer,
53+
expected_output="LaTeX methods section.",
54+
)
55+
t3 = Task(
56+
name="write_paper",
57+
description=(
58+
f"Combine the literature review and methodology into a full "
59+
f"scientific paper on: {topic}. Use LaTeX formatting."
60+
),
61+
agent=scientific_writer,
62+
expected_output="Complete LaTeX paper.",
63+
)
64+
team = AgentTeam(
65+
agents=[literature_reviewer, methodology_designer, scientific_writer],
66+
tasks=[t1, t2, t3],
67+
)
68+
return team.start()
69+
70+
71+
if __name__ == "__main__":
72+
result = run("Transformer architectures for protein folding prediction")
73+
print(result)
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
"""Scientific Paper Writer — single-agent example using the CAJAL model.
2+
3+
CAJAL (``Agnuxo/CAJAL-4B-P2PCLAW``) is a 4B-parameter HuggingFace model
4+
specialised for LaTeX-formatted academic writing. This example shows how
5+
to use it with a regular ``Agent`` and ``@tool`` functions — no new Agent
6+
subclass is needed. Run 3 ways: Python (this file), CLI, or YAML.
7+
8+
Requirements:
9+
pip install "praisonaiagents[llm]" # for litellm / HuggingFace
10+
export HUGGINGFACE_API_KEY=... # or run a local inference server
11+
12+
Usage:
13+
python examples/scientific_writing/scientific_writer.py
14+
"""
15+
from praisonaiagents import Agent, tool
16+
17+
18+
@tool
19+
def format_latex_section(title: str, content: str) -> str:
20+
"""Wrap prose content in a LaTeX ``\\section{}`` block.
21+
22+
Args:
23+
title: Section heading (e.g. ``"Introduction"``).
24+
content: The body text.
25+
26+
Returns:
27+
LaTeX-formatted section string.
28+
"""
29+
return f"\\section{{{title}}}\n{content}\n"
30+
31+
32+
@tool
33+
def format_citation(authors: str, year: int, title: str, venue: str) -> str:
34+
"""Render a single APA-style citation line.
35+
36+
Args:
37+
authors: ``"Smith, J. & Jones, A."``
38+
year: Publication year.
39+
title: Paper title.
40+
venue: Journal / conference name.
41+
42+
Returns:
43+
One APA-formatted citation string.
44+
"""
45+
return f"{authors} ({year}). {title}. *{venue}*."
46+
47+
48+
SCIENTIFIC_INSTRUCTIONS = """You are a specialised scientific paper writer.
49+
50+
Produce high-quality academic content with:
51+
- Clear structure (Abstract, Introduction, Methodology, Results, Discussion,
52+
Conclusion, References)
53+
- Rigorous methodology and precise language
54+
- Proper APA citations and LaTeX formatting
55+
- Honest limitations and future work sections
56+
57+
Use ``format_latex_section`` to wrap each section in LaTeX, and
58+
``format_citation`` to build the References list.
59+
"""
60+
61+
62+
def build_scientific_writer(
63+
model: str = "huggingface/Agnuxo/CAJAL-4B-P2PCLAW",
64+
) -> Agent:
65+
"""Construct a scientific-writing Agent.
66+
67+
Override ``model`` for any HuggingFace / local / API model — the default
68+
targets the CAJAL-4B checkpoint for LaTeX-style academic output.
69+
"""
70+
return Agent(
71+
name="Scientific Writer",
72+
instructions=SCIENTIFIC_INSTRUCTIONS,
73+
llm=model,
74+
tools=[format_latex_section, format_citation],
75+
)
76+
77+
78+
if __name__ == "__main__":
79+
agent = build_scientific_writer()
80+
paper = agent.start(
81+
"Write a short scientific paper (Abstract + Introduction + Conclusion) "
82+
"on 'Climate change effects on coral reef biodiversity'. "
83+
"Use LaTeX formatting and include at least 3 APA citations."
84+
)
85+
print(paper)

0 commit comments

Comments
 (0)