Skip to content

3.5 Convert Prompty To Code

1. Add Code For Prompty

  1. First, let's copy over final versions of our Prompty file:

    1
    cp ../docs/workshop/src/1-build/chat-3.prompty .
    
  2. And copy over the final version of our input data:

    1
    cp ../docs/workshop/src/1-build/chat-3.json .
    

  3. In the Explorer pane, right-click on the new chat-3.prompty file and select "Add Code > Add Prompty Code". This creates a new Python file chat-3.py and opens it in VS Code.

  4. Run the default code by clicking the play icon. It will fail with an error that may look something like this, indicating a missing environment variable. Let's fix that, next.

    1
    ValueError: Variable AZURE_OPENAI_ENDPOINT not found in environment
    

2. Update Default Code

  1. Add the three lines below to the top of chat-3.py:

    chat-3.py
    1
    2
    3
    ## Load environment variables
    from dotenv import load_dotenv
    load_dotenv()
    

    These lines load environment varianbles from your .env file for use in the Python script.`

  2. Execute chat-3.py by clicking the "play" at the top-right of its VS Code window. You should now see a valid response being generated.


CONGRATULATIONS. You converted the Prompty asset to executable code!