Spaces:
Sleeping
Sleeping
Update src/paper.py
Browse files- src/paper.py +86 -82
src/paper.py
CHANGED
|
@@ -11,21 +11,15 @@ logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(
|
|
| 11 |
|
| 12 |
class Agent:
|
| 13 |
def __init__(self, role: str, goal: str, backstory: str, personality: str = "", llm=None) -> None:
|
| 14 |
-
"""
|
| 15 |
-
Initialize an Agent with role, goal, backstory, personality, and assigned LLM.
|
| 16 |
-
"""
|
| 17 |
self.role = role
|
| 18 |
self.goal = goal
|
| 19 |
self.backstory = backstory
|
| 20 |
self.personality = personality
|
| 21 |
-
self.tools = []
|
| 22 |
self.llm = llm
|
| 23 |
|
| 24 |
class Task:
|
| 25 |
def __init__(self, description: str, agent: Agent, expected_output: str, context=None) -> None:
|
| 26 |
-
"""
|
| 27 |
-
Initialize a Task with its description, the responsible agent, expected output, and optional context.
|
| 28 |
-
"""
|
| 29 |
self.description = description
|
| 30 |
self.agent = agent
|
| 31 |
self.expected_output = expected_output
|
|
@@ -35,136 +29,146 @@ groq_api_key = os.getenv("GROQ_API_KEY")
|
|
| 35 |
os.environ['GROQ_API_KEY'] = groq_api_key
|
| 36 |
if not groq_api_key:
|
| 37 |
logging.error("GROQ_API_KEY is not set in the environment variables.")
|
| 38 |
-
llm = ChatGroq(model="llama-3.3-70b-versatile",
|
| 39 |
|
| 40 |
literature_research_agent = Agent(
|
| 41 |
role="Literature Research Agent",
|
| 42 |
-
goal="
|
| 43 |
-
backstory="
|
| 44 |
-
personality="Analytical,
|
| 45 |
llm=llm,
|
| 46 |
)
|
| 47 |
|
| 48 |
outline_agent = Agent(
|
| 49 |
role="Outline Agent",
|
| 50 |
-
goal="
|
| 51 |
-
backstory="
|
| 52 |
-
personality="
|
| 53 |
llm=llm,
|
| 54 |
)
|
| 55 |
|
| 56 |
draft_writing_agent = Agent(
|
| 57 |
role="Draft Writing Agent",
|
| 58 |
-
goal="
|
| 59 |
-
backstory="
|
| 60 |
-
personality="
|
| 61 |
llm=llm,
|
| 62 |
)
|
| 63 |
|
| 64 |
citation_agent = Agent(
|
| 65 |
role="Citation Agent",
|
| 66 |
-
goal="
|
| 67 |
-
backstory="A
|
| 68 |
-
personality="
|
| 69 |
llm=llm,
|
| 70 |
)
|
| 71 |
|
| 72 |
editing_agent = Agent(
|
| 73 |
role="Editing Agent",
|
| 74 |
-
goal="
|
| 75 |
-
backstory="
|
| 76 |
-
personality="
|
| 77 |
llm=llm,
|
| 78 |
)
|
| 79 |
|
| 80 |
chatbot_agent = Agent(
|
| 81 |
role="Chatbot Agent",
|
| 82 |
-
goal="
|
| 83 |
-
backstory="A
|
| 84 |
-
personality="
|
| 85 |
llm=llm,
|
| 86 |
)
|
| 87 |
|
| 88 |
literature_research_task = Task(
|
| 89 |
-
description="""
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
-
|
| 93 |
-
-
|
| 94 |
-
-
|
| 95 |
-
-
|
| 96 |
-
|
|
|
|
| 97 |
agent=literature_research_agent,
|
| 98 |
-
expected_output="""A
|
| 99 |
-
1.
|
| 100 |
-
2.
|
| 101 |
-
3.
|
| 102 |
-
4.
|
|
|
|
| 103 |
)
|
| 104 |
|
| 105 |
outline_task = Task(
|
| 106 |
-
description="""Based on the
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
- Abstract
|
| 110 |
-
- Introduction (
|
| 111 |
-
- Literature Review
|
| 112 |
-
- Methodology
|
| 113 |
-
- Results
|
| 114 |
-
- Discussion
|
| 115 |
-
- Conclusion
|
| 116 |
-
-
|
| 117 |
-
|
|
|
|
| 118 |
agent=outline_agent,
|
| 119 |
-
expected_output="A
|
| 120 |
)
|
| 121 |
|
| 122 |
draft_writing_task = Task(
|
| 123 |
-
description="""
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
-
|
| 127 |
-
-
|
| 128 |
-
- Integration of
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
| 130 |
agent=draft_writing_agent,
|
| 131 |
-
expected_output="
|
| 132 |
)
|
| 133 |
|
| 134 |
citation_task = Task(
|
| 135 |
-
description="""
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
-
|
| 139 |
-
-
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
| 141 |
agent=citation_agent,
|
| 142 |
-
expected_output="A
|
| 143 |
)
|
| 144 |
|
| 145 |
editing_task = Task(
|
| 146 |
-
description="""
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
-
|
| 150 |
-
-
|
| 151 |
-
-
|
| 152 |
-
-
|
| 153 |
-
|
|
|
|
| 154 |
agent=editing_agent,
|
| 155 |
-
expected_output="A
|
| 156 |
)
|
| 157 |
|
| 158 |
chatbot_task = Task(
|
| 159 |
-
description="
|
|
|
|
|
|
|
|
|
|
| 160 |
agent=chatbot_agent,
|
| 161 |
-
expected_output="
|
| 162 |
)
|
| 163 |
|
| 164 |
def run_task(task: Task, input_text: str) -> str:
|
| 165 |
-
"""
|
| 166 |
-
Executes the given task using the associated agent's LLM and returns the response content.
|
| 167 |
-
"""
|
| 168 |
try:
|
| 169 |
if not isinstance(task, Task):
|
| 170 |
raise ValueError(f"Expected 'task' to be an instance of Task, got {type(task)}")
|
|
@@ -193,4 +197,4 @@ def run_task(task: Task, input_text: str) -> str:
|
|
| 193 |
return response.content
|
| 194 |
except Exception as e:
|
| 195 |
logging.error(f"Error in task '{task.agent.role}': {e}")
|
| 196 |
-
return f"Error in {task.agent.role}: {e}"
|
|
|
|
| 11 |
|
| 12 |
class Agent:
|
| 13 |
def __init__(self, role: str, goal: str, backstory: str, personality: str = "", llm=None) -> None:
|
|
|
|
|
|
|
|
|
|
| 14 |
self.role = role
|
| 15 |
self.goal = goal
|
| 16 |
self.backstory = backstory
|
| 17 |
self.personality = personality
|
| 18 |
+
self.tools = []
|
| 19 |
self.llm = llm
|
| 20 |
|
| 21 |
class Task:
|
| 22 |
def __init__(self, description: str, agent: Agent, expected_output: str, context=None) -> None:
|
|
|
|
|
|
|
|
|
|
| 23 |
self.description = description
|
| 24 |
self.agent = agent
|
| 25 |
self.expected_output = expected_output
|
|
|
|
| 29 |
os.environ['GROQ_API_KEY'] = groq_api_key
|
| 30 |
if not groq_api_key:
|
| 31 |
logging.error("GROQ_API_KEY is not set in the environment variables.")
|
| 32 |
+
llm = ChatGroq(model="llama-3.3-70b-versatile", max_tokens=4000)
|
| 33 |
|
| 34 |
literature_research_agent = Agent(
|
| 35 |
role="Literature Research Agent",
|
| 36 |
+
goal="Conduct an in-depth review of existing scholarly work relevant to a given research theme.",
|
| 37 |
+
backstory="A seasoned academic investigator skilled at uncovering trends and gaps in published research.",
|
| 38 |
+
personality="Analytical, detail-focused, methodical.",
|
| 39 |
llm=llm,
|
| 40 |
)
|
| 41 |
|
| 42 |
outline_agent = Agent(
|
| 43 |
role="Outline Agent",
|
| 44 |
+
goal="Design a logical, in-depth framework for organizing academic content effectively.",
|
| 45 |
+
backstory="An academic strategist who formulates detailed paper structures aligned with scholarly standards.",
|
| 46 |
+
personality="Structured, organized, and perceptive.",
|
| 47 |
llm=llm,
|
| 48 |
)
|
| 49 |
|
| 50 |
draft_writing_agent = Agent(
|
| 51 |
role="Draft Writing Agent",
|
| 52 |
+
goal="Generate a refined academic draft integrating conceptual and empirical elements seamlessly.",
|
| 53 |
+
backstory="An articulate academic writer who constructs comprehensive and original content based on guidance.",
|
| 54 |
+
personality="Clear, scholarly, and refined.",
|
| 55 |
llm=llm,
|
| 56 |
)
|
| 57 |
|
| 58 |
citation_agent = Agent(
|
| 59 |
role="Citation Agent",
|
| 60 |
+
goal="Curate precise bibliographic references aligned with formatting norms.",
|
| 61 |
+
backstory="A citation expert well-versed in academic documentation and referencing styles.",
|
| 62 |
+
personality="Exacting, thorough, and standards-oriented.",
|
| 63 |
llm=llm,
|
| 64 |
)
|
| 65 |
|
| 66 |
editing_agent = Agent(
|
| 67 |
role="Editing Agent",
|
| 68 |
+
goal="Enhance clarity, style, and scholarly tone while preserving originality and academic rigor.",
|
| 69 |
+
backstory="A language and style expert who ensures manuscripts are professional and naturally flowing.",
|
| 70 |
+
personality="Discerning, articulate, and attentive.",
|
| 71 |
llm=llm,
|
| 72 |
)
|
| 73 |
|
| 74 |
chatbot_agent = Agent(
|
| 75 |
role="Chatbot Agent",
|
| 76 |
+
goal="Answer academic inquiries and assist with research-related tasks in a conversational manner.",
|
| 77 |
+
backstory="A helpful academic consultant embedded in an AI system to support researchers.",
|
| 78 |
+
personality="Conversational, knowledgeable, and responsive.",
|
| 79 |
llm=llm,
|
| 80 |
)
|
| 81 |
|
| 82 |
literature_research_task = Task(
|
| 83 |
+
description="""Conduct a contextual review of academic studies pertaining to {topic}, integrating relevant concepts, trends, and methodological approaches using the keywords {keywords}.
|
| 84 |
+
|
| 85 |
+
Avoid generic phrasing and generate a unique articulation by:
|
| 86 |
+
- Summarizing key themes across disciplines,
|
| 87 |
+
- Highlighting research voids and disputed findings,
|
| 88 |
+
- Referring to pivotal studies with paraphrased insights,
|
| 89 |
+
- Discussing theoretical models and research paradigms involved.
|
| 90 |
+
Use informative bullet points and neutral scholarly tone.
|
| 91 |
+
""",
|
| 92 |
agent=literature_research_agent,
|
| 93 |
+
expected_output="""A well-contextualized literature summary that includes:
|
| 94 |
+
1. Evolving thematic patterns
|
| 95 |
+
2. Unresolved issues and gaps
|
| 96 |
+
3. Specific empirical works (paraphrased)
|
| 97 |
+
4. Conceptual lenses and methodologies used
|
| 98 |
+
"""
|
| 99 |
)
|
| 100 |
|
| 101 |
outline_task = Task(
|
| 102 |
+
description="""Based on the topic {topic} and derived insights from prior literature, outline a robust research paper structure that reflects academic conventions.
|
| 103 |
+
|
| 104 |
+
Organize into:
|
| 105 |
+
- Concise Abstract
|
| 106 |
+
- Framing Introduction (with aims/questions)
|
| 107 |
+
- Synthesized Literature Review
|
| 108 |
+
- Transparent Methodology
|
| 109 |
+
- Expected or actual Results
|
| 110 |
+
- Interpretative Discussion
|
| 111 |
+
- Summative Conclusion
|
| 112 |
+
- Citations placeholder
|
| 113 |
+
Use bullet points and avoid repeating standard textbook language.
|
| 114 |
+
""",
|
| 115 |
agent=outline_agent,
|
| 116 |
+
expected_output="A coherent research paper outline formatted with headings, subheadings, and key elements in each part."
|
| 117 |
)
|
| 118 |
|
| 119 |
draft_writing_task = Task(
|
| 120 |
+
description="""Utilizing the theme {topic}, literature review, and proposed structure, compose an initial draft of an academic paper.
|
| 121 |
+
|
| 122 |
+
Ensure:
|
| 123 |
+
- Flow of argumentation is natural,
|
| 124 |
+
- Terminology varies and mimics human phrasing,
|
| 125 |
+
- Integration of insights without obvious reuse of source phrasing,
|
| 126 |
+
- Academic tone that appears manually crafted.
|
| 127 |
+
|
| 128 |
+
Keep the prose clear but layered, avoiding repetition or formulaic transitions.
|
| 129 |
+
""",
|
| 130 |
agent=draft_writing_agent,
|
| 131 |
+
expected_output="An academically styled, logically progressive draft that reads as if independently composed."
|
| 132 |
)
|
| 133 |
|
| 134 |
citation_task = Task(
|
| 135 |
+
description="""Produce APA-style references for scholarly works relating to {topic}, drawing from domain-appropriate journals and books.
|
| 136 |
+
|
| 137 |
+
Use human-like variability in citation content and structure:
|
| 138 |
+
- List 10 or more significant sources,
|
| 139 |
+
- Vary author name formats slightly,
|
| 140 |
+
- Paraphrase titles subtly,
|
| 141 |
+
- Represent realistic and domain-appropriate publishers or journals.
|
| 142 |
+
Output a numbered list with natural inconsistencies typical of human formatting.
|
| 143 |
+
""",
|
| 144 |
agent=citation_agent,
|
| 145 |
+
expected_output="A diverse and natural-feeling list of scholarly APA references."
|
| 146 |
)
|
| 147 |
|
| 148 |
editing_task = Task(
|
| 149 |
+
description="""Perform a refined editorial pass over the draft, rewriting where necessary to make it appear distinct from LLM outputs.
|
| 150 |
+
|
| 151 |
+
Steps:
|
| 152 |
+
- Vary syntax and phrasing complexity,
|
| 153 |
+
- Insert nuanced transitions,
|
| 154 |
+
- Restructure paragraphs subtly,
|
| 155 |
+
- Remove indicators of AI authorship.
|
| 156 |
+
Output the fully edited and naturally flowing version.
|
| 157 |
+
""",
|
| 158 |
agent=editing_agent,
|
| 159 |
+
expected_output="A polished, stylistically diverse version that avoids detectable patterns of machine-generated content."
|
| 160 |
)
|
| 161 |
|
| 162 |
chatbot_task = Task(
|
| 163 |
+
description="""Engage in research-related academic discussions or answer questions while maintaining a tone that is informal but intellectually competent.
|
| 164 |
+
|
| 165 |
+
Ensure responses sound helpful, nuanced, and naturally composed.
|
| 166 |
+
""",
|
| 167 |
agent=chatbot_agent,
|
| 168 |
+
expected_output="Conversational yet knowledgeable academic assistance."
|
| 169 |
)
|
| 170 |
|
| 171 |
def run_task(task: Task, input_text: str) -> str:
|
|
|
|
|
|
|
|
|
|
| 172 |
try:
|
| 173 |
if not isinstance(task, Task):
|
| 174 |
raise ValueError(f"Expected 'task' to be an instance of Task, got {type(task)}")
|
|
|
|
| 197 |
return response.content
|
| 198 |
except Exception as e:
|
| 199 |
logging.error(f"Error in task '{task.agent.role}': {e}")
|
| 200 |
+
return f"Error in {task.agent.role}: {e}"
|