Ollama Python

Ollama offers a way to interact with open source large language models interactively. The easiest way to do that is with the Ollama Chatbot app in Strudel2, but if you need more power, you can use the Ollama python library.

In this example we are going to use Ollama to summarise Wikipedia articles from the Wikitext 2 dataset.

Preparing the dataset

The Wikitext 2 dataset is easily available through the Hugging Face library ‘datasets’, but rather than splitting the dataset by article like we need the data is split by paragraph. This means we’ll need to reprocess the dataset back into articles for our example.

from datasets import load_dataset
ds = load_dataset("Salesforce/wikitext", "wikitext-2-raw-v1")
ds["test"]["text"][:10]
['',
 ' = Robert Boulter = \n',
 '',
 ' Robert Boulter is an English film , television and theatre actor . He had a guest @-@ starring role on the television series The Bill in 2000 . This was followed by a starring role in the play Herons written by Simon Stephens , which was performed in 2001 at the Royal Court Theatre . He had a guest role in the television series Judge John Deed in 2002 . In 2004 Boulter landed a role as " Craig " in the episode " Teddy \'s Story " of the television series The Long Firm ; he starred alongside actors Mark Strong and Derek Jacobi . He was cast in the 2005 theatre productions of the Philip Ridley play Mercury Fur , which was performed at the Drum Theatre in Plymouth and the Menier Chocolate Factory in London . He was directed by John Tiffany and starred alongside Ben Whishaw , Shane Zaza , Harry Kent , Fraser Ayres , Sophie Stanton and Dominic Hall . \n',
 ' In 2006 , Boulter starred alongside Whishaw in the play Citizenship written by Mark Ravenhill . He appeared on a 2006 episode of the television series , Doctors , followed by a role in the 2007 theatre production of How to Curse directed by Josie Rourke . How to Curse was performed at Bush Theatre in the London Borough of Hammersmith and Fulham . Boulter starred in two films in 2008 , Daylight Robbery by filmmaker Paris Leonti , and Donkey Punch directed by Olly Blackburn . In May 2008 , Boulter made a guest appearance on a two @-@ part episode arc of the television series Waking the Dead , followed by an appearance on the television series Survivors in November 2008 . He had a recurring role in ten episodes of the television series Casualty in 2010 , as " Kieron Fletcher " . Boulter starred in the 2011 film Mercenaries directed by Paris Leonti . \n',
 '',
 ' = = Career = = \n',
 '',
 '',
 ' = = = 2000 – 2005 = = = \n']

We can identify the split between articles by looking out for the article headings. In this dataset:
- Headers have the format: " = <HEADING> = \n"
- Subheadings have the format: " = = <SUBHEADING> = = \n"
- Subsubheadings have the format: " = = = <SUBSUBHEADING> = = = \n"

articles = []

# Drop the 0th line as its empty
for line in ds["test"]["text"][1:]:
    # Identify headings, but exclude subheadings and subsubheadings
    if line[:3] == " = " and line[3] != "=":
        articles.append(line)
    else:
        articles[-1] += line
# Check the first 100 characters of the first 5 articles to make sure we split the dataset correctly
for article in articles[:5]:
    print(article[:100])
 = Robert Boulter = 
 Robert Boulter is an English film , television and theatre actor . He had a gu
 = Du Fu = 
 Du Fu ( Wade – Giles : Tu Fu ; Chinese : 杜甫 ; 712 – 770 ) was a prominent Chinese poet 
 = Kiss You ( One Direction song ) = 
 " Kiss You " is a song recorded by English @-@ Irish boy band
 = Ise @-@ class battleship = 
 The Ise @-@ class battleships ( 伊勢型戦艦 , Ise @-@ gata senkan ) were a
 = Dick Rifenburg = 
 Richard Gale " Dick " Rifenburg ( August 21 , 1926 – December 5 , 1994 ) was a

Passing the articles through to Ollama

To use the ollama library we need to point it to a running Ollama server.

MLeRP is running an Ollama server on mlerp-monash-node13 which you can access from any other MLeRP node. To connect the library to our API you will need to define a custom client and call it:

from ollama import Client

client = Client(
  host='http://mlerp-monash-node13:80'
)

