prince-canuma commited on
Commit
12581cc
1 Parent(s): 236b6b0

update template examples [tools and RAG]

Browse files
Files changed (1) hide show
  1. README.md +169 -0
README.md CHANGED
@@ -34,3 +34,172 @@ from mlx_lm import load, generate
34
  model, tokenizer = load("mlx-community/c4ai-command-r-plus-4bit")
35
  response = generate(model, tokenizer, prompt="hello", verbose=True)
36
  ```
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  model, tokenizer = load("mlx-community/c4ai-command-r-plus-4bit")
35
  response = generate(model, tokenizer, prompt="hello", verbose=True)
36
  ```
37
+
38
+ ## Tool use 🛠️
39
+
40
+ ```python
41
+ from mlx_lm import load, generate
42
+
43
+ model, tokenizer = load("mlx-community/c4ai-command-r-v01-4bit")
44
+
45
+ # Format message with the command-r tool use template
46
+ conversation = [
47
+ {"role": "user", "content": "Whats the biggest penguin in the world?"}
48
+ ]
49
+ # Define tools available for the model to use:
50
+ tools = [
51
+ {
52
+ "name": "internet_search",
53
+ "description": "Returns a list of relevant document snippets for a textual query retrieved from the internet",
54
+ "parameter_definitions": {
55
+ "query": {
56
+ "description": "Query to search the internet with",
57
+ "type": 'str',
58
+ "required": True
59
+ }
60
+ }
61
+ },
62
+ {
63
+ 'name': "directly_answer",
64
+ "description": "Calls a standard (un-augmented) AI chatbot to generate a response given the conversation history",
65
+ 'parameter_definitions': {}
66
+ }
67
+ ]
68
+
69
+ formatted_input = tokenizer.apply_tool_use_template(conversation, tools=tools, tokenize=False, add_generation_prompt=True)
70
+ response = generate(model, tokenizer, prompt=formatted_input, verbose=True)
71
+ ```
72
+
73
+ <details>
74
+ <summary><b> Prompt [CLICK TO EXPAND]</b></summary>
75
+
76
+ ````
77
+ <|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|># Safety Preamble
78
+ The instructions in this section override those in the task description and style guide sections. Don't answer questions that are harmful or immoral.
79
+
80
+ # System Preamble
81
+ ## Basic Rules
82
+ 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.
83
+
84
+ # User Preamble
85
+ ## Task and Context
86
+ 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.
87
+
88
+ ## Style Guide
89
+ Unless the user asks for a different style of answer, you should answer in full sentences, using proper grammar and spelling.
90
+
91
+ ## Available Tools
92
+ Here is a list of tools that you have available to you:
93
+
94
+ ```python
95
+ def internet_search(query: str) -> List[Dict]:
96
+ """Returns a list of relevant document snippets for a textual query retrieved from the internet
97
+
98
+ Args:
99
+ query (str): Query to search the internet with
100
+ """
101
+ pass
102
+ ```
103
+
104
+ ```python
105
+ def directly_answer() -> List[Dict]:
106
+ """Calls a standard (un-augmented) AI chatbot to generate a response given the conversation history
107
+ """
108
+ pass
109
+ ```<|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:
110
+ ```json
111
+ [
112
+ {
113
+ "tool_name": title of the tool in the specification,
114
+ "parameters": a dict of parameters to input into the tool as they are defined in the specs, or {} if it takes no parameters
115
+ }
116
+ ]```<|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>
117
+ ````
118
+ </details>
119
+
120
+ <summary><b> Output </b></summary>
121
+
122
+ ````
123
+ Action:```json
124
+ [
125
+ {
126
+ "tool_name": "internet_search",
127
+ "parameters": {
128
+ "query": "biggest penguin in the world"
129
+ }
130
+ }
131
+ ]
132
+ ```
133
+ ````
134
+
135
+
136
+ ## RAG use 📚
137
+
138
+ ```python
139
+ from mlx_lm import load, generate
140
+
141
+ model, tokenizer = load("mlx-community/c4ai-command-r-v01-4bit")
142
+
143
+ # Format message with the command-r tool use template
144
+ conversation = [
145
+ {"role": "user", "content": "Whats the biggest penguin in the world?"}
146
+ ]
147
+ # define documents to ground on:
148
+ documents = [
149
+ { "title": "Tall penguins", "text": "Emperor penguins are the tallest growing up to 122 cm in height." },
150
+ { "title": "Penguin habitats", "text": "Emperor penguins only live in Antarctica."}
151
+ ]
152
+
153
+
154
+ formatted_input = tokenizer.apply_grounded_generation_template(
155
+ conversation,
156
+ documents=documents,
157
+ citation_mode="accurate", # or "fast"
158
+ tokenize=False,
159
+ add_generation_prompt=True,
160
+ )
161
+
162
+ response = generate(model, tokenizer, prompt=formatted_input, verbose=True)
163
+ ```
164
+
165
+ <details>
166
+ <summary><b> Prompt [CLICK TO EXPAND]</b></summary>
167
+
168
+ ````
169
+ <BOS_TOKEN><BOS_TOKEN><|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|># Safety Preamble
170
+ The instructions in this section override those in the task description and style guide sections. Don't answer questions that are harmful or immoral.
171
+
172
+ # System Preamble
173
+ ## Basic Rules
174
+ 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.
175
+
176
+ # User Preamble
177
+ ## Task and Context
178
+ 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.
179
+
180
+ ## Style Guide
181
+ 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>
182
+ Document: 0
183
+ title: Tall penguins
184
+ text: Emperor penguins are the tallest growing up to 122 cm in height.
185
+
186
+ Document: 1
187
+ title: Penguin habitats
188
+ text: Emperor penguins only live in Antarctica.
189
+ </results><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|>Carefully perform the following instructions, in order, starting each with a new line.
190
+ 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'.
191
+ 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'.
192
+ 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.
193
+ 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|>
194
+ ````
195
+ </details>
196
+
197
+ <summary><b> Output </b></summary>
198
+
199
+ ````
200
+
201
+ Relevant Documents: 0,1
202
+ Cited Documents: 0
203
+ Answer: The tallest species of penguin in the world is the emperor penguin (Aptenodytes forsteri), which can reach heights of up to 122 cm.
204
+ 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>
205
+ ````