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 start an ollama server in the background inside of our SLURM job. We can do this by running the commands below in a Jupyter Lab terminal. This is not the same as launching a terminal app as that will launch a new SLURM job instead. Instead, select ‘Terminal’ from the Launcher after clicking on the ‘+’ button in the top left corner of your Jupyter Lab application.

We also need to point Ollama towards our shared model repository in order to be able to import the models that we’ve downloaded for you. Alternatively if you’d like to experiment with models on your own, feel free to modify that path to a directory that you control. Note that LLMs are big and they will count towards your disk quota.

export OLLAMA_MODELS="/apps/ollama/models/"
/apps/ollama/ollama serve

import ollama
response = ollama.chat(model='llama3', messages=[
  {
    'role': 'user',
    'content': 'Why is the sky blue?',
  },
])
print(response)
{'model': 'llama3', 'created_at': '2024-08-14T05:34:36.062060907Z', 'message': {'role': 'assistant', 'content': "What a great question!\n\nThe sky appears blue because of a phenomenon called Rayleigh scattering. This is when short (blue) wavelengths of light are scattered more than longer (red) wavelengths by tiny molecules of gases in the atmosphere, such as nitrogen and oxygen.\n\nHere's what happens:\n\n1. When sunlight enters Earth's atmosphere, it encounters tiny molecules of gases like nitrogen (N2) and oxygen (O2).\n2. These molecules scatter the light in all directions.\n3. The shorter blue wavelengths are scattered more than longer red wavelengths because they have a higher energy and interact more strongly with the gas molecules.\n4. As a result, our eyes perceive the scattered blue light as a blue color, making the sky appear blue.\n\nThis scattering effect is most pronounced when the sun is overhead (at an angle of about 90 degrees), which is why the sky often appears more vibrant blue during midday. As the sun sets or rises lower in the sky, the light has to travel longer distances through the atmosphere, which means more of the shorter wavelengths are scattered away, making the sky appear more orange or red due to the dominance of the longer wavelengths.\n\nSo, to summarize: the sky appears blue because of the scattering of short blue wavelengths by tiny gas molecules in the atmosphere!\n\n(P.S. There are other factors that can affect the apparent color of the sky, such as dust particles, water vapor, and pollution. But Rayleigh scattering is the primary reason for the sky's blue hue.)"}, 'done_reason': 'stop', 'done': True, 'total_duration': 6096306797, 'load_duration': 2782395, 'prompt_eval_count': 11, 'prompt_eval_duration': 234876000, 'eval_count': 301, 'eval_duration': 5719801000}

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

response = ollama.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 Robert Boulter, an English actor. Here's a summary:

**Early Career (2000-2005)**

* Guest starred in TV series "The Bill" (2000)
* Starred in play "Herons" at Royal Court Theatre (2001)
* Appeared in TV series "Judge John Deed" (2002) and "The Bill" (2003)
* Had recurring role on "The Bill" (2003)

**Middle Career (2006-Present)**

* Starred in play "Citizenship" at National Theatre (2006)
* Appeared in TV series "Doctors" (2006)
* Starred in play "How to Curse" at Bush Theatre (2007)
* Starred in two films: "Daylight Robbery" and "Donkey Punch" (2008)
* Guest starred on TV series "Waking the Dead" (2008) and "Survivors" (2008)
* Had recurring role on TV series "Casualty" (2010)
* Starred in film "Mercenaries" (2011)

**Theatre Credits**

* "Mercury Fur" at Drum Theatre and Menier Chocolate Factory (2005)
* "How to Curse" at Bush Theatre (2007)

Overall, Robert Boulter has had a diverse career, appearing in TV shows, films, and theatre productions. He has worked with various directors and actors, including Ben Whishaw, Derek Jacobi, and Josie Rourke.
# And finally we can automate 
responses = []
for article in articles:
    response = ollama.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 biography of English actor Robert Boulter. Here's a summary:

**Early Career (2000-2005)**

* Guest star role on TV series "The Bill" in 2000
* Starred in play "Herons" by Simon Stephens at Royal Court Theatre in 2001
* Appeared in TV series "Judge John Deed" in 2002 as "Addem Armitage"
* Had recurring role on TV series "The Bill" in 2003 and 2004

**Mid-Career (2006-2010)**

* Starred in play "Citizenship" by Mark Ravenhill at National Theatre in 2006
* Appeared in TV series "Doctors" in 2006 as "Jason Tyler"
* Starred in play "How to Curse" directed by Josie Rourke at Bush Theatre in 2007
* Starred in two films: "Daylight Robbery" (2008) and "Donkey Punch" (2008)
* Guest-starred on TV series "Waking the Dead" in 2008 as "Jimmy Dearden"
* Appeared on TV series "Survivors" in 2008 as "Neil"

**Recent Career (2010-Present)**

* Had recurring role on TV series "Casualty" in 2010 as "Kieron Fletcher", playing an emergency physician
* Starred in film "Mercenaries" directed by Paris Leonti in 2011

