prince-canuma commited on
Commit
7ee6ce2
1 Parent(s): cd825a9

Add tool and rag use prompts

Browse files
Files changed (1) hide show
  1. README.md +200 -0
README.md CHANGED
@@ -55,6 +55,206 @@ gen_text = tokenizer.decode(gen_tokens[0])
55
  print(gen_text)
56
  ```
57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  ## Model Details
59
 
60
  **Input**: Models input text only.
 
55
  print(gen_text)
56
  ```
57
 
58
+
59
+ ## Tool use 🛠️
60
+
61
+ ```python
62
+ # pip install transformers
63
+ from transformers import AutoTokenizer, AutoModelForCausalLM
64
+
65
+ model_id = "prince-canuma/c4ai-command-r-v01-4bit"
66
+ tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
67
+ model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True)
68
+
69
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
70
+
71
+
72
+ # Format message with the command-r tool use template
73
+ conversation = [
74
+ {"role": "user", "content": "Whats the biggest penguin in the world?"}
75
+ ]
76
+ # Define tools available for the model to use:
77
+ tools = [
78
+ {
79
+ "name": "internet_search",
80
+ "description": "Returns a list of relevant document snippets for a textual query retrieved from the internet",
81
+ "parameter_definitions": {
82
+ "query": {
83
+ "description": "Query to search the internet with",
84
+ "type": 'str',
85
+ "required": True
86
+ }
87
+ }
88
+ },
89
+ {
90
+ 'name': "directly_answer",
91
+ "description": "Calls a standard (un-augmented) AI chatbot to generate a response given the conversation history",
92
+ 'parameter_definitions': {}
93
+ }
94
+ ]
95
+
96
+ formatted_input = tokenizer.apply_tool_use_template(conversation, tools=tools, tokenize=False, add_generation_prompt=True)
97
+ input_ids = tokenizer(formatted_input, return_tensors='pt')['input_ids'].to(device)
98
+
99
+ outputs = model.generate(
100
+ input_ids,
101
+ max_new_tokens=100,
102
+ )
103
+
104
+ print("Output:\n" + 100 * '-')
105
+ print(tokenizer.decode(outputs[0], skip_special_tokens=False))
106
+ ```
107
+
108
+ <details>
109
+ <summary><b> Prompt [CLICK TO EXPAND]</b></summary>
110
+
111
+ ````
112
+ <|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|># Safety Preamble
113
+ The instructions in this section override those in the task description and style guide sections. Don't answer questions that are harmful or immoral.
114
+
115
+ # System Preamble
116
+ ## Basic Rules
117
+ You are a powerful conversational AI trained by Cohere to help people. You are augmented by a number of tools, and your job is to use and consume the output of these tools to best help the user. You will see a conversation history between yourself and a user, ending with an utterance from the user. You will then see a specific instruction instructing you what kind of response to generate. When you answer the user's requests, you cite your sources in your answers, according to those instructions.
118
+
119
+ # User Preamble
120
+ ## Task and Context
121
+ You help people answer their questions and other requests interactively. You will be asked a very wide array of requests on all kinds of topics. You will be equipped with a wide range of search engines or similar tools to help you, which you use to research your answer. You should focus on serving the user's needs as best you can, which will be wide-ranging.
122
+
123
+ ## Style Guide
124
+ Unless the user asks for a different style of answer, you should answer in full sentences, using proper grammar and spelling.
125
+
126
+ ## Available Tools
127
+ Here is a list of tools that you have available to you:
128
+
129
+ ```python
130
+ def internet_search(query: str) -> List[Dict]:
131
+ """Returns a list of relevant document snippets for a textual query retrieved from the internet
132
+
133
+ Args:
134
+ query (str): Query to search the internet with
135
+ """
136
+ pass
137
+ ```
138
+
139
+ ```python
140
+ def directly_answer() -> List[Dict]:
141
+ """Calls a standard (un-augmented) AI chatbot to generate a response given the conversation history
142
+ """
143
+ pass
144
+ ```<|START_OF_TURN_TOKEN|><|USER_TOKEN|>Whats the biggest penguin in the world?<|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|>Write 'Action:' followed by a json-formatted list of actions that you want to perform in order to produce a good response to the user's last input. You can use any of the supplied tools any number of times, but you should aim to execute the minimum number of necessary actions for the input. You should use the `directly-answer` tool if calling the other tools is unnecessary. The list of actions you want to call should be formatted as a list of json objects, for example:
145
+ ```json
146
+ [
147
+ {
148
+ "tool_name": title of the tool in the specification,
149
+ "parameters": a dict of parameters to input into the tool as they are defined in the specs, or {} if it takes no parameters
150
+ }
151
+ ]```<|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>
152
+ ````
153
+ </details>
154
+
155
+ <summary><b> Output </b></summary>
156
+
157
+ ````
158
+ Output:
159
+ ----------------------------------------------------------------------------------------------------
160
+ Action:```json
161
+ [
162
+ {
163
+ "tool_name": "internet_search",
164
+ "parameters": {
165
+ "query": "biggest penguin in the world"
166
+ }
167
+ }
168
+ ]
169
+ ```<|END_OF_TURN_TOKEN|>
170
+ ````
171
+
172
+
173
+ ## RAG use 📚
174
+
175
+ ```python
176
+ # pip install transformers
177
+ from transformers import AutoTokenizer, AutoModelForCausalLM
178
+
179
+ model_id = "prince-canuma/c4ai-command-r-v01-4bit"
180
+ tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
181
+ model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True)
182
+
183
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
184
+
185
+
186
+ # Format message with the command-r tool use template
187
+ conversation = [
188
+ {"role": "user", "content": "Whats the biggest penguin in the world?"}
189
+ ]
190
+ # define documents to ground on:
191
+ documents = [
192
+ { "title": "Tall penguins", "text": "Emperor penguins are the tallest growing up to 122 cm in height." },
193
+ { "title": "Penguin habitats", "text": "Emperor penguins only live in Antarctica."}
194
+ ]
195
+
196
+
197
+ formatted_input = grounded_generation_prompt = tokenizer.apply_grounded_generation_template(
198
+ conversation,
199
+ documents=documents,
200
+ citation_mode="accurate", # or "fast"
201
+ tokenize=False,
202
+ add_generation_prompt=True,
203
+ )
204
+ input_ids = tokenizer(formatted_input, return_tensors='pt')['input_ids'].to(device)
205
+
206
+ outputs = model.generate(
207
+ input_ids,
208
+ max_new_tokens=100,
209
+ )
210
+
211
+ print("Output:\n" + 100 * '-')
212
+ print(tokenizer.decode(outputs[0], skip_special_tokens=False))
213
+ ```
214
+
215
+ <details>
216
+ <summary><b> Prompt [CLICK TO EXPAND]</b></summary>
217
+
218
+ ````
219
+ <BOS_TOKEN><BOS_TOKEN><|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|># Safety Preamble
220
+ The instructions in this section override those in the task description and style guide sections. Don't answer questions that are harmful or immoral.
221
+
222
+ # System Preamble
223
+ ## Basic Rules
224
+ You are a powerful conversational AI trained by Cohere to help people. You are augmented by a number of tools, and your job is to use and consume the output of these tools to best help the user. You will see a conversation history between yourself and a user, ending with an utterance from the user. You will then see a specific instruction instructing you what kind of response to generate. When you answer the user's requests, you cite your sources in your answers, according to those instructions.
225
+
226
+ # User Preamble
227
+ ## Task and Context
228
+ You help people answer their questions and other requests interactively. You will be asked a very wide array of requests on all kinds of topics. You will be equipped with a wide range of search engines or similar tools to help you, which you use to research your answer. You should focus on serving the user's needs as best you can, which will be wide-ranging.
229
+
230
+ ## Style Guide
231
+ Unless the user asks for a different style of answer, you should answer in full sentences, using proper grammar and spelling.<|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|USER_TOKEN|>Whats the biggest penguin in the world?<|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|><results>
232
+ Document: 0
233
+ title: Tall penguins
234
+ text: Emperor penguins are the tallest growing up to 122 cm in height.
235
+
236
+ Document: 1
237
+ title: Penguin habitats
238
+ text: Emperor penguins only live in Antarctica.
239
+ </results><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|>Carefully perform the following instructions, in order, starting each with a new line.
240
+ Firstly, Decide which of the retrieved documents are relevant to the user's last input by writing 'Relevant Documents:' followed by comma-separated list of document numbers. If none are relevant, you should instead write 'None'.
241
+ Secondly, Decide which of the retrieved documents contain facts that should be cited in a good answer to the user's last input by writing 'Cited Documents:' followed a comma-separated list of document numbers. If you dont want to cite any of them, you should instead write 'None'.
242
+ Thirdly, Write 'Answer:' followed by a response to the user's last input in high quality natural english. Use the retrieved documents to help you. Do not insert any citations or grounding markup.
243
+ Finally, Write 'Grounded answer:' followed by a response to the user's last input in high quality natural english. Use the symbols <co: doc> and </co: doc> to indicate when a fact comes from a document in the search result, e.g <co: 0>my fact</co: 0> for a fact from document 0.<|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>
244
+ ````
245
+ </details>
246
+
247
+ <summary><b> Output </b></summary>
248
+
249
+ ````
250
+ Output:
251
+ ----------------------------------------------------------------------------------------------------
252
+ Relevant Documents: 0,1
253
+ Cited Documents: 0
254
+ Answer: The tallest species of penguin in the world is the emperor penguin (Aptenodytes forsteri), which can reach heights of up to 122 cm.
255
+ Grounded answer: The tallest species of penguin in the world is the <co: 0>emperor penguin</co: 0> <co: 0>(Aptenodytes forsteri)</co: 0>, which can reach <co: 0>heights of up to 122 cm.</co: 0><|END_OF_TURN_TOKEN|>
256
+ ````
257
+
258
  ## Model Details
259
 
260
  **Input**: Models input text only.