jondurbin commited on
Commit
3f8462e
1 Parent(s): d5ff8ce

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +316 -0
README.md CHANGED
@@ -165,6 +165,20 @@ I also didn't want to randomly select a single prompt format for each item (hopi
165
 
166
  This means each epoch of our fine-tune is the equivalent of 3 epochs.
167
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168
  <details>
169
  <summary>Llama-2 chat (recommended)</summary>
170
 
@@ -212,4 +226,306 @@ This means each epoch of our fine-tune is the equivalent of 3 epochs.
212
  {text}
213
  <|im_end|>{eos}
214
  ```
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
215
  </details>
 
165
 
166
  This means each epoch of our fine-tune is the equivalent of 3 epochs.
167
 
168
+ The default prompt format, which is specified in `chat_template` in the tokenizer config, is llama-2. You can use the `apply_chat_template` method to accurate format prompts, e.g.:
169
+
170
+ ```python
171
+ import transformers
172
+ tokenizer = transformers.AutoTokenizer.from_pretrained("jondurbin/bagel-7b-v0.4")
173
+ chat = [
174
+ {"role": "system", "content": "You are Bob, a friendly AI assistant."},
175
+ {"role": "user", "content": "Hello, how are you?"},
176
+ {"role": "assistant", "content": "I'm doing great. How can I help you today?"},
177
+ {"role": "user", "content": "I'd like to show off how chat templating works!"},
178
+ ]
179
+ print(tokenizer.apply_chat_template(chat, tokenize=False))
180
+ ```
181
+
182
  <details>
183
  <summary>Llama-2 chat (recommended)</summary>
184
 
 
226
  {text}
227
  <|im_end|>{eos}
228
  ```
