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 from agents import DeepResearchAgent, get_llms import threading from queue import Queue lock = threading.Lock() 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...

Idea

{{ idea | safe }}
{% if error %}

Error

{{ 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() request_queue = Queue() @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() # 将请求放入队列 request_id = threading.get_ident() request_queue.put(request_id) # 等待轮到当前请求 while request_queue.queue[0] != request_id: time.sleep(10) # 等待一段时间后再检查 # 根据自己的request_id,判断前面还有多少人在排队 len = 0 for i in request_queue.queue: if i == request_id: break len += 1 return Template(html_template).render(idea="", error=f"The server is busy. Please try again later.There are {len}", reply_count=reply_count) with lock: 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 = 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)