Commit ·
46dd24e
1
Parent(s): d0267ca
First commit.
Browse filesFiles changed (6)
- LCToolFlow.pyAdded+52
- LCToolFlow.yamlAdded+23
- README.mdAdded+27
- __init__.pyAdded+1
- pip_requirements.pyAdded+1
- run.pyAdded+63
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152from __future__ import annotationsfrom copy import deepcopyfrom typing import Any, Dict
import hydrafrom langchain.tools import BaseTool
from flows.base_flows import AtomicFlow
class LCToolFlow(AtomicFlow): REQUIRED_KEYS_CONFIG = ["backend"]
# KEYS_TO_IGNORE_WHEN_RESETTING_NAMESPACE = {"backend"},TODO this will overwrite the KEYS_TO_IGNORE_WHEN_RESETTING_NAMESPACE in base_flows.py
SUPPORTS_CACHING: bool = False
backend: BaseTool
def __init__(self, backend: BaseTool, **kwargs) -> None: super().__init__(**kwargs) self.backend = backend @classmethod def _set_up_backend(cls, config: Dict[str, Any]) -> BaseTool: if config["_target_"].startswith("."): # assumption: cls is associated with relative data_transformation_configs # for example, CF_Code and CF_Code.yaml should be in the same directory, # and all _target_ in CF_Code.yaml should be relative cls_parent_module = ".".join(cls.__module__.split(".")[:-1]) config["_target_"] = cls_parent_module + config["_target_"] tool = hydra.utils.instantiate(config, _convert_="partial")
return tool
@classmethod def instantiate_from_config(cls, config: Dict[str, Any]) -> LCToolFlow: flow_config = deepcopy(config)
kwargs = {"flow_config": flow_config}
# ~~~ Set up LangChain backend ~~~ kwargs["backend"] = cls._set_up_backend(config["backend"])
# ~~~ Instantiate flow ~~~ return cls(**kwargs)
def run(self, input_data: Dict[str, Any]) -> Dict[str, Any]: observation = self.backend.run(tool_input=input_data)
return {"observation": observation}
1234567891011121314151617181920212223name: "search"description: "useful when you need to look for the answer online, especially for recent events"
######################################################## Input keys#######################################################
input_keys: - "query"
######################################################## Output keys#######################################################
output_keys: - "observation"
keep_raw_response: falseclear_flow_namespace_on_run_end: false
backend: _target_: langchain.tools.DuckDuckGoSearchRun123456789101112131415161718192021222324252627---license: mit---ToDo
## DescriptionToDo: Add description
< Flow description >
## Configuration parameters
< Name 1 > (< Type 1 >): < Description 1 >. Required parameter.
< Name 2 > (< Type 2 >): < Description 2 >. Default value is: < value 2 >
## Input interface
< Name 1 > (< Type 1 >): < Description 1 >.
(Note that the interface might depend on the state of the Flow.)
## Output interface
< Name 1 > (< Type 1 >): < Description 1 >.
(Note that the interface might depend on the state of the Flow.)1from .LCToolFlow import LCToolFlow1# ToDo123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263"""A simple script to run a Flow that can be used for development and debugging."""
import os
import hydra
import flowsfrom flows.flow_launchers import FlowLauncher, ApiInfofrom flows.utils.general_helpers import read_yaml_file
from flows import loggingfrom flows.flow_cache import CACHING_PARAMETERS, clear_cache
CACHING_PARAMETERS.do_caching = False # Set to True to enable caching# clear_cache() # Uncomment this line to clear the cache
logging.set_verbosity_debug()
if __name__ == "__main__": # ~~~ Set the API information ~~~ # OpenAI backend # api_information = ApiInfo("openai", os.getenv("OPENAI_API_KEY")) # Azure backend api_information = ApiInfo("azure", os.getenv("AZURE_OPENAI_KEY"), os.getenv("AZURE_OPENAI_ENDPOINT"))
# ~~~ Instantiate the Flow ~~~ root_dir = "." cfg_path = os.path.join(root_dir, "FlowName.yaml") cfg = read_yaml_file(cfg_path)
flow_with_interfaces = { "flow": hydra.utils.instantiate(cfg['flow'], _recursive_=False, _convert_="partial"), "input_interface": ( None if getattr(cfg, "input_interface", None) is None else hydra.utils.instantiate(cfg['input_interface'], _recursive_=False) ), "output_interface": ( None if getattr(cfg, "output_interface", None) is None else hydra.utils.instantiate(cfg['output_interface'], _recursive_=False) ), }
# ~~~ Get the data ~~~ # This can be a list of samples data = {"id": 0} # Add your data here
# ~~~ Run inference ~~~ path_to_output_file = None # path_to_output_file = "output.jsonl" # Uncomment this line to save the output to disk
_, outputs = FlowLauncher.launch( flow_with_interfaces=flow_with_interfaces, data=data, path_to_output_file=path_to_output_file, api_information=api_information, )
# ~~~ Print the output ~~~ flow_output_data = outputs[0] print(flow_output_data)LCToolFlow.pyAdded+52
LCToolFlow.yamlAdded+23
README.mdAdded+27
__init__.pyAdded+1
pip_requirements.pyAdded+1
run.pyAdded+63