229
+ </details>
230
+
231
+ ## Helpful usage tips
232
+
233
+ <details>
234
+ <summary>Context obedient question answering/RAG prompting</summary>
235
+
236
+ By obedient, I mean the model was trained to ignore what it thinks it knows, and uses the context to answer the question. The model was also tuned to limit the values to the provided context as much as possible to reduce hallucinations.
237
+
238
+ The format for a closed-context prompt is as follows:
239
+ ```
240
+ BEGININPUT
241
+ BEGINCONTEXT
242
+ [key0: value0]
243
+ [key1: value1]
244
+ ... other metdata ...
245
+ ENDCONTEXT
246
+ [insert your text blocks here]
247
+ ENDINPUT
248
+ [add as many other blocks, in the exact same format]
249
+ BEGININSTRUCTION
250
+ [insert your instruction(s). The model was tuned with single questions, paragraph format, lists, etc.]
251
+ ENDINSTRUCTION
252
+ ```
253
+
254
+ It's also helpful to add "Don't make up answers if you don't know." to your instruction block to make sure if the context is completely unrelated it doesn't make something up.
255
+
256
+ *The __only__ prompts that need this closed context formating are closed-context instructions. Normal questions/instructions do not!*
257
+
258
+ I know it's a bit verbose and annoying, but after much trial and error, using these explicit delimiters helps the model understand where to find the responses and how to associate specific sources with it.
259
+ - `BEGININPUT` - denotes a new input block
260
+ - `BEGINCONTEXT` - denotes the block of context (metadata key/value pairs) to associate with the current input block
261
+ - `ENDCONTEXT` - denotes the end of the metadata block for the current input
262
+ - [text] - Insert whatever text you want for the input block, as many paragraphs as can fit in the context.
263
+ - `ENDINPUT` - denotes the end of the current input block
264
+ - [repeat as many input blocks in this format as you want]
265
+ - `BEGININSTRUCTION` - denotes the start of the list (or one) instruction(s) to respond to for all of the input blocks above.
266
+ - [instruction(s)]
267
+ - `ENDINSTRUCTION` - denotes the end of instruction set
268
+
269
+ It sometimes works without `ENDINSTRUCTION`, but by explicitly including that in the prompt, the model better understands that all of the instructions in the block should be responded to.
270
+
271
+ __Use a very low temperature!__
272
+
273
+ Here's a trivial, but important example to prove the point:
274
+ ```
275
+ BEGININPUT
276
+ BEGINCONTEXT
277
+ date: 2021-01-01
278
+ url: https://web.site/123
279
+ ENDCONTEXT
280
+ In a shocking turn of events, blueberries are now green, but will be sticking with the same name.
281
+ ENDINPUT
282
+ BEGININSTRUCTION
283
+ What color are bluberries? Source?
284
+ ENDINSTRUCTION
285
+ ```
286
+
287
+ And the response:
288
+ ```
289
+ Blueberries are now green.
290
+ Source:
291
+ date: 2021-01-01
292
+ url: https://web.site/123
293
+ ```
294
+ </details>
295
+
296
+ <details>
297
+ <summary>Summarization</summary>
298
+
299
+ Summarization is primarily fine-tuned with [this dataset](https://huggingface.co/datasets/mattpscott/airoboros-summarization), which uses the same format as above, e.g.:
300
+ ```
301
+ BEGININPUT
302
+ {text to summarize}
303
+ ENDINPUT
304
+ BEGININSTRUCTION
305
+ Summarize the input in around 130 words.
306
+ ENDINSTRUCTION
307
+ ```
308
+ </details>
309
+
310
+ <details>
311
+ <summary>Function calling</summary>
312
+
313
+ There are two function-calling related formats used in fine-tuning this model.
314
+
315
+ 1. Providing an input and list of possible functions within the instruction (from airoboros dataset), e.g.:
316
+
317
+ Prompt:
318
+
319
+ ```text
320
+ As an AI assistant, please select the most suitable function and parameters from the list of available functions below, based on the user's input. Provide your response in JSON format.
321
+
322
+ Input: I want to know how many times 'Python' is mentioned in my text file.
323
+
324
+ Available functions:
325
+ file_analytics:
326
+ description: This tool performs various operations on a text file.
327
+ params:
328
+ action: The operation we want to perform on the data, such as "count_occurrences", "find_line", etc.
329
+ filters:
330
+ keyword: The word or phrase we want to search for.
331
+ ```
332
+
333
+ Response:
334
+ ```json
335
+ {
336
+ "function": "file_analytics",
337
+ "params": {
338
+ "action": "count_occurrences",
339
+ "filters": {
340
+ "keyword": "Python"
341
+ }
342
+ }
343
+ }
344
+ ```
345
+
346
+ 2. GlaiveAI function calling, which uses special tags and adds function specs in the system prompt, e.g. (llama2 prompt format):
347
+
348
+ Prompt:
349
+
350
+ ```text
351
+ [INST] <<SYS>>
352
+ You are a helpful assistant with access to the following functions. Use them if required -
353
+ {
354
+ "name": "generate_random_name",
355
+ "description": "Generate a random name",
356
+ "parameters": {
357
+ "type": "object",
358
+ "properties": {
359
+ "gender": {
360
+ "type": "string",
361
+ "description": "The gender of the name (e.g. male, female)"
362
+ }
363
+ },
364
+ "required": [
365
+ "gender"
366
+ ]
367
+ }
368
+ }
369
+ <</SYS>>
370
+
371
+ I need a random male name for my novel's character. [/INST]
372
+ ```
373
+
374
+ Response:
375
+
376
+ ```text
377
+ <|begin_func|> {"name": "generate_random_name", "arguments": '{"gender": "male"}'} <|end_func|>
378
+ ```
379
+
380
+ Then, you re-prompt the model with the function response.
381
+
382
+ ```text
383
+ [INST] <|begin_func_response|>{"name": "James"}<|end_func_response|>
384
+ ```
385
+
386
+ Which has a response of:
387
+ ```text
388
+ How about the name "James" for your novel's character? </s><s>[INST] That sounds good. Now, I need a female name too.
389
+ ```
390
+ </details>
391
+
392
+ <details>
393
+ <summary>Chain of thought</summary>
394
+
395
+ You can ask for several possible responses to a given problem, with a ranking and final answer selection.
396
+
397
+ Example prompt:
398
+
399
+ ```
400
+ A group of people decide to play a round-robin tournament where every player plays every other player exactly once. If a total of 45 games were played, how many players participated in the tournament? Offer several potential answers, rank them according to mathematical logic, and pick the most feasible one.
401
+ ```
402
+
403
+ Example response:
404
+ ```
405
+ Solution 1:
406
+ In a round-robin tournament, each player plays every other player exactly once. This means that the total number of games played is equal to the combination of all players taken two at a time (since a game involves two players).
407
+
408
+ The formula for combinations is nC2 = n(n-1)/2, where n is the total number of players. We know that nC2 equals 45 (the total number of games), so we can set up the equation as follows:
409
+
410
+ n(n-1)/2 = 45
411
+
412
+ Solving this equation gives us n=10.
413
+
414
+ Final answer: There were 10 players in the tournament.
415
+
416
+ Solution 2:
417
+ Another way to approach this problem is by using the concept of triangular numbers. Triangular numbers are a sequence of numbers where each number can be represented as a triangle with dots. The nth triangular number is given by the sum of the first n natural numbers.
418
+
419
+ If we consider each game as a dot and each player as a side of the triangle, then the total number of games (45) would correspond to the 9th triangular number because 1+2+3+4+5+6+7+8+9=45. However, since each side of the triangle represents a player, and there's one more player than sides in our model (because the last player has no one left to play against), we need to add one more to get the total number of players.
420
+
421
+ Final answer: There were 10 players in the tournament.
422
+
423
+ Solution 3:
424
+ We could also use trial and error to solve this problem. Starting from a small number of players, we calculate how many games would be played in a round-robin tournament until we reach 45 games. For example, with 2 players, only 1 game is played; with 3 players, 3 games are played; with 4 players, 6 games are played, and so on. Continuing this process, we find that with 10 players, 45 games are played.
425
+
426
+ Final answer: There were 10 players in the tournament.
427
+
428
+ Ranking of solutions from best to worst:
429
+ 1. Solution 1: This solution uses direct mathematical reasoning based on the properties of a round-robin tournament and solves an equation to find the answer.
430
+ 2. Solution 2: This solution uses the concept of triangular numbers which requires additional steps and knowledge beyond the immediate problem context, but still arrives at the correct answer.
431
+ 3. Solution 3: This solution relies on trial and error, which is less efficient and reliable than the other methods, but it eventually gets to the right answer.
432
+
433
+ Best and final answer: There were 10 players in the tournament.
434
+ ```
435
+ </details>
436
+
437
+ <details>
438
+ <summary>reWOO style execution planning</summary>
439
+
440
+ The model now supports execution planning for complex instructions that would require making use of several tools. The output is just the plan, you must implement a mechanism to parse the output and actually call the functions!
441
+
442
+ Example prompt:
443
+ ```
444
+ Please construct a systematic plan to generate an optimal response to the user instruction, utilizing a set of provided tools. Each plan will correspond to an evidence value, which will be the output of one of the available functions given an input string
445
+ that could be the user's question, one or more prior evidence values, or a combination of both.
446
+
447
+ Here are the tools available to be called:
448
+ DuckDuckGo[input]: Tool that enables the user to search for information using the DuckDuckGo search engine. This tool is helpful in locating a suitable list of sites that may contain the answer to the user's query. It does not directly answer the question but finds a list of sites that might have the answer.
449
+ TextScraper[input]: Load one or more websites from the input string containing newline delimited links, where input is one or more links, and produces plain text output containing the content of the links.
450
+ HyperlinkExtractor[input]: Extract links from plain text and produces a plain text, newline delimited response of links.
451
+ KnowledgeModel[input]: Question answering language model, particularly useful in answering questions based on an input passage of text. The input must be a text question that references an :evidence[n]: variable, e.g. What color is the cat, given :evidence1:?
452
+
453
+ The output should be in format:
454
+ Plan: [first action to take based on input question]
455
+ :evidence0: = [function to call with input parameter]
456
+ Plan: [next action to take, based on result of :evidence0:]
457
+ :evidence1: = [next function to call and input parameter, which may include reference to previous evidence, e.g. "Given context :evidence0"]
458
+ ...
459
+ Answer: [:evidence[n]: containing the final answer.]
460
+
461
+ Question: Who won the Nobel Prize in Literature in 2020 and what is their most famous work?
462
+ ```
463
+
464
+ Response:
465
+ ```
466
+ Plan: Begin by conducting a web search to find out who won the Nobel Prize in Literature in 2020.
467
+ :evidence0: = DuckDuckGo[Nobel Prize in Literature 2020 winner]
468
+ Plan: Utilize the KnowledgeModel to interpret the search results and find the name of the laureate.
469
+ :evidence1: = KnowledgeModel[Who won the Nobel Prize in Literature in 2020, given :evidence0:?]
470
+ Plan: Conduct another web search to find the most famous work of the identified laureate.
471
+ :evidence2: = DuckDuckGo[Most famous work of :evidence1:]
472
+ Plan: Extract the relevant links from the DuckDuckGo search results for a more focused search.
473
+ :evidence3: = HyperlinkExtractor[:evidence2:]
474
+ Plan: Use the TextScraper tool to extract information from the relevant links.
475
+ :evidence4: = TextScraper[:evidence3:]
476
+ Plan: Finally, utilize the KnowledgeModel to identify and summarize the most famous work of the laureate from the extracted information.
477
+ :evidence5: = KnowledgeModel[What is the most famous work of :evidence1:, given :evidence4:?]
478
+ Answer: :evidence5:
479
+ ```
480
+
481
+ For this to be useful, you'd have to parse the output plan text, and implement/call each of the functions. This is just pseudo-code, completely untested off the top of my head, and obviously would requiring full implementation + hardening:
482
+
483
+ ```python
484
+ import re
485
+ import requests
486
+
487
+ def inject_context(input_text, **context):
488
+ for ref in set(re.findall(r"(:evidence[0-9]+:)", input_text, re.I)):
489
+ input_text = input_text.replace(ref, context.get(ref, ""))
490
+ return input_text
491
+
492
+ def duckduckgo(input_text, **context):
493
+ search_string = inject_context(input_text, **context)
494
+ ... search via duck duck go using search_string
495
+ ... return text content
496
+
497
+ def link_extractor(input_text, **context):
498
+ input_text = inject_context(input_text, **context)
499
+ return "\n".join(list(set(re.findall(r"(https?://[^\s]+?\.?)", input_text, re.I))))
500
+
501
+ def scrape(input_text, **context):
502
+ input_text = inject_context(input_text, **context)
503
+ text = []
504
+ for link in input_text.splitlines():
505
+ text.append(requests.get(link).text)
506
+ return "\n".join(text)
507
+
508
+ def infer(input_text, **context)
509
+ prompt = inject_context(input_text, **context)
510
+ ... call model with prompt, return output
511
+
512
+ def parse_plan(plan):
513
+ method_map = {
514
+ "DuckDuckGo": duckduckgo,
515
+ "HyperlinkExtractor": link_extractor,
516
+ "KnowledgeModel": infer,
517
+ "TextScraper": scrape,
518
+ }
519
+ context = {}
520
+ for line in plan.strip().splitlines():
521
+ if line.startswith("Plan:"):
522
+ print(line)
523
+ continue
524
+ parts = re.match("^(:evidence[0-9]+:)\s*=\s*([^\[]+])(\[.*\])\s$", line, re.I)
525
+ if not parts:
526
+ if line.startswith("Answer: "):
527
+ return context.get(line.split(" ")[-1].strip(), "Answer couldn't be generated...")
528
+ raise RuntimeError("bad format: " + line)
529
+ context[parts.group(1)] = method_map[parts.group(2)](parts.group(3), **context)
530
+ ```
531
  </details>