Tuesday, May 30, 2023

Copy MongoDB indexes from one host database to another

Do it in one environment. Use it in all the rest:
#!/bin/bash -e
# copyIndexes.sh
# @description Copies indexes from one mongodb database to another
# @author Nestor Urquiza
# @date 20230530
USAGE='Usage: copyIndexes.sh --user1 <user1> --host1 <host1> --db1 <db1> --user2 <user2> --host2 <host2> --db2 <db2>'
# options
while [[ $# -gt 1 ]]; do
key="$1"
case $key in
--user1)
user1="$2"
shift
;;
--host1)
host1="$2"
shift
;;
--db1)
db1="$2"
shift
;;
--user2)
user2="$2"
shift
;;
--host2)
host2="$2"
shift
;;
--db2)
db2="$2"
shift
;;
*)
# unknown
;;
esac
shift
done
if [[ "$user1" == "" || "$host1" == "" || "$db1" == "" || "$user2" == "" || "$host2" == "" || "$db2" == "" ]]; then
echo $USAGE
exit 1
fi
# Retrieve indexes from host1
echo "Copying indexes *FROM* $host1/$db1 using user $user1 ..."
indexes=$(mongo "$host1/$db1" -u $user1 -p --quiet --eval 'JSON.stringify(db.getCollectionNames().map(function(collectionName) { return {collection: collectionName, indexes: db.getCollection(collectionName).getIndexes()}; }))')
# Connect to host2 and import indexes
echo "Copying indexes *TO* $host2/$db2 using user $user2 ..."
mongo "$host2/$db2" -u $user2 -p --quiet --eval '
var indexData = '$indexes';
indexData.forEach(function(indexInfo) {
var collectionName = indexInfo.collection;
var indexes = indexInfo.indexes;
indexes.forEach(function(index) {
db.getCollection(collectionName).createIndex(index.key, index.options);
});
});
'
echo "Indexes have been copied successfully from $host1/$db1 to $host2/$db2."
view raw copyIndexes.sh hosted with ❤ by GitHub

Friday, May 12, 2023

A CLI script to interact with ChatGPT via API


I built this script to find out how much faster would I be translating from the command line interface (CLI) using ChatGPT API versus translating from its web interface, and I was not disappointed. My new poetry book is coming up in English as well as in Spanish at once. Writing in both languages at the same time is test driven delivery (#TDD) because at the time I am done creating, I am also done with reviewed everything, and therefore it is ready to be shipped to my Editor. Exiting times in front of us with #LLMs. These engines are our best pals when it comes to productivity.
#!/usr/bin/env python3
#
# clichatgpt.py
#
# A CLI script to interact with ChatGPT via API
#
# Nestor Urquiza 20230512
#
import sys
import openai
import os
from sty import fg, rs
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
if OPENAI_API_KEY is None:
print(fg.red + 'ERROR: You must set the OPENAI_API_KEY environment variable.' + fg.rs)
exit(1)
openai.api_key = OPENAI_API_KEY
one_line_content = ''
msgs = []
try:
print(fg.blue + 'Type a session prompt or press enter to skip (will be sent with your next prompts)' + fg.rs)
session_prompt = input()
print(fg.blue + 'Type your prompt and press enter on an empty line to get the response from ChatGPT:' + fg.rs)
for line in sys.stdin:
one_line_content += line
if line == '\n':
if session_prompt != '':
msgs.append({"role": "user", "content": session_prompt})
msgs.append({"role": "user", "content": one_line_content})
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages = msgs
)
print(response.choices[0].message.content + '\n')
one_line_content = ''
msgs = []
except KeyboardInterrupt:
print('\n')
exit
view raw clichatgpt.py hosted with ❤ by GitHub

Saturday, April 29, 2023

ChatGPT does not get it yet - Learning is not about repeating, but instead about [critical] thinking.


I translated the two books that I have published so far from Spanish to English using #ChatGPT, and here is what I learned:

