llama-index

Форк
0
467 строк · 15.9 Кб
1
"""Set of default prompts."""
2

3
from llama_index.legacy.prompts.base import PromptTemplate
4
from llama_index.legacy.prompts.prompt_type import PromptType
5

6
############################################
7
# Tree
8
############################################
9

10
DEFAULT_SUMMARY_PROMPT_TMPL = (
11
    "Write a summary of the following. Try to use only the "
12
    "information provided. "
13
    "Try to include as many key details as possible.\n"
14
    "\n"
15
    "\n"
16
    "{context_str}\n"
17
    "\n"
18
    "\n"
19
    'SUMMARY:"""\n'
20
)
21

22
DEFAULT_SUMMARY_PROMPT = PromptTemplate(
23
    DEFAULT_SUMMARY_PROMPT_TMPL, prompt_type=PromptType.SUMMARY
24
)
25

26
# insert prompts
27
DEFAULT_INSERT_PROMPT_TMPL = (
28
    "Context information is below. It is provided in a numbered list "
29
    "(1 to {num_chunks}), "
30
    "where each item in the list corresponds to a summary.\n"
31
    "---------------------\n"
32
    "{context_list}"
33
    "---------------------\n"
34
    "Given the context information, here is a new piece of "
35
    "information: {new_chunk_text}\n"
36
    "Answer with the number corresponding to the summary that should be updated. "
37
    "The answer should be the number corresponding to the "
38
    "summary that is most relevant to the question.\n"
39
)
40
DEFAULT_INSERT_PROMPT = PromptTemplate(
41
    DEFAULT_INSERT_PROMPT_TMPL, prompt_type=PromptType.TREE_INSERT
42
)
43

44

45
# # single choice
46
DEFAULT_QUERY_PROMPT_TMPL = (
47
    "Some choices are given below. It is provided in a numbered list "
48
    "(1 to {num_chunks}), "
49
    "where each item in the list corresponds to a summary.\n"
50
    "---------------------\n"
51
    "{context_list}"
52
    "\n---------------------\n"
53
    "Using only the choices above and not prior knowledge, return "
54
    "the choice that is most relevant to the question: '{query_str}'\n"
55
    "Provide choice in the following format: 'ANSWER: <number>' and explain why "
56
    "this summary was selected in relation to the question.\n"
57
)
58
DEFAULT_QUERY_PROMPT = PromptTemplate(
59
    DEFAULT_QUERY_PROMPT_TMPL, prompt_type=PromptType.TREE_SELECT
60
)
61

62
# multiple choice
63
DEFAULT_QUERY_PROMPT_MULTIPLE_TMPL = (
64
    "Some choices are given below. It is provided in a numbered "
65
    "list (1 to {num_chunks}), "
66
    "where each item in the list corresponds to a summary.\n"
67
    "---------------------\n"
68
    "{context_list}"
69
    "\n---------------------\n"
70
    "Using only the choices above and not prior knowledge, return the top choices "
71
    "(no more than {branching_factor}, ranked by most relevant to least) that "
72
    "are most relevant to the question: '{query_str}'\n"
73
    "Provide choices in the following format: 'ANSWER: <numbers>' and explain why "
74
    "these summaries were selected in relation to the question.\n"
75
)
76
DEFAULT_QUERY_PROMPT_MULTIPLE = PromptTemplate(
77
    DEFAULT_QUERY_PROMPT_MULTIPLE_TMPL, prompt_type=PromptType.TREE_SELECT_MULTIPLE
78
)
79

80

81
DEFAULT_REFINE_PROMPT_TMPL = (
82
    "The original query is as follows: {query_str}\n"
83
    "We have provided an existing answer: {existing_answer}\n"
84
    "We have the opportunity to refine the existing answer "
85
    "(only if needed) with some more context below.\n"
86
    "------------\n"
87
    "{context_msg}\n"
88
    "------------\n"
89
    "Given the new context, refine the original answer to better "
90
    "answer the query. "
91
    "If the context isn't useful, return the original answer.\n"
92
    "Refined Answer: "
93
)
94
DEFAULT_REFINE_PROMPT = PromptTemplate(
95
    DEFAULT_REFINE_PROMPT_TMPL, prompt_type=PromptType.REFINE
96
)
97

