From f00799a7ab85fd25bd7eacdc48d1e06e841ac05d Mon Sep 17 00:00:00 2001 From: jolheiser Date: Wed, 7 Aug 2024 19:19:51 -0500 Subject: [PATCH] feat: llm and q Signed-off-by: jolheiser --- apps/nogui/default.nix | 1 + apps/nogui/llm.nix | 55 +++++++++++++++++++++++++++++++++ apps/nogui/nushell/jolheiser.nu | 16 ++++++++++ flake.nix | 4 +++ justfile | 1 + secrets/secrets.nix | 1 + secrets/shared/llm.age | 14 +++++++++ 7 files changed, 92 insertions(+) create mode 100644 apps/nogui/llm.nix create mode 100644 secrets/shared/llm.age diff --git a/apps/nogui/default.nix b/apps/nogui/default.nix index cfabab8..1d4c04f 100644 --- a/apps/nogui/default.nix +++ b/apps/nogui/default.nix @@ -14,6 +14,7 @@ ./jj.nix ./jq.nix ./lazygit.nix + ./llm.nix ./nushell.nix ./oh-my-posh.nix ./senpai.nix diff --git a/apps/nogui/llm.nix b/apps/nogui/llm.nix new file mode 100644 index 0000000..07a6275 --- /dev/null +++ b/apps/nogui/llm.nix @@ -0,0 +1,55 @@ +{ pkgs, lib, ... }: +let + claude = + let + inherit (pkgs) fetchFromGitHub nix-update-script python3Packages; + inherit (python3Packages) + buildPythonPackage + setuptools + anthropic + llm + pytestCheckHook + pytest + pytest-recording + ; + in + buildPythonPackage rec { + pname = "llm-claude-3"; + version = "0.4"; + pyproject = true; + + src = fetchFromGitHub { + owner = "simonw"; + repo = "llm-claude-3"; + rev = "refs/tags/${version}"; + hash = "sha256-5qF5BK319PNzB4XsLdYvtyq/SxBDdHJ9IoKWEnvNRp4="; + }; + + build-system = [ setuptools ]; + buildInputs = [ llm ]; + dependencies = [ anthropic ]; + optional-dependencies = { + test = [ + pytest + pytest-recording + ]; + }; + + # Test suite requires network access to talk to Claude (duh). + nativeCheckInputs = [ pytestCheckHook ]; + doCheck = false; + pythonImportsCheck = [ "llm_claude_3" ]; + + passthru.updateScript = nix-update-script { }; + + meta = { + description = "LLM plugin for interacting with the Claude 3 family of models"; + homepage = "https://github.com/simonw/llm-claude-3"; + license = lib.licenses.asl20; + maintainers = with lib.maintainers; [ jkachmar ]; + }; + }; +in +{ + home.packages = [ (pkgs.llm.withPlugins [ claude ]) ]; +} diff --git a/apps/nogui/nushell/jolheiser.nu b/apps/nogui/nushell/jolheiser.nu index ee508fd..c9ff053 100644 --- a/apps/nogui/nushell/jolheiser.nu +++ b/apps/nogui/nushell/jolheiser.nu @@ -69,6 +69,22 @@ def gomodsri [] { echo 'nixpkgs.lib.fileContents ./go.mod.sri' } +def q [ + --debug # Debug mode, only echo query + ...args: string + ] { + let input = $in + mut query = $args | str join ' ' + if ($input | is-not-empty) { + $query = $"($query)\n\n($input)" + } + if ($debug) { + print $query + return + } + ^llm -s "Answer in as few words as possible. Use a brief style with short replies." -m claude-3.5-sonnet $"($query)" +} + # Run a command if known, otherwise run it with nix def , [ cmd: string # The command to run diff --git a/flake.nix b/flake.nix index ffd1641..c5edb42 100644 --- a/flake.nix +++ b/flake.nix @@ -186,6 +186,10 @@ file = ./secrets/shared/cachix.age; path = "/home/${username}/.config/cachix/cachix.dhall"; }; + llm = { + file = ./secrets/shared/llm.age; + path = "/home/${username}/.config/io.datasette.llm/keys.json"; + }; }; }; } diff --git a/justfile b/justfile index bd211f5..24bc254 100644 --- a/justfile +++ b/justfile @@ -12,6 +12,7 @@ switch *args: @just rebuild switch {{args}} hm: + @git add . @home-manager switch --flake . |& nix run nixpkgs#nix-output-monitor @systemctl --user start agenix.service diff --git a/secrets/secrets.nix b/secrets/secrets.nix index 58cd839..a2c3d34 100644 --- a/secrets/secrets.nix +++ b/secrets/secrets.nix @@ -20,6 +20,7 @@ in "shared/irc-pw.age".publicKeys = all; "shared/gist-pw.age".publicKeys = all; "shared/git-send-email.age".publicKeys = all; + "shared/llm.age".publicKeys = all; "personal/restic-env.age".publicKeys = [ jolheiser dragonwell diff --git a/secrets/shared/llm.age b/secrets/shared/llm.age new file mode 100644 index 0000000..6ca7f7d --- /dev/null +++ b/secrets/shared/llm.age @@ -0,0 +1,14 @@ +age-encryption.org/v1 +-> ssh-ed25519 E8j6/g p21/KPTNTZrRXQUvPtlkHZuiFyNGkJWxF/yfhn643gk +jnN29WaHvFjMIjzVe0hkOEcolovBwjcgc2RfDlgY97E +-> ssh-ed25519 xUMv2w Dvet20WUzCfQeoKrdmCb12ouUNlePgFDdKEUZP82PmA +Prj/pjjj+oDqwKq+Q59aSj80YqICcgvulCBVy8CYx5Q +-> ssh-ed25519 ph+d2g vvjz0afKneI+lpiGGm8JuDTlJDlmoUygm/roW1vQphw +imYWGoW9O4MTbwTqXC5+5bH9sBf6qgkEJ8tNDtx6B0E +-> ssh-ed25519 Cuo7gw zUGiEgCtGzwZOVUl5T3jMBdwYzf657jMAbfJ/G1uGX8 +jeoWPVh4+AVyGqdZlG1Do3XHT/bwojZsT14I8E3aF1s +-> ssh-ed25519 f31uNA KmRTboipwSZZyDV1d69x4Apn0IjeboMxhmFVJBm72XI +Rgk4ranEXY0mcyBX7nWnW31yKZ8J3hrfzMV5NeQ3tLk +--- RW/X1U5gc3VSKroi8ZAIHZt4tvNShY/viK2TE2WSY4Q +|ut֐ Pf^