-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfortune.py
More file actions
219 lines (202 loc) · 6.08 KB
/
fortune.py
File metadata and controls
219 lines (202 loc) · 6.08 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import json
import random
import time
from dataclasses import dataclass
from datetime import datetime
from pathlib import Path
from langchain_ollama import ChatOllama
from cyber_orakel.log_db import Fortune
from cyber_orakel.log_db import log_to_sqlite
@dataclass
class CyberZodiac:
key: str
display_name: str
prompt_snippet: str
SENTIMENTS = ["positive", "ecstatic", "optimistic", "dismal", "neutral", "mythical"]
ZODIAC_SIGNS: list[CyberZodiac] = [
CyberZodiac(
key="cyber_krieger",
display_name="Cyber-Krieger",
prompt_snippet="""
- Hacking
- Virtuelle Realität
- Cyberspace
- VIM
- Fairydust
- Chaos Computer Club
- Chaos Communication Congress
- Cyberdeck
- Cyber-Angriff
- Cyber-Attacke
- Intrusion
- Virus
- Antivirus
"""),
CyberZodiac(
key="wissensanarcho",
display_name="Wissensanarcho",
prompt_snippet="""
- Open Source
- Datenschutz
- Verschlüsselung
- Whistleblowing
- Anonymität
- Hackerethik
- Wissen ist Macht
- Wissen teilen
- Wissen ist frei
- Creative Commons
"""),
CyberZodiac(
key="einhorn",
display_name="Einhorn",
prompt_snippet="""
- Regenbogen
- Glitzer
- Einhörner
- Magie
- Liebe
- Freundschaft
- Konfetti
- Kreativität
- Chaos ist schön
- Lächeln
- Löten
- Lötkolben
- Lichterkette
- Blinkende Lichter
"""),
CyberZodiac(
key="cryptogeek",
display_name="Cryptogeek",
prompt_snippet="""
- Public Key
- Private Key
- Alles verschlüsseln
- Blockchain
- Keysigning Party
- GPG Key
- https everywhere
- Private Daten schützen, öffentliche Daten nützen
"""),
CyberZodiac(
key="codeglaeubig",
display_name="Codegläubig",
prompt_snippet="""
- Code ist Gesetz
- Alles ist 1 außer der 0
- Code ist Poesie
- Code ist Kunst
- Code hat immer Recht
- Misstraue Autoritäten
- Computer können dein Leben zum Besseren verändern
- Der Code ist mit dir
- Code ist Schönheit
"""),
CyberZodiac(
key="schwurbler",
display_name="Schwurbler",
prompt_snippet="""
- Chemtrails
- Die Erde ist eine Scheibe
- 5G
- Globuli
- Aluhut
- Aluburka
- Verschwörungstheorie
- Bill Gates
- Flat earther
- Impfgegner
"""),
CyberZodiac(
key="retrohacker",
display_name="Retrohacker",
prompt_snippet="""
- 8-Bit
- C64
- Floppy Disk
- Retro
- Hacker Manifest
- Funkausstellung
- BTX
- Demoszene
- Atari
- Lötkolben
- Löten
"""),
CyberZodiac(
key="datenelch",
display_name="Datenelch",
prompt_snippet="""
- Wlan Geweih
- Empfangs-Turbo
- High speed Internet
- Wald
- Dorf-Internet
- Datenautobahn
- Logbuch Netzpolitik
- Neuland
"""),
CyberZodiac(
key="tschunky",
display_name="Tschunky",
prompt_snippet="""
- Tschunk ist Liebe
- Mate macht wach
- Limetten sind die neuen Zitronen
- Tschunk ist Leben
- Um 3 an der Tschunk-Bar
- Tschunk ist die Antwort
- 42 Tschunk pro Stunde
- Tschunk o'clock
- Ohne Tschunk kein Leben
- Ein Leben ohne Tschunk ist möglich, aber sinnlos
""")
]
def generate_fortune(zodiac_key: str, sentiment: str, num_lines: int = 2, language: str = "German",
include_entropy_words: bool = True) -> str:
zodiac = next((z for z in ZODIAC_SIGNS if z.key == zodiac_key), None)
if not zodiac:
# invalid zodiac sign, pick a random one
print(f"Invalid zodiac sign: {zodiac_key}, picking a random one")
zodiac = random.choice(ZODIAC_SIGNS)
entropy_snippet = ""
if include_entropy_words:
if PATH_CURRENT_ENTROPY_JSON.exists():
with open(PATH_CURRENT_ENTROPY_JSON, "r") as f:
entropy_words = json.load(f)
entropy_snippet = "\n".join([f"- {word}" for word in entropy_words])
prompt = f"""You are a fortune teller in a cyberpunk story.
Write a fortune cookie message for the cyber zodiac "{zodiac.display_name}"
with a sentiment of "{sentiment}". The message should be exactly {num_lines} lines long.
Write in {language}. Do not explain your answer. Be short and concise. Add no special characters.
The following terms and phrases are typical for the cyber zodiac {zodiac.display_name}.
Use them as inspiration for the message but don't just copy them verbatim:
{zodiac.prompt_snippet}\n{entropy_snippet}"""
# cleanup prompt: remove leading whitespace in every line and remove double line breaks
prompt = "\n".join([line.strip() for line in prompt.split("\n")]).replace("\n\n", "\n")
print(prompt)
start_time = time.time()
chat = ChatOllama(model="gemma2:2b")
duration = time.time() - start_time
fortune_obj = Fortune(
generation_time=datetime.now(),
fortune=chat.invoke(prompt).content,
prompt=prompt,
generation_duration=duration,
zodiac_key=zodiac_key,
sentiment=sentiment
)
log_to_sqlite(fortune_obj)
response = chat.invoke(prompt)
return response.content
def generate_many_fortunes():
for zodiac in ZODIAC_SIGNS:
for sentiment in SENTIMENTS:
for _ in range(2):
print(f"Zodiac: {zodiac.key}, Sentiment: {sentiment}")
print(generate_fortune(zodiac.key, sentiment))
print()
if __name__ == '__main__':
generate_many_fortunes()
PATH_CURRENT_ENTROPY_JSON = Path(__file__).parent.parent / "current_entropy.json"