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 | from typing import Dict, List, Optional |
| #2 | |
| #3 | from mem0.configs.llms.base import BaseLlmConfig |
| #4 | from mem0.llms.base import LLMBase |
| #5 | |
| #6 | try: |
| #7 | from langchain.chat_models.base import BaseChatModel |
| #8 | from langchain_core.messages import AIMessage |
| #9 | except ImportError: |
| #10 | raise ImportError("langchain is not installed. Please install it using `pip install langchain`") |
| #11 | |
| #12 | |
| #13 | class LangchainLLM(LLMBase): |
| #14 | def __init__(self, config: Optional[BaseLlmConfig] = None): |
| #15 | super().__init__(config) |
| #16 | |
| #17 | if self.config.model is None: |
| #18 | raise ValueError("`model` parameter is required") |
| #19 | |
| #20 | if not isinstance(self.config.model, BaseChatModel): |
| #21 | raise ValueError("`model` must be an instance of BaseChatModel") |
| #22 | |
| #23 | self.langchain_model = self.config.model |
| #24 | |
| #25 | def _parse_response(self, response: AIMessage, tools: Optional[List[Dict]]): |
| #26 | """ |
| #27 | Process the response based on whether tools are used or not. |
| #28 | |
| #29 | Args: |
| #30 | response: AI Message. |
| #31 | tools: The list of tools provided in the request. |
| #32 | |
| #33 | Returns: |
| #34 | str or dict: The processed response. |
| #35 | """ |
| #36 | if not tools: |
| #37 | return response.content |
| #38 | |
| #39 | processed_response = { |
| #40 | "content": response.content, |
| #41 | "tool_calls": [], |
| #42 | } |
| #43 | |
| #44 | for tool_call in response.tool_calls: |
| #45 | processed_response["tool_calls"].append( |
| #46 | { |
| #47 | "name": tool_call["name"], |
| #48 | "arguments": tool_call["args"], |
| #49 | } |
| #50 | ) |
| #51 | |
| #52 | return processed_response |
| #53 | |
| #54 | def generate_response( |
| #55 | self, |
| #56 | messages: List[Dict[str, str]], |
| #57 | response_format=None, |
| #58 | tools: Optional[List[Dict]] = None, |
| #59 | tool_choice: str = "auto", |
| #60 | ): |
| #61 | """ |
| #62 | Generate a response based on the given messages using langchain_community. |
| #63 | |
| #64 | Args: |
| #65 | messages (list): List of message dicts containing 'role' and 'content'. |
| #66 | response_format (str or object, optional): Format of the response. Not used in Langchain. |
| #67 | tools (list, optional): List of tools that the model can call. |
| #68 | tool_choice (str, optional): Tool choice method. |
| #69 | |
| #70 | Returns: |
| #71 | str: The generated response. |
| #72 | """ |
| #73 | # Convert the messages to LangChain's tuple format |
| #74 | langchain_messages = [] |
| #75 | for message in messages: |
| #76 | role = message["role"] |
| #77 | content = message["content"] |
| #78 | |
| #79 | if role == "system": |
| #80 | langchain_messages.append(("system", content)) |
| #81 | elif role == "user": |
| #82 | langchain_messages.append(("human", content)) |
| #83 | elif role == "assistant": |
| #84 | langchain_messages.append(("ai", content)) |
| #85 | |
| #86 | if not langchain_messages: |
| #87 | raise ValueError("No valid messages found in the messages list") |
| #88 | |
| #89 | langchain_model = self.langchain_model |
| #90 | if tools: |
| #91 | langchain_model = langchain_model.bind_tools(tools=tools, tool_choice=tool_choice) |
| #92 | |
| #93 | response: AIMessage = langchain_model.invoke(langchain_messages) |
| #94 | return self._parse_response(response, tools) |
| #95 |