|
| 1 | +""" |
| 2 | +Example usage of the citations intrinsic for RAG applications. |
| 3 | +
|
| 4 | +To run this script from the root of the Mellea source tree, use the command: |
| 5 | +``` |
| 6 | +uv run python docs/examples/intrinsics/citations.py |
| 7 | +``` |
| 8 | +""" |
| 9 | + |
| 10 | +from mellea.backends.huggingface import LocalHFBackend |
| 11 | +from mellea.stdlib.base import ChatContext, Document |
| 12 | +from mellea.stdlib.chat import Message |
| 13 | +from mellea.stdlib.intrinsics import rag |
| 14 | +import json |
| 15 | + |
| 16 | + |
| 17 | +backend = LocalHFBackend(model_id="ibm-granite/granite-3.3-2b-instruct") |
| 18 | +context = ChatContext().add( |
| 19 | + Message( |
| 20 | + "user", |
| 21 | + "How does Murdoch's expansion in Australia compare to his expansion " |
| 22 | + "in New Zealand?", |
| 23 | + ) |
| 24 | +) |
| 25 | +assistant_response = ( |
| 26 | + "Murdoch expanded in Australia and New Zealand by acquiring and expanding local " |
| 27 | + "newspapers. I do not have information about his expansion in New Zealand after " |
| 28 | + "purchasing The Dominion." |
| 29 | +) |
| 30 | +documents = [ |
| 31 | + Document( |
| 32 | + doc_id="1", |
| 33 | + text="Keith Rupert Murdoch was born on 11 March 1931 in Melbourne, Australia, " |
| 34 | + "the son of Sir Keith Murdoch (1885-1952) and Dame Elisabeth Murdoch (nee " |
| 35 | + "Greene; 1909-2012). He is of English, Irish, and Scottish ancestry. Murdoch's " |
| 36 | + "parents were also born in Melbourne. Keith Murdoch was a war correspondent " |
| 37 | + "and later a regional newspaper magnate owning two newspapers in Adelaide, " |
| 38 | + "South Australia, and a radio station in a faraway mining town. Following his " |
| 39 | + "father's death, when he was 21, Murdoch returned from Oxford to take charge " |
| 40 | + "of the family business News Limited, which had been established in 1923. " |
| 41 | + "Rupert Murdoch turned its Adelaide newspaper, The News, its main asset, into " |
| 42 | + "a major success. He began to direct his attention to acquisition and " |
| 43 | + "expansion, buying the troubled Sunday Times in Perth, Western Australia " |
| 44 | + "(1956) and over the next few years acquiring suburban and provincial " |
| 45 | + "newspapers in New South Wales, Queensland, Victoria and the Northern " |
| 46 | + "Territory, including the Sydney afternoon tabloid, The Daily Mirror (1960). " |
| 47 | + 'The Economist describes Murdoch as "inventing the modern tabloid", as he ' |
| 48 | + "developed a pattern for his newspapers, increasing sports and scandal " |
| 49 | + "coverage and adopting eye-catching headlines. Murdoch's first foray outside " |
| 50 | + "Australia involved the purchase of a controlling interest in the New Zealand " |
| 51 | + "daily The Dominion. In January 1964, while touring New Zealand with friends " |
| 52 | + "in a rented Morris Minor after sailing across the Tasman, Murdoch read of a " |
| 53 | + "takeover bid for the Wellington paper by the British-based Canadian newspaper " |
| 54 | + "magnate, Lord Thomson of Fleet. On the spur of the moment, he launched a " |
| 55 | + "counter-bid. A four-way battle for control ensued in which the 32-year-old " |
| 56 | + "Murdoch was ultimately successful. Later in 1964, Murdoch launched The " |
| 57 | + "Australian, Australia's first national daily newspaper, which was based " |
| 58 | + "first in Canberra and later in Sydney. In 1972, Murdoch acquired the Sydney " |
| 59 | + "morning tabloid The Daily Telegraph from Australian media mogul Sir Frank " |
| 60 | + "Packer, who later regretted selling it to him. In 1984, Murdoch was appointed " |
| 61 | + "Companion of the Order of Australia (AC) for services to publishing. In 1999, " |
| 62 | + "Murdoch significantly expanded his music holdings in Australia by acquiring " |
| 63 | + "the controlling share in a leading Australian independent label, Michael " |
| 64 | + "Gudinski's Mushroom Records; he merged that with Festival Records, and the " |
| 65 | + "result was Festival Mushroom Records (FMR). Both Festival and FMR were " |
| 66 | + "managed by Murdoch's son James Murdoch for several years.", |
| 67 | + ), |
| 68 | + Document( |
| 69 | + doc_id="2", |
| 70 | + text="This document has nothing to do with Rupert Murdoch. This document is " |
| 71 | + "two sentences long.", |
| 72 | + ), |
| 73 | +] |
| 74 | + |
| 75 | + |
| 76 | +result = rag.find_citations(assistant_response, documents, context, backend) |
| 77 | +print(f"Result of citations intrinsic:\n{json.dumps(result, indent=2)}") |
0 commit comments