-
Notifications
You must be signed in to change notification settings - Fork 376
add claude support #1080
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
add claude support #1080
Changes from 1 commit
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -21,6 +21,7 @@ class LazyLLMOnlineChatModuleBase(LazyLLMOnlineBase, LLMBase): | |||||
| VLM_MODEL_PREFIX = [] | ||||||
| NO_PROXY = True | ||||||
| __lazyllm_registry_key__ = LLMType.CHAT | ||||||
| __format__ = 'openai' | ||||||
|
|
||||||
| def __init__(self, api_key: Union[str, List[str]], base_url: str, model_name: str, | ||||||
| stream: Union[bool, Dict[str, str]], return_trace: bool = False, skip_auth: bool = False, | ||||||
|
|
@@ -70,6 +71,9 @@ def _get_models_list(self): | |||||
| def _convert_msg_format(self, msg: Dict[str, Any]): | ||||||
| return msg | ||||||
|
|
||||||
| def _prepare_request_data(self, data: Dict[str, Any]) -> Dict[str, Any]: | ||||||
| return data | ||||||
|
|
||||||
| def _str_to_json(self, msg: str, stream_output: bool): | ||||||
| if isinstance(msg, bytes): | ||||||
| pattern = re.compile(r'^data:\s*') | ||||||
|
|
@@ -131,7 +135,7 @@ def forward(self, __input: Union[Dict, str] = None, *, llm_chat_history: List[Li | |||||
| runtime_url = self._get_chat_url(url) if url else self._chat_url | ||||||
| runtime_model = model or self._model_name | ||||||
|
|
||||||
| params = {'input': __input, 'history': llm_chat_history, 'return_dict': True} | ||||||
| params = {'input': __input, 'history': llm_chat_history, 'format': self.__format__} | ||||||
|
||||||
| params = {'input': __input, 'history': llm_chat_history, 'format': self.__format__} | |
| params = {'input': __input, 'history': llm_chat_history, 'format': self._format} |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,132 @@ | ||||||
| import json | ||||||
| import re | ||||||
| import requests | ||||||
| from typing import Any, Dict, List, Optional, Union | ||||||
| from urllib.parse import urljoin | ||||||
| from ..base import OnlineChatModuleBase | ||||||
|
|
||||||
|
|
||||||
| class ClaudeChat(OnlineChatModuleBase): | ||||||
| # Anthropic native Messages API (/v1/messages). | ||||||
| # Differs from OpenAI: x-api-key header, system as top-level field, | ||||||
| # max_tokens required, and SSE event types for streaming. | ||||||
|
|
||||||
| _ANTHROPIC_VERSION = '2023-06-01' | ||||||
| _DEFAULT_MAX_TOKENS = 4096 | ||||||
| __format__ = 'anthropic' | ||||||
|
||||||
|
|
||||||
| def __init__(self, base_url: Optional[str] = None, model: Optional[str] = None, | ||||||
| api_key: str = None, stream: bool = True, return_trace: bool = False, **kwargs): | ||||||
| base_url = base_url or 'https://api.anthropic.com/v1/' | ||||||
| model = model or 'claude-opus-4-5' | ||||||
|
||||||
| model = model or 'claude-opus-4-5' | |
| model = model or 'claude-3-5-sonnet-20241022' |
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.
Anthropic's API does not provide a /v1/models endpoint. This validation logic will consistently return a 404 error, causing API key validation to fail even for valid keys. Consider validating the key by making a minimal request to the /v1/messages endpoint (e.g., with max_tokens=1) or simply verifying that the key is provided in the configuration.
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.
Using
__format__as a class attribute is problematic because double underscores trigger Python's name mangling. In theforwardmethod (line 138),self.__format__will always resolve to_LazyLLMOnlineChatModuleBase__format__(i.e., 'openai'), even if a subclass likeClaudeChatdefines its own__format__. Additionally,__format__is a special Python method name. It should be renamed to something like_formatormessage_formatto allow proper polymorphism.