What's new

chatai - version two - have a chat session with either Googles Gemini AI or Anthropics Claude (or both at once)

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

JGrana

Very Senior Member
I have done a major re-write of an addon I created called - chatai

This addon will allow you to ask questions of Googles Gemini or Anthropics Claude from your routers CLI.
It also supports an analyze file mode - especially useful for log files and small shell scripts.

Here is the github Readme:

chatai​

chatai will start an interactive "chat" session with either Googles Gemini AI or Anthropics Claude (or both at once) bots.chatai also supports an analyze mode for files such as log files, small shell scripts etc.


Installation​

For Asuswrt-merlin based routers running Entware, using your preferred SSH client/terminal, copy and paste the following command, then press Enter:

/usr/sbin/curl --retry 3 "https://raw.githubusercontent.com/JGrana01/chatai/master/chatai" -o "/jffs/scripts/chatai" && chmod 0755 /jffs/scripts/chatai && /jffs/scripts/chatai install

Install will check for Entware (and bail of not found), check and install the apps jq and fold, then setup a default chatai.conf file in /jffs/addons/chatai.

Before actually using chatai for the first time, you will need to get an API_KEY from Google and/or Anthropic.
Get the API key for Googles Gemini from https://aistudio.google.com/app/apikey

1722024847831.png


Get the API key for Anthropics Claude by going to this website:

https://console.anthropic.com/login

Setup a user account using your email address, then go to your Dashboard and select API Keys


1722024893655.png

1722024925601.jpeg


Edit the /jffs/addons/chatai/chatai.conf file and put the api key string in the line:

GAPI_KEY="Put your API key here" # Google Gemini API key
CAPI_KEY="Put your API key here" # Anthropics Claude API

You only need one of the AI API keys for chatai to work. It depends on which AI bot you want to converse with.Googles Gemini is the default. This can be overriden either in the command line or in the chatai.conf file.If you want answers from both, you will need both companies API key.


Command and Command Line Arguments​

chatai (Ver 0.3.2) - start an interactive chat session with Googles Gemini or Anthropics Claude AI

Usage: chatai [gemini|claude|both] [analyze filename] [log] [show] [help] [install] [uninstall] [update]

gemini - use Googles Gemini for chat (default)
claude - use Anthropics Claude for chat
both - use both AI and display the results in different color output
analyze filename - analyze the suppled filename contents
log - log the session for later viewing
show - show all the saved chat log sessions
help - show this message
install - install chatai and create addon dir and config file
uninstall - remove chatai and its directory, chatlogs and config file
update - check for and optionally update

Command Line Options​

gemini - (default) use Google Gemini for the chat session (overrides .conf file WHICHAI setting)

claude - use Anthropics Claude for the session (overrides .conf file WHICHAI setting)

both - show answers from both AI bots. Geminis output will be cyan and Claude in blue (overrides .conf file WHICHAI setting)

analyze filename - rather then start a chat session, send filename to AI for analysys.

log - record a session of the chat session. This will show the questions and responses. The files at date/time stamped and stored in /jffs/addons/chatai/logs.

show - display all the latest log sessions (using the Linux "more" command)

help - show a help screen

install - install chatai, create the addons directory and create a chatai.conf file

uninstall - remove chatai and its directories and files (including log files!!!)

update - check github for a new version. If so, offer to install it.

Here is a typical /jffs/addons/chatai/chatai.conf file

#
# chatai conf file
#

