|
| 1 | +from typing import List, Union, Generator, Iterator, Optional |
| 2 | +from pprint import pprint |
| 3 | +import time |
| 4 | + |
| 5 | +# Uncomment to disable SSL verification warnings if needed. |
| 6 | +# warnings.filterwarnings('ignore', message='Unverified HTTPS request') |
| 7 | + |
| 8 | + |
| 9 | +class Pipeline: |
| 10 | + def __init__(self): |
| 11 | + self.name = "Pipeline with Status Event" |
| 12 | + self.description = ( |
| 13 | + "This is a pipeline that demonstrates how to use the status event." |
| 14 | + ) |
| 15 | + self.debug = True |
| 16 | + self.version = "0.1.0" |
| 17 | + self.author = "Anthony Durussel" |
| 18 | + |
| 19 | + async def on_startup(self): |
| 20 | + # This function is called when the server is started. |
| 21 | + print(f"on_startup: {__name__}") |
| 22 | + pass |
| 23 | + |
| 24 | + async def on_shutdown(self): |
| 25 | + # This function is called when the server is shutdown. |
| 26 | + print(f"on_shutdown: {__name__}") |
| 27 | + pass |
| 28 | + |
| 29 | + async def inlet(self, body: dict, user: Optional[dict] = None) -> dict: |
| 30 | + # This function is called before the OpenAI API request is made. You can modify the form data before it is sent to the OpenAI API. |
| 31 | + print(f"inlet: {__name__}") |
| 32 | + if self.debug: |
| 33 | + print(f"inlet: {__name__} - body:") |
| 34 | + pprint(body) |
| 35 | + print(f"inlet: {__name__} - user:") |
| 36 | + pprint(user) |
| 37 | + return body |
| 38 | + |
| 39 | + async def outlet(self, body: dict, user: Optional[dict] = None) -> dict: |
| 40 | + # This function is called after the OpenAI API response is completed. You can modify the messages after they are received from the OpenAI API. |
| 41 | + print(f"outlet: {__name__}") |
| 42 | + if self.debug: |
| 43 | + print(f"outlet: {__name__} - body:") |
| 44 | + pprint(body) |
| 45 | + print(f"outlet: {__name__} - user:") |
| 46 | + pprint(user) |
| 47 | + return body |
| 48 | + |
| 49 | + def pipe( |
| 50 | + self, |
| 51 | + user_message: str, |
| 52 | + model_id: str, |
| 53 | + messages: List[dict], |
| 54 | + body: dict, |
| 55 | + ) -> Union[str, Generator, Iterator]: |
| 56 | + print(f"pipe: {__name__}") |
| 57 | + |
| 58 | + if self.debug: |
| 59 | + print(f"pipe: {__name__} - received message from user: {user_message}") |
| 60 | + |
| 61 | + yield { |
| 62 | + "event": { |
| 63 | + "type": "status", |
| 64 | + "data": { |
| 65 | + "description": "Fake Status", |
| 66 | + "done": False, |
| 67 | + }, |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + time.sleep(5) # Sleep for 5 seconds |
| 72 | + |
| 73 | + yield f"user_message: {user_message}" |
| 74 | + |
| 75 | + yield { |
| 76 | + "event": { |
| 77 | + "type": "status", |
| 78 | + "data": { |
| 79 | + "description": "", |
| 80 | + "done": True, |
| 81 | + }, |
| 82 | + } |
| 83 | + } |
0 commit comments