98

99
DEFAULT_TEXT_QA_PROMPT_TMPL = (
100
    "Context information is below.\n"
101
    "---------------------\n"
102
    "{context_str}\n"
103
    "---------------------\n"
104
    "Given the context information and not prior knowledge, "
105
    "answer the query.\n"
106
    "Query: {query_str}\n"
107
    "Answer: "
108
)
109
DEFAULT_TEXT_QA_PROMPT = PromptTemplate(
110
    DEFAULT_TEXT_QA_PROMPT_TMPL, prompt_type=PromptType.QUESTION_ANSWER
111
)
112

113
DEFAULT_TREE_SUMMARIZE_TMPL = (
114
    "Context information from multiple sources is below.\n"
115
    "---------------------\n"
116
    "{context_str}\n"
117
    "---------------------\n"
118
    "Given the information from multiple sources and not prior knowledge, "
119
    "answer the query.\n"
120
    "Query: {query_str}\n"
121
    "Answer: "
122
)
123
DEFAULT_TREE_SUMMARIZE_PROMPT = PromptTemplate(
124
    DEFAULT_TREE_SUMMARIZE_TMPL, prompt_type=PromptType.SUMMARY
125
)
126

127

128
############################################
129
# Keyword Table
130
############################################
131

132
DEFAULT_KEYWORD_EXTRACT_TEMPLATE_TMPL = (
133
    "Some text is provided below. Given the text, extract up to {max_keywords} "
134
    "keywords from the text. Avoid stopwords."
135
    "---------------------\n"
136
    "{text}\n"
137
    "---------------------\n"
138
    "Provide keywords in the following comma-separated format: 'KEYWORDS: <keywords>'\n"
139
)
140
DEFAULT_KEYWORD_EXTRACT_TEMPLATE = PromptTemplate(
141
    DEFAULT_KEYWORD_EXTRACT_TEMPLATE_TMPL, prompt_type=PromptType.KEYWORD_EXTRACT
142
)
143

144

145
# NOTE: the keyword extraction for queries can be the same as
146
# the one used to build the index, but here we tune it to see if performance is better.
147
DEFAULT_QUERY_KEYWORD_EXTRACT_TEMPLATE_TMPL = (
148
    "A question is provided below. Given the question, extract up to {max_keywords} "
149
    "keywords from the text. Focus on extracting the keywords that we can use "
150
    "to best lookup answers to the question. Avoid stopwords.\n"
151
    "---------------------\n"
152
    "{question}\n"
153
    "---------------------\n"
154
    "Provide keywords in the following comma-separated format: 'KEYWORDS: <keywords>'\n"
155
)
156
DEFAULT_QUERY_KEYWORD_EXTRACT_TEMPLATE = PromptTemplate(
157
    DEFAULT_QUERY_KEYWORD_EXTRACT_TEMPLATE_TMPL,
158
    prompt_type=PromptType.QUERY_KEYWORD_EXTRACT,
159
)
160

161

162
############################################
163
# Structured Store
164
############################################
165

166
DEFAULT_SCHEMA_EXTRACT_TMPL = (
167
    "We wish to extract relevant fields from an unstructured text chunk into "
168
    "a structured schema. We first provide the unstructured text, and then "
169
    "we provide the schema that we wish to extract. "
170
    "-----------text-----------\n"
171
    "{text}\n"
172
    "-----------schema-----------\n"
173
    "{schema}\n"
174
    "---------------------\n"
175
    "Given the text and schema, extract the relevant fields from the text in "
176
    "the following format: "
177
    "field1: <value>\nfield2: <value>\n...\n\n"
178
    "If a field is not present in the text, don't include it in the output."
179
    "If no fields are present in the text, return a blank string.\n"
180
    "Fields: "
181
)
182
DEFAULT_SCHEMA_EXTRACT_PROMPT = PromptTemplate(
183
    DEFAULT_SCHEMA_EXTRACT_TMPL, prompt_type=PromptType.SCHEMA_EXTRACT
184
)
185

