Skip to content
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

[开发中]支持调用LangChain Community 中的Tool实现 #164

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions appbuilder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def check_version(self):
from .core.components.image_understand.component import ImageUnderstand
from .core.components.mix_card_ocr.component import MixCardOCR

from .core.components.langchain.component import LangChainToolWrapper

from appbuilder.core.message import Message
from appbuilder.core.agent import AgentRuntime
Expand Down Expand Up @@ -141,15 +142,16 @@ def check_version(self):
'Message',
'AnimalRecognition',
'DocCropEnhance',
QRcodeOCR,
TableOCR,
"QRcodeOCR",
"TableOCR",

'Embedding',

'Matching',

"PlantRecognition",
HandwriteOCR,
"HandwriteOCR",
"ImageUnderstand",
MixCardOCR,
"MixCardOCR",
"LangChainToolWrapper",
]
Empty file.
61 changes: 61 additions & 0 deletions appbuilder/core/components/langchain/component.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Copyright (c) 2023 Baidu, Inc. All Rights Reserved.
#
# 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.

r"""landmark recognize component."""
import base64

from appbuilder.core.component import Component
from appbuilder.core.component import ComponentArguments
from appbuilder.core.message import Message

from typing import Optional
from langchain_community.tools import BaseTool
from copy import deepcopy

class LangChainToolWrapper(Component):
def __init__(
self,
langchain_tool: None,
meta: Optional[ComponentArguments] = ComponentArguments(),
secret_key: Optional[str] = None,
gateway: str = "",
lazy_certification: bool = False,
):
self.langchain_tool = langchain_tool
tmp_meta = self._parse_langchain_schema()
super().__init__(meta=tmp_meta, secret_key=secret_key, gateway=gateway, lazy_certification=lazy_certification)


def run(self, message: Message) -> Message:
params = self._before_langchain_tool_run(message)
response = self.langchain_tool.run(params)
return self._after_langchain_tool_run(response)

def _parse_langchain_schema(self) -> None:
# convert langchain tool schema to component schema
component_argument = ComponentArguments(
tool_desc={
"description": self.langchain_tool.__doc__,
}
)
return component_argument

def _before_langchain_tool_run(self, message: Message):
# convert message to tool params
params = deepcopy(message.content)
return params

def _after_langchain_tool_run(self, response: str):
# convert tool response to message
return Message(content=response)
Empty file.