GAPI_KEY="AIzaSyB1y4Nn4rwrSVxH0a3KE7_XXXXXXXX8" # Put Google Gemini API key here ;-)
CAPI_KEY="sk-ant-api03-PQGJE_F-LlsovCnChPXBGag_sioN8aX8JAkaM2-XyTFgZq4kZnTp8zFY-p6G6kCUE7wytdyoZXXXXXXXXXXw-z6CFJgAA" # Put Anthropics Cl
WHICHAI="0" # which ai bot - 0: Gemini 1: Claude 2: both
LOGDIR="$SCRIPTDIR/logs" # location to store chat log sessions
NUMLOGS="5" # number of log sessions to save for later viewing
MAXLINES="200" # Maximum number of lines in a file to analyze
# use caution in making it too large...
MAXSIZE="32767" # Maximum number of characters in any request
ALWAYSTRIM="0" # always trim to MAZLINES any file for analysis
# 0: Ask if triming needed 1: Always trim the file


GAPI_KEY - the key created by you when you asked for a Gemini API key
CAPI_KEY - the key created by you when to setup Antropics Claude developer
WHICHAI - set to 0 (default) for Gemini, 1 for Claude or 2 to have both respond to your questions or analysis
LOGDIR - where you want chatai to store log files of the session. Default is in the /jffs/addons/chatai/log
NUMLOGS - the number of log files to store at a time. Its a circular method - a new log file overrides the oldest
MAXLINES - the maximium number of lines of text to send. This can be increased - but should be cautiously - more lines - more tokens used.
MAXSIZE - this is the maximium number of characters to send. Again, can be increased - more dependent on shell variable length :)
ALWAYSTRIM - If a file for analysys is greater than MAXLINES, either ask if it should be trimmed (0) or just trim it without prompting (1)
 

Usage​

Typical usage is to just start chatai:

$ chatai

When chatai begins, it shows you in chat mode, which AI bot and mentions to enter "q" when done:

Code:
GeminiAI Chat mode. Enter a line of text and get a response.
    Enter q to exit this mode

chat>

Ask questions and get (typically) a response in either cyan if using Gemini or blue if Claude

Chatai supports a logging function where it will save a number of chat sesstions in a log file for later viewing. The number of chat sessions saved is configurable (NUMLOGS) in the /jffs/addons/chatai/chatai.conf file.You can also change the location of the chat session logs to a different directory (LOGDIR), again by editing /jffs/addons/chatai/chatai.conf.To enable logging for a session, use the "log" argument:


Code:
$ chatai log

To view the chat session, you can use any editor or text viewer to look at the logs/session stored in /jffs/addons/chatai/logs. The files will have the date and time when the chat session started, like this:

chatlog_2024-04-09-12-05-14

As new chat logs are created, chatai will remove the earlist logs to keep the number of files to NUMLOGS. This is configurable.

There is also an argument (show) that will display all the chat sessions (using "more") from the earliest to the most recent.

Commands line arguments can be combined. For example, to have both bots answer your questions (or analyze your file) and log the session:

Code:
$ chatai both log

If chatai is invoked with the analyze command, it will take the passed filename, check that is exists and is less than MAXLINES long and MAXSIZE bytes and pass on for analysis. If the file is too long, chatai will offer to shorten it to MAXLINES.

Code:
$ chatai analyze myfile

You can specify which AI should do the analysis or use both. You can also log the analysis:

Code:
$chatai both log analyze /tmp/syslog.log

This can be useful to analyze log files such as /tmp/syslog.log. Keep in mind, if the file is longer than MAXLINES long and ALWAYSTRIM is 0, chatai will offer to shorten it to the last MAXLINES long. If the file is longer than MAXLINES and ALWAYSTRIM is 1, chatai will use tail to shorten the file.

If there is a specific series of lines you want anaylzed, shorten the file to just those lines. Also, be careful using analyze in a script with potentially long files. It will block asking to shorten...

There is a special mode while in chat - analyze while in chat. To invoke this mode, while at the chat prompt, enter just the word "analyze"
chatai will ask for the type of file and filename:


Code:
chat> analyze

What type of file to analyze:
  1) text
  2) code
  3) log

(1 or 2 or 3) ?
chatai will ask what type of file you plan to have analyzed (makes for more accurate analysis). Enter either 1 (text), 2 (code) or 3 (log)

