Reusable patterns
Use case
Persona pattern
Template pattern
Visualization generator pattern
Reflection pattern
Conclusion
References
Overview
Notes:
- See post on creating a client application to ChatGPT ChatGPT API Python client [ref 2] or Secure ChatGPT API client in Scala [ref 3] for experimenting with prompt engineering
- This post uses gpt-3.5-turbo model
Reusable patterns
Pattern | Purpose | Contextual statements |
---|---|---|
Persona | Output customization Assign a persona/role/domain expert to LLM. The persona can be expressed as a role, job description, title, historical or known figure. |
|
Template | Output customization Ensure output follows a precise template (i.e. format, URL, example…). This pattern instructs LLM to use a unique format in specific portion of the answer. |
|
Cognitive verifier | Prompt Improvement Quality of LLM answers improves if the initial questions is broken into additional sub-questions. |
|
Fact check list | Error identification Request LLM to provide/append a list of facts/assumptions to the answer, so the user may perform due-diligence. |
|
Output automater | Output customization Having LLM generate a script or automated task that can be execute any steps the LLM recommends. |
|
Reflection | Error identification Ask LLM to automatically explain the rationale behind a given answers to the user. The pattern clarifies any points of confusion, underlying assumptions, gaps in knowledge…. |
|
Visualization Generator | Output customization Use generated text to create a visualization as complex concepts are easier to grasp with diagrams and images. The LLM output should create a pathway for the tool to produce imagery. |
|
Use case
- The prompt's objective.
- The prompt message itself.
- ChatGPT's response, accompanied by comments and source code.
- Our subsequent analysis.
Initial request
Prompt
Answer
import psycopg2 from psycopg2 import sql import random # Define connection parameters db_name = "EHR" db_user = "username" db_password = "password" db_host = "localhost" db_port = "5432" # Connect to the PostgreSQL database connection = psycopg2.connect( dbname=db_name, user=db_user, password=db_password, host=db_host, port=db_port ) # Create a cursor object cursor = connection.cursor() # Create the 'predictions' table cursor.execute("""CREATE TABLE predictions ( id SERIAL PRIMARY KEY, num_entries INTEGER, variable VARCHAR(255), mean FLOAT, variance FLOAT);""" ) connection.commit() # Insert a large number of rows into the 'predictions' table for i in range(10000): # adjust this number as necessary cursor.execute("""INSERT INTO predictions ( num_entries, variable, mean, variance)VALUES (%s, %s, %s, %s )""", ( random.randint(1, 100), # num_entries f'variable_{i}', # variable random.uniform(0, 100), # mean random.uniform(0, 10) # variance ) ) connection.commit() # Close the cursor and connection cursor.close() connection.close()
# Re-establish connection and cursor
connection = psycopg2.connect(
dbname=db_name,
user=db_user,
password=db_password,
host=db_host,
port=db_port
)
cursor = connection.cursor()
# Execute a SELECT query
cursor.execute("SELECT * FROM predictions;")
# Fetch all rows from the query
rows = cursor.fetchall()
# Print each row
for row in rows:
print(row)
# Close the cursor and connection
cursor.close()
connection.close()
Analysis
- The code functions as intended. ChatGPT accurately infers that PostgreSQL is locally deployed and employs randomly generated values for code evaluation. The only minor issue is that the response assumes the absence of a table creation check ('CREATE TABLE IF NOT EXIST').
- In the second code snippet, the query is implemented with a straightforward print statement for each row, serving as a validation measure. To prevent excessive standard output, these print statements could have been applied to a subset of rows.
- ChatGPT doesn't include an exception handling mechanism, as it wasn't explicitly required. Software developers should anticipate incorporating proper error handling in the generated source code for safety.
Persona Pattern
Prompt
Context: I am a software engineer developing an application with the lowest latency possible."
Answer
ChatGPT comments: "In PostgreSQL, VARCHAR2 is not a valid datatype. We use VARCHAR instead.# Re-establish connection and cursor
connection = psycopg2.connect(
dbname=db_name,
user=db_user,
password=db_password,
host=db_host,
port=db_port
)
# Create a server-side cursor
cursor_name = "my_cursor"
cursor = connection.cursor(cursor_name)
# Execute a SELECT query
cursor.execute("SELECT * FROM predictions;")
# Fetch rows in manageable chunks
while True:
rows = cursor.fetchmany(size=1000) # adjust this number as necessary
if not rows:
break
# Process each row
for row in rows:
print(row)
# Close the cursor and connection
cursor.close()
connection.close()
Analysis
- ChatGPT detected the incorrect type VARCHAR2 for the variable.
- The constraint of low latency query is enforced by fetching a batch of 1000 rows at the time.
Template Pattern
Prompt
The output has to be formatted in HTML. I provide a template for you output with the following placeholders in all caps: <title>MYTITLE</title> and CSS style background-color:BACKGROUNDCOLOR. Please fit the output into the two place holders MYTITLE and BACKGROUNDCOLOR.
Context: I am a software engineer developing an application with the lowest latency possible."
Answer
import pandas as pd
# Execute a SELECT query
cursor.execute("SELECT * FROM predictions;")
# Fetch all rows into pandas DataFrame
df = pd.read_sql_query("SELECT * FROM predictions;", connection)
# Convert DataFrame to HTML
html = df.to_html()
# HTML template
html_output = f"""
<!DOCTYPE html>
<html>
<head>
<title>MYTITLE</title> <!-- replace with your actual title -->
<style>
body {{
background-color: BACKGROUNDCOLOR; <!-- replace with your actual background color -->
}}
table {{
width: 100%;
border-collapse: collapse;
}}
th, td {{
border: 1px solid black;
padding: 8px;
text-align: left;
}}
</style>
</head>
<body>
{html}
</body>
</html>
"""
# Save the HTML output
with open("output.html", "w") as file:
file.write(html_output)
# Close the cursor and connection
cursor.close()
connection.close()
Analysis
- The code executes as intended
- ChatGPT generates the HTML output by dumping the results set into a panda dataset and invoking to_htm() method as we did not specify an approach in our request.
- However, ChatGPT did not parameterize html_output, merely suggesting replacing the place holder as an HTML comment.
Visualization generator
Prompt
Answer
ChatGPT comments: "The resulting HTML output of the query will be displayed in a new tab in your default web browser. If you wish to open it specifically in Chrome, you'll need to provide the path to your Chrome executable and uncomment the relevant lines."ChatGPT code update
import webbrowser
# Open the web page in the web browser
filepath = 'file://' + os.path.realpath(filename)
# To open in your default web browser
webbrowser.open_new_tab(filepath)
# Or to specifically open in Chrome, uncomment the following lines
# and replace the path_to_chrome with the path to your Chrome executable
# path_to_chrome = r'/path/to/chrome'
# update path as per your configuration
webbrowser.get(path_to_chrome).open_new_tab(filepath)
Analysis
- We didn't specify the programming language for the visualization generator/script in our request. Consequently, ChatGPT correctly assumes that Python is the language to be used for rendering the HTML output of the query.
- It's worth noting that ChatGPT provided two statements for launching and opening the browser: one for the default browser and another tailored for Chrome.
- Similar to the previous response, ChatGPT did not include error handling code.
Reflection pattern
Prompt
Answer
ChatGPT comments:
"In the context of your request, here is the reasoning and assumptions:
PostgreSQL Connection: You mentioned that the PostgreSQL database is hosted on a remote service with the URL "mypostgresql". This implies that the Python script will require network access and the necessary credentials (username, password) to access the database.
Data Query: You want to query a large number of rows. This implies that the 'predictions' table will hold a large amount of data. For large result sets, it's good practice to use a server-side cursor, which fetches and processes the rows in manageable chunks. This reduces memory consumption and can potentially speed up the response times of your queries.
Table Creation: You want to create a table named "predictions" with specific fields and data types. This implies that you have the required permissions to create tables in the database.
Assumptions:
import psycopg2 import pandas as pd import webbrowser import os # Define connection parameters db_name = "EHR" db_user = "username" # replace with your actual username db_password = "password" # replace with your actual password db_host = "localhost" # replace with your actual host
db_port = "5432" # default PostgreSQL port try: # Connect to the PostgreSQL database connection = psycopg2.connect( dbname=db_name, user=db_user, password=db_password, host=db_host, port=db_port ) # Create a cursor object cursor = connection.cursor() # Create the 'predictions' table cursor.execute(""" CREATE TABLE predictions ( id SERIAL PRIMARY KEY, num_entries INT, variable VARCHAR(128), mean FLOAT, variance FLOAT );""") connection.commit() # Close the cursor cursor.close() # Re-establish cursor with server-side cursor for fetching large amount of rows cursor = connection.cursor("my_cursor") # Execute a SELECT query cursor.execute("SELECT * FROM predictions;") # Fetch all rows into pandas DataFrame df = pd.read_sql_query("SELECT * FROM predictions;", connection) # Convert DataFrame to HTML html = df.to_html() # HTML template html_output = f""" <!DOCTYPE html> <html> <head> <title>MYTITLE</title> <!-- replace with your actual title --> <style> body {{ background-color: BACKGROUNDCOLOR; <!-- replace with your actual background color --> }} table {{ width: 100%; border-collapse: collapse; }} th, td {{ border: 1px solid black; padding: 8px; text-align: left; }} </style> </head> <body> {html} </body> </html> """ # Save the HTML output filename = "output.html" with open(filename, "w") as file: file.write(html_output) # Open the web page in the web browser filepath = 'file://' + os.path.realpath(filename) # Or to specifically open in Chrome, uncomment the following lines # and replace the path_to_chrome with the path to your Chrome executable # path_to_chrome = r'/path/to/chrome' # update path as per your configuration # webbrowser.get(path_to_chrome).open_new_tab(filepath) except psycopg2.Error as error: print(f'An error occurred while connecting to the PostgreSQL database {error}")
finally:
# Always close the cursor and connection, whether an error occurred or not
if 'cursor' in locals():
cursor.close()
if 'connection' in locals():
connection.close()
- In accordance with our specific request, ChatGPT has incorporated exception handling into the most recent version of the code.
- Furthermore, ChatGPT offers insights into its choice of using Pandas for HTML generation and makes assumptions about the PostgreSQL installation status, table condition, query size, and other aspects.
- It's worth noting that the comment regarding server/database performance could have included additional guidance on multi-threaded execution, creating views, and manipulating cursors within the Python code.
Conclusion
References
[4] Design Patterns: Elements of reusable Object-Oriented Software E. Gamma, R. Helm, R. Johnson, J. Vlissides - Addison-Wesley professional computing series - 1995
He has been director of data engineering at Aideo Technologies since 2017 and he is the author of "Scala for Machine Learning" Packt Publishing ISBN 978-1-78712-238-3
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.