Let’s Talk About AI Chat
OK, so there really isn’t much to explain if you have been on social media in the past five years. Chat bots such as ChatGPT have grown to become extremely popular, but for good reason. They have given the internet access to a vast amount of information, which arguably saves time – right? For myself, the answer is “yes”.
AI chat bots are definitely handy with the correct application, which is because the amount of information they are being trained with is vast, and also begs the question – what do AI chat bots know? What we are questioning in this post is rather, what can they DO?
READ MORE: Building Dynamic Code With Spreadsheets
The Code
If you are reading this, you possibly dabble in writing code. Here is where a chat bot shines for my colleagues and me. They can totally transform the way you write your code! Let’s take a look at the follow tutorial code written in rust.
use std::fs;
use std::net::TcpListener;
use std::net::TcpStream;
use std::io::prelude::*;
use std::thread;
use std::time::Duration;
use server::ThreadPool;
fn main() {
let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
let pool = ThreadPool::new(4);
for stream in listener.incoming() {
let stream = stream.unwrap();
pool.execute(|| {
handle_connection(stream);
});
}
}
fn handle_connection(mut stream: TcpStream) {
let mut buffer = [0; 1024];
stream.read(&mut buffer).unwrap();
let get = b"GET / HTTP/1.1\r\n";
let sleep = b"GET /sleep HTTP/1.1\r\n";
let (status_line, filename) =
if buffer.starts_with(get) {
("HTTP/1.1 200 OK", "index.html")
} else if buffer.starts_with(sleep) {
thread::sleep(Duration::from_secs(5));
("HTTP/1.1 200 OK", "index.html")
} else {
("HTTP/1.1 404 NOT FOUND", "404.html")
};
let contents = fs::read_to_string(filename).unwrap();
let response = format!(
"{}\r\nContent-Length: {}\r\n\r\n{}",
status_line,
contents.len(),
contents
);
stream.write(response.as_bytes()).unwrap();
stream.flush().unwrap();
}
You can see that this code has no comments at all, which is not really best practice in my book. When I pass this over to my team, it will need comments to let everyone know what was performed, and I cannot assume that they will understand it, even if they do. Let’s ask the chat bot to clean this up for me.

The Comments
Voilà! My chat bot knows what to do, and will do a fine job of cleaning this up for me. Below are my results.
// Import necessary modules:
// - fs for file system operations (like reading HTML files)
// - TcpListener and TcpStream for network communication
// - prelude::* to get Read and Write traits for I/O operations
// - thread and time for handling delays
use std::fs;
use std::net::TcpListener;
use std::net::TcpStream;
use std::io::prelude::*;
use std::thread;
use std::time::Duration;
// Import a custom ThreadPool from the `server` module
use server::ThreadPool;
fn main() {
// Bind the TCP listener to localhost on port 8080
// This panics if the port is already in use or invalid
let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
// Create a thread pool with 4 worker threads to handle requests concurrently
let pool = ThreadPool::new(4);
// Loop over incoming TCP connections
for stream in listener.incoming() {
// Unwrap the incoming stream (panic on error)
let stream = stream.unwrap();
// Submit the stream to the thread pool for processing
pool.execute(|| {
handle_connection(stream);
});
}
}
// Function to handle a single client connection
fn handle_connection(mut stream: TcpStream) {
// Create a buffer to hold the incoming HTTP request
let mut buffer = [0; 1024];
// Read the request from the stream into the buffer
stream.read(&mut buffer).unwrap();
// Byte patterns to match specific GET requests
let get = b"GET / HTTP/1.1\r\n";
let sleep = b"GET /sleep HTTP/1.1\r\n";
// Determine the HTTP response based on the request path
let (status_line, filename) =
if buffer.starts_with(get) {
// Respond with index.html for GET /
("HTTP/1.1 200 OK", "index.html")
} else if buffer.starts_with(sleep) {
// Simulate a slow response for GET /sleep by sleeping for 5 seconds
thread::sleep(Duration::from_secs(5));
("HTTP/1.1 200 OK", "index.html")
} else {
// Respond with 404.html for all other paths
("HTTP/1.1 404 NOT FOUND", "404.html")
};
// Read the contents of the appropriate file to serve
let contents = fs::read_to_string(filename).unwrap();
// Build a full HTTP response with headers and body
let response = format!(
"{}\r\nContent-Length: {}\r\n\r\n{}",
status_line,
contents.len(), // HTTP/1.1 requires Content-Length header
contents
);
// Send the response to the client
stream.write(response.as_bytes()).unwrap();
// Flush the stream to ensure all data is sent
stream.flush().unwrap();
}
Conclusion
To wrap up, you should take advantage of the abilities AI chat bots like ChatGPT have to offer. This will save you a large amount of time and typos, while using best practices in your programming.
What are your thoughts on chat bots and their uses? Leave a comment!
Leave a Reply