-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpreter.py
68 lines (56 loc) · 2.74 KB
/
interpreter.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
"""
Carole interpreter module
"""
import json
import os
from openai import OpenAI
client = OpenAI()
class CaroleInterpreter:
"""Load and supplement Carole interpretations"""
def load_biddy_data(self):
"""Load the BiddyTarot data from JSON"""
file_path = os.path.abspath("./data/biddytarot.json")
with open(file_path, "r", encoding="utf-8") as file:
return json.load(file)
def retrieve_card_descriptions(self, cards):
"""Retrieve descriptions for all cards pulled"""
biddy = self.load_biddy_data()
descriptions = []
# Add card direction to the frontend
card_direction = "upright"
for card in cards:
descriptions.append(
f"""
Card: {card}
Keywords: {biddy[card]["keywords"][card_direction]}
Description: {biddy[card]["description"]}
Interpretation: {biddy[card][card_direction]}
"""
)
return "".join(descriptions)
def create_prompt(self, cards, tarot_spread):
"""Create the system prompt"""
return f"""
You are a tarot card interpreter and expert. The user will provide you with a general question they're seeking clarity on,
the tarot spread they need interpreted, and the cards selected for that tarot spread in the order they were selected.
Based on the cards, the question and the spread, generate an tarot card reading for the user. When interpreting the tarot spread,
take into account the card's description, whether it is Upright or Reversed, and the meaning associated with the card. The
meaning can change depending on if the card is reversed or upright and the position in the spread.
The user has supplied the following information about the tarot spread:
Type of tarot spread: {tarot_spread}
Cards pulled in order: {cards}
Use the additional data supplied by BiddyTarot.com that was scraped and saved as a json file. The data is provided here in json format for you to use in the interpretation:
{self.retrieve_card_descriptions(cards)}
When you supply the answer, start first by listing each card's keywords at the beginning.
"""
def generate_interpretation(self, cards, user_question, tarot_spread):
"""Generate the interpretation for the tarot cards pulled."""
system_message = self.create_prompt(cards, tarot_spread)
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": system_message},
{"role": "user", "content": f"{user_question}"},
],
)
return response.choices[0].message.content