microsoft / TaskWeaver

A code-first agent framework for seamlessly planning and executing data analytics tasks.
https://microsoft.github.io/TaskWeaver/
MIT License
5.38k stars 689 forks source link

Run scripts deterministically in kernel #268

Closed twtester2 closed 7 months ago

twtester2 commented 8 months ago

Is your feature request related to a problem? Please describe. I have a Python script that I wrote. I want the script to run every time I start a session. Therefore, it would be nice to avoid having to go through the LLM, code interpreter, etc. because LLM can give stochastic results, and it's simpler and faster to just run the code.

Describe the solution you'd like I would like to be able to send a message to the kernel manager, such that a Python script in TaskWeaver/project/custom_code will run every time a session starts.

Describe alternatives you've considered I defined a new function in code executor:

def execute_my_custom_code(self, exec_id: str, code: str):
    return self.exec_client.execute_code(exec_id, code)

Then I tried using the code executor in TaskWeaver/taskweaver/session/session.py initialization as follows: self.code_executor.execute_restclient("1", "persistent_variable= 23")

This appears to work successfully, because I can make other calls to self.code_executor.execute_restclient that will see the variable. For example CLI output:

Initializing TaskWeaver
Calling first function
Done! ExecutionResult(execution_id='1', code=persistent_variable= 23', is_success=True, error='', output=23, stdout=[], stderr=[], log=[], artifact=[])
Calling second function
Done!  ExecutionResult(execution_id='1', code='persistent_variable + 1', is_success=True, error='', output=24, stdout=[], stderr=[], log=[], artifact=[])
Initialization complete

However, when I send another message using session.send_message(message), e.g. "print the value of the variable persistent_variable", I get the following error:

Calling task assignment:  Print the value of the variable persistent_variable
Response round:  Round:
- Query: Print the value of the variable persistent_variable
- State: finished
- Post Num:5
- Post List:
  * Post: User -> Planner:
    # Message: Print the value of the variable persistent_variable
    # Attachment List: []
  * Post: Planner -> CodeInterpreter:
    # Message: Please print the value of the variable persistent_variable
    # Attachment List: [INIT_PLAN: 1. Print the value of the variable persistent_variable, PLAN: 1. Print the value of the variable persistent_variable, CURRENT_PLAN_STEP: 1. Print the value of the variable persistent_variable] 
  * Post: CodeInterpreter -> CodeInterpreter:
    # Message: The following python code has been executed:
python
# Printing the value of the variable persistent_variable
print(persistent_variable)

The execution of the generated python code above has failed

During execution, the following messages were logged:
Traceback (most recent call last):

  Cell In[3], line 2
    print(persistent_variable)

NameError: name 'persistent_variable' is not defined

... etc

Additional context The purpose of starting the same function every time is to initialize a class in the kernel context so that it can be used every time by other plugins. I want the code to run deterministically without affecting other parts of TaskWeaver, e.g. the number of conversation rounds, etc.

liqul commented 8 months ago

We currently don't support this. If you need this feature, you can modify the code inside code interpreter adding the custom code before the generated code before sending it to the executor.

liqul commented 7 months ago

close inactive issues