|
| 1 | +import os |
| 2 | +from typing import List, Union, Generator, Iterator |
| 3 | +from pydantic import BaseModel |
| 4 | +import requests |
| 5 | + |
| 6 | +APISERVER_HOST = os.getenv("META_SUPERVISOR_HOST") |
| 7 | + |
| 8 | + |
| 9 | +class RequestBody(BaseModel): |
| 10 | + query: str |
| 11 | + model: str |
| 12 | + temperature: float |
| 13 | + |
| 14 | + |
| 15 | +class ResponseBody(BaseModel): |
| 16 | + answer: str |
| 17 | + |
| 18 | + |
| 19 | +class Pipeline: |
| 20 | + class Valves(BaseModel): |
| 21 | + pass |
| 22 | + |
| 23 | + def __init__(self): |
| 24 | + self.name = "Meta Supervisor - Main" |
| 25 | + self.agent_name = "metasupervisor" |
| 26 | + self.endpoint = f"http://{APISERVER_HOST}/api/query" |
| 27 | + pass |
| 28 | + |
| 29 | + async def on_startup(self): |
| 30 | + # This function is called when the server is started. |
| 31 | + print(f"on_startup:{__name__}") |
| 32 | + pass |
| 33 | + |
| 34 | + async def on_shutdown(self): |
| 35 | + # This function is called when the server is stopped. |
| 36 | + print(f"on_shutdown:{__name__}") |
| 37 | + pass |
| 38 | + |
| 39 | + def pipe( |
| 40 | + self, user_message: str, model_id: str, messages: List[dict], body: dict |
| 41 | + ) -> Union[str, Generator, Iterator]: |
| 42 | + # This is where you can add your custom pipelines like RAG. |
| 43 | + print(f"pipe:{__name__}") |
| 44 | + |
| 45 | + print("messages: ", messages) |
| 46 | + print("user_message: ", user_message) |
| 47 | + print("body: ", body) |
| 48 | + |
| 49 | + headers = {} |
| 50 | + headers["accept"] = "application/json" |
| 51 | + headers["Content-Type"] = "application/json" |
| 52 | + |
| 53 | + try: |
| 54 | + r = requests.post( |
| 55 | + url=f"{self.endpoint}", |
| 56 | + json={"query": user_message }, |
| 57 | + headers=headers, |
| 58 | + ) |
| 59 | + |
| 60 | + r.raise_for_status() |
| 61 | + return ResponseBody(**r.json()).answer |
| 62 | + except Exception as e: |
| 63 | + return f"Error: {e}" |
0 commit comments