From b32adf501d0f39ee0956bfda4d0f51fdc920fb10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Ho=CC=88nicke?= Date: Wed, 19 Apr 2023 01:03:24 +0200 Subject: [PATCH] docs: fix documentation --- src/apis/gpt.py | 2 +- src/cli.py | 6 +- src/options/generate/generator.py | 140 ++++++++++++------------- src/options/generate/templates_user.py | 5 +- 4 files changed, 74 insertions(+), 79 deletions(-) diff --git a/src/apis/gpt.py b/src/apis/gpt.py index ddf6a25..c7577c8 100644 --- a/src/apis/gpt.py +++ b/src/apis/gpt.py @@ -82,7 +82,7 @@ class _GPTConversation: self.messages.append(chat_message) if os.environ['VERBOSE'].lower() == 'true': print_colored('user', prompt, 'blue') - print_colored('assistant', '', 'green', end='') + print_colored('assistant', '', 'green', end='') response = self._chat([self.system_message] + self.messages) if os.environ['VERBOSE'].lower() == 'true': print() diff --git a/src/cli.py b/src/cli.py index 713c8c0..133318d 100644 --- a/src/cli.py +++ b/src/cli.py @@ -46,7 +46,7 @@ def main(ctx): @click.option('--description', required=True, help='Description of the microservice.') @click.option('--test', required=True, help='Test scenario for the microservice.') @click.option('--model', default='gpt-4', help='GPT model to use (default: gpt-4).') -@click.option('--verbose', default=False, is_flag=True, help='Verbose mode.') +@click.option('--verbose', default=False, is_flag=True, help='Verbose mode.') # only for development @path_param def generate( description, @@ -67,8 +67,8 @@ def generate( generator = Generator(description, test, model=model) with get_openai_callback() as cb: generator.generate(path) - print(f"Prompt/Completion/Total Tokens: {cb.prompt_tokens}/{cb.completion_tokens}/{cb.total_tokens}") - print(f"Total Cost on OpenAI (USD): ${cb.total_cost}") + # print(f"Prompt/Completion/Total Tokens: {cb.prompt_tokens}/{cb.completion_tokens}/{cb.total_tokens}") + # print(f"Total Cost on OpenAI (USD): ${cb.total_cost}") @main.command() @path_param diff --git a/src/options/generate/generator.py b/src/options/generate/generator.py index e3a93e0..2e27e2e 100644 --- a/src/options/generate/generator.py +++ b/src/options/generate/generator.py @@ -52,6 +52,26 @@ metas: all_microservice_files_string += f'**{file_name}**\n```{tag}\n{file_name_to_content[file_name]}\n```\n\n' return all_microservice_files_string.strip() + + def generate_and_persist_file(self, section_title, template, destination_folder, file_name, **kwargs): + print_colored('', f'\n############# {section_title} #############', 'blue') + conversation = self.gpt_session.get_conversation() + kwargs = {k: v for k, v in kwargs.items() if k in template.input_variables} + content_raw = conversation.chat( + template.format( + file_name=file_name, + **kwargs + ) + ) + content = self.extract_content_from_result(content_raw, file_name, match_single_block=True) + if content == '': + content_raw = conversation.chat(f'You must add the {file_name} code.') + content = self.extract_content_from_result( + content_raw, file_name, match_single_block=True + ) + persist_file(content, os.path.join(destination_folder, file_name)) + return content_raw + def generate_microservice( self, path, @@ -62,84 +82,58 @@ metas: MICROSERVICE_FOLDER_v1 = get_microservice_path(path, microservice_name, packages, num_approach, 1) os.makedirs(MICROSERVICE_FOLDER_v1) - print_colored('', '\n############# Microservice #############', 'blue') - conversation = self.gpt_session.get_conversation() - microservice_content_raw = conversation.chat( - template_generate_executor.format( - microservice_name=microservice_name, - microservice_description=self.task_description, - test=self.test_description, - packages=packages, - file_name_purpose=EXECUTOR_FILE_NAME, - tag_name=EXECUTOR_FILE_TAG, - file_name=EXECUTOR_FILE_NAME, - ) - ) - microservice_content = self.extract_content_from_result( - microservice_content_raw, 'microservice.py', match_single_block=True - ) - if microservice_content == '': - microservice_content_raw = conversation.chat('You must add the executor code.') - microservice_content = self.extract_content_from_result( - microservice_content_raw, 'microservice.py', match_single_block=True - ) - persist_file(microservice_content, os.path.join(MICROSERVICE_FOLDER_v1, 'microservice.py')) - - 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, - ) - ) - test_microservice_content = self.extract_content_from_result( - test_microservice_content_raw, 'microservice.py', match_single_block=True - ) - persist_file(test_microservice_content, os.path.join(MICROSERVICE_FOLDER_v1, 'test_microservice.py')) - - 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( - template_generate_requirements.format( - code_files_wrapped=self.files_to_string( - {'microservice.py': microservice_content, 'test_microservice.py': test_microservice_content} - ), - file_name_purpose=REQUIREMENTS_FILE_NAME, - file_name=REQUIREMENTS_FILE_NAME, - tag_name=REQUIREMENTS_FILE_TAG, - ) + microservice_content = self.generate_and_persist_file( + 'Microservice', + template_generate_executor, + MICROSERVICE_FOLDER_v1, + microservice_name=microservice_name, + microservice_description=self.task_description, + test_description=self.test_description, + packages=packages, + file_name_purpose=EXECUTOR_FILE_NAME, + tag_name=EXECUTOR_FILE_TAG, + file_name=EXECUTOR_FILE_NAME, ) - requirements_content = self.extract_content_from_result(requirements_content_raw, 'requirements.txt', - match_single_block=True) - persist_file(requirements_content, requirements_path) + test_microservice_content = self.generate_and_persist_file( + 'Test Microservice', + template_generate_test, + MICROSERVICE_FOLDER_v1, + code_files_wrapped=self.files_to_string({'microservice.py': microservice_content}), + microservice_name=microservice_name, + microservice_description=self.task_description, + test_description=self.test_description, + file_name_purpose=TEST_EXECUTOR_FILE_NAME, + tag_name=TEST_EXECUTOR_FILE_TAG, + file_name=TEST_EXECUTOR_FILE_NAME, + ) - print_colored('', '\n############# Dockerfile #############', 'blue') - conversation = self.gpt_session.get_conversation() - dockerfile_content_raw = conversation.chat( - template_generate_dockerfile.format( - code_files_wrapped=self.files_to_string( - { - 'microservice.py': microservice_content, - 'test_microservice.py': test_microservice_content, - 'requirements.txt': requirements_content, - } - ), - file_name_purpose=DOCKER_FILE_NAME, - file_name=DOCKER_FILE_NAME, - tag_name=DOCKER_FILE_TAG, - ) + requirements_content = self.generate_and_persist_file( + 'Requirements', + template_generate_requirements, + MICROSERVICE_FOLDER_v1, + code_files_wrapped=self.files_to_string({ + 'microservice.py': microservice_content, + 'test_microservice.py': test_microservice_content + }), + file_name_purpose=REQUIREMENTS_FILE_NAME, + file_name=REQUIREMENTS_FILE_NAME, + tag_name=REQUIREMENTS_FILE_TAG, ) - dockerfile_content = self.extract_content_from_result( - dockerfile_content_raw, 'Dockerfile', match_single_block=True + + self.generate_and_persist_file( + 'Dockerfile', + template_generate_dockerfile, + MICROSERVICE_FOLDER_v1, + code_files_wrapped=self.files_to_string({ + 'microservice.py': microservice_content, + 'test_microservice.py': test_microservice_content, + 'requirements.txt': requirements_content + }), + file_name_purpose=DOCKER_FILE_NAME, + file_name=DOCKER_FILE_NAME, + tag_name=DOCKER_FILE_TAG, ) - persist_file(dockerfile_content, os.path.join(MICROSERVICE_FOLDER_v1, 'Dockerfile')) self.write_config_yml(microservice_name, MICROSERVICE_FOLDER_v1) print('\nFirst version of the microservice generated. Start iterating on it to make the tests pass...') diff --git a/src/options/generate/templates_user.py b/src/options/generate/templates_user.py index e9e92bd..03d58fb 100644 --- a/src/options/generate/templates_user.py +++ b/src/options/generate/templates_user.py @@ -90,7 +90,7 @@ template_generate_executor = PromptTemplate.from_template( Write the executor called '{microservice_name}'. The name is very important to keep. It matches the following description: '{microservice_description}'. -It will be tested with the following scenario: '{test}'. +It will be tested with the following scenario: '{test_description}'. For the implementation use the following package: '{packages}'. Obey the following rules: @@ -101,7 +101,7 @@ Your approach: 1. Identify the core challenge when implementing the executor. 2. Think about solutions for these challenges. 3. Decide for one of the solutions. -4. Write the code. +4. Write the code for the executor. Don't write code for the test. ''' + '\n' + template_code_wrapping_string ) @@ -264,6 +264,7 @@ template_generate_playground = PromptTemplate.from_template( Create a playground for the executor {microservice_name} using streamlit. The playground must look like it was made by a professional designer. All the ui elements are well thought out to make them visually appealing and easy to use. +The playground contains emojis that fit the theme of the playground. This is an example how you can connect to the executor assuming the document (d) is already defined: ``` from jina import Client, Document, DocumentArray