Spaces:
Running
Running
Upload paper.py
Browse files
paper.py
ADDED
@@ -0,0 +1,210 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import json
|
3 |
+
import logging
|
4 |
+
from datetime import datetime, timedelta
|
5 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
6 |
+
from langchain.schema import SystemMessage, HumanMessage
|
7 |
+
|
8 |
+
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
9 |
+
|
10 |
+
class Agent:
|
11 |
+
def __init__(self, role: str, goal: str, backstory: str, personality: str = "", llm=None) -> None:
|
12 |
+
"""
|
13 |
+
Initialize an Agent with role, goal, backstory, personality, and assigned LLM.
|
14 |
+
"""
|
15 |
+
self.role = role
|
16 |
+
self.goal = goal
|
17 |
+
self.backstory = backstory
|
18 |
+
self.personality = personality
|
19 |
+
self.tools = [] # Initialize with empty list for future tool integrations
|
20 |
+
self.llm = llm
|
21 |
+
|
22 |
+
class Task:
|
23 |
+
def __init__(self, description: str, agent: Agent, expected_output: str, context=None) -> None:
|
24 |
+
"""
|
25 |
+
Initialize a Task with its description, the responsible agent, expected output, and optional context.
|
26 |
+
"""
|
27 |
+
self.description = description
|
28 |
+
self.agent = agent
|
29 |
+
self.expected_output = expected_output
|
30 |
+
self.context = context or []
|
31 |
+
|
32 |
+
google_api_key = os.getenv("GEMINI_API_KEY") # 실제 Google API 키 사용
|
33 |
+
if not google_api_key:
|
34 |
+
logging.error("GEMINI_API_KEY is not set in the environment variables.")
|
35 |
+
llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash", google_api_key=google_api_key)
|
36 |
+
|
37 |
+
# -------------------------------------------------------------------------------
|
38 |
+
# Define Academic Research Agents
|
39 |
+
# -------------------------------------------------------------------------------
|
40 |
+
literature_research_agent = Agent(
|
41 |
+
role="Assumsions Analysis Agent",
|
42 |
+
goal="Provide a comprehensive review of existing assumsions on the feasibility study topic.",
|
43 |
+
backstory="An experienced academic researcher specialized in feasibility study assumsions and meta-analyses.",
|
44 |
+
personality="Analytical, thorough, and detail-oriented.",
|
45 |
+
llm=llm,
|
46 |
+
)
|
47 |
+
|
48 |
+
outline_agent = Agent(
|
49 |
+
role="Outline Agent",
|
50 |
+
goal="Generate a structured and detailed outline for a feasibility study based on the assumsions. The outline that needs to be prepared includes: Market Analysis, Technical Analysis, Financial Analysis, Legal and Regulatory Analysis, Risk Analysis, Environmental and Social Impact Analysis, Conclusions and Recommendations.",
|
51 |
+
backstory="A methodical academic planner who organizes feasibility study findings into coherent paper structures.",
|
52 |
+
personality="Organized, systematic, and insightful.",
|
53 |
+
llm=llm,
|
54 |
+
)
|
55 |
+
|
56 |
+
draft_writing_agent = Agent(
|
57 |
+
role="Draft Writing Agent",
|
58 |
+
goal="Compose a first draft of the feasibility paper based on the assumsions review and outline. The writings that needs to be prepared includes: Market Analysis, Technical Analysis, Financial Analysis, Legal and Regulatory Analysis, Risk Analysis, Environmental and Social Impact Analysis, Conclusions and Recommendations.",
|
59 |
+
backstory="A skilled academic writer capable of synthesizing feasibility study findings into well-structured drafts.",
|
60 |
+
personality="Articulate, precise, and scholarly.",
|
61 |
+
llm=llm,
|
62 |
+
)
|
63 |
+
|
64 |
+
citation_agent = Agent(
|
65 |
+
role="Citation Agent",
|
66 |
+
goal="Generate a list of relevant citations and references in the required format for the research paper.",
|
67 |
+
backstory="A detail-oriented bibliographic expert with extensive knowledge of citation standards.",
|
68 |
+
personality="Meticulous, accurate, and research-savvy.",
|
69 |
+
llm=llm,
|
70 |
+
)
|
71 |
+
|
72 |
+
editing_agent = Agent(
|
73 |
+
role="Editing Agent",
|
74 |
+
goal="Revise and polish the draft for clarity, coherence, and academic tone. The writings that needs to be prepared includes: Market Analysis, Technical Analysis, Financial Analysis, Legal and Regulatory Analysis, Risk Analysis, Environmental and Social Impact Analysis, Conclusions and Recommendations.",
|
75 |
+
backstory="An expert editor skilled in improving feasibility manuscripts and ensuring high-quality presentation.",
|
76 |
+
personality="Critical, precise, and supportive.",
|
77 |
+
llm=llm,
|
78 |
+
)
|
79 |
+
|
80 |
+
chatbot_agent = Agent(
|
81 |
+
role="Chatbot Agent",
|
82 |
+
goal="Engage in interactive conversation to answer queries related to the feasibility study documents",
|
83 |
+
backstory="A conversational AI assistant with extensive knowledge in academia,feasibility study, and research methodologies.",
|
84 |
+
personality="Helpful, conversational, and knowledgeable.",
|
85 |
+
llm=llm,
|
86 |
+
)
|
87 |
+
|
88 |
+
# -------------------------------------------------------------------------------
|
89 |
+
# Define Tasks for Academic Research and Writing
|
90 |
+
# -------------------------------------------------------------------------------
|
91 |
+
literature_research_task = Task(
|
92 |
+
description="""prepare the creation of a Feasibility Study with the theme on {topic} considering the keywords {keywords}.
|
93 |
+
|
94 |
+
Please provide:
|
95 |
+
- Market Analysis
|
96 |
+
- Technical Analysis
|
97 |
+
- Financial Analysis
|
98 |
+
- Legal and Regulatory Analysis
|
99 |
+
- Risk Analysis
|
100 |
+
- Environmental and Social Impact Analysis
|
101 |
+
- Conclusions and Recommendations
|
102 |
+
Format the response with bullet points and concise summaries.""",
|
103 |
+
agent=literature_research_agent,
|
104 |
+
expected_output="""A comprehensive Feasibility Study covering:
|
105 |
+
1. Market Analysis
|
106 |
+
2. Technical Analysis
|
107 |
+
3. Financial Analysis
|
108 |
+
4. Legal and Regulatory Analysis
|
109 |
+
5. Risk Analysis
|
110 |
+
6. Environmental and Social Impact Analysis
|
111 |
+
7. Conclusions and Recommendations"""
|
112 |
+
)
|
113 |
+
|
114 |
+
outline_task = Task(
|
115 |
+
description="""Based on the Feasibility Study topic {topic} and assumsions review findings, generate a detailed outline for a Feasibility Study paper.
|
116 |
+
|
117 |
+
Include sections such as:
|
118 |
+
- Market Analysis
|
119 |
+
- Technical Analysis
|
120 |
+
- Financial Analysis
|
121 |
+
- Legal and Regulatory Analysis
|
122 |
+
- Risk Analysis
|
123 |
+
- Environmental and Social Impact Analysis
|
124 |
+
- Conclusions and Recommendations
|
125 |
+
- References
|
126 |
+
Format the outline in a structured manner with bullet points and subheadings.""",
|
127 |
+
agent=outline_agent,
|
128 |
+
expected_output="A structured outline for a Feasibility Study paper including all major sections and key points to cover in each section."
|
129 |
+
)
|
130 |
+
|
131 |
+
draft_writing_task = Task(
|
132 |
+
description="""Using the Feasibility Study topic {topic}, the assumsions review, and the generated outline, compose a first draft of the Feasibility Study paper.
|
133 |
+
|
134 |
+
The draft should include:
|
135 |
+
- Market Analysis
|
136 |
+
- Technical Analysis
|
137 |
+
- Financial Analysis
|
138 |
+
- Legal and Regulatory Analysis
|
139 |
+
- Risk Analysis
|
140 |
+
- Environmental and Social Impact Analysis
|
141 |
+
- Conclusions and Recommendations
|
142 |
+
- References
|
143 |
+
Ensure the tone is academic and the content is well-organized.""",
|
144 |
+
agent=draft_writing_agent,
|
145 |
+
expected_output="A complete first draft of the Feasibility Study paper covering all sections with sufficient academic detail."
|
146 |
+
)
|
147 |
+
|
148 |
+
citation_task = Task(
|
149 |
+
description="""Based on the Feasibility Study review for {topic}, generate a list of key references and citations in APA format.
|
150 |
+
|
151 |
+
Include:
|
152 |
+
- Author names, publication year, title, and source,
|
153 |
+
- At least 10 key references relevant to the research topic.
|
154 |
+
Format the output as a numbered list of citations.""",
|
155 |
+
agent=citation_agent,
|
156 |
+
expected_output="A list of 10+ relevant citations in APA format."
|
157 |
+
)
|
158 |
+
|
159 |
+
editing_task = Task(
|
160 |
+
description="""Review and edit the draft for clarity, coherence, and academic tone.
|
161 |
+
|
162 |
+
Focus on:
|
163 |
+
- Improving sentence structure,
|
164 |
+
- Ensuring logical flow between sections,
|
165 |
+
- Correcting grammar and stylistic issues,
|
166 |
+
- Enhancing academic tone.
|
167 |
+
Provide the polished version of the paper.""",
|
168 |
+
agent=editing_agent,
|
169 |
+
expected_output="A refined and polished version of the Feasibility Study paper draft."
|
170 |
+
)
|
171 |
+
|
172 |
+
chatbot_task = Task(
|
173 |
+
description="Provide a conversational and detailed response to Feasibility Study-related queries.",
|
174 |
+
agent=chatbot_agent,
|
175 |
+
expected_output="A friendly, informative response addressing the query."
|
176 |
+
)
|
177 |
+
|
178 |
+
def run_task(task: Task, input_text: str) -> str:
|
179 |
+
"""
|
180 |
+
Executes the given task using the associated agent's LLM and returns the response content.
|
181 |
+
"""
|
182 |
+
try:
|
183 |
+
if not isinstance(task, Task):
|
184 |
+
raise ValueError(f"Expected 'task' to be an instance of Task, got {type(task)}")
|
185 |
+
if not hasattr(task, 'agent') or not isinstance(task.agent, Agent):
|
186 |
+
raise ValueError("Task must have a valid 'agent' attribute of type Agent.")
|
187 |
+
system_input = (
|
188 |
+
f"Agent Details:\n"
|
189 |
+
f"Role: {task.agent.role}\n"
|
190 |
+
f"Goal: {task.agent.goal}\n"
|
191 |
+
f"Backstory: {task.agent.backstory}\n"
|
192 |
+
f"Personality: {task.agent.personality}\n"
|
193 |
+
)
|
194 |
+
task_input = (
|
195 |
+
f"Task Details:\n"
|
196 |
+
f"Task Description: {task.description}\n"
|
197 |
+
f"Expected Output: {task.expected_output}\n"
|
198 |
+
f"Input for Task:\n{input_text}\n"
|
199 |
+
)
|
200 |
+
messages = [
|
201 |
+
SystemMessage(content=system_input),
|
202 |
+
HumanMessage(content=task_input)
|
203 |
+
]
|
204 |
+
response = task.agent.llm.invoke(messages)
|
205 |
+
if not response or not response.content:
|
206 |
+
raise ValueError("Empty response from LLM.")
|
207 |
+
return response.content
|
208 |
+
except Exception as e:
|
209 |
+
logging.error(f"Error in task '{task.agent.role}': {e}")
|
210 |
+
return f"Error in {task.agent.role}: {e}"
|