Run ChatGPT generated and saved code in Python

This is where we ask the question: Just because you can do it, should you do it? The likely answer to this question in this case is “No” because the following script uses the Python exec() function and it just assumes that ChatGPT is writing proper and ethical code. However, I because I am a good chat bot… 9_9 Here’s the code.
import asyncio
import os
import openai

from dotenv import load_dotenv
# Get environment variables
load_dotenv()

openai.api_type = "azure"
openai.api_base = os.getenv("OPENAI_API_BASE")
openai.api_version = "2022-12-01"
openai.api_key = os.getenv("OPENAI_API_KEY")

async def main():

response = openai.Completion.create(
engine="gpt-35-turbo-0301",
prompt=f"<|im_start|>system\nYou are a Python developer.\n<|im_end|>\n<|im_start|>user\nWrite a short Python script that prints the words Hello World! Don't explain the code. Don't put the code in a code block.\n\n<|im_end|>\n<|im_start|>assistant",
temperature=1,
max_tokens=4000,
top_p=0.5,
frequency_penalty=0,
presence_penalty=0,
stop=None)

# I haven't figured out why this is in the response yet
# So I just remove it from the string
code = str.replace(response['choices'][0]['text'],"<|im_end|>","")

# Print the code so you can see it before you run it.
print(code)

# Ask if you want to save the code to a file
save = input("Save the code to a file? (y/n): ")
if save == "n":
    return

#Save the code to a Python file
with open("hello_world.py", mode="w", encoding="utf-8") as hello:
     hello.write(code)

# Ask if you want to run the code
run = input("Run the code? (y/n): ")
if run == "n":
     return

# Run the code
with open("hello_world.py", mode="r", encoding="utf-8") as hello:
     code = hello.read()
exec(code)

if __name__ == '__main__':
     asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
     asyncio.run(main())
  P.S. In case you are wondering why I always use async, it is because I sometimes use Azure API long running operations and it is just easier for me to assume that even though a script may not use it now, when I iterate on it, it probably will. So I leave it in there.

Using ChatGPT to Create PlantUML Entity diagrams

More messing around with ChatGPT and automating documentation for APIs.  This time, instead of using a JSON file, I used a description of one of the entities in Azure Media Services.

I also installed the PlantUML plugin on this website. The rendered chart is below the code sample. It’s not exactly right, as an asset is really a storage container that extends the storage container with its own properties and methods.  It’s a start though.

Test 1

PROMPT

1. Determine the entities in the following text: An Asset is mapped to a blob container in the Azure Storage account and the files in the Asset are stored as block blobs in that container. Assets contain information about digital files stored in Azure Storage (including video, audio, images, thumbnail collections, text tracks, and closed caption files).

2. Describe the relationship of the entities in PlantUML.

RESULTS

@startuml
class Asset {
+info: String
}
class BlobContainer {
}
class AzureStorage {
}
class File {
}
class BlockBlob {
}
class Video {
}
class Audio {
}
class Image {
}
class ThumbnailCollection {
}
class TextTrack {
}
class ClosedCaptionFile {
}

Asset –> BlobContainer
BlobContainer –> AzureStorage
Asset –> File
File –> BlockBlob
Asset –> Video
Asset –> Audio
Asset –> Image
Asset –> ThumbnailCollection
Asset –> TextTrack
Asset –> ClosedCaptionFile

@enduml

PlantUML Syntax:<br />
@startuml<br />
class Asset {<br />
+info: String<br />
}<br />
class BlobContainer {<br />
}<br />
class AzureStorage {<br />
}<br />
class File {<br />
}<br />
class BlockBlob {<br />
}<br />
class Video {<br />
}<br />
class Audio {<br />
}<br />
class Image {<br />
}<br />
class ThumbnailCollection {<br />
}<br />
class TextTrack {<br />
}<br />
class ClosedCaptionFile {<br />
}</p>
<p>Asset –> BlobContainer<br />
BlobContainer –> AzureStorage<br />
Asset –> File<br />
File –> BlockBlob<br />
Asset –> Video<br />
Asset –> Audio<br />
Asset –> Image<br />
Asset –> ThumbnailCollection<br />
Asset –> TextTrack<br />
Asset –> ClosedCaptionFile</p>
<p>@enduml</p>
<p>

TEST 2

This time, I tried using a truncated version of the Asset JSON for Azure Media Services.

