generated from cheshire-cat-ai/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththe_catscii_artist.py
111 lines (102 loc) · 3.56 KB
/
the_catscii_artist.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from cat.mad_hatter.decorators import tool, plugin
import cowsay
import random
import requests
from cat.log import log
import json
from pydantic import BaseModel, Field
class MySettings(BaseModel):
danger_zone: bool = Field(
default=False,
title="**danger zone**: If activated, random facts are generated by the cat itself, no api key required",
)
NINJAS_API_KEY: str = Field(
title="ninjas api key goes here",
description="Paste here your ninjas api key in order to make the magic works",
default="",
extra={"type": "Text"}
)
cats: bool = Field(
default=True,
title="activates/deactivates use of cats ascii art",
)
@plugin
def settings_model():
return MySettings
def cat_one():
return r"""
|\ _,,,---,,_
ZZZzz /,`.-'`' -. ;-;;,_
|,4- ) )-,_. ,\ ( `'-'
'---''(_/--' `-'\_)
"""
def cat_two():
return r"""
|\__/,| (`\
_.|o o |_ ) )
-(((---(((--------
"""
def cat_three():
return r"""
/\_/\
( o.o )
> ^ <
"""
already_said = [] # TODO fix this
@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. You never repeat already said facts.
"""
if len(already_said) > 0:
prompt += "\nAlready said facts:\n"
try:
already_said_str = '\n'.join(already_said)
prompt += already_said_str
fact = cat.llm(prompt)
if ascii_art_type:
output = get_output(fact, True, cats, animals)
else:
output = get_output(fact, False, cats, animals)
already_said.append(fact)
except Exception as e:
output = "No funny facts today, meowy."
elif (NINJAS_API_KEY != ""):
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
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>"