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 importlib |
| #2 | import os |
| #3 | from typing import Any, Optional |
| #4 | |
| #5 | try: |
| #6 | from langchain_together import ChatTogether |
| #7 | except ImportError: |
| #8 | raise ImportError( |
| #9 | "Please install the langchain_together package by running `pip install langchain_together==0.1.3`." |
| #10 | ) |
| #11 | |
| #12 | from embedchain.config import BaseLlmConfig |
| #13 | from embedchain.helpers.json_serializable import register_deserializable |
| #14 | from embedchain.llm.base import BaseLlm |
| #15 | |
| #16 | |
| #17 | @register_deserializable |
| #18 | class TogetherLlm(BaseLlm): |
| #19 | def __init__(self, config: Optional[BaseLlmConfig] = None): |
| #20 | try: |
| #21 | importlib.import_module("together") |
| #22 | except ModuleNotFoundError: |
| #23 | raise ModuleNotFoundError( |
| #24 | "The required dependencies for Together are not installed." |
| #25 | 'Please install with `pip install --upgrade "embedchain[together]"`' |
| #26 | ) from None |
| #27 | |
| #28 | super().__init__(config=config) |
| #29 | if not self.config.api_key and "TOGETHER_API_KEY" not in os.environ: |
| #30 | raise ValueError("Please set the TOGETHER_API_KEY environment variable or pass it in the config.") |
| #31 | |
| #32 | def get_llm_model_answer(self, prompt) -> tuple[str, Optional[dict[str, Any]]]: |
| #33 | if self.config.system_prompt: |
| #34 | raise ValueError("TogetherLlm does not support `system_prompt`") |
| #35 | |
| #36 | if self.config.token_usage: |
| #37 | response, token_info = self._get_answer(prompt, self.config) |
| #38 | model_name = "together/" + self.config.model |
| #39 | if model_name not in self.config.model_pricing_map: |
| #40 | raise ValueError( |
| #41 | f"Model {model_name} not found in `model_prices_and_context_window.json`. \ |
| #42 | You can disable token usage by setting `token_usage` to False." |
| #43 | ) |
| #44 | total_cost = ( |
| #45 | self.config.model_pricing_map[model_name]["input_cost_per_token"] * token_info["prompt_tokens"] |
| #46 | ) + self.config.model_pricing_map[model_name]["output_cost_per_token"] * token_info["completion_tokens"] |
| #47 | response_token_info = { |
| #48 | "prompt_tokens": token_info["prompt_tokens"], |
| #49 | "completion_tokens": token_info["completion_tokens"], |
| #50 | "total_tokens": token_info["prompt_tokens"] + token_info["completion_tokens"], |
| #51 | "total_cost": round(total_cost, 10), |
| #52 | "cost_currency": "USD", |
| #53 | } |
| #54 | return response, response_token_info |
| #55 | return self._get_answer(prompt, self.config) |
| #56 | |
| #57 | @staticmethod |
| #58 | def _get_answer(prompt: str, config: BaseLlmConfig) -> str: |
| #59 | api_key = config.api_key or os.environ["TOGETHER_API_KEY"] |
| #60 | kwargs = { |
| #61 | "model_name": config.model or "mixtral-8x7b-32768", |
| #62 | "temperature": config.temperature, |
| #63 | "max_tokens": config.max_tokens, |
| #64 | "together_api_key": api_key, |
| #65 | } |
| #66 | |
| #67 | chat = ChatTogether(**kwargs) |
| #68 | chat_response = chat.invoke(prompt) |
| #69 | if config.token_usage: |
| #70 | return chat_response.content, chat_response.response_metadata["token_usage"] |
| #71 | return chat_response.content |
| #72 |