paint-brush
Using Python in ChatGPT with AImarkdown Script - Part Oneby@robmccormack
359 reads
359 reads

Using Python in ChatGPT with AImarkdown Script - Part One

by Rob McCormackFebruary 22nd, 2024
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Ever struggled to reliably execute Python scripts within ChatGPT or wished you could customize the way Python output is displayed? This article changes all that.

People Mentioned

Mention Thumbnail
featured image - Using Python in ChatGPT with AImarkdown Script - Part One
Rob McCormack HackerNoon profile picture

ChatGPT 4 is required.


Powered by AImarkdown

In a Hurry?

Introduction

Ever struggled to reliably execute Python scripts within ChatGPT or wished you could customize the way Python output is displayed? This article changes all that. In it, we'll explore:

  1. This example demonstrates a dependable method for executing Python within ChatGPT, giving you full control over how the output is presented. We'll embed Python code for a simple calendar and display the results with Markdown formatting. With this technique, you can easily break free from ChatGPT's default text displays, creating visually appealing and customized output formats.
  2. A comprehensive guide to utilizing AImarkdown Script — an MIT Licensed scripting language for ChatGPT that I'm currently developing.
  3. The ease with which you can accomplish this.

Screenshot from iPhone

Screenshot ChatGPT Calendar


What is AImarkdown Script?

AImarkdown Script is a user-friendly way to create dynamic interactions with conversational AI platforms like ChatGPT. It empowers you to script interactions, control conversation flow, and tailor how the AI behaves and responds. Think of it as a versatile blend:

  • For programmers: Provides the flexibility of a scripting language, including the ability to embed Python scripts.
  • For everyone else: Offers a straightforward way to customize AI experiences without being a coder. See the example with hardly any programming code on Github.

AImarkdown Script seamlessly combines the familiarity of YAML and Markdown with natural language instructions. Unlike strict programming languages, even your comments actively guide the AI, lending it unmatched power and adaptability.

NOTE: AImarkdown Script is under development and is currently not natively supported by ChatGPT 4. To use it effectively, be prepared to provide clear instructions and explanations to ChatGPT in your scripts. Consider having ChatGPT generate YAML instructions for easier use. Expect several iterations to fine-tune your AImarkdown apps. Future native support would greatly enhance AImarkdown Script.

Reasons Why it is better to run Python inside of AImarkdown

First, it is obvious that with AImarkdown Script, the output to the user is under your control.

Secondly, it appears to me that Python scripts embedded in AImarkdown are more reliable and less prone to errors or hang-ups.

Let's Hear from ChatGPT Itself

Actual ChatGPT session on Feb 15, 2024:

User:

You can see how I embed the Python in a YAML block in this example. When I compare this to just asking AI to run Python, it seems to hang up once in a while. I have better luck when running Python inside of an AI markdown file. Is this possible, or my imagination?

ChatGPT:

Your observation could have a practical basis, especially considering the structured approach you're using by embedding Python code within an AImarkdown document. Here are a few reasons why this method might offer you better consistency:

  • Structured Execution Context: By embedding Python within an AImarkdown document, you're providing a clear, structured context for code execution. This structure might help in managing the execution flow better, as the AI has a clear indication of when and how to execute the code based on the document's configuration.

  • Controlled Execution Environment: The YAML block defines a specific execution environment for the Python code. This controlled environment could potentially reduce the occurrence of errors or issues related to variable scope, dependencies, or execution timing, leading to a smoother experience.

  • Focused Resource Allocation: When the Python execution is embedded within a document following specific instructions, it's possible that the underlying system allocates resources more efficiently, focusing on executing the code block as defined without the overhead of interpreting freeform text inputs.

  • Optimized Parsing and Execution: The AI's interpretation of your structured AImarkdown document might be more optimized compared to processing freeform Python code requests. The structured format allows the AI to parse and execute the code more reliably, as it follows a predefined pattern.


However, it's also worth noting that the success and reliability of code execution can be influenced by various factors, including the complexity of the code, the current load on the system, and the specific mechanisms the AI uses to interpret and execute Python code. Your structured approach via AImarkdown likely helps mitigate some of these variables, leading to a more consistent experience.


In essence, while some parts of your observation might be subjective, there's a logical explanation for why the AImarkdown approach could yield better results for you. It harnesses the power of structured data and clear execution directives, potentially leading to more reliable and efficient processing by the AI.

Getting Out of the Classical Programming Mindset

The most exciting aspect of using AImarkdown Script, for me, lies in harnessing the power of AI-driven text generation. It allows me to achieve this with minimal, structured code.