186
# NOTE: taken from langchain and adapted
187
# https://github.com/langchain-ai/langchain/blob/v0.0.303/libs/langchain/langchain/chains/sql_database/prompt.py
188
DEFAULT_TEXT_TO_SQL_TMPL = (
189
    "Given an input question, first create a syntactically correct {dialect} "
190
    "query to run, then look at the results of the query and return the answer. "
191
    "You can order the results by a relevant column to return the most "
192
    "interesting examples in the database.\n\n"
193
    "Never query for all the columns from a specific table, only ask for a "
194
    "few relevant columns given the question.\n\n"
195
    "Pay attention to use only the column names that you can see in the schema "
196
    "description. "
197
    "Be careful to not query for columns that do not exist. "
198
    "Pay attention to which column is in which table. "
199
    "Also, qualify column names with the table name when needed. "
200
    "You are required to use the following format, each taking one line:\n\n"
201
    "Question: Question here\n"
202
    "SQLQuery: SQL Query to run\n"
203
    "SQLResult: Result of the SQLQuery\n"
204
    "Answer: Final answer here\n\n"
205
    "Only use tables listed below.\n"
206
    "{schema}\n\n"
207
    "Question: {query_str}\n"
208
    "SQLQuery: "
209
)
210

211
DEFAULT_TEXT_TO_SQL_PROMPT = PromptTemplate(
212
    DEFAULT_TEXT_TO_SQL_TMPL,
213
    prompt_type=PromptType.TEXT_TO_SQL,
214
)
215

216
DEFAULT_TEXT_TO_SQL_PGVECTOR_TMPL = """\
217
Given an input question, first create a syntactically correct {dialect} \
218
query to run, then look at the results of the query and return the answer. \
219
You can order the results by a relevant column to return the most \
220
interesting examples in the database.
221

222
Pay attention to use only the column names that you can see in the schema \
223
description. Be careful to not query for columns that do not exist. \
224
Pay attention to which column is in which table. Also, qualify column names \
225
with the table name when needed.
226

227
IMPORTANT NOTE: you can use specialized pgvector syntax (`<->`) to do nearest \
228
neighbors/semantic search to a given vector from an embeddings column in the table. \
229
The embeddings value for a given row typically represents the semantic meaning of that row. \
230
The vector represents an embedding representation \
231
of the question, given below. Do NOT fill in the vector values directly, but rather specify a \
232
`[query_vector]` placeholder. For instance, some select statement examples below \
233
(the name of the embeddings column is `embedding`):
234
SELECT * FROM items ORDER BY embedding <-> '[query_vector]' LIMIT 5;
235
SELECT * FROM items WHERE id != 1 ORDER BY embedding <-> (SELECT embedding FROM items WHERE id = 1) LIMIT 5;
236
SELECT * FROM items WHERE embedding <-> '[query_vector]' < 5;
237

238
You are required to use the following format, \
239
each taking one line:
240

241
Question: Question here
242
SQLQuery: SQL Query to run
243
SQLResult: Result of the SQLQuery
244
Answer: Final answer here
245

246
Only use tables listed below.
247
{schema}
248

249

250
Question: {query_str}
251
SQLQuery: \
252
"""
253

254
DEFAULT_TEXT_TO_SQL_PGVECTOR_PROMPT = PromptTemplate(
255
    DEFAULT_TEXT_TO_SQL_PGVECTOR_TMPL,
256
    prompt_type=PromptType.TEXT_TO_SQL,
257
)
258

259

260
# NOTE: by partially filling schema, we can reduce to a QuestionAnswer prompt
261
# that we can feed to ur table
262
DEFAULT_TABLE_CONTEXT_TMPL = (
263
    "We have provided a table schema below. "
264
    "---------------------\n"
265
    "{schema}\n"
266
    "---------------------\n"
267
    "We have also provided context information below. "
268
    "{context_str}\n"
269
    "---------------------\n"
270
    "Given the context information and the table schema, "
271
    "give a response to the following task: {query_str}"
272
)
273

