Trying to execute script from text inside a streamlit chat

Hi! I am currently trying to execute a code that the openAI GPT LLM is writing for me, then executing this code and lastly using a variable in that script named answer as the chat response, unfortunately it is not working using exec() as i did in a regular python script, and i am having difficulty finding an alternative for it, any way in making this functionality another way in the streamlit app?