Skip to content

Commit a9d664f

Browse files
author
P. Scott DeVos
committedJan 4, 2025
Retrieve list of Claude models dynamically
- Use client.models.list() to retrieve model list. - Update requirements.txt to require anthropic 0.42 or higher.
1 parent cc17db6 commit a9d664f

File tree

2 files changed

+9
-14
lines changed

2 files changed

+9
-14
lines changed
 

‎requirements.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
anthropic
2-
pynvim
1+
anthropic>=0.42
2+
pynvim

‎rplugin/python3/claude_plugin.py

+7-12
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import pynvim
3030
from httpx import Timeout
3131

32+
ANTHROPIC_API_KEY = os.getenv('ANTHROPIC_API_KEY')
33+
3234

3335
# Pattern to match :b<buffer_number> with at least one surrounding whitespace
3436
BUFFER_PATTERN = re.compile(r'\s+:b(\d+)\s+')
@@ -184,18 +186,6 @@ def _get_filename_from_response(response: anthropic.types.Message) -> str:
184186
# Escape spaces and special characters in filename
185187
return shlex.quote(filename)
186188

187-
@staticmethod
188-
def get_claude_models() -> List[str]:
189-
"""Get a list of available Anthropic models."""
190-
models = [
191-
"claude-3-5-sonnet-20240620",
192-
"claude-3-5-haiku-20241022",
193-
"claude-3-opus-20240229",
194-
"claude-3-sonnet-20240229",
195-
"claude-3-haiku-20240229",
196-
]
197-
return models
198-
199189
def _count_tokens(self, messages: List[dict]) -> Tuple[int, int]:
200190
if not messages:
201191
return 0, []
@@ -491,6 +481,11 @@ def replace_buffer(match):
491481
self.nvim.err_write(
492482
f"Error generating response:\n{format_exc()}\n")
493483

484+
def get_claude_models(self) -> List[str]:
485+
"""Get a list of available Anthropic models."""
486+
models = self.claude_client.models.list()
487+
return [model.id for model in models.data]
488+
494489
@pynvim.command('Cl', nargs='0', sync=False)
495490
def claude_command(self, args: List[str]) -> None:
496491
return self.do_completion(self.claude_client.messages.create, args)

0 commit comments

Comments
 (0)
Please sign in to comment.