Upload 14 files
Browse files- __pycache__/data_fetcher.cpython-39.pyc +0 -0
- __pycache__/system_prompt_config.cpython-39.pyc +0 -0
- app.py +1 -1
- data_fetcher.py +98 -140
- requirements.txt +38 -7
- system_prompt_config.py +141 -31
__pycache__/data_fetcher.cpython-39.pyc
CHANGED
Binary files a/__pycache__/data_fetcher.cpython-39.pyc and b/__pycache__/data_fetcher.cpython-39.pyc differ
|
|
__pycache__/system_prompt_config.cpython-39.pyc
CHANGED
Binary files a/__pycache__/system_prompt_config.cpython-39.pyc and b/__pycache__/system_prompt_config.cpython-39.pyc differ
|
|
app.py
CHANGED
@@ -144,7 +144,7 @@ def generate(
|
|
144 |
global_chat_history.append({
|
145 |
"message": message,
|
146 |
"chat_history": chat_history,
|
147 |
-
"system_prompt":
|
148 |
"output": outputs[-1], # Assuming you want to save the latest model output
|
149 |
})
|
150 |
|
|
|
144 |
global_chat_history.append({
|
145 |
"message": message,
|
146 |
"chat_history": chat_history,
|
147 |
+
"system_prompt": input_prompt,
|
148 |
"output": outputs[-1], # Assuming you want to save the latest model output
|
149 |
})
|
150 |
|
data_fetcher.py
CHANGED
@@ -4,7 +4,105 @@
|
|
4 |
|
5 |
# Example from here: https://www.techgropse.com/blog/how-to-return-sql-data-in-json-format-python/
|
6 |
# Use the built-in json module and the pymysql package if using MySQL as database:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
import mysql.connector
|
9 |
import json
|
10 |
|
@@ -47,145 +145,5 @@ for row in rows:
|
|
47 |
# Convert the list of dictionaries to JSON and print it
|
48 |
json_result = json.dumps(result)
|
49 |
print(json_result)
|
50 |
-
|
51 |
-
# In the above code, we first connect to the database using pymysql, then create a cursor object to execute the SQL query.
|
52 |
-
# We then fetch all rows from the query result and convert them to a list of dictionaries,
|
53 |
-
# where each dictionary represents a row in the table with column names as keys and their values as values.
|
54 |
-
# Finally, we convert the list of dictionaries to JSON format using the json.dumps() method and print it.
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
|
76 |
'''
|
77 |
-
# Example from here: https://www.a2hosting.com/kb/developer-corner/mysql/connecting-to-mysql-using-python/
|
78 |
-
print("Using pymysql:")
|
79 |
-
import pymysql
|
80 |
-
myConnection = pymysql.connect( host='ec2-18-153-94-89.eu-central-1.compute.amazonaws.com', user='root', password='N12RXMKtKxRj', db='lionessdb' )
|
81 |
-
doQuery(myConnection)
|
82 |
-
myConnection.close()
|
83 |
-
print("Connection closed")
|
84 |
-
|
85 |
-
print( "Using mysqlclient (MySQLdb):" )
|
86 |
-
import MySQLdb
|
87 |
-
myConnection = MySQLdb.connect( host='ec2-18-153-94-89.eu-central-1.compute.amazonaws.com', user='root', password='N12RXMKtKxRj', db='lionessdb' )
|
88 |
-
doQuery( myConnection )
|
89 |
-
myConnection.close()
|
90 |
-
|
91 |
-
|
92 |
-
print( "Using mysql.connector:" )
|
93 |
-
import mysql.connector
|
94 |
-
myConnection = mysql.connector.connect( host='ec2-18-153-94-89.eu-central-1.compute.amazonaws.com', user='root', password='N12RXMKtKxRj', db='lionessdb' )
|
95 |
-
doQuery( myConnection )
|
96 |
-
myConnection.close()
|
97 |
-
'''
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
"""
|
103 |
-
# Own idea - did not work - no error message whatsoever
|
104 |
-
|
105 |
-
import pandas as pd
|
106 |
-
import sqlite3
|
107 |
-
|
108 |
-
def fetch_data_as_json():
|
109 |
-
conn = sqlite3.connect('lionessdb')
|
110 |
-
query = 'SELECT * FROM e5390g36407_decisions'
|
111 |
-
df = pd.read_sql(query, conn)
|
112 |
-
json_data = df.to_json(orient='records', lines=True)
|
113 |
-
print(json_data)
|
114 |
-
conn.close()
|
115 |
-
return json_data
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
# Example from here: https://www.bacancytechnology.com/qanda/python/return-sql-data-in-json-format-python
|
121 |
-
# Establish a Connection
|
122 |
-
connection_string = 'Driver={SQL Server};Server=ec2-18-153-94-89.eu-central-1.compute.amazonaws.com;Database=lionessdb;UID=root;PWD=N12RXMKtKxRj;'
|
123 |
-
conn = pyodbc.connect(connection_string)
|
124 |
-
|
125 |
-
# Execute SQL Query
|
126 |
-
cursor = conn.cursor()
|
127 |
-
sql_query = "SELECT * FROM e5390g36407_decisions;"
|
128 |
-
cursor.execute(sql_query)
|
129 |
-
|
130 |
-
# Fetch Data
|
131 |
-
columns = [column[0] for column in cursor.description]
|
132 |
-
data = [dict(zip(columns, row)) for row in cursor.fetchall()]
|
133 |
-
|
134 |
-
#Convert to JSON
|
135 |
-
json_data = json.dumps(data, indent=4)
|
136 |
-
|
137 |
-
# Save or Use JSON Data - save JSON data to a file:
|
138 |
-
with open('data.json', 'w') as json_file:
|
139 |
-
json_file.write(json_data)
|
140 |
-
|
141 |
-
# Alternatively, you can use the JSON data directly in your program:
|
142 |
-
print(json_data)
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
# Example from here: https://stackoverflow.com/questions/43796423/python-converting-mysql-query-result-to-json
|
149 |
-
# Simpler way: Return a dictionary and convert it to JSON.
|
150 |
-
# Just pass dictionary=True to the cursor constructor as mentioned
|
151 |
-
# in MySQL's documents: https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlconnection-cursor.html
|
152 |
-
|
153 |
-
import json
|
154 |
-
import mysql.connector
|
155 |
-
|
156 |
-
db = mysql.connector.connect(host='18.153.94.89',
|
157 |
-
user='root',
|
158 |
-
passwd='N12RXMKtKxRj',
|
159 |
-
db='lionessdb',
|
160 |
-
port=3306)
|
161 |
-
|
162 |
-
# This is the line that you need
|
163 |
-
cursor = db.cursor(dictionary=True)
|
164 |
-
|
165 |
-
name = "Bob"
|
166 |
-
cursor.execute("SELECT fname, lname FROM table WHERE fname=%s;", (name))
|
167 |
-
|
168 |
-
result = cursor.fetchall()
|
169 |
-
|
170 |
-
print(f"json: {json.dumps(result)}")
|
171 |
-
# Which will print for example: json: [{'fname': "Bob", 'lname': "Dole"}, {'fname': "Bob", 'lname': "Marley"}]
|
172 |
-
# Note that types are preserved this way, a good thing
|
173 |
-
# BUT will need to be transformed, parsed, or serialized into a string;
|
174 |
-
# for instance, if there is a date, the SQL query may return a datetime object, which will need to be parsed or serialized depending on your next step.
|
175 |
-
# A great way to serialize is in this answer: https://stackoverflow.com/a/36142844/4513509
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
# Example from here: https://dev.mysql.com/doc/connector-python/en/connector-python-example-connecting.html
|
182 |
-
|
183 |
-
import mysql.connector
|
184 |
-
|
185 |
-
cnx = mysql.connector.connect(user='root', password='N12RXMKtKxRj',
|
186 |
-
host='18.153.94.89',
|
187 |
-
database='lionessdb')
|
188 |
-
cnx.close()
|
189 |
-
|
190 |
-
"""
|
191 |
-
|
|
|
4 |
|
5 |
# Example from here: https://www.techgropse.com/blog/how-to-return-sql-data-in-json-format-python/
|
6 |
# Use the built-in json module and the pymysql package if using MySQL as database:
|
7 |
+
import mysql.connector
|
8 |
+
import json
|
9 |
+
|
10 |
+
# fetch data from the decisions database
|
11 |
+
def fetch_data_as_json():
|
12 |
+
# Connect to the database
|
13 |
+
conn = mysql.connector.connect(
|
14 |
+
host="18.153.94.89",
|
15 |
+
user="root",
|
16 |
+
password="N12RXMKtKxRj",
|
17 |
+
database="lionessdb"
|
18 |
+
)
|
19 |
+
|
20 |
+
# Create a cursor object
|
21 |
+
cursor = conn.cursor()
|
22 |
+
|
23 |
+
# Execute the SQL query
|
24 |
+
query = "SELECT playerNr, subjectNr, initialCredit, transfer1, tripledAmount1, keptForSelf1, returned1, " \
|
25 |
+
"newCreditRound2, transfer2, tripledAmount2, keptForSelf2, returned2, results2rounds, " \
|
26 |
+
"newCreditRound3, transfer3, tripledAmount3, keptForSelf3, returned3, results3rounds FROM e5390g36814_decisions"
|
27 |
+
cursor.execute(query)
|
28 |
+
|
29 |
+
# Fetch all rows and convert to a list of dictionaries
|
30 |
+
rows = cursor.fetchall()
|
31 |
+
result = []
|
32 |
+
for row in rows:
|
33 |
+
d = {}
|
34 |
+
for i, col in enumerate(cursor.description):
|
35 |
+
d[col[0]] = row[i]
|
36 |
+
result.append(d)
|
37 |
+
|
38 |
+
# Convert the list of dictionaries to JSON
|
39 |
+
json_result = json.dumps(result)
|
40 |
+
|
41 |
+
# Close the database connection
|
42 |
+
conn.close()
|
43 |
+
|
44 |
+
# Return the JSON result
|
45 |
+
return json_result
|
46 |
+
|
47 |
+
# Call the function to fetch data as JSON
|
48 |
+
json_result = fetch_data_as_json()
|
49 |
+
|
50 |
+
# Print or use the json_result variable as needed
|
51 |
+
print(json_result)
|
52 |
+
|
53 |
+
|
54 |
+
# In the above code, we first connect to the database using pymysql, then create a cursor object to execute the SQL query.
|
55 |
+
# We then fetch all rows from the query result and convert them to a list of dictionaries,
|
56 |
+
# where each dictionary represents a row in the table with column names as keys and their values as values.
|
57 |
+
# Finally, we convert the list of dictionaries to JSON format using the json.dumps() method and print it.
|
58 |
+
|
59 |
+
|
60 |
+
#In general: Track the page that participants are currently on - via the core table from the database using the variable onPage
|
61 |
+
#Once multiple groups: Track the participants’ group number - via the core table from the database using the variable groupNr
|
62 |
+
|
63 |
+
# fetch data from the core database
|
64 |
+
def fetch_data_core():
|
65 |
+
# Connect to the database
|
66 |
+
conn = mysql.connector.connect(
|
67 |
+
host="18.153.94.89",
|
68 |
+
user="root",
|
69 |
+
password="N12RXMKtKxRj",
|
70 |
+
database="lionessdb"
|
71 |
+
)
|
72 |
+
|
73 |
+
# Create a cursor object
|
74 |
+
cursor = conn.cursor()
|
75 |
+
|
76 |
+
# Execute the SQL query
|
77 |
+
query = "SELECT playerNr, subjectNr, groupNrStart, onPage FROM e5390g36814_core"
|
78 |
+
cursor.execute(query)
|
79 |
+
|
80 |
+
# Fetch all rows and convert to a list of dictionaries
|
81 |
+
rows = cursor.fetchall()
|
82 |
+
result = []
|
83 |
+
for row in rows:
|
84 |
+
d = {}
|
85 |
+
for i, col in enumerate(cursor.description):
|
86 |
+
d[col[0]] = row[i]
|
87 |
+
result.append(d)
|
88 |
+
|
89 |
+
# Convert the list of dictionaries to JSON
|
90 |
+
core_result = json.dumps(result)
|
91 |
+
|
92 |
+
# Close the database connection
|
93 |
+
conn.close()
|
94 |
|
95 |
+
# Return the JSON result
|
96 |
+
return core_result
|
97 |
+
|
98 |
+
# Call the function to fetch data as JSON
|
99 |
+
core_result = fetch_data_core()
|
100 |
+
|
101 |
+
# Print or use the json_result variable as needed
|
102 |
+
print(core_result)
|
103 |
+
|
104 |
+
|
105 |
+
'''
|
106 |
import mysql.connector
|
107 |
import json
|
108 |
|
|
|
145 |
# Convert the list of dictionaries to JSON and print it
|
146 |
json_result = json.dumps(result)
|
147 |
print(json_result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
148 |
|
149 |
'''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
@@ -1,3 +1,5 @@
|
|
|
|
|
|
1 |
accelerate==0.25.0
|
2 |
bitsandbytes==0.41.1
|
3 |
gradio==4.14.0
|
@@ -9,10 +11,39 @@ torch==2.0.0
|
|
9 |
transformers==4.36.2
|
10 |
|
11 |
auto-gptq==0.3.0
|
12 |
-
tiktoken
|
13 |
-
CTransformers
|
14 |
-
mysqlclient
|
15 |
-
mysql-
|
16 |
-
mysql-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# If new requirements added, open Llama2_local in terminal again and run: pip3 install -r requirements.txt
|
2 |
+
|
3 |
accelerate==0.25.0
|
4 |
bitsandbytes==0.41.1
|
5 |
gradio==4.14.0
|
|
|
11 |
transformers==4.36.2
|
12 |
|
13 |
auto-gptq==0.3.0
|
14 |
+
tiktoken #newly added for Prompt Engineering from here: 23:53 https://www.youtube.com/watch?v=BP9fi_0XTlw
|
15 |
+
CTransformers #newly added for Prompt Engineering from here: https://www.youtube.com/watch?v=AJOhV6Ryy5o
|
16 |
+
mysqlclient #newly added for data_fetcher.py
|
17 |
+
#mysql-client installation did not work via terminal or requirements.txt, so I did run brew install mysql aswell as brew install mysql-client in terminal
|
18 |
+
#Note: MySQL is configured to only allow connections from localhost by default. To connect run: mysql -u root in terminal. Did not work for me.
|
19 |
+
mysql-connector-python #newly added for data_fetcher.py
|
20 |
+
mysql-connector #newly added for data_fetcher.py
|
21 |
+
pymysql #newly added for data_fetcher.py
|
22 |
+
|
23 |
+
#from: https://singhaldhruv.medium.com/the-heros-sqlguide-conquering-the-subprocess-exited-with-error-error-92732cbe300#:~:text=The%20%E2%80%9Csubprocess%2Dexited%2Dwith%2Derror%E2%80%9D%20error%20is,confound%20even%20the%20bravest%20heroes.
|
24 |
+
#ran brew install python3-dev libmysqlclient-dev build-essential, then pip3 install --upgrade pip, then again pip3 install mysqlclient
|
25 |
+
#mysqlclient still did not work
|
26 |
+
|
27 |
+
#from: https://pypi.org/project/mysqlclient/
|
28 |
+
#ran brew install mysql pkg-config and then pip3 install mysqlclient
|
29 |
+
#mysqlclient could be installed successfully!!
|
30 |
+
|
31 |
+
#Homebrew if zhs brew not found, run: export PATH="/opt/homebrew/bin:$PATH"
|
32 |
+
|
33 |
+
#mysql module not found :(
|
34 |
+
#Troubleshooting from: https://stackabuse.com/bytes/fixing-modulenotfounderror-no-module-named-mysql-in-python/
|
35 |
+
#Checking if MySQL Package is Installed
|
36 |
+
#Before we proceed to install the MySQL module, it's good practice to first check if it's already installed.
|
37 |
+
#You can do this by using the pip show command in your terminal:
|
38 |
+
#pip3 show mysql-connector-python
|
39 |
+
#If the MySQL module is installed, the above command will return information about the module.
|
40 |
+
#However, if it's not installed, the command will return nothing.
|
41 |
+
#If not installed, then run in terminal: pip3 install mysql-connector-python
|
42 |
+
|
43 |
+
#Still did not work, error: import mysql.connector ModuleNotFoundError: No module named 'mysql'
|
44 |
+
#Try to run in terminal: pip3 install mysql -> Getting requirements to build wheel ... error error: subprocess-exited-with-error
|
45 |
+
#Need to run: pip3 install mysql-connector BECAUSE THE MYSQL_CONNECTOR IS NEEDED IN ORDER TO RUN MYSQL WITH PYTHON, found at https://www.youtube.com/watch?v=Yugm4lzEPTU
|
46 |
+
#mysql-connector
|
47 |
+
|
48 |
+
#ALSO, pip3 install tokenizer needed!
|
49 |
+
#tokenizer
|
system_prompt_config.py
CHANGED
@@ -1,19 +1,14 @@
|
|
1 |
# From Llama2 Local code
|
2 |
-
from data_fetcher import
|
|
|
|
|
|
|
|
|
|
|
3 |
'''
|
4 |
-
# These variables represent the beginning-of-sequence (BOS) and end-of-sequence (EOS) tokens.
|
5 |
BOS, EOS = "<s>", "</s>"
|
6 |
-
|
7 |
-
# These variables represent the beginning and end markers for an instruction.
|
8 |
BINST, EINST = "[INST]", "[/INST]"
|
9 |
-
|
10 |
-
# These variables define markers to denote the beginning and end of a system message.
|
11 |
-
# In the context of a chatbot, a system message is information or instructions provided by the system rather than the user.
|
12 |
-
# The markers here use <<SYS>> to indicate the start and <</SYS>> to indicate the end, with newline characters \n for formatting.
|
13 |
BSYS, ESYS = "<<SYS>>\n", "\n<</SYS>>\n\n"
|
14 |
-
|
15 |
-
# This line initializes a multiline string (""") for the default system prompt.
|
16 |
-
# It's the set of instructions for the chatbot to follow during a conversation in the trust game scenario that will be contained in the system message.
|
17 |
DEFAULT_SYSTEM_PROMPT = """\ Your task is to answer in a consistent style. You answer per question is maximum 2 sentences long.
|
18 |
You are an intelligent and fair game guide in a 2-player trust game.
|
19 |
Your role is to assist players in making decisions during the game.
|
@@ -30,31 +25,27 @@ Your goal is to guide players through the game, providing clear instructions and
|
|
30 |
If any question or action seems unclear, explain it rather than providing inaccurate information.
|
31 |
If you're unsure about an answer, it's better not to guess.
|
32 |
|
33 |
-
Example JSON context after a round:
|
34 |
-
{
|
35 |
-
"round": 1,
|
36 |
-
"trust_data": {
|
37 |
-
"player_A": {"trusts": "player_B", "amount": 5},
|
38 |
-
"player_B": {"trusts": "player_A", "amount": 3}
|
39 |
-
}
|
40 |
-
}
|
41 |
|
42 |
-
|
|
|
|
|
|
|
43 |
|
44 |
-
|
45 |
{BSYS} Give an overview of the trust game. {ESYS}
|
46 |
{BSYS} Explain how trust amounts are calculated. {ESYS}
|
47 |
{BSYS} What happens if a player doesn't trust in a round? {ESYS}
|
48 |
-
"""
|
49 |
|
50 |
-
|
51 |
|
52 |
def construct_input_prompt(chat_history, message) -> str:
|
53 |
# chat_history has the following structure:
|
54 |
# - dialogs
|
55 |
# --- instruction
|
56 |
# --- response (None for the most recent dialog)
|
57 |
-
|
58 |
for i, dialog in enumerate(chat_history[:-1]):
|
59 |
instruction, response = dialog[0], dialog[1]
|
60 |
# prepend system instruction before first instruction
|
@@ -63,8 +54,8 @@ def construct_input_prompt(chat_history, message) -> str:
|
|
63 |
else:
|
64 |
# the tokenizer automatically adds a bos_token during encoding,
|
65 |
# for this reason the bos_token is not added for the first instruction
|
66 |
-
|
67 |
-
|
68 |
|
69 |
# new instruction from the user
|
70 |
new_instruction = chat_history[-1][0].strip()
|
@@ -72,19 +63,54 @@ def construct_input_prompt(chat_history, message) -> str:
|
|
72 |
# the tokenizer automatically adds a bos_token during encoding,
|
73 |
# for this reason the bos_token is not added for the first instruction
|
74 |
if len(chat_history) > 1:
|
75 |
-
|
76 |
else:
|
77 |
# prepend system instruction before first instruction
|
78 |
new_instruction = f"{BSYS}{DEFAULT_SYSTEM_PROMPT}{ESYS}" + new_instruction
|
79 |
|
80 |
-
|
81 |
-
return
|
82 |
-
|
83 |
'''
|
84 |
|
85 |
# From own code
|
|
|
|
|
|
|
|
|
|
|
86 |
def get_default_system_prompt():
|
87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
|
89 |
def construct_input_prompt(chat_history, message):
|
90 |
input_prompt = f"<s>[INST] <<SYS>>\n{get_default_system_prompt()}\n<</SYS>>\n\n "
|
@@ -95,3 +121,87 @@ def construct_input_prompt(chat_history, message):
|
|
95 |
input_prompt += f"{message} [/INST] "
|
96 |
|
97 |
return input_prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
# From Llama2 Local code
|
2 |
+
from data_fetcher import fetch_data_as_json, fetch_data_core
|
3 |
+
json_data = fetch_data_as_json()
|
4 |
+
print(json_data)
|
5 |
+
core_data = fetch_data_core()
|
6 |
+
print(core_data)
|
7 |
+
|
8 |
'''
|
|
|
9 |
BOS, EOS = "<s>", "</s>"
|
|
|
|
|
10 |
BINST, EINST = "[INST]", "[/INST]"
|
|
|
|
|
|
|
|
|
11 |
BSYS, ESYS = "<<SYS>>\n", "\n<</SYS>>\n\n"
|
|
|
|
|
|
|
12 |
DEFAULT_SYSTEM_PROMPT = """\ Your task is to answer in a consistent style. You answer per question is maximum 2 sentences long.
|
13 |
You are an intelligent and fair game guide in a 2-player trust game.
|
14 |
Your role is to assist players in making decisions during the game.
|
|
|
25 |
If any question or action seems unclear, explain it rather than providing inaccurate information.
|
26 |
If you're unsure about an answer, it's better not to guess.
|
27 |
|
28 |
+
Example JSON context after a round: {json_data}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
+
You are currently assisting player: check the groupNr variable from {core_data}
|
31 |
+
You are currently in round: check the onPage variable from {core_data}
|
32 |
+
There are x rounds left.
|
33 |
+
Please keep that in mind when giving advice.
|
34 |
|
35 |
+
Few-shot training examples
|
36 |
{BSYS} Give an overview of the trust game. {ESYS}
|
37 |
{BSYS} Explain how trust amounts are calculated. {ESYS}
|
38 |
{BSYS} What happens if a player doesn't trust in a round? {ESYS}
|
39 |
+
""".format(json_data=json_data,BSYS=BSYS, ESYS=ESYS, core_data=core_data)
|
40 |
|
41 |
+
print(DEFAULT_SYSTEM_PROMPT)
|
42 |
|
43 |
def construct_input_prompt(chat_history, message) -> str:
|
44 |
# chat_history has the following structure:
|
45 |
# - dialogs
|
46 |
# --- instruction
|
47 |
# --- response (None for the most recent dialog)
|
48 |
+
prompt = ""
|
49 |
for i, dialog in enumerate(chat_history[:-1]):
|
50 |
instruction, response = dialog[0], dialog[1]
|
51 |
# prepend system instruction before first instruction
|
|
|
54 |
else:
|
55 |
# the tokenizer automatically adds a bos_token during encoding,
|
56 |
# for this reason the bos_token is not added for the first instruction
|
57 |
+
prompt += BOS
|
58 |
+
prompt += f"{BINST} {instruction.strip()} {EINST} {response.strip()} " + EOS
|
59 |
|
60 |
# new instruction from the user
|
61 |
new_instruction = chat_history[-1][0].strip()
|
|
|
63 |
# the tokenizer automatically adds a bos_token during encoding,
|
64 |
# for this reason the bos_token is not added for the first instruction
|
65 |
if len(chat_history) > 1:
|
66 |
+
prompt += BOS
|
67 |
else:
|
68 |
# prepend system instruction before first instruction
|
69 |
new_instruction = f"{BSYS}{DEFAULT_SYSTEM_PROMPT}{ESYS}" + new_instruction
|
70 |
|
71 |
+
prompt += f"{BINST} {new_instruction} {message} {EINST}"
|
72 |
+
return prompt
|
|
|
73 |
'''
|
74 |
|
75 |
# From own code
|
76 |
+
|
77 |
+
BOS, EOS = "<s>", "</s>"
|
78 |
+
BINST, EINST = "[INST]", "[/INST]"
|
79 |
+
BSYS, ESYS = "<<SYS>>\n", "\n<</SYS>>\n\n"
|
80 |
+
|
81 |
def get_default_system_prompt():
|
82 |
+
DEFAULT_SYSTEM_PROMPT = """\ Your task is to answer in a consistent style. You answer per question is maximum 2 sentences long.
|
83 |
+
You are an intelligent and fair game guide in a 2-player trust game.
|
84 |
+
Your role is to assist players in making decisions during the game.
|
85 |
+
The game consists of 3 rounds, and each player starts with an initial asset of 10€.
|
86 |
+
In each round, both players can trust each other with an amount between 0€ and 10€.
|
87 |
+
The trusted amounts are added, multiplied by 3, divided by 2, and then evenly distributed among the participants.
|
88 |
+
This sum, along with what's left of their initial assets, becomes their new asset for the next round.
|
89 |
+
For example, if player A trusts player B with 5€, and player B trusts player A with 3€, the combined trust is (5 + 3) = 8€.
|
90 |
+
After the multiplier and division, both players receive (8 * 3 / 2) = 12€.
|
91 |
+
Adding this to what's left from their initial 10€ forms their asset for the next round.
|
92 |
+
After 3 rounds, the final earnings are calculated using the same process.
|
93 |
+
You will receive a JSON with information on who trusted whom with how much money after each round as context.
|
94 |
+
Your goal is to guide players through the game, providing clear instructions and explanations.
|
95 |
+
If any question or action seems unclear, explain it rather than providing inaccurate information.
|
96 |
+
If you're unsure about an answer, it's better not to guess.
|
97 |
+
|
98 |
+
Example JSON context after a round: {json_data}
|
99 |
+
|
100 |
+
You are currently assisting player: check the groupNr variable from {core_data}
|
101 |
+
You are currently in round: check the onPage variable from {core_data}
|
102 |
+
There are x rounds left.
|
103 |
+
Please keep that in mind when giving advice.
|
104 |
+
|
105 |
+
Few-shot training examples
|
106 |
+
{BSYS} Give an overview of the trust game. {ESYS}
|
107 |
+
{BSYS} Explain how trust amounts are calculated. {ESYS}
|
108 |
+
{BSYS} What happens if a player doesn't trust in a round? {ESYS}
|
109 |
+
""".format(json_data=json_data,BSYS=BSYS, ESYS=ESYS, core_data=core_data)
|
110 |
+
|
111 |
+
print(DEFAULT_SYSTEM_PROMPT)
|
112 |
+
return DEFAULT_SYSTEM_PROMPT
|
113 |
+
|
114 |
|
115 |
def construct_input_prompt(chat_history, message):
|
116 |
input_prompt = f"<s>[INST] <<SYS>>\n{get_default_system_prompt()}\n<</SYS>>\n\n "
|
|
|
121 |
input_prompt += f"{message} [/INST] "
|
122 |
|
123 |
return input_prompt
|
124 |
+
|
125 |
+
|
126 |
+
|
127 |
+
'''
|
128 |
+
# From Llama 2 Local
|
129 |
+
# General code from here: https://www.youtube.com/watch?v=WzCS8z9GqHw
|
130 |
+
# https://github.com/thisserand/llama2_local
|
131 |
+
# These variables represent the beginning-of-sequence (BOS) and end-of-sequence (EOS) tokens.
|
132 |
+
BOS, EOS = "<s>", "</s>"
|
133 |
+
|
134 |
+
# These variables represent the beginning and end markers for an instruction.
|
135 |
+
BINST, EINST = "[INST]", "[/INST]"
|
136 |
+
|
137 |
+
# These variables define markers to denote the beginning and end of a system message.
|
138 |
+
# In the context of a chatbot, a system message is information or instructions provided by the system rather than the user.
|
139 |
+
# The markers here use <<SYS>> to indicate the start and <</SYS>> to indicate the end, with newline characters \n for formatting.
|
140 |
+
BSYS, ESYS = "<<SYS>>\n", "\n<</SYS>>\n\n"
|
141 |
+
|
142 |
+
# This line initializes a multiline string (""") for the default system prompt.
|
143 |
+
# It's the set of instructions for the chatbot to follow during a conversation in the trust game scenario that will be contained in the system message.
|
144 |
+
DEFAULT_SYSTEM_PROMPT = """\ Your task is to answer in a consistent style. You answer per question is maximum 2 sentences long.
|
145 |
+
You are an intelligent and fair game guide in a 2-player trust game.
|
146 |
+
Your role is to assist players in making decisions during the game.
|
147 |
+
The game consists of 3 rounds, and each player starts with an initial asset of 10€.
|
148 |
+
In each round, both players can trust each other with an amount between 0€ and 10€.
|
149 |
+
The trusted amounts are added, multiplied by 3, divided by 2, and then evenly distributed among the participants.
|
150 |
+
This sum, along with what's left of their initial assets, becomes their new asset for the next round.
|
151 |
+
For example, if player A trusts player B with 5€, and player B trusts player A with 3€, the combined trust is (5 + 3) = 8€.
|
152 |
+
After the multiplier and division, both players receive (8 * 3 / 2) = 12€.
|
153 |
+
Adding this to what's left from their initial 10€ forms their asset for the next round.
|
154 |
+
After 3 rounds, the final earnings are calculated using the same process.
|
155 |
+
You will receive a JSON with information on who trusted whom with how much money after each round as context.
|
156 |
+
Your goal is to guide players through the game, providing clear instructions and explanations.
|
157 |
+
If any question or action seems unclear, explain it rather than providing inaccurate information.
|
158 |
+
If you're unsure about an answer, it's better not to guess.
|
159 |
+
|
160 |
+
Example JSON context after a round: {json_data}
|
161 |
+
|
162 |
+
You are currently assisting player: check the groupNr variable from {core_data}
|
163 |
+
You are currently in round: check the onPage variable from {core_data}
|
164 |
+
There are x rounds left.
|
165 |
+
Please keep that in mind when giving advice.
|
166 |
+
|
167 |
+
Few-shot training examples
|
168 |
+
{BSYS} Give an overview of the trust game. {ESYS}
|
169 |
+
{BSYS} Explain how trust amounts are calculated. {ESYS}
|
170 |
+
{BSYS} What happens if a player doesn't trust in a round? {ESYS}
|
171 |
+
""".format(json_data=json_data,BSYS=BSYS, ESYS=ESYS, core_data=core_data)
|
172 |
+
|
173 |
+
print(DEFAULT_SYSTEM_PROMPT)
|
174 |
+
|
175 |
+
# Original: DEFAULT_SYSTEM_PROMPT = """\ You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature. If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information."""
|
176 |
+
|
177 |
+
def format_to_llama_chat_style(history) -> str:
|
178 |
+
# history has the following structure:
|
179 |
+
# - dialogs
|
180 |
+
# --- instruction
|
181 |
+
# --- response (None for the most recent dialog)
|
182 |
+
prompt = ""
|
183 |
+
for i, dialog in enumerate(history[:-1]):
|
184 |
+
instruction, response = dialog[0], dialog[1]
|
185 |
+
# prepend system instruction before first instruction
|
186 |
+
if i == 0:
|
187 |
+
instruction = f"{BSYS}{DEFAULT_SYSTEM_PROMPT}{ESYS}" + instruction
|
188 |
+
else:
|
189 |
+
# the tokenizer automatically adds a bos_token during encoding,
|
190 |
+
# for this reason the bos_token is not added for the first instruction
|
191 |
+
prompt += BOS
|
192 |
+
prompt += f"{BINST} {instruction.strip()} {EINST} {response.strip()} " + EOS
|
193 |
+
|
194 |
+
# new instruction from the user
|
195 |
+
new_instruction = history[-1][0].strip()
|
196 |
+
|
197 |
+
# the tokenizer automatically adds a bos_token during encoding,
|
198 |
+
# for this reason the bos_token is not added for the first instruction
|
199 |
+
if len(history) > 1:
|
200 |
+
prompt += BOS
|
201 |
+
else:
|
202 |
+
# prepend system instruction before first instruction
|
203 |
+
new_instruction = f"{BSYS}{DEFAULT_SYSTEM_PROMPT}{ESYS}" + new_instruction
|
204 |
+
|
205 |
+
prompt += f"{BINST} {new_instruction} {EINST}"
|
206 |
+
return prompt
|
207 |
+
'''
|