274
DEFAULT_TABLE_CONTEXT_QUERY = (
275
    "Provide a high-level description of the table, "
276
    "as well as a description of each column in the table. "
277
    "Provide answers in the following format:\n"
278
    "TableDescription: <description>\n"
279
    "Column1Description: <description>\n"
280
    "Column2Description: <description>\n"
281
    "...\n\n"
282
)
283

284
DEFAULT_TABLE_CONTEXT_PROMPT = PromptTemplate(
285
    DEFAULT_TABLE_CONTEXT_TMPL, prompt_type=PromptType.TABLE_CONTEXT
286
)
287

288
# NOTE: by partially filling schema, we can reduce to a refine prompt
289
# that we can feed to ur table
290
DEFAULT_REFINE_TABLE_CONTEXT_TMPL = (
291
    "We have provided a table schema below. "
292
    "---------------------\n"
293
    "{schema}\n"
294
    "---------------------\n"
295
    "We have also provided some context information below. "
296
    "{context_msg}\n"
297
    "---------------------\n"
298
    "Given the context information and the table schema, "
299
    "give a response to the following task: {query_str}\n"
300
    "We have provided an existing answer: {existing_answer}\n"
301
    "Given the new context, refine the original answer to better "
302
    "answer the question. "
303
    "If the context isn't useful, return the original answer."
304
)
305
DEFAULT_REFINE_TABLE_CONTEXT_PROMPT = PromptTemplate(
306
    DEFAULT_REFINE_TABLE_CONTEXT_TMPL, prompt_type=PromptType.TABLE_CONTEXT
307
)
308

309

310
############################################
311
# Knowledge-Graph Table
312
############################################
313

314
DEFAULT_KG_TRIPLET_EXTRACT_TMPL = (
315
    "Some text is provided below. Given the text, extract up to "
316
    "{max_knowledge_triplets} "
317
    "knowledge triplets in the form of (subject, predicate, object). Avoid stopwords.\n"
318
    "---------------------\n"
319
    "Example:"
320
    "Text: Alice is Bob's mother."
321
    "Triplets:\n(Alice, is mother of, Bob)\n"
322
    "Text: Philz is a coffee shop founded in Berkeley in 1982.\n"
323
    "Triplets:\n"
324
    "(Philz, is, coffee shop)\n"
325
    "(Philz, founded in, Berkeley)\n"
326
    "(Philz, founded in, 1982)\n"
327
    "---------------------\n"
328
    "Text: {text}\n"
329
    "Triplets:\n"
330
)
331
DEFAULT_KG_TRIPLET_EXTRACT_PROMPT = PromptTemplate(
332
    DEFAULT_KG_TRIPLET_EXTRACT_TMPL,
333
    prompt_type=PromptType.KNOWLEDGE_TRIPLET_EXTRACT,
334
)
335

336
############################################
337
# HYDE
338
##############################################
339

340
HYDE_TMPL = (
341
    "Please write a passage to answer the question\n"
342
    "Try to include as many key details as possible.\n"
343
    "\n"
344
    "\n"
345
    "{context_str}\n"
346
    "\n"
347
    "\n"
348
    'Passage:"""\n'
349
)
350

351
DEFAULT_HYDE_PROMPT = PromptTemplate(HYDE_TMPL, prompt_type=PromptType.SUMMARY)
352

353

354
############################################
355
# Simple Input
356
############################################
357

358
DEFAULT_SIMPLE_INPUT_TMPL = "{query_str}"
359
DEFAULT_SIMPLE_INPUT_PROMPT = PromptTemplate(
360
    DEFAULT_SIMPLE_INPUT_TMPL, prompt_type=PromptType.SIMPLE_INPUT
361
)
362

363

364
############################################
365
# Pandas
366
############################################
367

