-
Notifications
You must be signed in to change notification settings - Fork 64
feat: support langchain and langgraph community #416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. |
13 changes: 13 additions & 0 deletions
13
veadk/community/langchain_ai/checkpoint/memory/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| # Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from langchain.agents import AgentState | ||
| from langchain.agents.middleware import after_agent | ||
| from langchain_core.messages.ai import AIMessage | ||
| from langchain_core.messages.human import HumanMessage | ||
| from langgraph.runtime import Runtime | ||
|
|
||
| from veadk.community.langchain_ai.store.memory.viking_memory import ( | ||
| VikingMemoryStore, | ||
| ) | ||
| from veadk.utils.logger import get_logger | ||
|
|
||
| logger = get_logger(__name__) | ||
|
|
||
|
|
||
| @after_agent | ||
| def save_session(state: AgentState, runtime: Runtime) -> None: | ||
| """Save the session to the memory store.""" | ||
| store: VikingMemoryStore | None = runtime.store | ||
| if not store: | ||
| return | ||
|
|
||
| app_name = store.index | ||
| user_id = runtime.context.user_id | ||
| session_id = runtime.context.session_id | ||
|
|
||
| messages = state.get("messages", []) | ||
| logger.debug( | ||
| f"Save session {session_id} for user {user_id} with {len(messages)} messages. messages={messages}" | ||
| ) | ||
|
|
||
| events = {} | ||
| for message in messages: | ||
| print(type(message)) | ||
| if isinstance(message, HumanMessage): | ||
| event = {"role": "user", "parts": [{"text": message.content}]} | ||
|
|
||
| elif isinstance(message, AIMessage): | ||
| event = {"role": "assistant", "parts": [{"text": message.content}]} | ||
| else: | ||
| ... | ||
|
|
||
| events[message.id] = event | ||
|
|
||
| store.put(namespace=(app_name, user_id), key=session_id, value=events) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| # Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from langchain_core.utils.utils import ( | ||
| from_env, | ||
| secret_from_env, | ||
| ) | ||
| from langchain_openai import ChatOpenAI | ||
|
|
||
|
|
||
| class ArkChatModel(ChatOpenAI): | ||
| def __init__(self, model: str, **kwargs): | ||
| super().__init__( | ||
| model=model, | ||
| api_key=secret_from_env("MODEL_AGENT_API_KEY")(), | ||
| base_url=from_env( | ||
| "MODEL_AGENT_API_BASE_URL", | ||
| default="https://ark.cn-beijing.volces.com/api/v3", | ||
| )(), | ||
| **kwargs, | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. |
102 changes: 102 additions & 0 deletions
102
veadk/community/langchain_ai/store/memory/viking_memory.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| # Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| from collections.abc import Iterable | ||
|
|
||
| from langgraph.store.base import ( | ||
| BaseStore, | ||
| GetOp, | ||
| ListNamespacesOp, | ||
| Op, | ||
| PutOp, | ||
| Result, | ||
| SearchOp, | ||
| ) | ||
|
|
||
| from veadk.memory.long_term_memory_backends.vikingdb_memory_backend import ( | ||
| VikingDBLTMBackend, | ||
| ) | ||
|
|
||
|
|
||
| # mem0 | ||
| # viking db | ||
| class VikingMemoryStore(BaseStore): | ||
| def __init__(self, index: str): | ||
| # index = (index, user_id) | ||
| # key = session_id | ||
| self.index = index | ||
| self._backend = VikingDBLTMBackend(index=index) | ||
|
|
||
| def batch(self, ops: Iterable[Op]) -> list[Result]: | ||
| # The batch/abatch methods are treated as internal. | ||
| # Users should access via put/search/get/list_namespaces/etc. | ||
| results = [] | ||
| for op in ops: | ||
| if isinstance(op, PutOp): | ||
| self._apply_put_op(op) | ||
| elif isinstance(op, GetOp): | ||
| self._apply_get_op(op) | ||
| elif isinstance(op, SearchOp): | ||
| results.extend(self._apply_search_op(op)) | ||
| # elif isinstance(op, ListNamespacesOp): | ||
| # self._apply_list_namespaces_op(op) | ||
| else: | ||
| raise ValueError(f"Unknown op type: {type(op)}") | ||
|
|
||
| return results | ||
|
|
||
| def abatch( | ||
| self, ops: Iterable[GetOp | SearchOp | PutOp | ListNamespacesOp] | ||
| ) -> list[Result]: ... | ||
|
|
||
| def _apply_put_op(self, op: PutOp) -> None: | ||
| index, user_id = op.namespace | ||
| session_id = op.key | ||
|
|
||
| assert index == self._backend.index, ( | ||
| "index must be the same as the backend index" | ||
| ) | ||
|
|
||
| value = op.value | ||
|
|
||
| event_strings = [] | ||
|
|
||
| for _, event in value.items(): | ||
| event_strings.append(json.dumps(event)) | ||
|
|
||
| if self._backend.save_memory( | ||
| user_id=user_id, | ||
| session_id=session_id, | ||
| event_strings=event_strings, | ||
| ): | ||
| return None | ||
|
|
||
| def _apply_get_op(self, op: GetOp): | ||
| return ["Not implemented"] | ||
|
|
||
| def _apply_search_op(self, op: SearchOp): | ||
| index, user_id = op.namespace_prefix | ||
| assert index == self._backend.index, ( | ||
| "index must be the same as the backend index" | ||
| ) | ||
|
|
||
| query = op.query | ||
| if not query: | ||
| return [] | ||
|
|
||
| value = self._backend.search_memory(user_id=user_id, query=query, top_k=1) | ||
| return value |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| # Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from langchain.tools import ToolRuntime, tool | ||
|
|
||
| from veadk.knowledgebase import KnowledgeBase | ||
| from veadk.utils.logger import get_logger | ||
|
|
||
| logger = get_logger(__name__) | ||
|
|
||
|
|
||
| @tool | ||
| def load_knowledgebase(query: str, runtime: ToolRuntime) -> list[str]: | ||
| """Load knowledge base for the current user. | ||
|
|
||
| Args: | ||
| query: The query to search for in the knowledge base. | ||
| """ | ||
| knowledgeabse: KnowledgeBase = runtime.context.knowledgebase # type: ignore | ||
|
|
||
| results = knowledgeabse.search(query) | ||
|
|
||
| return [result.content for result in results] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| # Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
|
|
||
| from langchain.tools import ToolRuntime, tool | ||
|
|
||
| from veadk.community.langchain_ai.store.memory.viking_memory import ( | ||
| VikingMemoryStore, | ||
| ) | ||
| from veadk.utils.logger import get_logger | ||
|
|
||
| logger = get_logger(__name__) | ||
|
|
||
|
|
||
| @tool | ||
| def load_memory(query: str, runtime: ToolRuntime) -> list[str]: | ||
| """Load memories for the current user across all history sessions. | ||
|
|
||
| Args: | ||
| query: The query to search for in the memory. | ||
| """ | ||
| store: VikingMemoryStore | None = runtime.store # type: ignore | ||
| if not store: | ||
| return ["Long-term memory store is not initialized."] | ||
|
|
||
| app_name = store.index | ||
| user_id = runtime.context.user_id | ||
|
|
||
| logger.info(f"Load memory for user {user_id} with query {query}") | ||
| response = store.search((app_name, user_id), query=query) | ||
|
|
||
| return response |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MODEL_AGENT_API_BASE