-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
146 lines (114 loc) · 4.2 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from fastapi.middleware.cors import CORSMiddleware
import os
from crewai import Crew, Process
from agents import ResearchCrewAgents
from tasks import ResearchCrewTasks
# Setup environment variables
def setup_environment():
os.environ["TOKENIZERS_PARALLELISM"] = "false"
os.environ["PORT"] = os.getenv("PORT", "10000")
setup_environment()
class ResearchCrew:
def __init__(self, inputs):
self.inputs = inputs
self.agents = ResearchCrewAgents()
self.tasks = ResearchCrewTasks()
def serialize_crew_output(self, crew_output):
return {"output": crew_output}
async def run(self, is_discord=False):
from hybridsearch import hybrid_research
researcher = self.agents.researcher()
writer = self.agents.writer()
research_task = self.tasks.research_task(researcher, self.inputs)
writing_task = (
self.tasks.writing_task_discord if is_discord else self.tasks.writing_task
)(writer, [research_task], self.inputs)
crew = Crew(
agents=[researcher, writer],
tasks=[research_task, writing_task],
process=Process.sequential,
verbose=True,
)
self.result = await crew.kickoff_async(inputs=self.inputs)
self.citation = hybrid_research(self.inputs, 5)[1]
self.serialized_result = self.serialize_crew_output(self.result)
return {"result": self.serialized_result, "links": self.citation}
class QuestionRequest(BaseModel):
question: str
USELESS_INFO_PHRASES = [
"I don't know",
"does not contain information",
"does not contain any information",
"any information",
"Unfortunately",
"Agent stopped due to iteration limit or time limit",
]
def has_useful_information(result):
return "Agent stopped due to iteration limit or time limit" not in result
app = FastAPI()
# Add CORS middleware for FastAPI
app.add_middleware(
CORSMiddleware,
allow_origins=(
["https://gptgov.app"]
if os.getenv("ENV") == "production"
else ["http://localhost:3000", "http://127.0.0.1:3000"]
),
allow_methods=["*"],
allow_headers=["*"],
)
@app.post("/ask")
async def ask_question(request: QuestionRequest):
try:
question = request.question.strip()
if not question:
raise HTTPException(status_code=400, detail="No question provided")
research_crew = ResearchCrew({"question": question})
result = await research_crew.run()
crew_output = result["result"]["output"]
if has_useful_information(crew_output.raw):
return {"result": result}
else:
return {
"result": "I cannot find any relevant information on this topic",
"links": [],
}
except Exception as e:
print(f"Error occurred: {e}")
raise HTTPException(status_code=500, detail="Internal Server Error")
@app.post("/discord")
async def ask_question_discord(request: QuestionRequest):
try:
question = request.question.strip()
if not question:
raise HTTPException(status_code=400, detail="No question provided")
research_crew = ResearchCrew({"question": question})
result = await research_crew.run(is_discord=True)
crew_output = result["result"]["output"]
if has_useful_information(crew_output.raw):
return {"result": result}
else:
return {
"result": "I cannot find any relevant information on this topic",
"links": [],
}
except Exception as e:
print(f"Error occurred: {e}")
raise HTTPException(status_code=500, detail="Internal Server Error")
import uvicorn
if __name__ == "__main__":
try:
uvicorn.run(
"main:app",
host="0.0.0.0",
port=int(os.environ.get("PORT", 5001)),
workers=int(os.environ.get("UVICORN_WORKERS", 4)),
log_level="info",
timeout_keep_alive=120,
)
except KeyboardInterrupt:
print("Server shut down gracefully")
except Exception as e:
print(f"An error occurred: {e}")