From 83f1435b76cfc58123fecc15e1fa74a8f0cc284b Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Sat, 20 Apr 2024 01:27:34 +0000 Subject: [PATCH] Add uncommitted file. --- python/src/typechat/_internal/translator.py | 22 +++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/python/src/typechat/_internal/translator.py b/python/src/typechat/_internal/translator.py index 141a4772..6c649c1e 100644 --- a/python/src/typechat/_internal/translator.py +++ b/python/src/typechat/_internal/translator.py @@ -49,7 +49,7 @@ def __init__( self._type_name = conversion_result.typescript_type_reference self._schema_str = conversion_result.typescript_schema_str - async def translate(self, request: str, *, prompt_preamble: str | list[PromptSection] | None = None) -> Result[T]: + async def translate(self, input: str, *, prompt_preamble: str | list[PromptSection] | None = None) -> Result[T]: """ Translates a natural language request into an object of type `T`. If the JSON object returned by the language model fails to validate, repair attempts will be made up until `_max_repair_attempts`. @@ -57,23 +57,25 @@ async def translate(self, request: str, *, prompt_preamble: str | list[PromptSec This often helps produce a valid instance. Args: - request: A natural language request. + input: A natural language request. prompt_preamble: An optional string or list of prompt sections to prepend to the generated prompt.\ If a string is given, it is converted to a single "user" role prompt section. """ - request = self._create_request_prompt(request) - prompt: str | list[PromptSection] - if prompt_preamble is None: - prompt = request - else: + messages: list[PromptSection] = [] + + messages.append({"role": "user", "content": input}) + if prompt_preamble: if isinstance(prompt_preamble, str): prompt_preamble = [{"role": "user", "content": prompt_preamble}] - prompt = [*prompt_preamble, {"role": "user", "content": request}] + else: + messages.extend(prompt_preamble) + + messages.append({"role": "user", "content": self._create_request_prompt(input)}) num_repairs_attempted = 0 while True: - completion_response = await self.model.complete(prompt) + completion_response = await self.model.complete(messages) if isinstance(completion_response, Failure): return completion_response @@ -93,7 +95,7 @@ async def translate(self, request: str, *, prompt_preamble: str | list[PromptSec if num_repairs_attempted >= self._max_repair_attempts: return Failure(error_message) num_repairs_attempted += 1 - request = f"{text_response}\n{self._create_repair_prompt(error_message)}" + messages.append({"role": "user", "content": self._create_repair_prompt(error_message)}) def _create_request_prompt(self, intent: str) -> str: prompt = f"""