4. Prompty To Code¶
In the last section, we learned how to create and run a Prompty asset. In this section, we will learn how to convert a Prompty asset to Python code and run our first app.
1. Pre-requisites¶
To convert a Prompty asset to code and execute your first app, you need to have the following installed:
For our first app, we will focus on Azure Open AI and cover the following steps: - Create code from Prompty asset in VS Code - Install Prompty Package (Python library) - Configure code (use environment variables) - Execute code (from command line or VS Code)
2. Generate Code from Prompty Asset¶
Open the File Explorer
in Visual Studio Code open the Prompty asset we created earlier. Right click on the file name, and in the options, select add code
then select add Prompty code
. A new file will be created with the Python code generated from the Prompty asset.
☑ **You should see this `shakespeare.py` file created** (click to expand)
Python | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
3. Install Prompty Runtime¶
When you run the code generated, you will receive the error ModuleNotFoundError: No module named 'prompty'
. To resolve this, you need to install the Prompty runtime. The runtime supports different invokers that you can customize based on your needs. In this example, we are using Azure OpenAI API, therefore, we will need to install the azure
invoker. Run the following command in your terminal:
pip install prompty[azure]
The Prompty Package is a Python runtime that allows you to run your prompts in Python. It is available as a Python package and can be installed using pip
.
Depending on the type of prompt you are running, you may need to install additional dependencies. The runtime is designed to be extensible and can be customized to fit your needs.
4. Configure environment variables¶
In the code generated, we will need to load our environment variables to connect our Azure OpenAI API and generate an output. As we had already created the .env
file, you can load the environment variables in your code by adding the following code snippet at the top of your code:
Python | |
---|---|
1 2 |
|
5. Execute the code¶
You can now run the code by either clicking on the run
button on VS Code or executing the following command in your terminal:
python shakespeare.py
☑ **You should see this as part of the sample response from the python run** (click to expand)
JSON | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 |
|
6. How Python code works¶
- The
.py
code generated first imports the necessary modules and libraries.
☑ **Code importing Prompty, json and Prompty tracer** (click to expand)
Python | |
---|---|
1 2 3 4 5 6 7 |
|
- Next, we add observability using the tracer, allowing you to monitor the execution of the Prompty asset and log the output generated. The next section explains observability and how it works.
☑ **Code adding observability using the Prompty tracer** (click to expand)
Python | |
---|---|
1 2 3 4 5 6 |
|
- Next, we configure the environment variables to connect to the Azure OpenAI API. The code snippet below loads the environment variables from the
.env
file.
☑ **Code loading the environment variables** (click to expand)
Python | |
---|---|
1 2 3 4 |
|
- Next, we define a function that executes the Prompty asset. The function takes the question as an input and returns the response generated by the Prompty asset.
☑ **Function that executes the Prompty asset** (click to expand)
Python | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
- The code also includes a main execution block that loads the input from the prompty file and calls the function to execute the Prompty asset. The result is printed to the console.
☑ **Main execution code block** (click to expand)
Python | |
---|---|
1 2 3 4 5 6 7 |
|
7. Additional supported runtimes¶
The Prompty runtime supports additional runtimes, including frameworks such as LangChain, and Semantic Kernel. In the tutorials section, we will cover how to generate code from Prompty assets using these runtimes. (coming soon)