Spaces:
Runtime error
Runtime error
k-manjunath commited on
Commit ·
6b88892
1
Parent(s): 0a98373
workflows(til_v2): update til to include readability and headlines endpoints
Browse files* new endpoint for TIL headlines
* new endpoint for enhancing TIL readability
* enhance endpoints.py with utils
Files changed (4)
- endpoints.py+41-11
- utils/endpoints_utils.pyAdded+6
- workflows/til/rewrite_til_v2.pyAdded+84
- workflows/til/suggest_headlines_v2.pyAdded+84
5 unmodified lines67891011121314151617181932 unmodified lines525354555657585960616225 unmodified lines888990919293949596971 unmodified line9910010110210310410510610710810911011112 unmodified lines1241251261271281295 unmodified linesfrom tempenv import TemporaryEnvironmentfrom pydantic import UUID4from fastapi.middleware.cors import CORSMiddlewarefrom fastapi import FastAPI, Queryfrom .workflows.utils.feedback import Feedback, post_feedbackfrom .workflows.til.analyse_til_v2 import AnalyseTilV2, TilV2FeedbackResponsefrom .workflows.til.analyse_til import TilCrew, TilFeedbackResponsefrom .workflows.courses.suggest_expectations import SuggestExpectations, Inputs as SuggestExpectationsInputs, Expectation, Response as SuggestExpectationsResponsefrom .workflows.courses.suggest_check_question import SuggestCheckQuestion, Inputs as SuggestCheckQuestionInputs, Response as SuggestCheckQuestionResponsefrom .workflows.courses.expectation_revision import ExpectationRevision, Inputs as ExpectationRevisionInputs, Response as ExpectationRevisionResponseLANGSMITH_STAGING_PROJECT = "customer_agent"LANGSMITH_PROD_PROJECT = "growthy-agents"32 unmodified lines)@app.post("/til_feedback", tags=["til_feedback"])async def til_feedback_kickoff(content: List[str]) -> TilFeedbackResponse:separator = "\n* "content[0] = "* " + content[0]inputs = {"content": separator.join(content)}result = TilCrew().kickoff(inputs)return result25 unmodified linesreturn "ok"def til_v2_logic(content) -> TilV2FeedbackResponse:separator = "\n* "content[0] = "* " + content[0]inputs = {"content": separator.join(content)}result = AnalyseTilV2().kickoff(inputs)return result1 unmodified line@app.post("/v2/til_feedback", tags=["til_feedback"])async def til_v2_feedback_kickoff(content: List[str]) -> TilV2FeedbackResponse:with TemporaryEnvironment({"LANGCHAIN_PROJECT": LANGSMITH_PROD_PROJECT, "OPENAI_MODEL": "gpt-4o"}):return til_v2_logic(content)@app.post("/staging/v2/til_feedback", tags=["til_feedback", "staging"])async def staging_til_v2_feedback_kickoff(content: List[str]) -> TilV2FeedbackResponse:with TemporaryEnvironment({"LANGCHAIN_PROJECT": LANGSMITH_STAGING_PROJECT, "OPENAI_MODEL": "gpt-4o-mini"}):return til_v2_logic(content)@app.post("/v2/til_feedback/{run_id}/feedback", tags=["til_feedback"])12 unmodified linesreturn "ok"def course_learn_suggest_expectations_logic(inputs) -> SuggestExpectationsResponse:print("Inputs: ", inputs)result = SuggestExpectations().kickoff(inputs={5 unmodified lines67891011121314151617181920212232 unmodified lines5556575859606162636425 unmodified lines90919293949596971 unmodified line9910010110210310410510610710810911011112 unmodified lines1241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581595 unmodified linesfrom tempenv import TemporaryEnvironmentfrom pydantic import UUID4from fastapi.middleware.cors import CORSMiddlewarefrom fastapi import FastAPIfrom .utils.endpoints_utils import CreateTilInputsfrom .workflows.utils.feedback import Feedback, post_feedbackfrom .workflows.til.analyse_til_v2 import AnalyseTilV2, TilV2FeedbackResponsefrom .workflows.til.analyse_til import TilCrew, TilFeedbackResponsefrom .workflows.courses.suggest_expectations import SuggestExpectations, Inputs as SuggestExpectationsInputs, Expectation, Response as SuggestExpectationsResponsefrom .workflows.courses.suggest_check_question import SuggestCheckQuestion, Inputs as SuggestCheckQuestionInputs, Response as SuggestCheckQuestionResponsefrom .workflows.courses.expectation_revision import ExpectationRevision, Inputs as ExpectationRevisionInputs, Response as ExpectationRevisionResponsefrom .workflows.til.rewrite_til_v2 import RewriteTilV2, Response as RewriteTilResponsefrom .workflows.til.suggest_headlines_v2 import SuggestHeadlinesV2, Response as SuggestHeadlinesResponseLANGSMITH_STAGING_PROJECT = "customer_agent"LANGSMITH_PROD_PROJECT = "growthy-agents"32 unmodified lines)# 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 result25 unmodified linesreturn "ok"def til_v2_analyze_logic(content) -> TilV2FeedbackResponse:inputs = CreateTilInputs(content)result = AnalyseTilV2().kickoff(inputs)return result1 unmodified line@app.post("/v2/til_feedback", tags=["til_feedback"])async def til_v2_feedback_kickoff(content: List[str]) -> TilV2FeedbackResponse:with TemporaryEnvironment({"LANGCHAIN_PROJECT": LANGSMITH_PROD_PROJECT, "OPENAI_MODEL": "gpt-4o"}):return til_v2_analyze_logic(content)@app.post("/staging/v2/til_feedback", tags=["til_feedback", "staging"])async def staging_til_v2_feedback_kickoff(content: List[str]) -> TilV2FeedbackResponse:with TemporaryEnvironment({"LANGCHAIN_PROJECT": LANGSMITH_STAGING_PROJECT, "OPENAI_MODEL": "gpt-4o-mini"}):return til_v2_analyze_logic(content)@app.post("/v2/til_feedback/{run_id}/feedback", tags=["til_feedback"])12 unmodified linesreturn "ok"@app.post("/v2/til_rewrite", tags=["til_readability"])async def til_v2_rewrite_kickoff(content: List[str]) -> RewriteTilResponse:with TemporaryEnvironment({"LANGCHAIN_PROJECT": LANGSMITH_PROD_PROJECT, "OPENAI_MODEL": "gpt-4o"}):inputs = CreateTilInputs(content)result = RewriteTilV2().kickoff(inputs)return result@app.post("/staging/v2/til_rewrite", tags=["til_readability", "staging"])async def staging_til_v2_rewrite_kickoff(content: List[str]) -> RewriteTilResponse:with TemporaryEnvironment({"LANGCHAIN_PROJECT": LANGSMITH_STAGING_PROJECT, "OPENAI_MODEL": "gpt-4o-mini"}):inputs = CreateTilInputs(content)result = RewriteTilV2().kickoff(inputs)return result@app.post("/v2/til_headlines", tags=["til_headlines"])async def til_v2_suggest_headlines(content: List[str]) -> SuggestHeadlinesResponse:with TemporaryEnvironment({"LANGCHAIN_PROJECT": LANGSMITH_PROD_PROJECT, "OPENAI_MODEL": "gpt-4o"}):inputs = CreateTilInputs(content)result = SuggestHeadlinesV2().kickoff(inputs)return result@app.post("/staging/v2/til_headlines", tags=["til_headlines", "staging"])async def staging_til_v2_suggest_headlines(content: List[str]) -> SuggestHeadlinesResponse:with TemporaryEnvironment({"LANGCHAIN_PROJECT": LANGSMITH_STAGING_PROJECT, "OPENAI_MODEL": "gpt-4o-mini"}):inputs = CreateTilInputs(content)result = SuggestHeadlinesV2().kickoff(inputs)return result# Course Learndef course_learn_suggest_expectations_logic(inputs) -> SuggestExpectationsResponse:print("Inputs: ", inputs)result = SuggestExpectations().kickoff(inputs={
123456from typing import List
def CreateTilInputs(content: List[str]) -> dict: separator = "\n* " content[0] = "* " + content[0] return {"content": separator.join(content)}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384from langchain import callbacks, hubfrom langchain_core.output_parsers import JsonOutputParserfrom langchain_openai import ChatOpenAIfrom pydantic import BaseModel, Field, UUID4from typing import Listimport os
class Takeaway(BaseModel): takeaway: str feedback: str
class Response(BaseModel): run_id : UUID4 new_til: List[Takeaway]
class ReadableTilResult(BaseModel): original_til: str = Field( description="The original TIL without any modifications.", ) readability_score: str = Field( description="The readability score as High/Medium/Low based on the readability of the TIL.", ) reason: str = Field( description="The reason for the assessment of the TIL readability score in one sentence.", ) readable_til: str = Field( description="Rewrite the TIL in a readable manner only if the readability score is not High", )
class ReadableTilResults(BaseModel): tils: List[ReadableTilResult]
class RewriteTilV2():
def kickoff(self, inputs=[]) -> Response: self.content = inputs["content"] return self._get_understandable_til()
def _get_understandable_til(self) -> Response: prompt = hub.pull("til_understandability_revision") llm = ChatOpenAI(model=os.environ['OPENAI_MODEL'], temperature=0.2) parser = JsonOutputParser(pydantic_object=ReadableTilResults)
chain = (prompt | llm | parser).with_config({ "tags": ["til"], "run_name": "Rewrite Understandable TIL", "metadata": { "version": "v2.0.0", "growth_activity": "til", "env": os.environ["ENV"], "model": os.environ["OPENAI_MODEL"] } })
with callbacks.collect_runs() as cb: self.llm_response = chain.invoke({ "til_content": self.content, "format_instructions": parser.get_format_instructions(), }) self.run_id = cb.traced_runs[0].id
return self._handle_response()
def _handle_response(self) -> Response:
response = Response( run_id=self.run_id, new_til=[] )
recommended_tils = self.llm_response["tils"]
for til in recommended_tils: new_takeaway = Takeaway( feedback="not_ok", takeaway=til["original_til"] )
if til["readability_score"] != "High": new_takeaway.feedback = "ok" new_takeaway.takeaway = til["readable_til"]
response.new_til.append(new_takeaway)
return response123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384from langchain import callbacks, hubfrom langchain_core.messages import SystemMessagefrom langchain_core.output_parsers import JsonOutputParserfrom langchain_core.prompts import ChatPromptTemplate, HumanMessagePromptTemplatefrom langchain_openai import ChatOpenAIfrom pydantic import BaseModel, Field, UUID4from typing import List, Optionalimport osimport pprint
class HeadlineInfo(BaseModel): headline: str tone: str reason: str
class Response(BaseModel): run_id : UUID4 headlines_details: List[HeadlineInfo]
class HeadlineInfoLLM(BaseModel): headline: str = Field( description="The suggested headline for the given TIL.", ) tone: str = Field( description="The tone of the suggested headline.", ) reason: str = Field( description="Reason for the clickability_score in one sentence", ) clickability_score: int = Field( description="A score out of 10 on how likely the user is going to click on the headline and read the TIL.", )
class HeadlineResults(BaseModel): headlines_details: List[HeadlineInfoLLM]
class SuggestHeadlinesV2():
def kickoff(self, inputs=[]) -> Response: self.content = inputs["content"] return self._get_til_headline()
def _get_til_headline(self) -> Response: prompt = hub.pull("til_suggest_headline") llm = ChatOpenAI(model=os.environ['OPENAI_MODEL'], temperature=0.2) parser = JsonOutputParser(pydantic_object=HeadlineResults)
chain = (prompt | llm | parser).with_config({ "tags": ["til"], "run_name": "Suggest TIL Headlines", "metadata": { "version": "v2.0.0", "growth_activity": "til", "env": os.environ["ENV"], "model": os.environ["OPENAI_MODEL"] } })
with callbacks.collect_runs() as cb: self.llm_response = chain.invoke({ "til_content": self.content, "format_instructions": parser.get_format_instructions(), }) self.run_id = cb.traced_runs[0].id
return self._handle_response()
def _handle_response(self) -> Response: response = Response( run_id=self.run_id, headlines_details=[] )
headlines_data = self.llm_response["headlines_details"] for headline_datum in headlines_data: response.headlines_details.append( HeadlineInfo( headline=headline_datum["headline"], tone=headline_datum["tone"], reason=headline_datum["reason"], ) )
return response
endpoints.py+41-11
utils/endpoints_utils.pyAdded+6
workflows/til/rewrite_til_v2.pyAdded+84
workflows/til/suggest_headlines_v2.pyAdded+84