AI Chat generates and executes code
Morph makes it easy to create AI applications that use LLM to generate code and perform execution results. In this tutorial, you will create an application that automatically generates Python code in response to user input and displays the results of the visualisation in real-time.
Prerequisites
Obtain the OpenAI API key in advance from the OpenAI dashboard and save it in an .env
file.
If you are using a cloud environment, set this as an environment variable in the “Data” tab.
Output
Tutorial
By specifying layout="side-by-side"
in the <LLM />
component, you can generate code based on user input and display the results in real-time.
Furthermore, the code generation and execution are handled within a Python function using the text_to_plotly
function, which covers everything from visualization to executing the code.
The text_to_plotly
function takes a user prompt and a DataFrame as arguments to generate and execute code that performs data analysis using Plotly, according to the user’s request.
Additionally, the side-by-side
layout of the <LLM />
component allows the HTML passed as the second argument to the create_chunk
function to be displayed next to the chat UI, enabling real-time display.