-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
74 lines (58 loc) · 2.09 KB
/
api.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
from fastapi import FastAPI, HTTPException, WebSocket
from contextlib import asynccontextmanager
from pydantic import BaseModel
from langchain.llms import Ollama
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain.schema import StrOutputParser
import os
from fastapi.middleware.cors import CORSMiddleware
from crewai import Crew, Process
from tasks import PreparationTasks
from agents import PreparationAgents
class ResearchModel(BaseModel):
company: str
@asynccontextmanager
async def lifespan(app: FastAPI):
openai_api_key = os.environ["OPENAI_API_KEY"]
yield
api = FastAPI(lifespan=lifespan)
api.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:3000"], # React dev server
allow_credentials=True,
allow_methods=["*"], # Allows all methods (GET, POST, etc.)
allow_headers=["*"], # Allows all headers
)
@api.post("/research/")
async def ask(research_model: ResearchModel):
try:
tasks = PreparationTasks()
agents = PreparationAgents()
company = research_model.company
# Create Agents
researcher_agent = agents.research_agent()
summary_agent = agents.summarizer_agent()
recommender_agent = agents.recommender_agent()
# Create Tasks
research = tasks.research_task(researcher_agent, company)
summary = tasks.summarizer_task(summary_agent, company)
recommender = tasks.recommender_strategy_task(recommender_agent, company)
crew = Crew(
agents=[
researcher_agent,
summary_agent,
recommender_agent
],
tasks=[
research,
summary,
recommender
],
process=Process.sequential
)
result = crew.kickoff()
return {"response": f"Research result for {research_model.company} {result}"}
except Exception as e:
print(e)
raise HTTPException(status_code=500, detail=str(e))