-
Couldn't load subscription status.
- Fork 21
Add Anthropic Claude provider #55
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,10 @@ | ||
| " Author: w0rp <[email protected]> | ||
| " Description: A script describing how to use Anthropic Claude with Neural | ||
|
|
||
| function! neural#source#anthropic#Get() abort | ||
| return { | ||
| \ 'name': 'anthropic', | ||
| \ 'script_language': 'python', | ||
| \ 'script': neural#GetScriptDir() . '/anthropic.py', | ||
| \} | ||
| endfunction |
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,159 @@ | ||||||||||||||
| """ | ||||||||||||||
| A Neural datasource for loading generated text via Anthropic Claude. | ||||||||||||||
| """ | ||||||||||||||
| import json | ||||||||||||||
| import platform | ||||||||||||||
| import ssl | ||||||||||||||
| import sys | ||||||||||||||
| import urllib.error | ||||||||||||||
| import urllib.request | ||||||||||||||
| from typing import Any, Dict, List, Optional, Union | ||||||||||||||
|
|
||||||||||||||
| API_ENDPOINT = 'https://api.anthropic.com/v1/complete' | ||||||||||||||
|
|
||||||||||||||
| ANTHROPIC_DATA_HEADER = 'data: ' | ||||||||||||||
| ANTHROPIC_DONE = '[DONE]' | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| class Config: | ||||||||||||||
| """The sanitised configuration.""" | ||||||||||||||
|
|
||||||||||||||
| def __init__( | ||||||||||||||
| self, | ||||||||||||||
| api_key: str, | ||||||||||||||
| model: str, | ||||||||||||||
| temperature: float, | ||||||||||||||
| top_p: float, | ||||||||||||||
| max_tokens: int, | ||||||||||||||
| ) -> None: | ||||||||||||||
| self.api_key = api_key | ||||||||||||||
| self.model = model | ||||||||||||||
| self.temperature = temperature | ||||||||||||||
| self.top_p = top_p | ||||||||||||||
| self.max_tokens = max_tokens | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def get_claude_completion( | ||||||||||||||
| config: Config, | ||||||||||||||
| prompt: Union[str, List[Dict[str, str]]], | ||||||||||||||
| ) -> None: | ||||||||||||||
| headers = { | ||||||||||||||
| "Content-Type": "application/json", | ||||||||||||||
| "x-api-key": config.api_key, | ||||||||||||||
| "anthropic-version": "2023-06-01", | ||||||||||||||
| } | ||||||||||||||
| data = { | ||||||||||||||
| "model": config.model, | ||||||||||||||
| "prompt": ( | ||||||||||||||
| prompt | ||||||||||||||
| if isinstance(prompt, str) | ||||||||||||||
| else ''.join([msg.get("content", "") for msg in prompt]) | ||||||||||||||
| ), | ||||||||||||||
| "temperature": config.temperature, | ||||||||||||||
| "top_p": config.top_p, | ||||||||||||||
| "max_tokens_to_sample": config.max_tokens, | ||||||||||||||
| "stream": True, | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| req = urllib.request.Request( | ||||||||||||||
| API_ENDPOINT, | ||||||||||||||
| data=json.dumps(data).encode("utf-8"), | ||||||||||||||
| headers=headers, | ||||||||||||||
| method="POST", | ||||||||||||||
| ) | ||||||||||||||
| role: Optional[str] = None | ||||||||||||||
|
|
||||||||||||||
| context = ( | ||||||||||||||
| ssl._create_unverified_context() # type: ignore | ||||||||||||||
|
||||||||||||||
| ssl._create_unverified_context() # type: ignore | |
| ssl.create_default_context() |
Copilot
AI
Jun 17, 2025
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.
Catching all Exceptions is too broad. Narrow the exception to json.JSONDecodeError or KeyError to avoid masking unexpected errors.
| except Exception: | |
| pass | |
| except json.JSONDecodeError: | |
| pass | |
| except KeyError: | |
| pass |
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.
The variable
roleis declared but never used inget_claude_completion. Consider removing it to reduce dead code.