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_LLM_TOOLS,
_EXTRA_OPTIONAL_TOOLS, _EXTRA_OPTIONAL_TOOLS,
) )
from langflow.utils.graph import Graph
from langchain.agents import agent as agent_module 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): def load_flow_from_json(path: str):

View file

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

View file

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

View file

@ -281,3 +281,11 @@ def test_build():
agent = graph.build() agent = graph.build()
# The agent should be a AgentExecutor # The agent should be a AgentExecutor
assert isinstance(agent, 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 langchain import LLMChain, OpenAI
from langflow.utils.graph import Graph from langflow.utils.graph import Graph
import pytest import pytest
from pathlib import Path
from langflow import load_flow_from_json from langflow import load_flow_from_json
from langflow.interface.loading import extract_json from langflow.interface.loading import extract_json
from langflow.utils.payload import get_root_node, build_json from langflow.utils.payload import get_root_node, build_json