I got the following feedback from ChatGPT about the formatting of the JSON, which I appreciate.

“This diagram shows that “Asset” class has an attribute “AssetProperties” which is an object of type “AssetProperties”, which in turn has an attribute “properties” that is an object of “Properties”. And the “Properties” class has several attributes, including “assetId”, “created”, “lastModified”, “alternateId”, “description”, “container”, “storageAccountName”, and “storageEncryptionFormat”. It should be noted that the JSON provided has duplicate information and also the properties types are not well formatted to be used in PlantUML.”

PROMPT

Create a PlantUML class diagram using the following JSON:


{
"Asset" : {
"AssetProperties": {
"properties": {
"assetId": {
"type": "string",
"format": "uuid",
"description": "The Asset ID.",
"readOnly": true,
"x-nullable": false
},
"created": {
"type": "string",
"format": "date-time",
"description": "The creation date of the Asset.",
"readOnly": true,
"x-nullable": false
},
"lastModified": {
"type": "string",
"format": "date-time",
"description": "The last modified date of the Asset.",
"readOnly": true,
"x-nullable": false
},
"alternateId": {
"type": "string",
"description": "The alternate ID of the Asset."
},
"description": {
"type": "string",
"description": "The Asset description."
},
"container": {
"type": "string",
"description": "The name of the asset blob container."
},
"storageAccountName": {
"type": "string",
"description": "The name of the storage account."
},
"storageEncryptionFormat": {
"type": "string",
"enum": [
"None",
"MediaStorageClientEncryption"
],
"x-ms-enum": {
"name": "AssetStorageEncryptionFormat",
"values": [
{
"value": "None",
"description": "The Asset does not use client-side storage encryption (this is the only allowed value for new Assets)."
},
{
"value": "MediaStorageClientEncryption",
"description": "The Asset is encrypted with Media Services client-side encryption."
}
],
"modelAsString": true
},
"description": "The Asset encryption format. One of None or MediaStorageEncryption.",
"readOnly": true,
"x-nullable": false
}
},
"type": "object",
"description": "The Asset properties."
}
}
}

RESULTS

Here’s the resulting PlantUML:

@startuml
class Asset {
-AssetProperties : AssetProperties
}
class AssetProperties {
-properties : Properties
-type : object
-description : string
}
class Properties {
-assetId : string
-created : string
-lastModified : string
-alternateId : string
-description : string
-container : string
-storageAccountName : string
-storageEncryptionFormat : string
}
@enduml

Here’s the class diagram:

PlantUML Syntax:</p>
<p>@startuml<br />
class Asset {<br />
-AssetProperties : AssetProperties<br />
}<br />
class AssetProperties {<br />
-properties : Properties<br />
-type : object<br />
-description : string<br />
}<br />
class Properties {<br />
-assetId : string<br />
-created : string<br />
-lastModified : string<br />
-alternateId : string<br />
-description : string<br />
-container : string<br />
-storageAccountName : string<br />
-storageEncryptionFormat : string<br />
}<br />
@enduml</p>
<p>

Shaka Player

Azure Media Services streaming with the Shaka Player

Basic Azure Media Services Streaming with clear streaming only. The streaming endpoint is stopped, so no videos are going to stream.

Website recycle

I let this site rot for so long it smelled like New York city summer garbage during a sanitation strike. Cobbler’s children have no Birkenstocks.

Lately, the content work for Azure Media Services (AMS) has gotten pretty architecturey. I’m writing about how to monitor the service as well as planning Learn modules.

Planning for Learn modules has made me think about how to present information to the learner from the most basic application of the service to the most advanced. It isn’t YouTube.  It’s what a YouTube would be built on.

I’ve been messing around with adding Azure Active Directory to this site in order to experiment with restricting pages to certain user groups. That led to checking with my ISP about what in the Sam Hell happened to my SSL certificate and how do I wake it from deep, death-like slumber. (No, I don’t host this site on Azure.)

Half the day was spent trying to figure out how to get back into wp-admin while the DNS entries I screwed up corrected themselves, and deactivating the WordPress AAD plugin I am now calling HAL.

Things were upgraded including the GrassBlade LRS – and deleted – like old portfolio sample pages and posts I probably shouldn’t have published in the first place.

The About page got a smoke-free edit.

I’ll be keeping post notes on the experiments here.

Anyway, post #1 done.