4
4
5
5
from pathlib import Path
6
6
7
+ import langchain .tools
8
+
7
9
from lwe .core .config import Config
8
10
from lwe .core .logger import Logger
9
11
import lwe .core .util as util
10
12
13
+ LANGCHAIN_TOOL_PREFIX = "Langchain-"
14
+
11
15
class FunctionManager ():
12
16
"""
13
17
Manage functions.
@@ -58,6 +62,45 @@ def load_function(self, function_name):
58
62
return True , function_filepath , message
59
63
return False , None , f"Function { function_name } not found"
60
64
65
+ def is_langchain_tool (self , function_name ):
66
+ self .log .debug (f"Checking for Langchain tool: { function_name } " )
67
+ return function_name .lower ().startswith (LANGCHAIN_TOOL_PREFIX .lower ())
68
+
69
+ def get_langchain_tool (self , function_name ):
70
+ self .log .debug (f"Loading Langchain tool: { function_name } " )
71
+ tool_name = util .remove_prefix (function_name , LANGCHAIN_TOOL_PREFIX )
72
+ tool = getattr (langchain .tools , tool_name )
73
+ try :
74
+ tool_instance = tool ()
75
+ return tool_instance
76
+ except Exception as e :
77
+ self .log .warning (f"Could not load Langchaine tool: { function_name } : { str (e )} " )
78
+ return None
79
+
80
+ def get_langchain_tool_spec (self , function_name ):
81
+ self .log .debug (f"Loading tool spec for Langchain tool: { function_name } " )
82
+ tool_instance = self .get_langchain_tool (function_name )
83
+ if not tool_instance :
84
+ raise RuntimeError (f"Langchain tool { function_name } not found" )
85
+ spec = langchain .tools .format_tool_to_openai_function (tool_instance )
86
+ spec ['name' ] = function_name
87
+ return spec
88
+
89
+ def run_langchain_tool (self , function_name , input_data ):
90
+ self .log .debug (f"Running langchaing tool: { function_name } with data: { input_data } " )
91
+ tool_instance = self .get_langchain_tool (function_name )
92
+ if not tool_instance :
93
+ raise RuntimeError (f"Langchain tool { function_name } not found" )
94
+ try :
95
+ result = tool_instance .run (input_data )
96
+ except Exception as e :
97
+ message = f"Error: Exception occurred while running langchain tool { function_name } : { str (e )} "
98
+ self .log .error (message )
99
+ return False , None , message
100
+ message = f"Langchain tool { function_name } executed successfully, output data: { result } "
101
+ self .log .info (message )
102
+ return True , result , message
103
+
61
104
def load_functions (self ):
62
105
self .log .debug ("Loading functions from dirs: %s" % ", " .join (self .all_function_dirs ))
63
106
self .functions = {}
@@ -99,6 +142,8 @@ def setup_function_instance(self, function_name, function_path):
99
142
100
143
def get_function_config (self , function_name ):
101
144
self .log .debug (f"Getting config for function: { function_name } " )
145
+ if self .is_langchain_tool (function_name ):
146
+ return self .get_langchain_tool_spec (function_name )
102
147
try :
103
148
_success , function_path , user_message = self .load_function (function_name )
104
149
function_instance = self .setup_function_instance (function_name , function_path )
@@ -111,6 +156,8 @@ def get_function_config(self, function_name):
111
156
def run_function (self , function_name , input_data ):
112
157
if isinstance (input_data , str ):
113
158
input_data = json .loads (input_data )
159
+ if self .is_langchain_tool (function_name ):
160
+ return self .run_langchain_tool (function_name , input_data )
114
161
self .log .debug (f"Running function: { function_name } with data: { input_data } " )
115
162
success , function_path , user_message = self .load_function (function_name )
116
163
if not success :
0 commit comments