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 json |
| #2 | import os |
| #3 | from typing import Dict, List, Optional |
| #4 | |
| #5 | try: |
| #6 | from together import Together |
| #7 | except ImportError: |
| #8 | raise ImportError("The 'together' library is required. Please install it using 'pip install together'.") |
| #9 | |
| #10 | from mem0.configs.llms.base import BaseLlmConfig |
| #11 | from mem0.llms.base import LLMBase |
| #12 | from mem0.memory.utils import extract_json |
| #13 | |
| #14 | |
| #15 | class TogetherLLM(LLMBase): |
| #16 | def __init__(self, config: Optional[BaseLlmConfig] = None): |
| #17 | super().__init__(config) |
| #18 | |
| #19 | if not self.config.model: |
| #20 | self.config.model = "mistralai/Mixtral-8x7B-Instruct-v0.1" |
| #21 | |
| #22 | api_key = self.config.api_key or os.getenv("TOGETHER_API_KEY") |
| #23 | self.client = Together(api_key=api_key) |
| #24 | |
| #25 | def _parse_response(self, response, tools): |
| #26 | """ |
| #27 | Process the response based on whether tools are used or not. |
| #28 | |
| #29 | Args: |
| #30 | response: The raw response from API. |
| #31 | tools: The list of tools provided in the request. |
| #32 | |
| #33 | Returns: |
| #34 | str or dict: The processed response. |
| #35 | """ |
| #36 | if tools: |
| #37 | processed_response = { |
| #38 | "content": response.choices[0].message.content, |
| #39 | "tool_calls": [], |
| #40 | } |
| #41 | |
| #42 | if response.choices[0].message.tool_calls: |
| #43 | for tool_call in response.choices[0].message.tool_calls: |
| #44 | processed_response["tool_calls"].append( |
| #45 | { |
| #46 | "name": tool_call.function.name, |
| #47 | "arguments": json.loads(extract_json(tool_call.function.arguments)), |
| #48 | } |
| #49 | ) |
| #50 | |
| #51 | return processed_response |
| #52 | else: |
| #53 | return response.choices[0].message.content |
| #54 | |
| #55 | def generate_response( |
| #56 | self, |
| #57 | messages: List[Dict[str, str]], |
| #58 | response_format=None, |
| #59 | tools: Optional[List[Dict]] = None, |
| #60 | tool_choice: str = "auto", |
| #61 | ): |
| #62 | """ |
| #63 | Generate a response based on the given messages using TogetherAI. |
| #64 | |
| #65 | Args: |
| #66 | messages (list): List of message dicts containing 'role' and 'content'. |
| #67 | response_format (str or object, optional): Format of the response. Defaults to "text". |
| #68 | tools (list, optional): List of tools that the model can call. Defaults to None. |
| #69 | tool_choice (str, optional): Tool choice method. Defaults to "auto". |
| #70 | |
| #71 | Returns: |
| #72 | str: The generated response. |
| #73 | """ |
| #74 | params = { |
| #75 | "model": self.config.model, |
| #76 | "messages": messages, |
| #77 | "temperature": self.config.temperature, |
| #78 | "max_tokens": self.config.max_tokens, |
| #79 | "top_p": self.config.top_p, |
| #80 | } |
| #81 | if response_format: |
| #82 | params["response_format"] = response_format |
| #83 | if tools: # TODO: Remove tools if no issues found with new memory addition logic |
| #84 | params["tools"] = tools |
| #85 | params["tool_choice"] = tool_choice |
| #86 | |
| #87 | response = self.client.chat.completions.create(**params) |
| #88 | return self._parse_response(response, tools) |
| #89 |