I have noticed that if the YAML section, or even the Python code block, may have errors (like level of indent), ChatGPT often correctly interprets and corrects them. This error-tolerant feature is not found in other languages. In classical programming, the best you could hope for is an "informative" error narrowing down where you went wrong. That being said, it still is better to write valid code, that is why I wrote an AImarkdown Validator: https://validator.aimarkdown.org/


In the classical programming world, functionality has rigid limits. If it's not explicitly coded, it simply doesn't exist. User responses have to be meticulously anticipated to provide meaningful interaction. In a calendar type of application, a user might ask, "What day of the week was May 8th, 1960?". If such behavior was not foreseen by the developer, the user would be out of luck and, as usual, restricted to the scope of the program. But by using AImarkdown and relying on the ChatGPT capabilities, the user will seamlessly get an answer. This powerful capability might do away, at least for a large part, with the age-honored response: "Good idea, I'll add that feature to the next version.”


This calendar custom ChatGPT application does have a considerable amount of structure and is reminiscent of classical programming, but to illustrate the point above, have a look at my article: Build a ChatGPT 4 Blackjack Game with AI Coaching (49 Lines of Code) published on dev.to Link

My blackjack article exemplifies how AImarkdown Script shifts that paradigm, delegating significant parts of the logic and experience to ChatGPT. The AImarkdown script resembles more of a conversational recipe than a program.


Imagine building apps where you provide the core structure, intentionally leaving space for the AI to address unpredictable user interactions. This truly is a brave new world of application development!

Installation Guide

It is easy to install the script into ChatGPT. Here are the three options:

  1. ChatGPT 4: copy and paste the source code below into any Chat session.
  2. ChatGPT Builder: Type AI to follow these instructions: into Configure|Instructions and then copy and paste the source code below that.

AImarkdown Script Source Code

Complete Source code

MIT License

Exploring Even More AImarkdown Script Action!


Part Two: Visualizing Power with Matplotlib

Get ready for more Python-powered possibilities! In Part Two, we'll dive into generating dynamic visuals using Matplotlib and AImarkdown Script. Challenges may lurk, but overcoming them leads to truly compelling results.

Sneak Peek: Check out how I've already embedded an output graph directly into a Markdown section!

Part Three: Unlocking ChatGPT's Hidden Toolbox

Discover the secret arsenal of libraries accessible within ChatGPT. The Appendix below lists available libraries – imagine what we'll build together!

Conclusion

You've seen how AImarkdown Script tames Python execution within ChatGPT, ensuring reliability and presentation mastery. This series sets the stage for pushing those boundaries further – think interactive charts, data analysis, and beyond.

Stay tuned! Your ChatGPT experience is about to reach new heights.

AImarkdown Github Repository


Appendix - ChatGPT session

User:

Can you give me the Python Libraries that are available as import that I can use within ChatGPT

ChatGPT:

Within ChatGPT, especially in environments like this one, you have access to a standard set of Python libraries that are generally included with Python's standard library, as well as a selection of third-party libraries that have been specifically included for their utility in data analysis, machine learning, visualization, and general utility tasks. Here's an overview of some categories and notable libraries you might find available:

Standard Python Libraries:

  • math: Provides access to mathematical functions.
  • datetime: For manipulating dates and times.
  • re: Regular expression operations for string searching and manipulation.
  • os: Operating system interfaces, including file and directory processing.
  • sys: Access to some variables used or maintained by the Python interpreter.
  • json: For parsing and generating JSON data.
  • urllib: For opening and reading URLs (mostly used in environments with internet access).

Data Analysis and Machine Learning:

  • numpy: Fundamental package for numerical computations.
  • pandas: Data structures and data analysis tools.
  • matplotlib: Basic plotting library.
  • scipy: More advanced scientific computing, including optimization, regression, interpolation, etc.
  • sklearn (or scikit-learn): Machine learning library for Python.

Visualization:

  • matplotlib.pyplot: A collection of command style functions that make matplotlib work like MATLAB.
  • seaborn: Statistical data visualization library based on matplotlib.

Utility and Extended Functionality:

  • requests: HTTP library for Python for making HTTP requests in environments where internet access is permitted.
  • BeautifulSoup (from bs4): Library for web scraping purposes.
  • Pillow (PIL Fork): Python Imaging Library adds image processing capabilities.

Note:

The availability of these libraries can depend on the specific environment or platform version you're using. In some versions of ChatGPT, especially those running in more restricted environments, not all libraries or their full functionalities are available, particularly those that require internet access or interact with the operating system at a lower level (e.g., requests, os).

To check if a specific library is available and what its capabilities are in your environment, you can attempt to import it and use its functions. For the most current and detailed information, always refer to the official documentation or help resources provided with your specific ChatGPT or Python environment.


Image Credit: AI-generated


Also published here.