Execute this program with the given variable values and return a new executed/executing program. Note that the returned program might not be fully executed if `stream=True`. When streaming you need to use the python `await` keyword if you want to ensure the program is finished (note
(self, from_agent=False, **kwargs)
| 358 | return self._stream_run_async() |
| 359 | |
| 360 | def __call__(self, from_agent=False, **kwargs): |
| 361 | """Execute this program with the given variable values and return a new executed/executing program. |
| 362 | |
| 363 | Note that the returned program might not be fully executed if `stream=True`. When streaming you need to |
| 364 | use the python `await` keyword if you want to ensure the program is finished (note that is different than |
| 365 | the `await` engine langauge command, which will cause the program to stop execution at that point). |
| 366 | """ |
| 367 | |
| 368 | # merge the given kwargs with the current variables |
| 369 | kwargs = { |
| 370 | **{ |
| 371 | "async_mode": self.async_mode, |
| 372 | "stream": self.stream, |
| 373 | "silent": self.silent, |
| 374 | "cache_seed": self.cache_seed, |
| 375 | "caching": self.caching, |
| 376 | "logprobs": self.logprobs, |
| 377 | "await_missing": self.await_missing, |
| 378 | "log": self.log.copy() if hasattr(self.log, "copy") else self.log, |
| 379 | "llm": self.llm, |
| 380 | }, |
| 381 | **kwargs, |
| 382 | } |
| 383 | |
| 384 | if self.memory is not None: |
| 385 | if not from_agent: |
| 386 | self.ConversationHistory = self.memory.get_memory() |
| 387 | kwargs["ConversationHistory"] = self.ConversationHistory |
| 388 | |
| 389 | log.debug(f"in __call__ with kwargs: {kwargs}") |
| 390 | |
| 391 | # create a new program object that we will execute in-place |
| 392 | new_program = Program( |
| 393 | text=self.marked_text, |
| 394 | # copy the (non-function) variables so that we don't modify the original program during execution |
| 395 | # TODO: what about functions? should we copy them too? |
| 396 | **{ |
| 397 | **{ |
| 398 | k: v if callable(v) else copy.deepcopy(v) |
| 399 | for k, v in self._variables.items() |
| 400 | }, |
| 401 | **kwargs, |
| 402 | }, |
| 403 | ) |
| 404 | |
| 405 | # create an executor for the new program (this also marks the program as executing) |
| 406 | new_program._executor = ProgramExecutor(new_program) |
| 407 | |
| 408 | # if we are in async mode, schedule the program in the current event loop |
| 409 | if new_program.async_mode: |
| 410 | loop = asyncio.get_event_loop() |
| 411 | assert ( |
| 412 | loop.is_running() |
| 413 | ), "The program is in async mode but there is no asyncio event loop running! Start one and try again." |
| 414 | update_task = loop.create_task( |
| 415 | new_program.update_display.run() |
| 416 | ) # start the display updater |
| 417 | execute_task = loop.create_task( |
nothing calls this directly
no test coverage detected