Execute the current program. Note that as execution progresses the program will be incrementally converted from a template into a completed string (with variables stored). At each point in this process the current template remains valid.
(self, memory=None, from_agent=False)
| 597 | self._displayed = True |
| 598 | |
| 599 | async def execute(self, memory=None, from_agent=False): |
| 600 | """Execute the current program. |
| 601 | |
| 602 | Note that as execution progresses the program will be incrementally converted |
| 603 | from a template into a completed string (with variables stored). At each point |
| 604 | in this process the current template remains valid. |
| 605 | """ |
| 606 | |
| 607 | log.debug( |
| 608 | f"Executing program (self.async_mode={self.async_mode}, self.silent={self.silent}, self._displaying_html={self._displaying_html})" |
| 609 | ) |
| 610 | |
| 611 | # if we are already displaying html, we need to yield to the event loop so the jupyter comm can initialize |
| 612 | if self._displaying_html: |
| 613 | await asyncio.sleep(0) |
| 614 | |
| 615 | # run the program and capture the output |
| 616 | try: |
| 617 | if self.llm is None: |
| 618 | await self._executor.run(None) |
| 619 | else: |
| 620 | with self.llm.session(asynchronous=True) as llm_session: |
| 621 | await self._executor.run(llm_session) |
| 622 | self._text = self._variables["@raw_prefix"] |
| 623 | |
| 624 | # if the execution failed, capture the exception so it can be re-raised |
| 625 | # in the main coroutine |
| 626 | except Exception as exception: |
| 627 | self._exception = exception |
| 628 | |
| 629 | finally: |
| 630 | # delete the executor and so mark the program as not executing |
| 631 | self._executor = None |
| 632 | |
| 633 | # update the display with the final output |
| 634 | self.update_display(last=True) |
| 635 | await self.update_display.done() |
| 636 | |
| 637 | # Handle memory updates here in case async_mode=True |
| 638 | if not from_agent and memory is not None: |
| 639 | all_text = extract_text(self.text) |
| 640 | for text_block in all_text: |
| 641 | for value in text_block: |
| 642 | memory.add_memory(prompt=value, llm_response=text_block[value]) |
| 643 | |
| 644 | # fire an event noting that execution is complete (this will release any await calls waiting on the program) |
| 645 | self._execute_complete.set() |
| 646 | |
| 647 | def __getitem__(self, key): |
| 648 | return self._variables[key] |
no test coverage detected