repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | import os |
| #2 | from typing import Dict, List, Optional, Union |
| #3 | |
| #4 | try: |
| #5 | import anthropic |
| #6 | except ImportError: |
| #7 | raise ImportError("The 'anthropic' library is required. Please install it using 'pip install anthropic'.") |
| #8 | |
| #9 | from mem0.configs.llms.anthropic import AnthropicConfig |
| #10 | from mem0.configs.llms.base import BaseLlmConfig |
| #11 | from mem0.llms.base import LLMBase |
| #12 | |
| #13 | |
| #14 | class AnthropicLLM(LLMBase): |
| #15 | def __init__(self, config: Optional[Union[BaseLlmConfig, AnthropicConfig, Dict]] = None): |
| #16 | # Convert to AnthropicConfig if needed |
| #17 | if config is None: |
| #18 | config = AnthropicConfig() |
| #19 | elif isinstance(config, dict): |
| #20 | config = AnthropicConfig(**config) |
| #21 | elif isinstance(config, BaseLlmConfig) and not isinstance(config, AnthropicConfig): |
| #22 | # Convert BaseLlmConfig to AnthropicConfig |
| #23 | config = AnthropicConfig( |
| #24 | model=config.model, |
| #25 | temperature=config.temperature, |
| #26 | api_key=config.api_key, |
| #27 | max_tokens=config.max_tokens, |
| #28 | top_p=config.top_p, |
| #29 | top_k=config.top_k, |
| #30 | enable_vision=config.enable_vision, |
| #31 | vision_details=config.vision_details, |
| #32 | http_client_proxies=config.http_client, |
| #33 | ) |
| #34 | |
| #35 | super().__init__(config) |
| #36 | |
| #37 | if not self.config.model: |
| #38 | self.config.model = "claude-3-5-sonnet-20240620" |
| #39 | |
| #40 | api_key = self.config.api_key or os.getenv("ANTHROPIC_API_KEY") |
| #41 | self.client = anthropic.Anthropic(api_key=api_key) |
| #42 | |
| #43 | def generate_response( |
| #44 | self, |
| #45 | messages: List[Dict[str, str]], |
| #46 | response_format=None, |
| #47 | tools: Optional[List[Dict]] = None, |
| #48 | tool_choice: str = "auto", |
| #49 | **kwargs, |
| #50 | ): |
| #51 | """ |
| #52 | Generate a response based on the given messages using Anthropic. |
| #53 | |
| #54 | Args: |
| #55 | messages (list): List of message dicts containing 'role' and 'content'. |
| #56 | response_format (str or object, optional): Format of the response. Defaults to "text". |
| #57 | tools (list, optional): List of tools that the model can call. Defaults to None. |
| #58 | tool_choice (str, optional): Tool choice method. Defaults to "auto". |
| #59 | **kwargs: Additional Anthropic-specific parameters. |
| #60 | |
| #61 | Returns: |
| #62 | str: The generated response. |
| #63 | """ |
| #64 | # Separate system message from other messages |
| #65 | system_message = "" |
| #66 | filtered_messages = [] |
| #67 | for message in messages: |
| #68 | if message["role"] == "system": |
| #69 | system_message = message["content"] |
| #70 | else: |
| #71 | filtered_messages.append(message) |
| #72 | |
| #73 | params = self._get_supported_params(messages=messages, **kwargs) |
| #74 | params.update( |
| #75 | { |
| #76 | "model": self.config.model, |
| #77 | "messages": filtered_messages, |
| #78 | "system": system_message, |
| #79 | } |
| #80 | ) |
| #81 | |
| #82 | if tools: # TODO: Remove tools if no issues found with new memory addition logic |
| #83 | params["tools"] = tools |
| #84 | params["tool_choice"] = tool_choice |
| #85 | |
| #86 | response = self.client.messages.create(**params) |
| #87 | return response.content[0].text |
| #88 |