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 logging |
| #2 | import os |
| #3 | from typing import Any, Optional |
| #4 | |
| #5 | try: |
| #6 | from langchain_anthropic import ChatAnthropic |
| #7 | except ImportError: |
| #8 | raise ImportError("Please install the langchain-anthropic package by running `pip install langchain-anthropic`.") |
| #9 | |
| #10 | from embedchain.config import BaseLlmConfig |
| #11 | from embedchain.helpers.json_serializable import register_deserializable |
| #12 | from embedchain.llm.base import BaseLlm |
| #13 | |
| #14 | logger = logging.getLogger(__name__) |
| #15 | |
| #16 | |
| #17 | @register_deserializable |
| #18 | class AnthropicLlm(BaseLlm): |
| #19 | def __init__(self, config: Optional[BaseLlmConfig] = None): |
| #20 | super().__init__(config=config) |
| #21 | if not self.config.api_key and "ANTHROPIC_API_KEY" not in os.environ: |
| #22 | raise ValueError("Please set the ANTHROPIC_API_KEY environment variable or pass it in the config.") |
| #23 | |
| #24 | def get_llm_model_answer(self, prompt) -> tuple[str, Optional[dict[str, Any]]]: |
| #25 | if self.config.token_usage: |
| #26 | response, token_info = self._get_answer(prompt, self.config) |
| #27 | model_name = "anthropic/" + self.config.model |
| #28 | if model_name not in self.config.model_pricing_map: |
| #29 | raise ValueError( |
| #30 | f"Model {model_name} not found in `model_prices_and_context_window.json`. \ |
| #31 | You can disable token usage by setting `token_usage` to False." |
| #32 | ) |
| #33 | total_cost = ( |
| #34 | self.config.model_pricing_map[model_name]["input_cost_per_token"] * token_info["input_tokens"] |
| #35 | ) + self.config.model_pricing_map[model_name]["output_cost_per_token"] * token_info["output_tokens"] |
| #36 | response_token_info = { |
| #37 | "prompt_tokens": token_info["input_tokens"], |
| #38 | "completion_tokens": token_info["output_tokens"], |
| #39 | "total_tokens": token_info["input_tokens"] + token_info["output_tokens"], |
| #40 | "total_cost": round(total_cost, 10), |
| #41 | "cost_currency": "USD", |
| #42 | } |
| #43 | return response, response_token_info |
| #44 | return self._get_answer(prompt, self.config) |
| #45 | |
| #46 | @staticmethod |
| #47 | def _get_answer(prompt: str, config: BaseLlmConfig) -> str: |
| #48 | api_key = config.api_key or os.getenv("ANTHROPIC_API_KEY") |
| #49 | chat = ChatAnthropic(anthropic_api_key=api_key, temperature=config.temperature, model_name=config.model) |
| #50 | |
| #51 | if config.max_tokens and config.max_tokens != 1000: |
| #52 | logger.warning("Config option `max_tokens` is not supported by this model.") |
| #53 | |
| #54 | messages = BaseLlm._get_messages(prompt, system_prompt=config.system_prompt) |
| #55 | |
| #56 | chat_response = chat.invoke(messages) |
| #57 | if config.token_usage: |
| #58 | return chat_response.content, chat_response.response_metadata["token_usage"] |
| #59 | return chat_response.content |
| #60 |