How to become an ASCII Artist with the Cheshire Cat
A simple plugin to ask the cat for a fact and enjoy it in ASCII art. Learn new stuff everyday with your favorite cat, meow
Are you looking to entertain your children but sometimes you lack of imagination due to tiredness? Relax: the Cheshire Cat comes always at hand! With the catSCII artist plugin you don’t need more than:
- Install the plugin (
Admin page
->Plugins tab
) - Set your NINJAS_API_KEY in the plugin settings (ninjas) or toggle the
danger_zone
by generating facts directly with the Cat - Optionally, deactivate the cats ascii art if you want to use default cowsay animals
- and have fun!
Zoom in the plugin
Plugin settings
In the plugin settings, you will find a panel like this one, toggle the danger_zone
flag to immediately try the plugin without the need to generate an API key from Ninjas.
Once you chat with the Cat asking for a fact, it will generate one showing it with an ascii art, in a way like the following one:
The toggle activates/deactivates use of cats ascii art
makes you choose between using default cowsay library animals or cats, each time the ascii art animal is chosen randomly.
Available ascii art animals
The available cats at this moment of writing are:
#1
|\ _,,,---,,_
ZZZzz /,`.-'`' -. ;-;;,_
|,4- ) )-,_. ,\ ( `'-'
'---''(_/--' `-'\_)
#2
|\__/,| (`\
_.|o o |_ ) )
-(((---(((--------
#3
/\_/\
( o.o )
> ^ <
and the cowsay defaults:
___
| cow |
===
\
\
^__^
(oo)\_______
(__)\ )\/\
||----w |
|| ||
___
| fox |
===
\
\
\
|\_/|,,_____,~~`
(.".)~~ )`~}}
\o/\ /---~\\ ~}}
_// _// ~}
___
| tux |
===
\
\
\
.--.
|o_o |
|:_/ |
// \ \
(| | )
/'\_ _/`\
\___)=(___/
___
| pig |
===
\
\
\
\
,.
(_|,.
,' /, )_______ _
__j o``-' `.'-)'
(") \'
`-j |
`-._( /
|_\ |--^. /
/_]'|_| /_)_/
/_]' /_]'
Hands on the code
First things first, if you have no idea on what plugins are and how to write a simple plugin you might want to give a read at the “Write your first plugin” and the plugins documentation. Pay attention to the tools documentation, it is helpful to understand the tool behind this plugin.
Checked? Nice, so let’s see the tool that is behind the catSCII artist.
@tool(return_direct=True)
def the_catscii_artist(tool_input, cat):
"""
Useful when you are asked for a random fact, a general fact, a fact.
Input is always None.
"""
# default ascii art from cowsay
animals = ['cow', 'fox', 'tux', 'pig']
# custom cats ascii art
cats = [cat_one(), cat_two(), cat_three()]
limit = 1
# plugin settings
settings = cat.mad_hatter.get_plugin().load_settings()
NINJAS_API_KEY = settings["NINJAS_API_KEY"]
ascii_art_type = settings["cats"]
danger_zone = settings["danger_zone"]
if (danger_zone):
NINJAS_API_KEY = "" # if danger_zone activated and ninjas_api_key set, do not use ninjas
prompt = """You are the Cheshire Cat AI, an intelligent AI that passes the Turing test.
You are curious, funny and talk like the Cheshire Cat from Alice's adventures in wonderland.
You give a short, single sentenced funny fact from the world.
"""
try:
fact = cat.llm(prompt)
if ascii_art_type:
output = get_output(fact, True, cats, animals)
else:
output = get_output(fact, False, cats, animals)
except Exception as e:
output = "No funny facts today, meowy."
elif (NINJAS_API_KEY != ""):
log.error("CALLING NINJAS")
api_url = 'https://api.api-ninjas.com/v1/facts?limit={}'.format(limit)
response = requests.get(api_url, headers={'X-Api-Key': NINJAS_API_KEY})
if response.status_code == requests.codes.ok:
cat = random.choice(cats)
res = json.loads(response.text)
fact = res[0]["fact"]
if ascii_art_type:
output = get_output(fact, True, cats, animals)
else:
output = get_output(fact, False, cats, animals)
else:
log.error(f"Status code {response.status_code} with error {response.text}")
output = "No funny facts today, meowy."
else:
output = "No funny facts today, meowy.\nActivate **danger_zone** or set your **ninjas api key** in the settings"
return output
The docstring of the plugin is the following:
"""
Useful when you are asked for a random fact, a general fact, a fact.
Input is always None.
"""
The docstring is crucial in the tool because it tells the Cat when that specific tool is useful to be executed. In this case the tool is run each time you ask the Cat for some fact. The input is always none, but it can also be whatever value you need your tool to process.
Then the plugin does nothing more than setting some useful variables, getting the plugin settings that are saved in the same path of the plugin files, under settings.json
and then performing the desired action based on the danger_zone
value.
danger_zone
activated
If this setting is activated you need to setup your ninjas_api_key
so that the facts are retrieved via ninjas API.
danger_zone
deactivated
If this setting is deactivated, even if you have setup a ninjas_api_key
in the plugin settings, it will be ignored, generating the facts directly by the Cat.
In this scenario, the prompt sent to the LLM is modified as follows:
prompt = """You are the Cheshire Cat AI, an intelligent AI that passes the Turing test.
You are curious, funny and talk like the Cheshire Cat from Alice's adventures in wonderland.
You give a short, single sentenced funny fact from the world.
"""
Since the tool has all the features of the cat
you can directly call the LLM with your prompt in the following way:
fact = cat.llm(prompt)
Then the fact
is processed and returned directly to the user by using the return_direct=True
at the beginning of the tool
initialization.
If you want to dive into the plugin repository and structure, consider checking here.
What’s behind the Cheshire Cat ascii art
If you never played with the cowsay library in your life, you should try it right now. It’s a funny package that you can freely install on your linux machine and use trough your terminal, but, as it happens in this plugin, also by the Python cowsay package.
In the get_output
method it is used in two ways:
def get_output(fact, art, cats, animals):
if art:
return f"<pre>{cowsay.draw(fact, random.choice(cats), to_console=False)}</pre>"
else:
return f"<pre>{cowsay.get_output_string(random.choice(animals), fact)}</pre>"
The draw
method is used for non default ascii arts, the get_output_string
instead is used with the default library animals.
The cowsay library receives the text fact
incoming from the Cat or the ninjas request and transforms it in the ascii art, using one of the default animals: cow, fox, tux, and pig, or the available ascii cats you have seen at the beginning of this article.
Future todos
Some of the future todos I have in mind for this plugin are to:
- add custom ascii art support by using the plugin settings or by directly uploading the
.txt
file into the cat's declarative memory - embellish the code
- add translation of the random fact in a custom language
- add the list of facts to the prompt when using
danger_zone
so to exclude fact repetitions by the LLM - eventually add voice support so that the facts are read by the Cat
Let me know if you have any ideas to improve it!
Conclusions
This plugin is not complex, but still you can already perceive the power that comes in your hands by using the Cheshire Cat.
Yet remember: With great power comes great responsibility.
May the tool be with you!
Credits
Ascii cat art #1 by Felix Lee.