response = client.chat(model='llama3', messages=[
  {
    'role': 'user',
    'content': 'Why is the sky blue?',
  },
])
print(response)
{'model': 'llama3', 'created_at': '2025-08-13T01:45:32.825751542Z', 'message': {'role': 'assistant', 'content': "The sky appears blue because of a phenomenon called Rayleigh scattering, named after the British physicist Lord Rayleigh. Here's what happens:\n\n1. **Sunlight enters Earth's atmosphere**: When sunlight travels through space and reaches our planet, it contains all the colors of the visible spectrum, including red, orange, yellow, green, blue, indigo, and violet.\n2. **Molecules in the air scatter shorter wavelengths**: As this light travels through the atmosphere, it encounters tiny molecules of gases like nitrogen (N2) and oxygen (O2). These molecules are much smaller than the wavelength of visible light.\n3. **Blue light is scattered more**: When the shorter-wavelength blue light hits these molecules, it is scattered in all directions by the molecules themselves. This scattering effect is more pronounced for shorter wavelengths, like blue and violet, because they interact more strongly with the small air molecules.\n4. **Red light continues to travel straight**: Meanwhile, the longer-wavelength red light travels through the atmosphere relatively unaffected, since it doesn't scatter as much due to its larger wavelength. This allows red light to reach our eyes from a more direct path.\n5. **Our eyes perceive blue**: When we look at the sky, our eyes see the scattered blue light as blue, while the unscattered longer-wavelength light (like red and orange) appears as the sky's subtle gradient of color. The exact shade of blue can vary depending on atmospheric conditions like pollution, dust, and water vapor.\n\nIn summary, the sky appears blue because shorter-wavelength blue light is scattered more by the tiny molecules in the air, while longer-wavelength red light continues to travel straighter to our eyes, resulting in the blue hue we see."}, 'done_reason': 'stop', 'done': True, 'total_duration': 5800302752, 'load_duration': 2759382579, 'prompt_eval_count': 16, 'prompt_eval_duration': 213570364, 'eval_count': 351, 'eval_duration': 2826615612}

We can then formulate our request to the LLM using the articles from our dataset and ask for it’s summary

response = client.chat(model='llama3', messages=[
  {
    'role': 'user',
    'content': "Please summarise this article:\n\n\n" + articles[0],
  },
])
print(response['message']['content'])
The article is about the career of English actor Robert Boulter. Here's a summary:

Early Career: Boulter started his acting career with guest roles on TV shows such as "The Bill" (2000) and "Judge John Deed" (2002). He also appeared in stage productions, including the play "Herons" at the Royal Court Theatre (2001).

Mid-Career: In 2004, Boulter landed a role in the episode "Teddy's Story" of the TV series "The Long Firm". He then starred in the theatre production "Mercury Fur" (2005), directed by John Tiffany. The play received favorable reviews, with critics praising his intense performance.

Recent Work: In 2006, Boulter starred in the play "Citizenship" and appeared on an episode of the TV series "Doctors". He then appeared in two films, "Daylight Robbery" (2008) and "Donkey Punch" (2008), as well as a guest role on the TV series "Waking the Dead".

Later Career: Boulter had a recurring role on the TV series "Casualty" (2010-2011) as an emergency physician, applying for a medical fellowship. He also starred in the film "Mercenaries" (2011).

Throughout his career, Boulter has received positive reviews from critics and has worked with notable actors such as Ben Whishaw and Mark Strong.
# And finally we can automate 
responses = []
for article in articles:
    response = client.chat(model='llama3', messages=[
        {
            'role': 'user',
            'content': "Please summarise this article:\n\n\n" + article,
        },
    ])
    responses.append(response['message']['content'])
# Print the first 5 summaries
for response in responses[:5]:
    print(response)
    print("======================")
The article is a biographical summary of Robert Boulter, an English actor. Here's a brief overview:

**Early Career (2000-2005)**: Boulter had guest roles on TV series "The Bill" and "Judge John Deed". He also appeared in several theatre productions, including "Herons" at the Royal Court Theatre and "Mercury Fur" at the Drum Theatre in Plymouth and Menier Chocolate Factory in London.

**Mid-Career (2006-Present)**: Boulter starred in the play "Citizenship" written by Mark Ravenhill. He also appeared on TV series "Doctors" and had a recurring role on "Casualty". In 2008, he starred in two films, "Daylight Robbery" and "Donkey Punch", and made guest appearances on TV series "Waking the Dead" and "Survivors".

**Theatre Credits**: Boulter has appeared in several theatre productions, including "Mercury Fur" (directed by John Tiffany), "How to Curse" (directed by Josie Rourke), and "Citizenship" (part of a triple bill at the National Theatre).

**Filmography**: The article lists Boulter's film credits, including "Daylight Robbery", "Donkey Punch", and "Mercenaries".
======================
The article is about Du Fu, a renowned Chinese poet who lived during the Tang Dynasty. Here's a summary:

**Life and Works**: Du Fu was born around 712 CE and died around 770 CE. He is considered one of the greatest poets in Chinese literature. His poetry is known for its depth, emotion, and social commentary.

**Innovations**: Du Fu introduced new poetic forms and styles, such as the "regulated verse" (lǜshi), which became a hallmark of his work. He also experimented with language, using colloquialisms and everyday expressions to convey complex ideas.

**Themes**: His poetry often explored themes such as politics, social justice, morality, and personal experience. He wrote about the struggles of common people, the beauty of nature, and the impermanence of life.

**Influence**: Du Fu's influence on Chinese literature is immense. He has been studied and admired by scholars and poets throughout history. His poetry has also had a significant impact on Japanese literature, with many notable writers, including Matsuo Bashō, drawing inspiration from his work.

