From 2678a4bd1ea624857d2a01f12ed7b45ed1576651 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Ho=CC=88nicke?= Date: Thu, 27 Apr 2023 18:21:14 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=A9=E2=80=8D=F0=9F=92=BC=20feat:=20pm?= =?UTF-8?q?=20role=20more=20robust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/apis/gpt.py | 2 + src/options/generate/generator.py | 56 ++++++++++++++---------- src/options/generate/templates_system.py | 2 +- src/options/generate/templates_user.py | 2 +- 4 files changed, 38 insertions(+), 24 deletions(-) diff --git a/src/apis/gpt.py b/src/apis/gpt.py index db11f52..386e411 100644 --- a/src/apis/gpt.py +++ b/src/apis/gpt.py @@ -1,4 +1,5 @@ import os +from copy import deepcopy from time import sleep from typing import List, Any @@ -49,6 +50,7 @@ class GPTSession: self.chars_generation_so_far = 0 def get_conversation(self, messages: List[BaseMessage] = [], print_stream: bool = True, print_costs: bool = True): + messages = deepcopy(messages) return _GPTConversation( self.model_name, self.cost_callback, messages, print_stream, print_costs ) diff --git a/src/options/generate/generator.py b/src/options/generate/generator.py index ccc4243..cff1f79 100644 --- a/src/options/generate/generator.py +++ b/src/options/generate/generator.py @@ -7,7 +7,7 @@ from typing import Callable, Union from typing import List, Text, Optional from langchain import PromptTemplate -from langchain.schema import SystemMessage, HumanMessage +from langchain.schema import SystemMessage, HumanMessage, AIMessage from pydantic.dataclasses import dataclass from src.apis import gpt @@ -327,6 +327,9 @@ metas: class MaxDebugTimeReachedException(BaseException): pass + class TaskRefinementException(BaseException): + pass + def is_dependency_issue(self, summarized_error, dock_req_string: str, package_manager: str): # a few heuristics to quickly jump ahead if any([error_message in summarized_error for error_message in ['AttributeError', 'NameError', 'AssertionError']]): @@ -417,34 +420,42 @@ Test scenario: ''') def refine_task(self, pm): - user_input = self.microservice_specification.task - if not user_input: - user_input = self.get_user_input(pm, 'What should your microservice do?') - messages = [ - SystemMessage(content=system_task_introduction + system_task_iteration), - ] + try: + user_input = self.microservice_specification.task + if not user_input: + user_input = self.get_user_input(pm, 'What should your microservice do?') + messages = [ + SystemMessage(content=system_task_introduction + system_task_iteration), + ] - while True: - conversation = self.gpt_session.get_conversation(messages, print_stream=os.environ['VERBOSE'].lower() == 'true', print_costs=False) - print('thinking...') - agent_response_raw = conversation.chat(template_task_refinement.format(user_input=user_input), role='user') + while True: + conversation = self.gpt_session.get_conversation(messages, print_stream=os.environ['VERBOSE'].lower() == 'true', print_costs=False) + print('thinking...') + agent_response_raw = conversation.chat(template_task_refinement.format(user_input=user_input), role='user') + + question = self.extract_content_from_result(agent_response_raw, 'prompt.txt', can_contain_code_block=False) + task_final = self.extract_content_from_result(agent_response_raw, 'task-final.txt', can_contain_code_block=False) + if task_final: + self.microservice_specification.task = task_final + break + if question: + messages.append(HumanMessage(content=user_input),) + user_input = self.get_user_input(pm, question) + messages.append(AIMessage(content=question)) + elif task_final: + user_input = self.get_user_input(pm, agent_response_raw + '\n: ') + else: + raise self.TaskRefinementException() + except self.TaskRefinementException as e: + print_colored('', f'{pm.emoji} Could not refine the task. Please try again...', 'red') + self.refine_task(pm) - question = self.extract_content_from_result(agent_response_raw, 'prompt.txt', can_contain_code_block=False) - task_final = self.extract_content_from_result(agent_response_raw, 'task-final.txt', can_contain_code_block=False) - if task_final: - self.microservice_specification.task = task_final - break - if question: - user_input = self.get_user_input(pm, question) - messages.extend([HumanMessage(content=user_input)]) - else: - user_input = self.get_user_input(pm, agent_response_raw + '\n: ') def refine_test(self, pm): + user_input = self.microservice_specification.task messages = [ SystemMessage(content=system_task_introduction + system_test_iteration), ] - user_input = self.microservice_specification.task while True: conversation = self.gpt_session.get_conversation(messages, print_stream=os.environ['VERBOSE'].lower() == 'true', print_costs=False) print('thinking...') @@ -487,6 +498,7 @@ Test scenario: @staticmethod def get_user_input(employee, prompt_to_user): val = input(f'{employee.emoji}❓ {prompt_to_user}\nyou: ') + print() while not val: val = input('you: ') return val diff --git a/src/options/generate/templates_system.py b/src/options/generate/templates_system.py index 06ed302..beffbb8 100644 --- a/src/options/generate/templates_system.py +++ b/src/options/generate/templates_system.py @@ -185,7 +185,7 @@ Please provide the input and output format. ''' system_test_iteration = f''' -The client gives you a description of the microservice. +The client gives you a description of the microservice (web service). Your task is to describe verbally a unit test for that microservice. There are two cases: a) The unit test requires an example file as input. diff --git a/src/options/generate/templates_user.py b/src/options/generate/templates_user.py index 4e29753..850384d 100644 --- a/src/options/generate/templates_user.py +++ b/src/options/generate/templates_user.py @@ -370,7 +370,7 @@ Either ask for clarification like this: ``` -Or write the summarized description like this: +Or write the summarized microservice description like this: **task-final.txt** ```text