Upload 6 files
Browse filesFiles changed (6)
- CodeGeneratorAtomicFlow.pyAdded+112
- CodeGeneratorAtomicFlow.yamlAdded+94
- README.md+79-3
- __init__.pyAdded+8
- pip_requirements.txtAdded+15
- run.pyAdded+52
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112import jsonfrom copy import deepcopyfrom typing import Any, Dictfrom flow_modules.aiflows.ChatFlowModule import ChatAtomicFlow
class CodeGeneratorAtomicFlow(ChatAtomicFlow): """This class wraps around the Chat API to generate code from a goal. One thing worth noting is that we need to make sure the code generator does not write repetitive code that is present in the library, so we need to inject the function signatures in the library to the system prompts.
*Input Interface Non Initialized*: - `goal` - `code_library` - `memory_files`
*Input Interface Initialized*: - `goal` - `code_library` - `memory_files`
*Output Interface*: - `code` - `language_of_code` """ def __init__(self, **kwargs): super().__init__(**kwargs) self.system_message_prompt_template = self.system_message_prompt_template.partial( code_library_file_location="no location yet", code_library="no code yet" ) self.hint_for_model = """ Make sure your response is in the following format: Response Format: { "language_of_code": "language of the code", "code": "String of the code and docstrings corresponding to the goal", } """
@classmethod def instantiate_from_config(cls, config): """Instantiate a CodeGeneratorAtomicFlow from a configuration. :param config: Configuration dictionary. :return: Instantiated CodeGeneratorAtomicFlow. """ flow_config = deepcopy(config)
kwargs = {"flow_config": flow_config}
# ~~~ Set up prompts ~~~ kwargs.update(cls._set_up_prompts(flow_config))
# ~~~ Set up backend ~~~ kwargs.update(cls._set_up_backend(flow_config))
# ~~~ Instantiate flow ~~~ return cls(**kwargs)
def _get_code_library_file(self, input_data: Dict[str, Any]): """Get the code library file location from the input data. :param input_data: Input data. :return: Code library file location. """ assert "memory_files" in input_data, "memory_files not passed to CodeGeneratorAtomicFlow" assert "code_library" in input_data['memory_files'], "code_library not in memory_files" code_library_file_location = input_data['memory_files']['code_library'] return code_library_file_location
def _get_code_library_content(self, input_data: Dict[str, Any]): """Get the code library content from the input data. :param input_data: Input data. :return: Code library content. """ assert "code_library" in input_data, "code_library not passed to CodeGeneratorAtomicFlow" code_library = input_data['code_library'] if len(code_library) == 0: code_library = "No code yet" return code_library
def _update_prompts_and_input(self, input_data: Dict[str, Any]): """Update the prompts and input data. :param input_data: Input data. """ if 'goal' in input_data: input_data['goal'] += self.hint_for_model code_library_file_location = self._get_code_library_file(input_data) code_library = self._get_code_library_content(input_data) self.system_message_prompt_template = self.system_message_prompt_template.partial( code_library_file_location=code_library_file_location, code_library=code_library )
def run(self, input_data: Dict[str, Any]) -> Dict[str, Any]: """Run the flow. :param input_data: Input data. :return: Output data. """ self._update_prompts_and_input(input_data)
while True: api_output = super().run(input_data)["api_output"].strip() try: response = json.loads(api_output) return response except (json.decoder.JSONDecodeError, json.JSONDecodeError): new_goal = "The previous response cannot be parsed with json.loads, it cannot be parsed with json.loads, it could be the backslashes usesd for escaping single quotes in the string arguments of the code are not properly escaped themselves within the JSON context. Next time, do not provide any comments or code blocks. Make sure your next response is purely json parsable." new_input_data = input_data.copy() new_input_data['goal'] = new_goal input_data = new_input_data12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394_target_: flow_modules.aiflows.CodeGeneratorFlowModule.CodeGeneratorAtomicFlow.instantiate_from_default_configname: "CodeGeneratorAtomicFlow"description: "Writes code with given instruction"enable_cache: True
input_interface_non_initialized: # initial input keys - "goal" - "code_library" - "memory_files"
input_interface_initialized: # input_keys - "goal" - "code_library" - "memory_files"
######################################################## Output keys#######################################################
output_interface: - 'language_of_code' - 'code'
#######################################################system_message_prompt_template: _target_: aiflows.prompt_template.JinjaPrompt template: |2- You are a world class programmer that can complete any goal with code. All code you write should be functions. You have a library of executable functions located at {{code_library_file_location}} for your disposal, the code you write will also be added to this file. Your tasks are: 1. Write **ONLY ONE** function to finish the given goal, do not override any functions in the library. 2. Also write docstrings to the new function. Notice that: 1. Write functions only. 2. Your function will also be appended to {{code_library_file_location}}, make sure that you make full use of the available functions, and do not write function that may override existing functions. 3. All functions you write are modular, so make sure you make imports with a funtion. 4. You must write docstrings for every function you write. Here are the contents in the code library located ta {{code_library_file_location}}, make sure you make full use of them: {{code_library}} Your function will then be imported and called by an executor to finish the goal, you do not need to worry about the execution part. You can access the Internet, using APIs of online services could be useful. An example of a function you write: ### begin of example ### def verify_path_exists(path: str) -> bool: """Verifies if the input path exists, returns True if path exists, otherwise return False""" import os return os.path.exists(path) ### end of example ### You are capable of **any** task.
Performance Evaluation: 1. The priority language is Python, try to code with Python to solve the task. 2. All code you write must be functions. 3. All imports must be done within the function body. 4. All functions must have docstrings. **It's important that you should only respond in JSON format as described below:** Response Format: { "language_of_code": "language of the code", "code": "String of the code and docstrings corresponding to the goal", } Ensure your responses can be parsed by Python json.loads **It's important that you make imports inside of the function body, but not outside of it.** **It's important that the code you generate can be written by Python write, and is human-readable. The written file must also be indented and formatted, so that it is human-readable.**
input_variables: ["code_library_file_location", "code_library"]
human_message_prompt_template: _target_: aiflows.prompt_template.JinjaPrompt template: |2- Here is the response to your last action: {{goal}} input_variables: - "goal"
init_human_message_prompt_template: _target_: aiflows.prompt_template.JinjaPrompt template: |2- Here is the goal you need to achieve: {{goal}} input_variables: - "goal"123---license: mit---12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879# Table of Contents* [CodeGeneratorAtomicFlow](#CodeGeneratorAtomicFlow)* [CodeGeneratorAtomicFlow](#CodeGeneratorAtomicFlow.CodeGeneratorAtomicFlow)* [instantiate\_from\_config](#CodeGeneratorAtomicFlow.CodeGeneratorAtomicFlow.instantiate_from_config)* [run](#CodeGeneratorAtomicFlow.CodeGeneratorAtomicFlow.run)* [\_\_init\_\_](#__init__)<a id="CodeGeneratorAtomicFlow"></a># CodeGeneratorAtomicFlow<a id="CodeGeneratorAtomicFlow.CodeGeneratorAtomicFlow"></a>## CodeGeneratorAtomicFlow Objects```pythonclass CodeGeneratorAtomicFlow(ChatAtomicFlow)```This class wraps around the Chat API to generate code from a goal. One thing worth noting is that we need tomake sure the code generator does not write repetitive code that is present in the library, so we need to injectthe function signatures in the library to the system prompts.*Input Interface Non Initialized*:- `goal`- `code_library`- `memory_files`*Input Interface Initialized*:- `goal`- `code_library`- `memory_files`*Output Interface*:- `code`- `language_of_code`<a id="CodeGeneratorAtomicFlow.CodeGeneratorAtomicFlow.instantiate_from_config"></a>#### instantiate\_from\_config```python@classmethoddef instantiate_from_config(cls, config)```Instantiate a CodeGeneratorAtomicFlow from a configuration.**Arguments**:- `config`: Configuration dictionary.**Returns**:Instantiated CodeGeneratorAtomicFlow.<a id="CodeGeneratorAtomicFlow.CodeGeneratorAtomicFlow.run"></a>#### run```pythondef run(input_data: Dict[str, Any]) -> Dict[str, Any]```Run the flow.**Arguments**:- `input_data`: Input data.**Returns**:Output data.<a id="__init__"></a># \_\_init\_\_
12345678dependencies = [ {"url": "aiflows/ChatFlowModule", "revision": "297c90d08087d9ff3139521f11d1a48d7dc63ed4"},]from aiflows import flow_verse
flow_verse.sync_dependencies(dependencies)
from .CodeGeneratorAtomicFlow import CodeGeneratorAtomicFlow123456789101112131415colorama==0.4.6pytest==7.3.1pytest-cov==4.1.0hydra-core==1.3.2hydra-colorlog==1.1.0wrapt-timeout-decorator==1.3.12.2diskcache==5.6.1openai==1.0.0huggingface_hub==0.19.4jsonlines==3.1.0jinja2==3.1.2mock==5.0.2rich==12.6.0litellm==1.0.0aiflows12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152import os
import hydra
from aiflows.backends.api_info import ApiInfofrom aiflows.messages import InputMessagefrom aiflows.utils.general_helpers import read_yaml_file
from aiflows import loggingfrom aiflows.flow_cache import CACHING_PARAMETERS, clear_cache
CACHING_PARAMETERS.do_caching = False # Set to True in order to disable caching
logging.set_verbosity_debug()logging.auto_set_dir()
dependencies = [ {"url": "aiflows/CodeGeneratorFlowModule", "revision": "main"},]
from aiflows import flow_verse
flow_verse.sync_dependencies(dependencies)
if __name__ == "__main__": current_dir = os.getcwd() cfg_path = os.path.join(current_dir, "CodeGeneratorAtomicFlow.yaml") cfg = read_yaml_file(cfg_path)
CodeGenFlow = hydra.utils.instantiate(cfg, _recursive_=False, _convert_="partial")
with os.path.join(current_dir, "example_library.py") as f: pass
memory_files = {"code_library": os.path.join(current_dir, "example_library.py")}
input_data = { "memory_files": memory_files, "code_library": os.path.join(current_dir, "example_library.py"), "goal": "write a function to add up two numbers" } input_message = InputMessage.build( data_dict=input_data, src_flow="Launcher", dst_flow=CodeGenFlow.name )
# ~~~ calling the flow ~~~ output_message = CodeGenFlow(input_message)
print(output_message.data)CodeGeneratorAtomicFlow.pyAdded+112
CodeGeneratorAtomicFlow.yamlAdded+94
README.md+79-3
__init__.pyAdded+8
pip_requirements.txtAdded+15
run.pyAdded+52