Update Vertex params with code type and add a my_conversation method to the MyPythonClass. Modify the instantiate_tool function to properly handle the CustomComponent node type. Add a dynamic field to the FrontendNode class.
This commit is contained in:
parent
13bb0280f5
commit
92b747b6da
6 changed files with 42 additions and 29 deletions
|
|
@ -110,7 +110,7 @@ class Vertex:
|
||||||
file_path = value.get("file_path")
|
file_path = value.get("file_path")
|
||||||
|
|
||||||
params[key] = file_path
|
params[key] = file_path
|
||||||
elif value.get("type") in ["str", "prompt"] and params.get(key) is None:
|
elif value.get("type") in ["code", "str", "prompt"] and params.get(key) is None:
|
||||||
params[key] = value.get("value")
|
params[key] = value.get("value")
|
||||||
# Add _type to params
|
# Add _type to params
|
||||||
self.params = params
|
self.params = params
|
||||||
|
|
|
||||||
|
|
@ -169,25 +169,22 @@ def get_function_custom(code):
|
||||||
class_name = "MyPythonClass"
|
class_name = "MyPythonClass"
|
||||||
|
|
||||||
code = """
|
code = """
|
||||||
|
from langchain.llms import OpenAI
|
||||||
from langchain.chains import ConversationChain
|
from langchain.chains import ConversationChain
|
||||||
|
from langchain.memory import ConversationBufferMemory
|
||||||
|
|
||||||
|
|
||||||
class MyPythonClass:
|
class MyPythonClass:
|
||||||
def __init__(self, title: str, author: str, year_published: int):
|
def my_conversation(self):
|
||||||
self.title = title
|
llm = OpenAI(temperature=0)
|
||||||
self.author = author
|
return ConversationChain(
|
||||||
self.year_published = year_published
|
llm=llm, verbose=True, memory=ConversationBufferMemory()
|
||||||
|
)
|
||||||
|
|
||||||
def get_details(self):
|
def build(self, name: str) -> ConversationChain:
|
||||||
return f"Title: {self.title}, Author: {self.author}, Year Published: {self.year_published}"
|
conversation = self.my_conversation()
|
||||||
|
|
||||||
def update_year_published(self, new_year: int):
|
return conversation
|
||||||
self.year_published = new_year
|
|
||||||
print(f"The year of publication has been updated to {new_year}.")
|
|
||||||
|
|
||||||
def build(self, name, my_int, my_str, my_bool, no_type):
|
|
||||||
# do something...
|
|
||||||
print("x")
|
|
||||||
return ""
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return validate.create_class(code, class_name)
|
return validate.create_class(code, class_name)
|
||||||
|
|
|
||||||
|
|
@ -178,9 +178,8 @@ def instantiate_tool(node_type, class_object, params):
|
||||||
params["func"] = get_function(params.get("code"))
|
params["func"] = get_function(params.get("code"))
|
||||||
return class_object(**params)
|
return class_object(**params)
|
||||||
elif node_type == "CustomComponent":
|
elif node_type == "CustomComponent":
|
||||||
return get_function_custom(params.get("code"))
|
params["func"] = get_function_custom(params.get("code"))
|
||||||
# params["func"] = get_function_custom(params.get("code"))
|
return class_object(**params)
|
||||||
# return class_object(**params)
|
|
||||||
# For backward compatibility
|
# For backward compatibility
|
||||||
elif node_type == "PythonFunction":
|
elif node_type == "PythonFunction":
|
||||||
function_string = params["code"]
|
function_string = params["code"]
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@ class FrontendNode(BaseModel):
|
||||||
name: str = ""
|
name: str = ""
|
||||||
display_name: str = ""
|
display_name: str = ""
|
||||||
custom_fields: List[str] = []
|
custom_fields: List[str] = []
|
||||||
|
dynamic: bool = False
|
||||||
|
|
||||||
def to_dict(self) -> dict:
|
def to_dict(self) -> dict:
|
||||||
return {
|
return {
|
||||||
|
|
@ -25,6 +26,7 @@ class FrontendNode(BaseModel):
|
||||||
"base_classes": self.base_classes,
|
"base_classes": self.base_classes,
|
||||||
"display_name": self.display_name or self.name,
|
"display_name": self.display_name or self.name,
|
||||||
"custom_fields": self.custom_fields,
|
"custom_fields": self.custom_fields,
|
||||||
|
"dynamic": self.dynamic,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -186,7 +188,8 @@ class FrontendNode(BaseModel):
|
||||||
def should_be_password(key: str, show: bool) -> bool:
|
def should_be_password(key: str, show: bool) -> bool:
|
||||||
"""Determines whether the field should be a password field."""
|
"""Determines whether the field should be a password field."""
|
||||||
return (
|
return (
|
||||||
any(text in key.lower() for text in {"password", "token", "api", "key"})
|
any(text in key.lower()
|
||||||
|
for text in {"password", "token", "api", "key"})
|
||||||
and show
|
and show
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -50,20 +50,22 @@ def python_function(text: str) -> str:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
DEFAULT_CUSTOM_COMPONENT_CODE = """
|
DEFAULT_CUSTOM_COMPONENT_CODE = """
|
||||||
|
from langchain.llms import OpenAI
|
||||||
from langchain.chains import ConversationChain
|
from langchain.chains import ConversationChain
|
||||||
|
from langchain.memory import ConversationBufferMemory
|
||||||
|
|
||||||
|
|
||||||
class MyPythonClass:
|
class MyPythonClass:
|
||||||
def __init__(self, name: str, year: int):
|
def my_conversation(self):
|
||||||
self.name = name
|
llm = OpenAI(temperature=0)
|
||||||
self.year = year
|
return ConversationChain(
|
||||||
|
llm=llm, verbose=True, memory=ConversationBufferMemory()
|
||||||
|
)
|
||||||
|
|
||||||
def get_details(self):
|
def build(self, name: str) -> ConversationChain:
|
||||||
return f"Name: {self.name}, Year: {self.year}"
|
conversation = self.my_conversation()
|
||||||
|
|
||||||
def build(self, name: str, year: int, true_or_false: bool, no_type) -> ConversationChain:
|
return conversation
|
||||||
# do something...
|
|
||||||
return ConversationChain()
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
DIRECT_TYPES = ["str", "bool", "code", "int", "float", "Any", "prompt"]
|
DIRECT_TYPES = ["str", "bool", "code", "int", "float", "Any", "prompt"]
|
||||||
|
|
|
||||||
|
|
@ -186,6 +186,16 @@ def create_class(code, class_name):
|
||||||
raise ModuleNotFoundError(
|
raise ModuleNotFoundError(
|
||||||
f"Module {alias.name} not found. Please install it and try again."
|
f"Module {alias.name} not found. Please install it and try again."
|
||||||
) from e
|
) from e
|
||||||
|
elif isinstance(node, ast.ImportFrom):
|
||||||
|
try:
|
||||||
|
imported_module = importlib.import_module(node.module)
|
||||||
|
for alias in node.names:
|
||||||
|
exec_globals[alias.name] = getattr(
|
||||||
|
imported_module, alias.name)
|
||||||
|
except ModuleNotFoundError as e:
|
||||||
|
raise ModuleNotFoundError(
|
||||||
|
f"Module {node.module} not found. Please install it and try again."
|
||||||
|
) from e
|
||||||
|
|
||||||
class_code = next(
|
class_code = next(
|
||||||
node
|
node
|
||||||
|
|
@ -209,6 +219,8 @@ def create_class(code, class_name):
|
||||||
instance = exec_globals[class_name](*args, **kwargs)
|
instance = exec_globals[class_name](*args, **kwargs)
|
||||||
return instance
|
return instance
|
||||||
|
|
||||||
|
build.__globals__.update(exec_globals)
|
||||||
|
|
||||||
return build
|
return build
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue