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.
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:
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.
shakespeare.py
file created (click to expand) import json
import prompty
# to use the azure invoker make
# sure to install prompty like this:
# pip install prompty[azure]
import prompty.azure
from prompty.tracer import trace, Tracer, console_tracer, PromptyTracer
# add console and json tracer:
# this only has to be done once
# at application startup
Tracer.add("console", console_tracer)
json_tracer = PromptyTracer()
Tracer.add("PromptyTracer", json_tracer.tracer)
# if your prompty file uses environment variables make
# sure they are loaded properly for correct execution
@trace
def run(
question: any
) -> str:
# execute the prompty file
result = prompty.execute(
"shakespeare.prompty",
inputs={
"question": question
}
)
return result
if __name__ == "__main__":
json_input = '''{
"question": "Please write a short text inviting friends to a Game Night."
}'''
args = json.loads(json_input)
result = run(**args)
print(result)
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.
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:
from dotenv import load_dotenv
load_dotenv()
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
Ending execute
result:
"Hark, dear friends! \n\nWith mirth and cheer, I extend a joyous summons unto thee for a night of merry games and friendly rivalry. Let us gather under yon stars at my abode this coming eve, and partake in laughter and revelry most grand. Come, let the spirit of camaraderie guide thy steps to my door, as we engage in diversions that shall bind our hearts in jocund fellowship.\n\nPray, grant me the boon of thy presence. The hour of merriment awaiteth us!\n\nFaithfully thine, \n[Thy Name]"
Ending run
Hark, dear friends!
With mirth and cheer, I extend a joyous summons unto thee for a night of merry games and friendly rivalry. Let us gather under yon stars at my abode this coming eve, and partake in laughter and revelry most grand. Come, let the spirit of camaraderie guide thy steps to my door, as we engage in diversions that shall bind our hearts in jocund fellowship.
Pray, grant me the boon of thy presence. The hour of merriment awaiteth us!
Faithfully thine,
[Thy Name]
.py
code generated first imports the necessary modules and libraries.import json
import prompty
# to use the azure invoker make
# sure to install prompty like this:
# pip install prompty[azure]
import prompty.azure
from prompty.tracer import trace, Tracer, console_tracer, PromptyTracer
# add console and json tracer:
# this only has to be done once
# at application startup
Tracer.add("console", console_tracer)
json_tracer = PromptyTracer()
Tracer.add("PromptyTracer", json_tracer.tracer)
.env
file.# if your prompty file uses environment variables make
# sure they are loaded properly for correct execution
from dotenv import load_dotenv
load_dotenv()
@trace
def run(
question: any
) -> str:
# execute the prompty file
result = prompty.execute(
"shakespeare.prompty",
inputs={
"question": question
}
)
return result
if __name__ == "__main__":
json_input = '''{
"question": "Please write a short text inviting friends to a Game Night."
}'''
args = json.loads(json_input)
result = run(**args)
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)
Want to Contribute To the Project? - Updated Guidance Coming Soon.