prince-canuma commited on
Commit
5a852e2
1 Parent(s): 51b178f

Add RAG and Tool use example

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