368
DEFAULT_PANDAS_TMPL = (
369
    "You are working with a pandas dataframe in Python.\n"
370
    "The name of the dataframe is `df`.\n"
371
    "This is the result of `print(df.head())`:\n"
372
    "{df_str}\n\n"
373
    "Follow these instructions:\n"
374
    "{instruction_str}\n"
375
    "Query: {query_str}\n\n"
376
    "Expression:"
377
)
378

379
DEFAULT_PANDAS_PROMPT = PromptTemplate(
380
    DEFAULT_PANDAS_TMPL, prompt_type=PromptType.PANDAS
381
)
382

383

384
############################################
385
# JSON Path
386
############################################
387

388
DEFAULT_JSON_PATH_TMPL = (
389
    "We have provided a JSON schema below:\n"
390
    "{schema}\n"
391
    "Given a task, respond with a JSON Path query that "
392
    "can retrieve data from a JSON value that matches the schema.\n"
393
    "Task: {query_str}\n"
394
    "JSONPath: "
395
)
396

397
DEFAULT_JSON_PATH_PROMPT = PromptTemplate(
398
    DEFAULT_JSON_PATH_TMPL, prompt_type=PromptType.JSON_PATH
399
)
400

401

402
############################################
403
# Choice Select
404
############################################
405

406
DEFAULT_CHOICE_SELECT_PROMPT_TMPL = (
407
    "A list of documents is shown below. Each document has a number next to it along "
408
    "with a summary of the document. A question is also provided. \n"
409
    "Respond with the numbers of the documents "
410
    "you should consult to answer the question, in order of relevance, as well \n"
411
    "as the relevance score. The relevance score is a number from 1-10 based on "
412
    "how relevant you think the document is to the question.\n"
413
    "Do not include any documents that are not relevant to the question. \n"
414
    "Example format: \n"
415
    "Document 1:\n<summary of document 1>\n\n"
416
    "Document 2:\n<summary of document 2>\n\n"
417
    "...\n\n"
418
    "Document 10:\n<summary of document 10>\n\n"
419
    "Question: <question>\n"
420
    "Answer:\n"
421
    "Doc: 9, Relevance: 7\n"
422
    "Doc: 3, Relevance: 4\n"
423
    "Doc: 7, Relevance: 3\n\n"
424
    "Let's try this now: \n\n"
425
    "{context_str}\n"
426
    "Question: {query_str}\n"
427
    "Answer:\n"
428
)
429
DEFAULT_CHOICE_SELECT_PROMPT = PromptTemplate(
430
    DEFAULT_CHOICE_SELECT_PROMPT_TMPL, prompt_type=PromptType.CHOICE_SELECT
431
)
432

433

434
############################################
435
# RankGPT Rerank template
436
############################################
437

438
RANKGPT_RERANK_PROMPT_TMPL = (
439
    "Search Query: {query}. \nRank the {num} passages above "
440
    "based on their relevance to the search query. The passages "
441
    "should be listed in descending order using identifiers. "
442
    "The most relevant passages should be listed first. "
443
    "The output format should be [] > [], e.g., [1] > [2]. "
444
    "Only response the ranking results, "
445
    "do not say any word or explain."
446
)
447
RANKGPT_RERANK_PROMPT = PromptTemplate(
448
    RANKGPT_RERANK_PROMPT_TMPL, prompt_type=PromptType.RANKGPT_RERANK
449
)
450

451

452
############################################
453
# JSONalyze Query Template
454
############################################
455

456
DEFAULT_JSONALYZE_PROMPT_TMPL = (
457
    "You are given a table named: '{table_name}' with schema, "
458
    "generate SQLite SQL query to answer the given question.\n"
459
    "Table schema:\n"
460
    "{table_schema}\n"
461
    "Question: {question}\n\n"
462
    "SQLQuery: "
463
)
464

465
DEFAULT_JSONALYZE_PROMPT = PromptTemplate(
466
    DEFAULT_JSONALYZE_PROMPT_TMPL, prompt_type=PromptType.TEXT_TO_SQL
467
)
468

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.