-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add OpenRouter provider support #71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [blocking] Please rework the tutorial to match the style of the existing tutorials (see Three specific points:
The notebook already gets picked up by the |
||
| "cells": [ | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "# OpenRouter sample annotation with Leiden clusters\n", | ||
| "\n", | ||
| "This tutorial shows how to annotate one or more samples with `CellAnnotator`\n", | ||
| "using an OpenRouter model and a user-provided Leiden key." | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "import scanpy as sc\n", | ||
| "from cell_annotator import CellAnnotator" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## Configuration\n", | ||
| "\n", | ||
| "- `OPENROUTER_API_KEY`: your OpenRouter API key\n", | ||
| "- `OPENROUTER_MODEL`: model slug (e.g. `openai/gpt-4o-mini`)\n", | ||
| "- `LEIDEN_KEY`: cluster column in `adata.obs`\n", | ||
| "- `SAMPLE_KEY`: sample column in `adata.obs`, or `None` for a single sample\n", | ||
| "- `ADATA_PATH`: path to your `.h5ad` dataset" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "OPENROUTER_API_KEY = \"\" # e.g. sk-or-v1-...\n", | ||
| "OPENROUTER_MODEL = \"openai/gpt-4o-mini\"\n", | ||
| "LEIDEN_KEY = \"leiden\"\n", | ||
| "ADATA_PATH = \"path/to/your_data.h5ad\"\n", | ||
| "\n", | ||
| "SPECIES = \"human\"\n", | ||
| "TISSUE = \"pancreas\"\n", | ||
| "STAGE = \"adult\"\n", | ||
| "SAMPLE_KEY = \"sample\" # set to None for single-sample datasets\n", | ||
| "\n", | ||
| "if not OPENROUTER_API_KEY:\n", | ||
| " raise ValueError(\"Set OPENROUTER_API_KEY before continuing.\")\n", | ||
| "if not OPENROUTER_MODEL:\n", | ||
| " raise ValueError(\"Set OPENROUTER_MODEL before continuing.\")" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## Load data" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "adata = sc.read_h5ad(ADATA_PATH)\n", | ||
| "\n", | ||
| "if LEIDEN_KEY not in adata.obs.columns:\n", | ||
| " raise KeyError(f\"Column '{LEIDEN_KEY}' was not found in adata.obs\")\n", | ||
| "if SAMPLE_KEY is not None and SAMPLE_KEY not in adata.obs.columns:\n", | ||
| " raise KeyError(f\"Column '{SAMPLE_KEY}' was not found in adata.obs\")\n", | ||
| "\n", | ||
| "print(adata)\n", | ||
| "print(\"Leiden key:\", LEIDEN_KEY)\n", | ||
| "print(\"Sample key:\", SAMPLE_KEY)" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## Initialize annotator" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "cell_ann = CellAnnotator(\n", | ||
| " adata=adata,\n", | ||
| " species=SPECIES,\n", | ||
| " tissue=TISSUE,\n", | ||
| " stage=STAGE,\n", | ||
| " cluster_key=LEIDEN_KEY,\n", | ||
| " sample_key=SAMPLE_KEY,\n", | ||
| " provider=\"openrouter\",\n", | ||
| " model=OPENROUTER_MODEL,\n", | ||
| " api_key=OPENROUTER_API_KEY,\n", | ||
| ")\n", | ||
| "\n", | ||
| "cell_ann" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## Run annotation" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "cell_ann.get_expected_cell_type_markers(n_markers=3)\n", | ||
| "cell_ann.get_cluster_markers()\n", | ||
| "cell_ann.annotate_clusters(key_added=\"cell_type_predicted\")" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "## Inspect and save results" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "adata.obs[[LEIDEN_KEY, \"cell_type_predicted\"]].head(10)" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "if \"X_umap\" in adata.obsm:\n", | ||
| " sc.pl.umap(adata, color=[LEIDEN_KEY, \"cell_type_predicted\"], wspace=0.35)\n", | ||
| "else:\n", | ||
| " print(\"No UMAP embedding found; skipping plot.\")\n", | ||
| "\n", | ||
| "output_path = ADATA_PATH.replace(\".h5ad\", \"_annotated.h5ad\")\n", | ||
| "adata.write(output_path)\n", | ||
| "print(f\"Saved annotated object to: {output_path}\")" | ||
| ] | ||
| } | ||
| ], | ||
| "metadata": { | ||
| "kernelspec": { | ||
| "display_name": "Python 3", | ||
| "language": "python", | ||
| "name": "python3" | ||
| }, | ||
| "language_info": { | ||
| "name": "python" | ||
| } | ||
| }, | ||
| "nbformat": 4, | ||
| "nbformat_minor": 5 | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,11 @@ class APIKeyManager: | |
| "setup_url": "https://console.anthropic.com/settings/keys", | ||
| "description": "Anthropic Claude models", | ||
| }, | ||
| "openrouter": { | ||
| "env_var": "OPENROUTER_API_KEY", | ||
| "setup_url": "https://openrouter.ai/settings/keys", | ||
| "description": "OpenRouter models (aggregated providers)", | ||
| }, | ||
| } | ||
|
|
||
| def __init__(self, auto_load_env: bool = True): | ||
|
|
@@ -186,6 +191,10 @@ def validate_model_access(self, model: str) -> tuple[bool, str | None]: | |
| provider = "gemini" | ||
| elif any(claude_name in model_lower for claude_name in ["claude", "anthropic"]): | ||
| provider = "anthropic" | ||
| elif "/" in model and not model_lower.startswith("models/"): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit / follow-up: this |
||
| # OpenRouter uses '<provider>/<model>' slugs (e.g. 'openai/gpt-4o-mini'). | ||
| # The 'models/' guard avoids false-matching Gemini IDs like 'models/gemini-1.5-flash'. | ||
| provider = "openrouter" | ||
|
Marius1311 marked this conversation as resolved.
|
||
| elif any(openai_name in model_lower for openai_name in ["gpt", "o1", "davinci", "curie", "babbage", "ada"]): | ||
| provider = "openai" | ||
| else: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since OpenRouter forwards requests to a chosen upstream provider under OpenRouter's own ToS, the effective attack surface for OpenRouter users is wider than for the other three providers. Could you strengthen this paragraph along the lines of: