mirror of
https://github.com/open-webui/pipelines
synced 2025-05-13 17:10:45 +00:00
feat: litellm manifold example
This commit is contained in:
parent
ae0fe8f691
commit
a8348a3197
79
pipelines/examples/litellm_manifold_pipeline.py
Normal file
79
pipelines/examples/litellm_manifold_pipeline.py
Normal file
@ -0,0 +1,79 @@
|
||||
from typing import List, Union, Generator, Iterator
|
||||
from schemas import OpenAIChatMessage
|
||||
from pydantic import BaseModel
|
||||
import requests
|
||||
|
||||
|
||||
class Pipeline:
|
||||
def __init__(self):
|
||||
# You can also set the pipelines that are available in this pipeline.
|
||||
# Set manifold to True if you want to use this pipeline as a manifold.
|
||||
# Manifold pipelines can have multiple pipelines.
|
||||
self.type = "manifold"
|
||||
|
||||
# Optionally, you can set the id and name of the pipeline.
|
||||
# Assign a unique identifier to the pipeline.
|
||||
# The identifier must be unique across all pipelines.
|
||||
# The identifier must be an alphanumeric string that can include underscores or hyphens. It cannot contain spaces, special characters, slashes, or backslashes.
|
||||
self.id = "litellm_manifold"
|
||||
|
||||
# Optionally, you can set the name of the manifold pipeline.
|
||||
self.name = "LiteLLM: "
|
||||
|
||||
class Valves(BaseModel):
|
||||
LITELLM_BASE_URL: str
|
||||
|
||||
# Initialize rate limits
|
||||
self.valves = Valves(**{"LITELLM_BASE_URL": "http://localhost:4000"})
|
||||
|
||||
self.pipelines = self.get_litellm_models()
|
||||
pass
|
||||
|
||||
def get_litellm_models(self):
|
||||
if self.valves.LITELLM_BASE_URL:
|
||||
r = requests.get(f"{self.valves.LITELLM_BASE_URL}/v1/models")
|
||||
models = r.json()
|
||||
return [
|
||||
{
|
||||
"id": model["id"],
|
||||
"name": model["name"] if "name" in model else model["id"],
|
||||
}
|
||||
for model in models["data"]
|
||||
]
|
||||
else:
|
||||
return []
|
||||
|
||||
async def on_startup(self):
|
||||
# This function is called when the server is started.
|
||||
print(f"on_startup:{__name__}")
|
||||
pass
|
||||
|
||||
async def on_shutdown(self):
|
||||
# This function is called when the server is stopped.
|
||||
print(f"on_shutdown:{__name__}")
|
||||
pass
|
||||
|
||||
def pipe(
|
||||
self, user_message: str, model_id: str, messages: List[dict], body: dict
|
||||
) -> Union[str, Generator, Iterator]:
|
||||
if "user" in body:
|
||||
print("######################################")
|
||||
print(f'# User: {body["user"]["name"]} ({body["user"]["id"]})')
|
||||
print(f"# Message: {user_message}")
|
||||
print("######################################")
|
||||
|
||||
try:
|
||||
r = requests.post(
|
||||
url=f"{self.valves.LITELLM_BASE_URL}/v1/chat/completions",
|
||||
json={**body, "model": model_id, "user_id": body["user"]["id"]},
|
||||
stream=True,
|
||||
)
|
||||
|
||||
r.raise_for_status()
|
||||
|
||||
if body["stream"]:
|
||||
return r.iter_lines()
|
||||
else:
|
||||
return r.json()
|
||||
except Exception as e:
|
||||
return f"Error: {e}"
|
Loading…
Reference in New Issue
Block a user