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

Followers