chatai will then ask for the filename. If it is too large (> MAXSIZE) it will tell you and not send on.If the file has more lines in it then MAXLINES and ALWAYSTRIM is 0, chatai will ask if you want it to reduce the number of lines to MAXLINES (using tail).

If ALWAYSTRIM is set to 1, chatai will always trim the file to MAXLINES without prompting.

Note below about special characters, especially with shell scripts. I'm working on solutions...

Once the file is analyzed and displayed, chatai returns to chat mode. Useful for asking more questions about its analysis.


Notes​

Be careful with JSON special characters - especially escape sequences in code (i.e. "\\n"). chatai attempts to clean up files and questions byreplacing "returns" to "\n" and escaping double quotes - but many large shell scripts likely wont get passed via JSON...

Following good Asuswrt-merlin Addon methods, chatai has install, uninstall and update functions.

Install - will install chatai in /jffs/scripts directory with a symbolic link to /opt/sbin. It also will download any additional applications from Entware that it requires (jq and fold).

A default chatai.conf file is created in /jffs/addons/chatai.

Unistall removes chatai, the symbolic link and the /jffs/addons/chatai directory (and all logs).

Update will query the latest version on github and offer to download and install it.
 
Here are some interesting replies from both

chat> Who is RMerlin of Asuswrt-merlin
Gemini

RMerlin is the pseudonym of a developer who creates custom firmware
for Asus routers. This firmware, known as Asuswrt-Merlin, is known for offering
more features and customization options than the stock firmware provided by
Asus.

RMerlin's true identity is unknown, but he is known to be an experienced
software developer with a deep knowledge of Asus routers. He has been
developing custom firmware for Asus routers for many years, and his work has
been praised by many users.

Asuswrt-Merlin is a popular choice among users who want more control over their
routers. It offers a wide range of features, including:

* Improved performance and stability
* More customization options
* Enhanced security features
* Support for a wider range of devices

Asuswrt-Merlin is regularly updated with new features and bug fixes. It is
available for download from RMerlin's website.


Claude

RMerlin is the developer behind the Asuswrt-Merlin firmware, which is
a custom firmware for Asus wireless routers based on Asuswrt.

Some key points about RMerlin and Asuswrt-Merlin:

- RMerlin is the online handle/nickname of the developer, whose real name is
Eric Sauvageau.

- He started developing Asuswrt-Merlin in 2012 as a hobby project to enhance
the functionality of Asus routers running the stock Asuswrt firmware.

- Asuswrt-Merlin adds many features and tweaks not found in the stock firmware,
such as improved QoS, traffic monitoring, overclocking options, and more
advanced wireless settings.

- It is distributed for free and has become very popular among Asus router
owners looking to get more out of their routers.

- RMerlin continues actively developing and maintaining Asuswrt-Merlin,
regularly releasing new versions with bug fixes and improvements.

- While unofficial, his work is highly respected in the router hacking/modding
community for the quality and stability of his firmware releases.

So in summary, RMerlin is the pseudonymous developer responsible for creating
and maintaining the highly popular Asuswrt-Merlin third-party custom firmware
for enhancing Asus wireless routers.

chat>
 
Another, on iptables from both

chat> write me an iptables rule to only allow access to port 9000 on the camera (192.168.3.29; guest network 1 2.4ghz) to a single IP (192.168.2.10; guest network 1 5ghz)
Gemini

```ing...
iptables -A INPUT -s 192.168.2.10 -d 192.168.3.29 -p tcp --dport 9000 -j ACCEPT
```


Claude

To allow access to port 9000 on the camera (192.168.3.29) from a
single IP (192.168.2.10), you can use the following iptables rule:

```
iptables -A INPUT -p tcp -s 192.168.2.10 --dport 9000 -d 192.168.3.29 -j ACCEPT
```

Here's a breakdown of the rule:

