from fastapi import FastAPI, Form
from fastapi.responses import HTMLResponse
from jinja2 import Template
import markdown
import time
from datetime import datetime, timedelta
from apscheduler.schedulers.background import BackgroundScheduler
import asyncio
from agents import DeepResearchAgent, get_llms
app = FastAPI()
# 每日最大回复次数
MAX_REPLIES_PER_DAY = 100
# 当日回复次数计数器
reply_count = 0
# 启动时设置计数器重置
last_reset_time = datetime.now()
# HTML模板
html_template = """
CoI Agent online demo 😊
CoI Agent online demo 😊
Time Taken: {{ time_taken }} seconds
Today's Replies: {{ reply_count }}
Example Input:
Generating content, Usually takes 3-4 minutes, please wait...
{% if error %}
{% endif %}
"""
# 重置每日计数器
def reset_counter():
global reply_count
reply_count = 0
# 设置定时任务每天0点重置计数器
scheduler = BackgroundScheduler()
scheduler.add_job(reset_counter, 'cron', hour=0, minute=0)
scheduler.start()
@app.get("/", response_class=HTMLResponse)
def form_get():
return Template(html_template).render(idea= "This is a example of the idea geneartion", error=None, reply_count=reply_count)
@app.post("/", response_class=HTMLResponse)
def form_post(topic: str = Form(...)):
global reply_count
start_time = time.time()
# 检查是否超过每日最大回复次数
if reply_count >= MAX_REPLIES_PER_DAY:
error_message = "Today's maximum number of replies has been reached. Please try again tomorrow."
return Template(html_template).render(idea="", error=error_message, reply_count=reply_count)
try:
main_llm, cheap_llm = get_llms()
deep_research_agent = DeepResearchAgent(llm=main_llm, cheap_llm=cheap_llm, improve_cnt=1, max_chain_length=5, min_chain_length=3, max_chain_numbers=1)
print(f"begin to generate idea of topic {topic}")
idea, related_experiments, entities, idea_chain, ideas, trend, future, human, year = asyncio.run(deep_research_agent.generate_idea_with_chain(topic))
idea_md = markdown.markdown(idea)
# 更新每日回复次数
reply_count += 1
end_time = time.time()
time_taken = round(end_time - start_time, 2)
return Template(html_template).render(idea=idea_md, error=None, reply_count=reply_count, time_taken=time_taken)
except Exception as e:
end_time = time.time()
time_taken = round(end_time - start_time, 2)
return Template(html_template).render(idea="", error=str(e), reply_count=reply_count, time_taken=time_taken)