Skip to content
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

FEAT: Add API for getting Neuronpedia feature #88

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion sae_lens/analysis/neuronpedia_integration.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import urllib.parse
import webbrowser
import requests


def get_neuronpedia_quick_list(
Expand All @@ -14,10 +15,29 @@ def get_neuronpedia_quick_list(
name = urllib.parse.quote(name)
url = url + "?name=" + name
list_feature = [
{"modelId": model, "layer": f"{layer}-{dataset}", "index": str(feature)}
{
"modelId": model,
"layer": f"{layer}-{dataset}",
"index": str(feature),
}
for feature in features
]
url = url + "&features=" + urllib.parse.quote(json.dumps(list_feature))
webbrowser.open(url)

return url


def get_neuronpedia_feature(
feature: int,
layer: int,
model: str = "gpt2-small",
dataset: str = "res-jb",
):
url = "https://neuronpedia.org/api/feature/"
url = url + f"{model}/{layer}-{dataset}/{feature}"

result = requests.get(url).json()
result["index"] = int(result["index"])

return result
11 changes: 11 additions & 0 deletions tests/unit/analysis/test_neuronpedia_integration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from sae_lens.analysis.neuronpedia_integration import get_neuronpedia_feature


def test_get_neuronpedia_feature():
result = get_neuronpedia_feature(
feature=0, layer=0, model="gpt2-small", dataset="res-jb"
)

assert result["modelId"] == "gpt2-small"
assert result["layer"] == "0-res-jb"
assert result["index"] == 0