1. Agreed with https://arxiv.org/pdf/2301.08745.pdf that ChatGPT is a good translator for spoken language.
2. 37 problems in a total of 31,459 words. Not bad at all.
3. 9 of the problems were also present in DeepL. The rest were not.
4. ChatGPT can be instructed to keep the format, and with that convert dialogs in Spanish (which use quotation dashes) to dialogs in English (which use quotes).
5. However, DeepL autosuggestion feature is a productivity boost and with just 9 errors shown I think it is still a more effective way to translate in cases where respecting format is not a must-have.
6. If text is heavily formatted like for example XML variants like HTML, then ChatGPT is unable to keep all escaped XML entities like '&lt;'. Instead, it randomly keeps them or shows them as literals, like in this case '<'. Prompting more gets the engine in repeating mode, demonstrating that there is no creativity and no critical thinking in ChatGPT. This to me is a demonstration that the ultimate question scientists have to answer is *how* these models actually "learn" so that we can predict how they will "behave". Learning is not about repeating, but instead about [critical] thinking. We know why and how an atomic bomb works, but we don't fully understand why and how AI works.

I will report back when I finish my upcoming poetry book, which I will only publish once I have the final material in the two languages because ChatGPT has taught me a lesson: In reviewing multilingual content, an author finds better ways to say the same even in their own native/mother tongue.

Thursday, April 27, 2023

Wishful thinking versus scientific evidence


Plotted you see all the numbers for the first draw (ball) of all Megamillions ever played in Europe since Fri Feb 13, 2004 to Tue Apr 25, 2023. Do you see any pattern there? No, you can't. The reason is that these numbers are all random.

If there would be a pattern, then a #LLM like #ChatGPT would be able to find it, but there is none. Yet you get news like "Man makes headlines after winning the lottery with ChatGPT" with statements like "The winning amount was small, but the approach was impressive.".

Take any lottery in the world, line up any amount of wining numbers separated by commas and pipe them through the below python script to find out if you find any pattern. 

  1 import sys

  2 import matplotlib.pyplot as plt

  3 

  4 numbers = []

  5 for token in sys.stdin.read().split(','):

  6     try:

  7         numbers.append(int(token.strip()))

  8     except ValueError:

  9         # ignore tokens that can't be converted to integers

 10         pass

 11 

 12 # Create a new figure and axis object

 13 fig, ax = plt.subplots()

 14 

 15 # Set the title of the plot

 16 ax.set_title("Input Numbers")

 17 

 18 # Set the labels for the X and Y axis

 19 ax.set_xlabel("Position")

 20 ax.set_ylabel("Number")

 21 

 22 # Plot the numbers as a line chart

 23 ax.plot(numbers)

 24 

 25 # Show the plot

 26 plt.show()

 27 

 28 # Save the plot

 29 plt.savefig('plot.png')


If you find one, then you will be using scientific evidence that you can hit the jackpot. If you cannot, then use scientific evidence at a job and you will make a great living. Do not expect though that wishful thinking will get you far.

Having hope is great. We all need it. But nothing can replace the scientific method when it comes to making educated decisions.

Tuesday, March 07, 2023

Remove web page elements that match a certain xpath

Taking for example a bitbucket pull requests page and assuming that we want to hide all PRs tat contain the string JIRA-80 just copy and paste the below to the browser console and press enter
// 1. define the text string to search for as content of a tag
// 2. define the xpath string that matches the tr that contains a td that contains 5 nested divs that contain an anchor with tagContent 
// 3. define the xpath result object that contains all the ORDERED_NODE_SNAPSHOT_TYPE nodes that match the xpath string 
// 4. iterate the xpath result to hide each node in it
var tagContent='JIRA-80';
var xpath="//tr[td[div[div[div[div[div/a[contains(text(),'" + tagContent + "')]]]]]]]";
var xpathResult = document.evaluate(xpath, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
for (let i = 0; i < xpathResult.snapshotLength; i++) {
  const xpathNode = xpathResult.snapshotItem(i);
  xpathNode.style.display = 'none';
}
Obviously you can inspect the element you want to remove and select the "delete element" option from the chrome inspector "elements tab" but automating the deletion of the elements that you do not want by knowing the xpath of them saves lots of time.

Wednesday, February 22, 2023

wipe out all docker files to gain back hdd space

Warning: Use only if you understand in full what and why you actually need this:
docker stop $(docker ps -q)
docker system prune --volumes

Friday, February 17, 2023

On mobile security - prepare in advance to report your stolen or lost phone if that ever happens


Open your phone app and "call" *#06#

Once you type the last sharp sign you will get important data that uniquely identifies your phone.

Make sure to keep a screenshot of that screen handy to share with authorities in case you lose your phone.

Followers