Skip to content

Commit

Permalink
Add uncommitted file.
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielRosenwasser committed Apr 20, 2024
1 parent 48a4ab3 commit 83f1435
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions python/src/typechat/_internal/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,31 +49,33 @@ 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`.
The prompt for the subsequent attempts will include the diagnostics produced for the prior attempt.
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

Expand All @@ -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"""
Expand Down

0 comments on commit 83f1435

Please sign in to comment.