- `-A INPUT`: Append the rule to the INPUT chain, which handles incoming
traffic.
- `-p tcp`: Match TCP protocol packets.
- `-s 192.168.2.10`: Match packets coming from the source IP address
192.168.2.10 (guest network 1 5GHz).
- `--dport 9000`: Match packets destined for port 9000.
- `-d 192.168.3.29`: Match packets destined for the IP address 192.168.3.29
(camera on guest network 1 2.4GHz).
- `-j ACCEPT`: Accept the packets that match the rule.

This rule will allow incoming TCP traffic from the IP address 192.168.2.10 to
port 9000 on the camera with IP address 192.168.3.29. All other traffic to port
9000 on the camera from other IP addresses will be blocked by the default
policy.

Please note that you need to run this command with root privileges (sudo) on
the system where you want to apply the rule (e.g., the router or a firewall
device). Additionally, this rule will be temporary and will be lost after a
system reboot or iptables reset. To make the rule persistent, you'll need to
save the iptables rules or add the rule to a script that runs at system
startup.

chat>
 
Ok, one more showing each writing a shell script.

chat> write me a Linux shell script that checks a list of IP addresses to see if they are online. If not output an error.
Gemini

```bash..
#!/bin/bash

# Read the list of IP addresses from a file
ip_list=$(cat ip_list.txt)

# Iterate over the list of IP addresses
for ip_address in $ip_list; do

# Check if the IP address is online using ping
ping -c 1 $ip_address > /dev/null 2>&1

# If the IP address is not online, output an error
if [ $? -ne 0 ]; then
echo "Error: $ip_address is not online"
fi
done
```


Claude

Here's a Linux shell script that checks a list of IP addresses to see
if they're online and outputs an error if not:

```bash
#!/bin/bash

# Function to check if an IP address is online
check_online() {
ping -c 1 "$1" >/dev/null 2>&1
if [ $? -eq 0 ]; then
echo "$1 is online"
else
echo "Error: $1 is offline"
fi
}

# Check if an IP list file is provided
if [ -z "$1" ]; then
echo "Usage: $0 <ip_list_file>"
exit 1
fi

# Read IP addresses from the file
ip_file="$1"
while IFS= read -r ip; do
# Skip empty lines or comments
if [ -z "$ip" ] || [[ "$ip" =~ ^#.* ]]; then
continue
fi

# Check if the IP address is online
check_online "$ip"
done < "$ip_file"
```

To use this script, save it to a file (e.g., `check_ips.sh`) and make it
executable with `chmod +x check_ips.sh`.

Create a text file (e.g., `ip_list.txt`) containing a list of IP addresses, one
per line:

```
192.168.1.1
8.8.8.8
example.com
# This is a comment
```

Then, run the script with the IP list file as an argument:

```
./check_ips.sh ip_list.txt
```

The script will iterate through the IP addresses in the file, check if each one
is online using the `ping` command, and output the corresponding status. If an
IP address is offline, it will print an error message.

Here's how the script works:

1. The `check_online` function takes an IP address as an argument, pings it
once using `ping -c 1`, and checks the exit status ($?) of the `ping` command.
If the exit status is 0 (success), it means the IP address is online, and the
function prints a message indicating that. If the exit status is non-zero
(failure), it prints an error message indicating that the IP address is offline.

2. The script checks if an IP list file is provided as an argument. If not, it
prints a usage message and exits.

3. The script reads the IP addresses from the provided file line by line using
a `while` loop with the `read` command.

4. It skips empty lines or lines starting with `#` (comments) using an `if`
statement and the `=~` regular expression operator.

5. For each valid IP address, the script calls the `check_online` function and
passes the IP address as an argument.

Note: This script assumes that the `ping` command is available on your system.
If you're using a different operating system or network configuration that
doesn't support `ping`, you may need to modify the `check_online` function
accordingly.

chat>
 

Support SNBForums w/ Amazon

If you'd like to support SNBForums, just use this link and buy anything on Amazon. Thanks!

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!

Staff online

Top