-
Notifications
You must be signed in to change notification settings - Fork 1
/
Prompts.py
203 lines (154 loc) · 8.07 KB
/
Prompts.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
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
from typing import Dict, List
import pandas as pd
from Entity_Resolver import *
def _get_system_prompt() -> str:
return "Generate a SPARQL query that answers the given 'Input question:'. Use 'Entities:', 'Relations:' and 'Types:' specified in the prompt to generate the query. The SPARQL query should be compatible with the Wikidata knowledge graph. Prefixes like 'wdt' and 'wd' have already been defined. No language tag is required. Use '?x' as variable name in the SPARQL query. Remember to provide only a SPARQL query in the response without any notes, comments, or explanations."
def get_zero_shot_chat_prompt(input_question: str, entities: Dict[str, str], relations: Dict[str, str], type_list: Dict[str, str], system_message: bool = False) -> List[dict]:
''' Returns the template for a prompt that converts a user question into a sparl query'''
message = []
if system_message:
message.append({"role": "system", "content": f"{_get_system_prompt()}"})
message.append({"role": "user", "content": f"""Input question: {input_question}
Entities: {entities}
Relations: {relations}
Types: {type_list}"""})
return message
def get_zero_shot_chat_history_prompt(dataframe: pd.DataFrame, index: int, system_message: bool = False) -> List[dict]:
''' Returns the template for a prompt that converts a user question into a sparl query'''
message = []
current_row = dataframe.iloc[index]
conversation_history = create_conversation_history(dataframe, index)
if system_message:
message.append({"role": "system", "content": f"{_get_system_prompt()}"})
message.append({"role": "user", "content": f"""{conversation_history}Input question: {current_row['utterance']}
Entities: {current_row['entities_in_utterance']}
Relations: {current_row['relations']}
Types: {current_row['type_list']}"""})
return message
def get_few_shot_chat_prompt(input_question: str, entities: Dict[str, str], relations: Dict[str, str], type_list: Dict[str, str], system_message: bool = False) -> List[dict]:
''' Returns the template for a prompt that converts a user question into a sparl query'''
message = []
if system_message:
message.append({"role": "system", "content": f"{_get_system_prompt()}"})
message.append({"role": "user", "content": f"""{_get_input_question_01()}
{_get_entities_01()}
{_get_relations_01()}
{_get_types_01()}"""})
message.append({"role": "assistant", "content": f"{_get_sparql_01()}"})
message.append({"role": "user", "content": f"""{_get_input_question_02()}
{_get_entities_02()}
{_get_relations_02()}
{_get_types_02()}"""})
message.append({"role": "assistant", "content": f"{_get_sparql_02()}"})
message.append({"role": "user", "content": f"""{_get_input_question_04()}
{_get_entities_04()}
{_get_relations_04()}
{_get_types_04()}"""})
message.append({"role": "assistant", "content": f"{_get_sparql_04()}"})
message.append({"role": "user", "content": f"""Input question: {input_question}
Entities: {entities}
Relations: {relations}
Types: {type_list}"""})
return message
def get_few_shot_chat_history_prompt(dataframe: pd.DataFrame, index: int, system_message: bool = False) -> List[dict]:
''' Returns the template for a prompt that converts a user question into a sparl query'''
message = []
current_row = dataframe.iloc[index]
conversation_history = create_conversation_history(dataframe, index)
if system_message:
message.append({"role": "system", "content": f"{_get_system_prompt()}"})
message.append({"role": "user", "content": f"""{_get_input_question_01()}
{_get_entities_01()}
{_get_relations_01()}
{_get_types_01()}"""})
message.append({"role": "assistant", "content": f"{_get_sparql_01()}"})
message.append({"role": "user", "content": f"""{_get_input_question_02()}
{_get_entities_02()}
{_get_relations_02()}
{_get_types_02()}"""})
message.append({"role": "assistant", "content": f"{_get_sparql_02()}"})
message.append({"role": "user", "content": f"""{_get_conversation_history_03()}{_get_input_question_03()}
{_get_entities_03()}
{_get_relations_03()}
{_get_types_03()}"""})
message.append({"role": "assistant", "content": f"{_get_sparql_03()}"})
message.append({"role": "user", "content": f"""{conversation_history}Input question: {current_row['utterance']}
Entities: {current_row['entities_in_utterance']}
Relations: {current_row['relations']}
Types: {current_row['type_list']}"""})
return message
def create_conversation_history(dataframe: pd.DataFrame, question_index: int) -> str:
conversation_history = ""
if question_index == 0:
return conversation_history
# Use at most the last three turns of the conversation history -> 6 rows
conversation_turns = 6
for i in range(question_index, 0, -1):
conversation_turn = i - 1
if conversation_turn < 0 or conversation_turn < question_index - conversation_turns:
break
current_turn = dataframe.iloc[conversation_turn]
speaker = current_turn['speaker']
if speaker == "USER":
conversation_history = f"{speaker}: {current_turn['utterance']}\n" + conversation_history
else:
response_entities = _get_first_values_of_dict(current_turn['entities_in_utterance'])
if len(response_entities) > 0:
conversation_history = f"{speaker}: {response_entities}\n" + conversation_history
else:
conversation_history = f"{speaker}: {current_turn['utterance']}\n" + conversation_history
return f"Conversation history:\n{conversation_history}\n"
def _get_first_values_of_dict(entities: str) -> Dict[str, str]:
# Get the first 3 values of the dictionary
result = {}
dictionary = eval(entities)
for i, (key, value) in enumerate(dictionary.items()):
if i == 3:
break
result[key] = value
return result
def _get_input_question_01() -> str:
return "Input question: Is New York City the place of death of Cirilo Villaverde ?"
def _get_entities_01() -> str:
return "Entities: {'Q727043': 'Cirilo Villaverde', 'Q60': 'New York City'}"
def _get_relations_01() -> str:
return "Relations: {'P20': 'place of death'}"
def _get_types_01() -> str:
return "Types: {'Q56061': 'administrative territorial entity'}"
def _get_sparql_01() -> str:
return "SPARQL query: ASK { wd:Q727043 wdt:P20 wd:Q60 . }"
def _get_input_question_02() -> str:
return "Input question: How many works of art express Michael Jordan or pain ?"
def _get_entities_02() -> str:
return "Entities: {'Q41421': 'Michael Jordan', 'Q81938': 'pain'}"
def _get_relations_02() -> str:
return "Relations: {'P180': 'depicts'}"
def _get_types_02() -> str:
return "Types: {'Q838948': 'work of art'}"
def _get_sparql_02() -> str:
return "SPARQL query: SELECT (COUNT(DISTINCT ?x) AS ?count) WHERE { { ?x wdt:P180 wd:Q41421 . ?x wdt:P31 wd:Q838948 . } UNION { ?x wdt:P180 wd:Q81938 . ?x wdt:P31 wd:Q838948 . } }"
def _get_conversation_history_03() -> str:
return """Conversation history:
USER: Which administrative territory is the native country of Cirilo Villaverde ?
SYSTEM: {'Q241': 'Cuba'}
"""
def _get_input_question_03() -> str:
return "Input question: Which is the national anthem of that administrative territory ?"
def _get_entities_03() -> str:
return "Entities: {'Q241': 'Cuba'}"
def _get_relations_03() -> str:
return "Relations: {'P85': 'anthem'}"
def _get_types_03() -> str:
return "Types: {'Q484692': 'hymn'}"
def _get_sparql_03() -> str:
return "SPARQL query: SELECT ?x WHERE { wd:Q241 wdt:P85 ?x . ?x wdt:P31 wd:Q484692 . }"
def _get_input_question_04() -> str:
return "Input question: Which administrative territory was Pavel Astakhov born in ?"
def _get_entities_04() -> str:
return "Entities: {'Q4071605': 'Pavel Astakhov'}"
def _get_relations_04() -> str:
return "Relations: {'P19': 'place of birth'}"
def _get_types_04() -> str:
return "Types: {'Q56061': 'administrative territorial entity'}"
def _get_sparql_04() -> str:
return "SPARQL query: SELECT ?x WHERE { wd:Q4071605 wdt:P19 ?x . ?x wdt:P31 wd:Q56061 . }"