diff --git a/src/apis/gpt.py b/src/apis/gpt.py index 1dd2cf7..59d1156 100644 --- a/src/apis/gpt.py +++ b/src/apis/gpt.py @@ -60,20 +60,20 @@ If you have updated it already, please restart your terminal. class AssistantStreamingStdOutCallbackHandler(StreamingStdOutCallbackHandler): def on_llm_new_token(self, token: str, **kwargs: Any) -> None: """Run on new LLM token. Only available when streaming is enabled.""" - if os.environ['VERBOSE'].lower() == 'true': - print_colored('', token, 'green', end='') + print_colored('', token, 'green', end='') class _GPTConversation: def __init__(self, model: str, task_description, test_description, system_definition_examples: List[str] = ['executor', 'docarray', 'client']): - self.chat = ChatOpenAI( + self._chat = ChatOpenAI( model_name=model, streaming=True, callback_manager=CallbackManager([AssistantStreamingStdOutCallbackHandler()]), - temperature=0 + verbose=os.environ['VERBOSE'].lower() == 'true', + temperature=0, ) self.messages: List[BaseMessage] = [] - self.system_message = self._create_system_message(system_definition_examples) + self.system_message = self._create_system_message(task_description, test_description, system_definition_examples) if os.environ['VERBOSE'].lower() == 'true': print_colored('system', self.system_message.content, 'magenta') @@ -83,9 +83,9 @@ class _GPTConversation: if os.environ['VERBOSE'].lower() == 'true': print_colored('user', prompt, 'blue') print_colored('assistant', '', 'green', end='') - response = self.chat([self.system_message] + self.messages) - self.messages.append(AIMessage(content=response)) - return response + response = self._chat([self.system_message] + self.messages) + self.messages.append(response) + return response.content @staticmethod def _create_system_message(task_description, test_description, system_definition_examples: List[str] = []) -> SystemMessage: diff --git a/src/options/generate/generator.py b/src/options/generate/generator.py index cd6694f..6414a94 100644 --- a/src/options/generate/generator.py +++ b/src/options/generate/generator.py @@ -51,7 +51,7 @@ metas: for file_name, tag in FILE_AND_TAG_PAIRS: if file_name in file_name_to_content and (not restrict_keys or file_name in restrict_keys): all_microservice_files_string += f'**{file_name}**\n```{tag}\n{file_name_to_content[file_name]}\n```\n\n' - return all_microservice_files_string + return all_microservice_files_string.strip() def generate_microservice( self, @@ -63,7 +63,7 @@ metas: MICROSERVICE_FOLDER_v1 = get_microservice_path(path, microservice_name, packages, num_approach, 1) os.makedirs(MICROSERVICE_FOLDER_v1) - print_colored('', '############# Microservice #############', 'blue') + print_colored('', '\n############# Microservice #############', 'blue') conversation = self.gpt_session.get_conversation() microservice_content_raw = conversation.chat( template_generate_executor.format( @@ -86,12 +86,13 @@ metas: ) persist_file(microservice_content, os.path.join(MICROSERVICE_FOLDER_v1, 'microservice.py')) - print_colored('', '############# Test Microservice #############', 'blue') + print_colored('', '\n############# Test Microservice #############', 'blue') conversation = self.gpt_session.get_conversation() test_microservice_content_raw = conversation.chat( template_generate_test.format( code_files_wrapped=self.files_to_string({'microservice.py': microservice_content}), microservice_name=microservice_name, + test_description=self.test_description, file_name_purpose=TEST_EXECUTOR_FILE_NAME, tag_name=TEST_EXECUTOR_FILE_TAG, file_name=TEST_EXECUTOR_FILE_NAME, @@ -102,7 +103,7 @@ metas: ) persist_file(test_microservice_content, os.path.join(MICROSERVICE_FOLDER_v1, 'test_microservice.py')) - print_colored('', '############# Requirements #############', 'blue') + print_colored('', '\n############# Requirements #############', 'blue') requirements_path = os.path.join(MICROSERVICE_FOLDER_v1, 'requirements.txt') conversation = self.gpt_session.get_conversation() requirements_content_raw = conversation.chat( @@ -120,7 +121,7 @@ metas: match_single_block=True) persist_file(requirements_content, requirements_path) - print_colored('', '############# Dockerfile #############', 'blue') + print_colored('', '\n############# Dockerfile #############', 'blue') conversation = self.gpt_session.get_conversation() dockerfile_content_raw = conversation.chat( template_generate_dockerfile.format( @@ -142,10 +143,10 @@ metas: persist_file(dockerfile_content, os.path.join(MICROSERVICE_FOLDER_v1, 'Dockerfile')) self.write_config_yml(microservice_name, MICROSERVICE_FOLDER_v1) - print('First version of the microservice generated. Start iterating on it to make the tests pass...') + print('\nFirst version of the microservice generated. Start iterating on it to make the tests pass...') def generate_playground(self, microservice_name, microservice_path): - print_colored('', '############# Playground #############', 'blue') + print_colored('', '\n############# Playground #############', 'blue') file_name_to_content = get_all_microservice_files_with_content(microservice_path) conversation = self.gpt_session.get_conversation([]) @@ -169,7 +170,7 @@ metas: def debug_microservice(self, path, microservice_name, num_approach, packages): for i in range(1, MAX_DEBUGGING_ITERATIONS): print('Debugging iteration', i) - print('Trying to build the microservice. Might take a while...') + print('Trying to debug the microservice. Might take a while...') previous_microservice_path = get_microservice_path(path, microservice_name, packages, num_approach, i) next_microservice_path = get_microservice_path(path, microservice_name, packages, num_approach, i + 1) log_hubble = push_executor(previous_microservice_path) diff --git a/src/options/generate/prompt_system.py b/src/options/generate/prompt_system.py index 42ced44..6d443c1 100644 --- a/src/options/generate/prompt_system.py +++ b/src/options/generate/prompt_system.py @@ -72,7 +72,7 @@ print(response[0].text) ```''' -system_base_definition = '''It is the year 2021. +system_message_base = '''It is the year 2021. You are a principal engineer working at Jina - an open source company. You accurately satisfy all of the user's requirements. To be more specific, you help the user to build a microservice with the following requirements: diff --git a/src/options/generate/templates.py b/src/options/generate/templates.py index 6447e6d..606093e 100644 --- a/src/options/generate/templates.py +++ b/src/options/generate/templates.py @@ -95,7 +95,7 @@ For the implementation use the following package: '{packages}'. Obey the following rules: Have in mind that d.uri is never a path to a local file. It is always a url. -''' + not_allowed_executor_string() + ''' +''' + not_allowed_executor_string + ''' Your approach: 1. Identify the core challenge when implementing the executor. @@ -111,7 +111,7 @@ template_generate_test = PromptTemplate.from_template( {code_files_wrapped} -Write a single test case that tests the following scenario: '{test}'. In case the test scenario is not precise enough, test a general case without any assumptions. +Write a single test case that tests the following scenario: '{test_description}'. In case the test scenario is not precise enough, test a general case without any assumptions. Start the test with an extensive comment about the test case. Use the following import to import the executor: @@ -119,7 +119,7 @@ Use the following import to import the executor: from microservice import {microservice_name} ``` -''' + not_allowed_executor_string() + ''' +''' + not_allowed_executor_string + ''' The test must not open local files. The test must not mock a function of the executor. The test must not use other data than the one provided in the test scenario. @@ -153,7 +153,7 @@ The base image of the Dockerfile is FROM jinaai/jina:3.14.1-py39-standard. The entrypoint is ENTRYPOINT ["jina", "executor", "--uses", "config.yml"]. Make sure the all files are in the /workdir. The Dockerfile runs the test during the build process. -''' + not_allowed_docker_string() + '\n' + template_code_wrapping_string +''' + not_allowed_docker_string + '\n' + template_code_wrapping_string ) @@ -190,7 +190,7 @@ To solve this error, you should: 3. Write down the files that need to be changed, but not files that don't need to be changed. For files that need to be changed, you must provide the complete file with the exact same syntax to wrap the code. Obey the following rules: -''' + not_allowed_docker_string() + ''' +''' + not_allowed_docker_string + ''' You are given the following files: @@ -217,7 +217,7 @@ jina==2.0.0 template_solve_code_issue = PromptTemplate.from_template( '''General rules: -''' + not_allowed_executor_string() + ''' +''' + not_allowed_executor_string + ''' Here is the description of the task the executor must solve: {description}