**Translations**: The article discusses the challenges of translating Du Fu's poetry into English. Various translators have approached this task in different ways, from free translations to more literal approaches.

Overall, the article presents Du Fu as a master poet who has left an indelible mark on Chinese literature and beyond. His innovative style, powerful themes, and enduring influence have made him one of the most celebrated poets in history.
======================
The article is about the song "Kiss You" by One Direction, a British-Irish boy band. Here's a summary:

**Background and Release**: The song was written by Kristoffer Fogelmark, Kristian Lundin, Albin Nedler, Savan Kotecha, Shellback, and producers Carl Falk and Rami Yacoub. It was released as the second single from their second studio album, Take Me Home (2012), in Germany on February 8, 2013.

**Composition and Reception**: The song is an upbeat power pop track with electronic effects, featuring lyrics about infatuation. Critics praised its production, calling it a standout track on the album. The song's chorus is hard to dislodge from your brain, according to some reviewers.

**Commercial Performance**: The single debuted at number 24 in Ireland and peaked at number 7, making it their sixth top ten hit in Ireland. It also reached the top ten in the UK Singles Chart, peaking at number 9. In the US, the song peaked at number 46 on the Billboard Hot 100 chart and was certified gold by the RIAA for shipments of 500,000 copies.

**Music Video**: The music video, directed by Vaughan Arnell, features the band shooting different scenes via a green screen, dressed as sailors, surfers, skiers, and jailers. The video garnered 10.4 million views in a 24-hour period and received positive reviews from critics.

**Live Performances**: One Direction performed "Kiss You" on various TV shows and during their worldwide Take Me Home Tour (2013), Where We Are Tour (2014), and On the Road Again Tour (2015).

Overall, "Kiss You" is a catchy and upbeat track that received positive reviews from critics and commercial success in several countries.
======================
The article is about two Ise-class battleships, Ise and Hyūga, which served during World War II. Here's a summary:

* Ise was converted into a hybrid carrier from February to September 1943 at Kure Naval Arsenal.
* Hyūga was also converted into a hybrid carrier from May to November 1943 at Sasebo Naval Arsenal.
* Both ships were part of the Fourth Carrier Division and participated in several battles, including the Battle of Cape Engaño on October 25, 1944.
* During this battle, Ise was attacked by American aircraft but managed to avoid significant damage. Hyūga was lightly damaged and later became flagship of the 5th Fleet.
* After the battle, the ships were used to bring strategic minerals back to Japan from Singapore.
* However, their mission was compromised when decrypted Japanese radio signals revealed their plan to the Allies, allowing American submarines to position themselves along their anticipated route.
* Only three submarine attacks were successful, and none of them sank either ship.
* The Fourth Carrier Division was disbanded in March 1945, and both ships were reduced to reserve status.
* In July 1945, both ships were attacked by American carrier aircraft again. Ise was hit multiple times and suffered significant damage, while Hyūga was badly damaged and eventually foundered.
* Ise's wreckage was later scrapped after the war.

Overall, the article highlights the service history of two Ise-class battleships during World War II, including their conversions into hybrid carriers, participation in several battles, and ultimate fate.
======================
The article is about Dick Rifenburg, an American football player and pioneer television broadcaster. Here's a summary:

Early Life:
Rifenburg was born on August 21, 1926, in Petoskey, Michigan. He grew up in Kalamazoo and Saginaw, Michigan, where he excelled in high school sports, winning state championships in basketball, track and field, and football.

College Career:
Rifenburg played college football at the University of Michigan from 1944 to 1948. He was a consensus All-American selection as an end in 1948 and led the Big Ten Conference in single-season receptions during his senior year. His college career was interrupted by World War II service, but he returned to play for the Wolverines in consecutive undefeated National Championship seasons in 1947 and 1948.

Professional Career:
Rifenburg was drafted by the Philadelphia Eagles and New York Yankees (AAF) in 1948. However, a knee injury kept him out of professional football for most of his career. He eventually played one season with the Detroit Lions in 1950, making 10 receptions for 96 yards and one touchdown.

Broadcasting Career:
After retiring from football, Rifenburg became a sports broadcaster in Buffalo, working for WBEN radio and television stations. He hosted various shows, including "Let's Talk Sports" and an early morning exercise program. He also worked as the sideline announcer for Buffalo Bills games and as the play-by-play announcer for University of Buffalo Bulls football.

Personal Life:
Rifenburg lived in Buffalo for 37 years and was married to Jane Morris. They had four children, including three sons who played football, and two grandchildren. Rifenburg died on December 5, 1994, at the age of 68.

Legacy:
Rifenburg was posthumously inducted into the Buffalo Broadcasters Hall of Fame in 2007, receiving the Golden Age Award for his pioneering work in television broadcasting. His wife accepted the award, noting that he never received a trophy during his playing career but was now being recognized for his achievements.
======================