Skip to content

Instantly share code, notes, and snippets.

@simonw
Created March 27, 2025 19:58
Show Gist options
  • Select an option

  • Save simonw/fdc1b48b20a99714200f5d3970b1dff4 to your computer and use it in GitHub Desktop.

Select an option

Save simonw/fdc1b48b20a99714200f5d3970b1dff4 to your computer and use it in GitHub Desktop.
{
"id": "llm/cli.py:1776",
"score": 0.37465753605256674,
"content": "@cli.command(name=\"plugins\")\n@click.option(\"--all\", help=\"Include built-in default plugins\", is_flag=True)\ndef plugins_list(all):\n \"List installed plugins\"\n click.echo(json.dumps(get_plugins(all), indent=2))",
"metadata": null
}
{
"id": "llm/cli.py:1791",
"score": 0.36442988318076636,
"content": "@cli.command()\n@click.argument(\"packages\", nargs=-1, required=False)\n@click.option(\n \"-U\", \"--upgrade\", is_flag=True, help=\"Upgrade packages to latest version\"\n)\n@click.option(\n \"-e\",\n \"--editable\",\n help=\"Install a project in editable mode from this path\",\n)\n@click.option(\n \"--force-reinstall\",\n is_flag=True,\n help=\"Reinstall all packages even if they are already up-to-date\",\n)\n@click.option(\n \"--no-cache-dir\",\n is_flag=True,\n help=\"Disable the cache\",\n)\ndef install(packages, upgrade, editable, force_reinstall, no_cache_dir):\n \"\"\"Install packages from PyPI into the same environment as LLM\"\"\"\n args = [\"pip\", \"install\"]\n if upgrade:\n args += [\"--upgrade\"]\n if editable:\n args += [\"--editable\", editable]\n if force_reinstall:\n args += [\"--force-reinstall\"]\n if no_cache_dir:\n args += [\"--no-cache-dir\"]\n args += list(packages)\n sys.argv = args\n run_module(\"pip\", run_name=\"__main__\")",
"metadata": null
}
{
"id": "llm/cli.py:1827",
"score": 0.3001879629201789,
"content": "@cli.command()\n@click.argument(\"packages\", nargs=-1, required=True)\n@click.option(\"-y\", \"--yes\", is_flag=True, help=\"Don't ask for confirmation\")\ndef uninstall(packages, yes):\n \"\"\"Uninstall Python packages from the LLM environment\"\"\"\n sys.argv = [\"pip\", \"uninstall\"] + list(packages) + ([\"-y\"] if yes else [])\n run_module(\"pip\", run_name=\"__main__\")",
"metadata": null
}
{
"id": "tests/test_plugins.py:8",
"score": 0.26169557855107234,
"content": "def test_register_commands():\n importlib.reload(cli)\n\n def plugin_names():\n return [plugin[\"name\"] for plugin in llm.get_plugins()]\n\n assert \"HelloWorldPlugin\" not in plugin_names()\n\n class HelloWorldPlugin:\n __name__ = \"HelloWorldPlugin\"\n\n @hookimpl\n def register_commands(self, cli):\n @cli.command(name=\"hello-world\")\n def hello_world():\n \"Print hello world\"\n click.echo(\"Hello world!\")\n\n try:\n plugins.pm.register(HelloWorldPlugin(), name=\"HelloWorldPlugin\")\n importlib.reload(cli)\n\n assert \"HelloWorldPlugin\" in plugin_names()\n\n runner = CliRunner()\n result = runner.invoke(cli.cli, [\"hello-world\"])\n assert result.exit_code == 0\n assert result.output == \"Hello world!\\n\"\n\n finally:\n plugins.pm.unregister(name=\"HelloWorldPlugin\")\n importlib.reload(cli)\n assert \"HelloWorldPlugin\" not in plugin_names()",
"metadata": null
}
{
"id": "tests/test_embed.py:9",
"score": 0.22549138284613493,
"content": "def test_demo_plugin():\n model = llm.get_embedding_model(\"embed-demo\")\n assert model.embed(\"hello world\") == [5, 5] + [0] * 14",
"metadata": null
}
{
"id": "llm/plugins.py:18",
"score": 0.21343024460897292,
"content": "def load_plugins():\n global _loaded\n if _loaded:\n return\n _loaded = True\n if not hasattr(sys, \"_called_from_test\") and LLM_LOAD_PLUGINS is None:\n # Only load plugins if not running tests\n pm.load_setuptools_entrypoints(\"llm\")\n\n # Load any plugins specified in LLM_LOAD_PLUGINS\")\n if LLM_LOAD_PLUGINS is not None:\n for package_name in [\n name for name in LLM_LOAD_PLUGINS.split(\",\") if name.strip()\n ]:\n try:\n distribution = metadata.distribution(package_name) # Updated call\n llm_entry_points = [\n ep for ep in distribution.entry_points if ep.group == \"llm\"\n ]\n for entry_point in llm_entry_points:\n mod = entry_point.load()\n pm.register(mod, name=entry_point.name)\n # Ensure name can be found in plugin_to_distinfo later:\n pm._plugin_distinfo.append((mod, distribution)) # type: ignore\n except metadata.PackageNotFoundError:\n sys.stderr.write(f\"Plugin {package_name} could not be found\\n\")\n\n for plugin in DEFAULT_PLUGINS:\n mod = importlib.import_module(plugin)\n pm.register(mod, plugin)",
"metadata": null
}
{
"id": "llm/cli.py:1836",
"score": 0.1972868321400161,
"content": "@cli.command()\n@click.argument(\"collection\", required=False)\n@click.argument(\"id\", required=False)\n@click.option(\n \"-i\",\n \"--input\",\n type=click.Path(exists=True, readable=True, allow_dash=True),\n help=\"File to embed\",\n)\n@click.option(\"-m\", \"--model\", help=\"Embedding model to use\")\n@click.option(\"--store\", is_flag=True, help=\"Store the text itself in the database\")\n@click.option(\n \"-d\",\n \"--database\",\n type=click.Path(file_okay=True, allow_dash=False, dir_okay=False, writable=True),\n envvar=\"LLM_EMBEDDINGS_DB\",\n)\n@click.option(\n \"-c\",\n \"--content\",\n help=\"Content to embed\",\n)\n@click.option(\"--binary\", is_flag=True, help=\"Treat input as binary data\")\n@click.option(\n \"--metadata\",\n help=\"JSON object metadata to store\",\n callback=json_validator(\"metadata\"),\n)\n@click.option(\n \"format_\",\n \"-f\",\n \"--format\",\n type=click.Choice([\"json\", \"blob\", \"base64\", \"hex\"]),\n help=\"Output format\",\n)\ndef embed(\n collection, id, input, model, store, database, content, binary, metadata, format_\n):\n \"\"\"Embed text and store or return the result\"\"\"\n if collection and not id:\n raise click.ClickException(\"Must provide both collection and id\")\n\n if store and not collection:\n raise click.ClickException(\"Must provide collection when using --store\")\n\n # Lazy load this because we do not need it for -c or -i versions\n def get_db():\n if database:\n return sqlite_utils.Database(database)\n else:\n return sqlite_utils.Database(user_dir() / \"embeddings.db\")\n\n collection_obj = None\n model_obj = None\n if collection:\n db = get_db()\n if Collection.exists(db, collection):\n # Load existing collection and use its model\n collection_obj = Collection(collection, db)\n model_obj = collection_obj.model()\n else:\n # We will create a new one, but that means model is required\n if not model:\n model = get_default_embedding_model()\n if model is None:\n raise click.ClickException(\n \"You need to specify an embedding model (no default model is set)\"\n )\n collection_obj = Collection(collection, db=db, model_id=model)\n model_obj = collection_obj.model()\n\n if model_obj is None:\n if model is None:\n model = get_default_embedding_model()\n try:\n model_obj = get_embedding_model(model)\n except UnknownModelError:\n raise click.ClickException(\n \"You need to specify an embedding model (no default model is set)\"\n )\n\n show_output = True\n if collection and (format_ is None):\n show_output = False\n\n # Resolve input text\n if not content:\n if not input or input == \"-\":\n # Read from stdin\n input_source = sys.stdin.buffer if binary else sys.stdin\n content = input_source.read()\n else:\n mode = \"rb\" if binary else \"r\"\n with open(input, mode) as f:\n content = f.read()\n\n if not content:\n raise click.ClickException(\"No content provided\")\n\n if collection_obj:\n embedding = collection_obj.embed(id, content, metadata=metadata, store=store)\n else:\n embedding = model_obj.embed(content)\n\n if show_output:\n if format_ == \"json\" or format_ is None:\n click.echo(json.dumps(embedding))\n elif format_ == \"blob\":\n click.echo(encode(embedding))\n elif format_ == \"base64\":\n click.echo(base64.b64encode(encode(embedding)).decode(\"ascii\"))\n elif format_ == \"hex\":\n click.echo(encode(embedding).hex())",
"metadata": null
}
{
"id": "llm/cli.py:151",
"score": 0.19148804067255487,
"content": "@click.group(\n cls=DefaultGroup,\n default=\"prompt\",\n default_if_no_args=True,\n)\n@click.version_option()\ndef cli():\n \"\"\"\n Access Large Language Models from the command-line\n\n Documentation: https://llm.datasette.io/\n\n LLM can run models from many different providers. Consult the\n plugin directory for a list of available models:\n\n https://llm.datasette.io/en/stable/plugins/directory.html\n\n To get started with OpenAI, obtain an API key from them and:\n\n \\b\n $ llm keys set openai\n Enter key: ...\n\n Then execute a prompt like this:\n\n llm 'Five outrageous names for a pet pelican'\n \"\"\"",
"metadata": null
}
{
"id": "llm/cli.py:1753",
"score": 0.1863117227526341,
"content": "@aliases.command(name=\"remove\")\n@click.argument(\"alias\")\ndef aliases_remove(alias):\n \"\"\"\n Remove an alias\n\n Example usage:\n\n \\b\n $ llm aliases remove turbo\n \"\"\"\n try:\n remove_alias(alias)\n except KeyError as ex:\n raise click.ClickException(ex.args[0])",
"metadata": null
}
{
"id": "llm/hookspecs.py:8",
"score": 0.1672872472388833,
"content": "@hookspec\ndef register_commands(cli):\n \"\"\"Register additional CLI commands, e.g. 'llm mycommand ...'\"\"\"",
"metadata": null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment