feat: complex example test passing

This commit is contained in:
Gabriel Almeida 2023-03-26 01:15:01 -03:00
commit 7dbda097f5
5 changed files with 39 additions and 27 deletions

View file

@ -17,8 +17,26 @@ from langchain.agents.load_tools import (
_EXTRA_LLM_TOOLS,
_EXTRA_OPTIONAL_TOOLS,
)
from langflow.utils.graph import Graph
from langchain.agents import agent as agent_module
from langflow.utils.graph import Graph
from langflow.interface.importing import import_by_type
from langchain.agents import ZeroShotAgent
def instantiate_class(module_type: str, base_type: str, params: Dict) -> Any:
"""Instantiate class from module type and key, and params"""
class_object = import_by_type(_type=base_type, name=module_type)
if base_type == "agents":
# We need to initialize it differently
allowed_tools = params["allowed_tools"]
llm_chain = params["llm_chain"]
return load_agent_executor(class_object, allowed_tools, llm_chain)
elif base_type == "tools" or module_type != "ZeroShotPrompt":
return class_object(**params)
else:
return ZeroShotAgent.create_prompt(**params, tools=[])
def load_flow_from_json(path: str):

View file

@ -1,6 +1,5 @@
from typing import Dict, List, Union
from langflow.interface import listing
from langflow.interface.importing import import_by_type
from langflow.interface import listing, loading
from langflow.utils import payload, util
LANGCHAIN_TYPES_DICT = {
@ -105,8 +104,6 @@ class Node:
self.params = params
def build(self):
from langflow.interface.loading import load_agent_executor
# The params dict is used to build the module
# it contains values and keys that point to nodes which
# have their own params dict
@ -122,8 +119,8 @@ class Node:
for key, value in self.params.items():
# Check if Node or list of Nodes
if isinstance(value, Node):
self.params[key] = value.build()
result = value.build()
self.params[key] = result.run if key == "func" else result
elif isinstance(value, list) and all(
isinstance(node, Node) for node in value
):
@ -133,26 +130,15 @@ class Node:
# and instantiate it with the params
# and return the instance
instance = None
for key, value in LANGCHAIN_TYPES_DICT.items():
if key == "tools":
for base_type, value in LANGCHAIN_TYPES_DICT.items():
if base_type == "tools":
value = util.get_tools_dict()
if self.module_type in value:
class_object = import_by_type(_type=key, name=self.module_type)
if key == "agents":
# We need to initialize it differently
allowed_tools = self.params["allowed_tools"]
llm_chain = self.params["llm_chain"]
instance = load_agent_executor(
class_object, allowed_tools, llm_chain
)
elif key == "tools":
instance = class_object(**self.params)
elif self.module_type == "ZeroShotPrompt":
from langchain.agents import ZeroShotAgent
instance = ZeroShotAgent.create_prompt(**self.params, tools=[])
else:
instance = class_object(**self.params)
instance = loading.instantiate_class(
module_type=self.module_type,
base_type=base_type,
params=self.params,
)
break
return instance

View file

@ -149,7 +149,7 @@
"show": true,
"password": true,
"multiline": false,
"value": null
"value": "sk-"
},
"batch_size": {
"type": "int",

View file

@ -281,3 +281,11 @@ def test_build():
agent = graph.build()
# The agent should be a AgentExecutor
assert isinstance(agent, AgentExecutor)
# Now we test the complex example
graph = get_graph(basic=False)
assert isinstance(graph, Graph)
# Now we test the build method
agent = graph.build()
# The agent should be a AgentExecutor
assert isinstance(agent, AgentExecutor)

View file

@ -2,7 +2,7 @@ import json
from langchain import LLMChain, OpenAI
from langflow.utils.graph import Graph
import pytest
from pathlib import Path
from langflow import load_flow_from_json
from langflow.interface.loading import extract_json
from langflow.utils.payload import get_root_node, build_json