ai_workflows / endpoints.py
theRealNG's picture
restructure the codebase
1f39bb8
from dotenv import load_dotenv
load_dotenv()
from app.workflows.til.suggest_headlines_v2 import SuggestHeadlinesV2, Response as SuggestHeadlinesResponse
from app.workflows.til.rewrite_til_v2 import RewriteTilV2, Response as RewriteTilResponse
from app.workflows.courses.expectation_revision import ExpectationRevision, Inputs as ExpectationRevisionInputs, Response as ExpectationRevisionResponse
from app.workflows.courses.suggest_check_question import SuggestCheckQuestion, Inputs as SuggestCheckQuestionInputs, Response as SuggestCheckQuestionResponse
from app.workflows.courses.suggest_expectations import SuggestExpectations, Inputs as SuggestExpectationsInputs, Expectation, Response as SuggestExpectationsResponse
from app.workflows.til.analyse_til import TilCrew, TilFeedbackResponse
from app.workflows.til.analyse_til_v2 import AnalyseTilV2, TilV2FeedbackResponse
from app.workflows.utils.feedback import Feedback, post_feedback, NewFeedback
from app.utils.endpoints_utils import CreateTilInputs
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import UUID4
from tempenv import TemporaryEnvironment
from typing import List
import os
import uvicorn
STAGING_ENV_CONFIG = {
"LANGCHAIN_PROJECT": "customer_agent",
"OPENAI_MODEL": "gpt-4o-mini",
"SUPABASE_URL": os.environ["SUPABASE_URL_STAGING"],
"SUPABASE_KEY": os.environ["SUPABASE_KEY_STAGING"]
}
PROD_ENV_CONFIG = {
"LANGCHAIN_PROJECT": "growthy-agents-prod",
"OPENAI_MODEL": "gpt-4o",
"SUPABASE_URL": os.environ["SUPABASE_URL_PROD"],
"SUPABASE_KEY": os.environ["SUPABASE_KEY_PROD"]
}
description = """
API helps you do awesome stuff. 🚀
"""
tags_metadata = [
{
"name": "til_feedback",
"description": "Gives the feedback on user's TIL content",
},
{
"name": "course_learn",
"description": "Workflows for course learn.",
},
]
app = FastAPI(
title="Growthy AI Worflows",
description=description,
summary="Deadpool's favorite app. Nuff said.",
version="0.0.1",
openapi_tags=tags_metadata,
docs_url="/documentation",
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# TIL
@app.post("/til_feedback", tags=["til_feedback"])
async def til_feedback_kickoff(content: List[str]) -> TilFeedbackResponse:
inputs = CreateTilInputs(content)
result = TilCrew().kickoff(inputs)
return result
@app.post("/til_feedback/{run_id}/feedback", tags=["til_feedback"])
async def capture_feedback(run_id: UUID4, feedback: Feedback) -> str:
post_feedback(run_id=run_id, feedback=feedback)
return "ok"
@app.post("/staging/til_feedback", tags=["til_feedback", "staging"])
async def staging_til_feedback_kickoff(content: List[str]) -> TilFeedbackResponse:
with TemporaryEnvironment(STAGING_ENV_CONFIG):
separator = "\n* "
content[0] = "* " + content[0]
inputs = {"content": separator.join(content)}
result = TilCrew().kickoff(inputs)
return result
@app.post("/staging/til_feedback/{run_id}/feedback", tags=["til_feedback", "staging"])
async def staging_capture_feedback(run_id: UUID4, feedback: Feedback) -> str:
with TemporaryEnvironment(STAGING_ENV_CONFIG):
post_feedback(run_id=run_id, feedback=feedback)
return "ok"
def til_v2_analyze_logic(content) -> TilV2FeedbackResponse:
inputs = CreateTilInputs(content)
result = AnalyseTilV2().kickoff(inputs)
return result
@app.post("/v2/til_feedback", tags=["til_feedback"])
async def til_v2_feedback_kickoff(content: List[str]) -> TilV2FeedbackResponse:
with TemporaryEnvironment(PROD_ENV_CONFIG):
return til_v2_analyze_logic(content)
@app.post("/v2/til_feedback/{run_id}/feedback", tags=["til_feedback"])
async def capture_til_v2_feedback(run_id: UUID4, feedback: NewFeedback) -> str:
with TemporaryEnvironment(PROD_ENV_CONFIG):
feedback.post({"run_id": run_id, "sub_workflow": "til_analysis"})
return "ok"
@app.post("/staging/v2/til_feedback", tags=["til_feedback", "staging"])
async def staging_til_v2_feedback_kickoff(content: List[str]) -> TilV2FeedbackResponse:
with TemporaryEnvironment(STAGING_ENV_CONFIG):
return til_v2_analyze_logic(content)
@app.post("/staging/v2/til_feedback/{run_id}/feedback", tags=["til_feedback", "staging"])
async def staging_capture_til_v2_feedback(run_id: UUID4, feedback: NewFeedback) -> str:
with TemporaryEnvironment(STAGING_ENV_CONFIG):
feedback.post({"run_id": run_id, "sub_workflow": "til_analysis"})
return "ok"
@app.post("/v2/til_rewrite", tags=["til_readability"])
async def til_v2_rewrite_kickoff(content: List[str]) -> RewriteTilResponse:
with TemporaryEnvironment(PROD_ENV_CONFIG):
inputs = CreateTilInputs(content)
result = RewriteTilV2().kickoff(inputs)
return result
@app.post("/v2/til_rewrite/{run_id}/feedback", tags=["til_readability"])
async def capture_til_v2_rewrite_feedback(run_id: UUID4, feedback: NewFeedback) -> str:
with TemporaryEnvironment(PROD_ENV_CONFIG):
feedback.post({"run_id": run_id, "sub_workflow": "til_understandability"})
return "ok"
@app.post("/staging/v2/til_rewrite", tags=["til_readability", "staging"])
async def staging_til_v2_rewrite_kickoff(content: List[str]) -> RewriteTilResponse:
with TemporaryEnvironment(STAGING_ENV_CONFIG):
inputs = CreateTilInputs(content)
result = RewriteTilV2().kickoff(inputs)
return result
@app.post("/staging/v2/til_rewrite/{run_id}/feedback", tags=["til_readability", "staging"])
async def staging_capture_til_v2_rewrite_feedback(run_id: UUID4, feedback: NewFeedback) -> str:
with TemporaryEnvironment(STAGING_ENV_CONFIG):
feedback.post({"run_id": run_id, "sub_workflow": "til_understandability"})
return "ok"
@app.post("/v2/til_headlines", tags=["til_headlines"])
async def til_v2_suggest_headlines(content: List[str]) -> SuggestHeadlinesResponse:
with TemporaryEnvironment(PROD_ENV_CONFIG):
inputs = CreateTilInputs(content)
result = SuggestHeadlinesV2().kickoff(inputs)
return result
@app.post("/v2/til_headlines/{run_id}/feedback", tags=["til_headlines"])
async def capture_til_v2_headlines(run_id: UUID4, feedback: NewFeedback) -> str:
with TemporaryEnvironment(PROD_ENV_CONFIG):
feedback.post({"run_id": run_id, "sub_workflow": "til_headline"})
return "ok"
@app.post("/staging/v2/til_headlines", tags=["til_headlines", "staging"])
async def staging_til_v2_suggest_headlines(content: List[str]) -> SuggestHeadlinesResponse:
with TemporaryEnvironment(STAGING_ENV_CONFIG):
inputs = CreateTilInputs(content)
result = SuggestHeadlinesV2().kickoff(inputs)
return result
@app.post("/staging/v2/til_headlines/{run_id}/feedback", tags=["til_headlines", "staging"])
async def staging_capture_til_v2_headlines(run_id: UUID4, feedback: NewFeedback) -> str:
with TemporaryEnvironment(STAGING_ENV_CONFIG):
feedback.post({"run_id": run_id, "sub_workflow": "til_headline"})
return "ok"
# Course Learn
def course_learn_suggest_expectations_logic(inputs) -> SuggestExpectationsResponse:
print("Inputs: ", inputs)
result = SuggestExpectations().kickoff(inputs={
"course": inputs.course,
"module": inputs.module,
"tasks": inputs.tasks,
"existing_expectations": inputs.existing_expectations,
})
return result
def course_learn_suggest_expectations_feedback_logic(run_id: UUID4, feedback: Feedback) -> str:
print("Helful Score: ", feedback.metric_type)
print("Feedback On: ", feedback.feedback_on)
post_feedback(run_id=run_id, feedback=feedback)
return "ok"
@app.post("/course_learn/suggest_expectations", tags=["course_learn"])
async def course_learn_suggest_expectations(inputs: SuggestExpectationsInputs) -> SuggestExpectationsResponse:
with TemporaryEnvironment(PROD_ENV_CONFIG):
return course_learn_suggest_expectations_logic(inputs)
@app.post("/staging/course_learn/suggest_expectations", tags=["course_learn", "staging"])
async def staging_course_learn_suggest_expectations(inputs: SuggestExpectationsInputs) -> SuggestExpectationsResponse:
with TemporaryEnvironment(STAGING_ENV_CONFIG):
return course_learn_suggest_expectations_logic(inputs)
@app.post("/course_learn/suggest_expectations/{run_id}/feedback", tags=["course_learn"])
async def capture_suggest_expectations_feedback(run_id: UUID4, feedback: Feedback) -> str:
with TemporaryEnvironment(PROD_ENV_CONFIG):
return course_learn_suggest_expectations_feedback_logic(run_id, feedback)
@app.post("/staging/course_learn/suggest_expectations/{run_id}/feedback", tags=["course_learn", "staging"])
async def staging_capture_suggest_expectations_feedback(run_id: UUID4, feedback: Feedback) -> str:
with TemporaryEnvironment(STAGING_ENV_CONFIG):
return course_learn_suggest_expectations_feedback_logic(run_id, feedback)
def course_learn_expectation_revision_logic(inputs: ExpectationRevisionInputs) -> ExpectationRevisionResponse:
print("Inputs: ", inputs)
result = ExpectationRevision().kickoff(inputs={
"expectation": inputs.expectation,
"check_question": inputs.check_question,
"request": inputs.request,
})
return result
@app.post("/course_learn/expectation_revision", tags=["course_learn"])
async def course_learn_expectation_revision(inputs: ExpectationRevisionInputs) -> ExpectationRevisionResponse:
with TemporaryEnvironment(PROD_ENV_CONFIG):
return course_learn_expectation_revision_logic(inputs)
@app.post("/staging/course_learn/expectation_revision", tags=["course_learn", "staging"])
async def staging_course_learn_expectation_revision(inputs: ExpectationRevisionInputs) -> ExpectationRevisionResponse:
with TemporaryEnvironment(STAGING_ENV_CONFIG):
return course_learn_expectation_revision_logic(inputs)
def capture_expectation_revision_feedback_logic(run_id: UUID4, feedback: Feedback) -> str:
print("Helful Score: ", feedback.metric_type)
print("Feedback On: ", feedback.feedback_on)
post_feedback(run_id=run_id, feedback=feedback)
return "ok"
@app.post("/course_learn/expectation_revision/{run_id}/feedback", tags=["course_learn"])
async def capture_expectation_revision_feedback(run_id: UUID4, feedback: Feedback) -> str:
with TemporaryEnvironment(PROD_ENV_CONFIG):
return capture_expectation_revision_feedback_logic(run_id, feedback)
@app.post("/staging/course_learn/expectation_revision/{run_id}/feedback", tags=["course_learn", "staging"])
async def staging_capture_expectation_revision_feedback(run_id: UUID4, feedback: Feedback) -> str:
with TemporaryEnvironment(STAGING_ENV_CONFIG):
return capture_expectation_revision_feedback_logic(run_id, feedback)
def course_learn_suggest_check_question_logic(inputs: SuggestCheckQuestionInputs) -> SuggestCheckQuestionResponse:
print("Inputs: ", inputs)
result = SuggestCheckQuestion().kickoff(inputs={
"course": inputs.course,
"module": inputs.module,
"tasks": inputs.tasks,
"expectation": inputs.expectation,
})
return result
@app.post("/course_learn/suggest_check_question", tags=["course_learn"])
async def course_learn_suggest_check_question(inputs: SuggestCheckQuestionInputs) -> SuggestCheckQuestionResponse:
with TemporaryEnvironment(PROD_ENV_CONFIG):
return course_learn_suggest_check_question_logic(inputs)
@app.post("/staging/course_learn/suggest_check_question", tags=["course_learn", "staging"])
async def staging_course_learn_suggest_check_question(inputs: SuggestCheckQuestionInputs) -> SuggestCheckQuestionResponse:
with TemporaryEnvironment(STAGING_ENV_CONFIG):
return course_learn_suggest_check_question_logic(inputs)
def course_learn_suggest_check_question_feedback_logic(run_id: UUID4, feedback: Feedback) -> str:
print("Helful Score: ", feedback.metric_type)
print("Feedback On: ", feedback.feedback_on)
post_feedback(run_id=run_id, feedback=feedback)
return "ok"
@app.post("/course_learn/suggest_check_question/{run_id}/feedback", tags=["course_learn"])
async def course_learn_suggest_check_question_feedback(run_id: UUID4, feedback: Feedback) -> str:
with TemporaryEnvironment(PROD_ENV_CONFIG):
return course_learn_suggest_check_question_feedback_logic(run_id, feedback)
@app.post("/staging/course_learn/suggest_check_question/{run_id}/feedback", tags=["course_learn", "staging"])
async def staging_course_learn_suggest_check_question_feedback(run_id: UUID4, feedback: Feedback) -> str:
with TemporaryEnvironment(STAGING_ENV_CONFIG):
return course_learn_suggest_check_question_feedback_logic(run_id, feedback)
@app.post("/llm_feedback/{run_id}/feedback", tags=["llm_feedback"])
async def capture_llm_feedback(run_id: UUID4, feedback: Feedback) -> str:
with TemporaryEnvironment(PROD_ENV_CONFIG):
post_feedback(run_id=run_id, feedback=feedback)
return "ok"
@app.post("/staging/llm_feedback/{run_id}/feedback", tags=["llm_feedback", "staging"])
async def staging_capture_llm_feedback(run_id: UUID4, feedback: Feedback) -> str:
with TemporaryEnvironment(STAGING_ENV_CONFIG):
post_feedback(run_id=run_id, feedback=feedback)
return "ok"
@app.get("/healthcheck")
async def read_root():
return {"status": "ok"}
if __name__ == "__main__":
uvicorn.run(app, host="127.0.0.1", port=8080)