| 118 | |
| 119 | |
| 120 | class TypeChatProgramTranslator(TypeChatJsonTranslator[JsonProgram]): |
| 121 | _api_declaration_str: str |
| 122 | |
| 123 | def __init__(self, model: TypeChatLanguageModel, validator: TypeChatProgramValidator, api_type: type): |
| 124 | super().__init__(model=model, validator=validator, target_type=api_type, _raise_on_schema_errors = False) |
| 125 | # TODO: the conversion result here has errors! |
| 126 | conversion_result = python_type_to_typescript_schema(api_type) |
| 127 | self._api_declaration_str = conversion_result.typescript_schema_str |
| 128 | |
| 129 | @override |
| 130 | def _create_request_prompt(self, intent: str) -> str: |
| 131 | |
| 132 | prompt = F""" |
| 133 | You are a service that translates user requests into programs represented as JSON using the following TypeScript definitions: |
| 134 | ``` |
| 135 | {program_schema_text} |
| 136 | ``` |
| 137 | The programs can call functions from the API defined in the following TypeScript definitions: |
| 138 | ``` |
| 139 | {self._api_declaration_str} |
| 140 | ``` |
| 141 | The following is a user request: |
| 142 | ''' |
| 143 | {intent} |
| 144 | ''' |
| 145 | The following is the user request translated into a JSON program object with 2 spaces of indentation and no properties with the value undefined: |
| 146 | """ |
| 147 | return prompt |
| 148 | |
| 149 | @override |
| 150 | def _create_repair_prompt(self, validation_error: str) -> str: |
| 151 | prompt = F""" |
| 152 | The JSON program object is invalid for the following reason: |
| 153 | ''' |
| 154 | {validation_error} |
| 155 | ''' |
| 156 | The following is a revised JSON program object: |
| 157 | """ |
| 158 | return prompt |