Overall, the article provides a chronological overview of Robert Boulter's acting career, highlighting his roles in various films, television shows, and theatre productions.
======================
The article discusses the impact and influence of ancient Chinese poet Du Fu (712-770 CE) on Japanese literature and poets. Du Fu's work was highly regarded in Japan, particularly during the Muromachi period (1336-1573 CE). His poetry had a profound impact on Japanese haiku master Matsuo Bashō (1644-1694 CE), who often cited Du Fu's poems in his own works.

In Japan, Du Fu's poetry was widely studied and admired. During the Edo period (1603-1868 CE), a commentary on Du Fu's poetry by Shào Chuán of the Ming Dynasty gained popularity among Confucian scholars and townspeople. This commentary established Du Fu as the greatest poet in history.

The article also touches on the challenges of translating Du Fu's work into English. Various translators have approached this task with different styles, ranging from free translations to literal translations that preserve the original poetic forms. Some translators, like Kenneth Rexroth, have taken a more flexible approach, while others, like Burton Watson, have prioritized conveying the formal constraints and allusions of Du Fu's poetry.

Overall, the article highlights the enduring influence of Du Fu on Japanese literature and poets, as well as the challenges and nuances involved in translating his work into English.
======================
The article is about the song "Kiss You" by One Direction. Here's a summary:

**Critical Reception**: The song was well-received by music critics, who praised its production quality and catchy melody.

**Commercial Performance**: The single debuted at number 24 on the Irish Singles Chart and peaked at number 7. It also reached the top 10 in the UK Singles Chart and the US Billboard Hot 100. The song was certified gold by the RIAA and platinum by the ARIA (Australian Recording Industry Association) and RIANZ (Recording Industry Association of New Zealand).

**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. It was characterized as "bigger than anything we've done before" and "a lot of fun" by the band members.

**Live Performances**: One Direction performed the song on televised programs, including The Today Show and The X Factor UK and USA. They also included it in their set list during their Take Me Home Tour (2013), Where We Are Tour (2014), and On the Road Again Tour (2015).

**Track Listing**: The CD single features "Kiss You" and "Little Things".

**Credits and Personnel**: The song was written by Carl Falk, Kristoffer Fogelmark, Niall Horan, Savan Kotecha, Kristian Lundin, Albin Nedler, Shellback, and Rami Yacoub. It was produced by Carl Falk and Rami Yacoub.

I hope that helps! Let me know if you have any further questions.
======================
The article describes the career of two Japanese battleships, Ise and Hyūga, during World War II. Here is a summary:

* In May 1943, Ise was converted into a hybrid carrier from a battleship.
* After completing sea trials, Ise was attached to the Imperial Japanese Naval Academy at Etajima and ferried troops and munitions to Truk in October.
* In November, Ise began working up with Hyūga, which had also been converted into a carrier. Both ships joined the 2nd Battleship Division.
* In May 1944, the sisters were transferred to Rear Admiral Matsuda Chiaki's reformed Fourth Carrier Division of the 3rd Fleet.

The article then describes the Battle of Cape Engaño:

* The Japanese plan was to use the surviving carriers, including Ise and Hyūga, to lure American carrier forces away from the invasion area.
* On October 24, the sisters launched aircraft to attack American carriers. Although they inflicted no damage, they caused the Americans to search in the direction from which they had attacked.
* The next day, the Americans found the Japanese carriers and launched multiple airstrikes. Ise claimed to have shot down five attacking dive bombers and was lightly damaged by near misses.
* Hyūga was successfully attacked by an American submarine on October 25.

After the battle, the sisters continued to serve in various roles:

* In early November, their catapults were removed, and they loaded troops and munitions. They then sailed for Lingga Island and eventually became flagship of the 5th Fleet.
* In December, Hyūga became flagship of the fleet at Cam Ranh Bay.
* In January 1945, the sisters sailed to Singapore and then to Lingga Island.

The article concludes with a description of the final days of Ise and Hyūga:

* After being struck off the Navy List in November, both ships were scrapped after the war.
======================
The article is about the life of Dick Rifenburg, an American football player and sportscaster. Here's a summary:

* Rifenburg played football at the University of Michigan, leading the team to consecutive undefeated national championships in 1947 and 1948.
* He was known for his practical jokes on the field and was a consensus All-American selection as a senior.
* Despite being one of the greatest Wolverine players of the 1940s, Rifenburg did not finish among the top eight Heisman Trophy voters in 1948 due to perceived bias against Michigan teams.
* After graduating from Michigan, Rifenburg was drafted by the Philadelphia Eagles and played for the Detroit Lions before retiring from professional football.
* He went on to become a sportscaster, hosting a popular panel show called "Let's Talk Sports" and pioneering an early morning exercise program in Buffalo, New York.
* Rifenburg worked at WBEN radio station for many years, hosting various programs and serving as the play-by-play announcer for Buffalo Bills games.
* He also taught communications at Medaille College and sold ads for a local newspaper.
* After 30 years with WBEN, Rifenburg's show was replaced, but he continued to work in broadcasting until his eventual retirement.
* Rifenburg passed away in 1994, leaving behind a legacy as a pioneering figure in sports broadcasting. He was posthumously inducted into the Buffalo Broadcasters Hall of Fame in 2007.
======================