Skip to content

Instantly share code, notes, and snippets.

@ilhamgusti
Created October 8, 2024 01:59
Show Gist options
  • Save ilhamgusti/fb09b11efaa6e2e3bfe656f8727c17b4 to your computer and use it in GitHub Desktop.
Save ilhamgusti/fb09b11efaa6e2e3bfe656f8727c17b4 to your computer and use it in GitHub Desktop.
OpenRouter Manifolds
from typing import List, Union, Generator, Iterator
from schemas import OpenAIChatMessage
from pydantic import BaseModel
import os
import requests
class Pipeline:
class Valves(BaseModel):
OPENROUTER_API_BASE_URL: str = "https://openrouter.ai/api/v1"
OPENROUTER_API_KEY: str = ""
pass
def __init__(self):
self.type = "manifold"
self.name = "OR: "
self.valves = self.Valves(
**{
"OPENROUTER_API_KEY": os.getenv(
"OPENROUTER_API_KEY", "your-openrouter-api-key-here"
)
}
)
self.pipelines = self.get_openrouter_models()
pass
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
async def on_valves_updated(self):
# This function is called when the valves are updated.
print(f"on_valves_updated:{__name__}")
self.pipelines = self.get_openrouter_models()
pass
def get_openrouter_models(self):
if self.valves.OPENROUTER_API_KEY:
try:
headers = {}
headers["Authorization"] = f"Bearer {self.valves.OPENROUTER_API_KEY}"
headers["Content-Type"] = "application/json"
r = requests.get(
f"{self.valves.OPENROUTER_API_BASE_URL}/models", headers=headers
)
models = r.json()
return [
{
"id": model["id"],
"name": model["name"] if "name" in model else model["id"],
}
for model in models["data"]
]
except Exception as e:
print(f"Error: {e}")
return [
{
"id": "error",
"name": "Could not fetch models from OpenRouter, please update the API Key in the valves.",
},
]
else:
return []
def pipe(
self, user_message: str, model_id: str, messages: List[dict], body: dict
) -> Union[str, Generator, Iterator]:
# This is where you can add your custom pipelines like RAG.
print(f"pipe:{__name__}")
print(messages)
print(user_message)
headers = {}
headers["Authorization"] = f"Bearer {self.valves.OPENROUTER_API_KEY}"
headers["Content-Type"] = "application/json"
payload = {**body, "model": model_id}
if "user" in payload:
del payload["user"]
if "chat_id" in payload:
del payload["chat_id"]
if "title" in payload:
del payload["title"]
print(payload)
try:
r = requests.post(
url=f"{self.valves.OPENROUTER_API_BASE_URL}/chat/completions",
json=payload,
headers=headers,
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}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment