refac: filter -> inlet

This commit is contained in:
Timothy J. Baek 2024-05-29 23:29:59 -07:00
parent 0b43a73996
commit f677bb9d0c
5 changed files with 45 additions and 12 deletions

41
main.py
View File

@ -463,9 +463,9 @@ async def update_valves(pipeline_id: str, form_data: dict):
return pipeline.valves
@app.post("/v1/{pipeline_id}/filter")
@app.post("/{pipeline_id}/filter")
async def filter(pipeline_id: str, form_data: FilterForm):
@app.post("/v1/{pipeline_id}/filter/inlet")
@app.post("/{pipeline_id}/filter/inlet")
async def filter_inlet(pipeline_id: str, form_data: FilterForm):
if (
pipeline_id not in app.state.PIPELINES
or app.state.PIPELINES[pipeline_id].get("type", "pipe") != "filter"
@ -478,8 +478,39 @@ async def filter(pipeline_id: str, form_data: FilterForm):
pipeline = PIPELINE_MODULES[pipeline_id]
try:
body = await pipeline.filter(form_data.body, form_data.user)
return body
if hasattr(pipeline, "inlet"):
body = await pipeline.inlet(form_data.body, form_data.user)
return body
else:
return form_data.body
except Exception as e:
print(e)
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"{str(e)}",
)
@app.post("/v1/{pipeline_id}/filter/outlet")
@app.post("/{pipeline_id}/filter/outlet")
async def filter_outlet(pipeline_id: str, form_data: FilterForm):
if (
pipeline_id not in app.state.PIPELINES
or app.state.PIPELINES[pipeline_id].get("type", "pipe") != "filter"
):
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Filter {pipeline_id} not found",
)
pipeline = PIPELINE_MODULES[pipeline_id]
try:
if hasattr(pipeline, "outlet"):
body = await pipeline.outlet(form_data.body, form_data.user)
return body
else:
return form_data.body
except Exception as e:
print(e)
raise HTTPException(

View File

@ -56,8 +56,9 @@ class Pipeline:
# This function is called when the valves are updated.
pass
async def filter(self, body: dict, user: Optional[dict] = None) -> dict:
print(f"filter:{__name__}")
async def inlet(self, body: dict, user: Optional[dict] = None) -> dict:
# This filter is applied to the form data before it is sent to the OpenAI API.
print(f"inlet:{__name__}")
print(body)
user_message = body["messages"][-1]["content"]

View File

@ -43,8 +43,9 @@ class Pipeline:
print(f"on_shutdown:{__name__}")
pass
async def filter(self, body: dict, user: Optional[dict] = None) -> dict:
print(f"pipe:{__name__}")
async def inlet(self, body: dict, user: Optional[dict] = None) -> dict:
# This filter is applied to the form data before it is sent to the OpenAI API.
print(f"inlet:{__name__}")
print(body)
print(user)

View File

@ -77,8 +77,8 @@ class Pipeline:
)
self.langfuse.auth_check()
async def filter(self, body: dict, user: Optional[dict] = None) -> dict:
print(f"filter:{__name__}")
async def inlet(self, body: dict, user: Optional[dict] = None) -> dict:
print(f"inlet:{__name__}")
trace = self.langfuse.trace(
name=f"filter:{__name__}",

View File

@ -103,7 +103,7 @@ class Pipeline:
return False
async def filter(self, body: dict, user: Optional[dict] = None) -> dict:
async def inlet(self, body: dict, user: Optional[dict] = None) -> dict:
print(f"pipe:{__name__}")
print(body)
print(user)