llama-index
149 строк · 4.6 Кб
1"""Prompts from evaporate repo.
2
3
4Full credits go to: https://github.com/HazyResearch/evaporate
5
6
7"""
8
9from llama_index.legacy.prompts import PromptTemplate
10
11# deprecated, kept for backward compatibility
12
13"""Pandas PromptTemplate. Convert query to python code.
14
15Required template variables: `chunk`, `topic`.
16
17Args:
18template (str): Template for the PromptTemplate.
19**prompt_kwargs: Keyword arguments for the PromptTemplate.
20
21"""
22SchemaIDPrompt = PromptTemplate
23
24"""Function generation PromptTemplate. Generate a function from existing text.
25
26Required template variables: `context_str`, `query_str`,
27`attribute`, `function_field`.
28
29Args:
30template (str): Template for the PromptTemplate.
31**prompt_kwargs: Keyword arguments for the PromptTemplate.
32
33"""
34FnGeneratePrompt = PromptTemplate
35
36# used for schema identification
37SCHEMA_ID_PROMPT_TMPL = f"""Sample text:
38<tr class="mergedrow"><th scope="row" class="infobox-label"><div style="text-indent:-0.9em;margin-left:1.2em;font-weight:normal;">• <a href="/wiki/Monarchy_of_Canada" title="Monarchy of Canada">Monarch</a> </div></th><td class="infobox-data"><a href="/wiki/Charles_III" title="Charles III">Charles III</a></td></tr>
39<tr class="mergedrow"><th scope="row" class="infobox-label"><div style="text-indent:-0.9em;margin-left:1.2em;font-weight:normal;">• <span class="nowrap"><a href="/wiki/Governor_General_of_Canada" title="Governor General of Canada">Governor General</a></span> </div></th><td class="infobox-data"><a href="/wiki/Mary_Simon" title="Mary Simon">Mary Simon</a></td></tr>
40<b>Provinces and Territories</b class='navlinking countries'>
41<ul>
42<li>Saskatchewan</li>
43<li>Manitoba</li>
44<li>Ontario</li>
45<li>Quebec</li>
46<li>New Brunswick</li>
47<li>Prince Edward Island</li>
48<li>Nova Scotia</li>
49<li>Newfoundland and Labrador</li>
50<li>Yukon</li>
51<li>Nunavut</li>
52<li>Northwest Territories</li>
53</ul>
54
55Question: List all relevant attributes about 'Canada' that are exactly mentioned in this sample text if any.
56Answer:
57- Monarch: Charles III
58- Governor General: Mary Simon
59- Provinces and Territories: Saskatchewan, Manitoba, Ontario, Quebec, New Brunswick, Prince Edward Island, Nova Scotia, Newfoundland and Labrador, Yukon, Nunavut, Northwest Territories
60
61----
62
63Sample text:
64Patient birth date: 1990-01-01
65Prescribed medication: aspirin, ibuprofen, acetaminophen
66Prescribed dosage: 1 tablet, 2 tablets, 3 tablets
67Doctor's name: Dr. Burns
68Date of discharge: 2020-01-01
69Hospital address: 123 Main Street, New York, NY 10001
70
71Question: List all relevant attributes about 'medications' that are exactly mentioned in this sample text if any.
72Answer:
73- Prescribed medication: aspirin, ibuprofen, acetaminophen
74- Prescribed dosage: 1 tablet, 2 tablets, 3 tablets
75
76----
77
78Sample text:
79{{chunk:}}
80
81Question: List all relevant attributes about '{{topic:}}' that are exactly mentioned in this sample text if any.
82Answer:"""
83
84SCHEMA_ID_PROMPT = PromptTemplate(SCHEMA_ID_PROMPT_TMPL)
85
86
87# used for function generation
88
89FN_GENERATION_PROMPT_TMPL = f"""Here is a sample of text:
90
91{{context_str:}}
92
93
94Question: {{query_str:}}
95
96Given the function signature, write Python code to extract the
97"{{attribute:}}" field from the text.
98Return the result as a single value (string, int, float), and not a list.
99Make sure there is a return statement in the code. Do not leave out a return statement.
100{{expected_output_str:}}
101
102import re
103
104def get_{{function_field:}}_field(text: str):
105\"""
106Function to extract the "{{attribute:}} field", and return the result
107as a single value.
108\"""
109"""
110
111FN_GENERATION_PROMPT = PromptTemplate(FN_GENERATION_PROMPT_TMPL)
112
113
114FN_GENERATION_LIST_PROMPT_TMPL = f"""Here is a sample of text:
115
116{{context_str:}}
117
118
119Question: {{query_str:}}
120
121Given the function signature, write Python code to extract the
122"{{attribute:}}" field from the text.
123Return the result as a list of values (if there is just one item, return a single \
124element list).
125Make sure there is a return statement in the code. Do not leave out a return statement.
126{{expected_output_str:}}
127
128import re
129
130def get_{{function_field:}}_field(text: str) -> List:
131\"""
132Function to extract the "{{attribute:}} field", and return the result
133as a single value.
134\"""
135"""
136
137FN_GENERATION_LIST_PROMPT = PromptTemplate(FN_GENERATION_LIST_PROMPT_TMPL)
138
139DEFAULT_EXPECTED_OUTPUT_PREFIX_TMPL = (
140"Here is the expected output on the text after running the function. "
141"Please do not write a function that would return a different output. "
142"Expected output: "
143)
144
145
146DEFAULT_FIELD_EXTRACT_QUERY_TMPL = (
147'Write a python function to extract the entire "{field}" field from text, '
148"but not any other metadata. Return the result as a list."
149)
150