Skip to content

Commit

Permalink
Support for async models, closes #15
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Dec 8, 2024
1 parent c437a05 commit a8dae4e
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions llm_openrouter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import llm
from llm.default_plugins.openai_models import Chat
from llm.default_plugins.openai_models import Chat, AsyncChat
from pathlib import Path
import json
import time
Expand All @@ -22,6 +22,14 @@ def __str__(self):
return "OpenRouter: {}".format(self.model_id)


class OpenRouterAsyncChat(AsyncChat):
needs_key = "openrouter"
key_env_var = "OPENROUTER_KEY"

def __str__(self):
return "OpenRouter: {}".format(self.model_id)


@llm.hookimpl
def register_models(register):
# Only do this if the openrouter key is set
Expand All @@ -30,14 +38,16 @@ def register_models(register):
return
for model_definition in get_openrouter_models():
supports_images = get_supports_images(model_definition)
kwargs = dict(
model_id="openrouter/{}".format(model_definition["id"]),
model_name=model_definition["id"],
vision=supports_images,
api_base="https://openrouter.ai/api/v1",
headers={"HTTP-Referer": "https://llm.datasette.io/", "X-Title": "LLM"},
)
register(
OpenRouterChat(
model_id="openrouter/{}".format(model_definition["id"]),
model_name=model_definition["id"],
vision=supports_images,
api_base="https://openrouter.ai/api/v1",
headers={"HTTP-Referer": "https://llm.datasette.io/", "X-Title": "LLM"},
)
OpenRouterChat(**kwargs),
OpenRouterAsyncChat(**kwargs),
)


Expand Down

0 comments on commit a8dae4e

Please sign in to comment.