Open In Colab

Bedrock#

Basic Usage#

Call complete with a prompt#

If you’re opening this Notebook on colab, you will probably need to install LlamaIndex 🦙.

%pip install llama-index
from llama_index.llms import Bedrock

resp = Bedrock(
    model="amazon.titan-text-express-v1",
).complete("Paul Graham is ")
print(resp)
son of John and Mary Graham.

Call chat with a list of messages#

from llama_index.llms import ChatMessage, Bedrock

messages = [
    ChatMessage(
        role="system", content="You are a pirate with a colorful personality"
    ),
    ChatMessage(role="user", content="Tell me a story"),
]

resp = Bedrock(
    model="amazon.titan-text-express-v1",
).chat(messages)
print(resp)
assistant:  Ahoy, matey! Here's a tale for you:

Once upon a time, in a faraway land, there was a pirate named Captain Jack Sparrow. He was the most notorious pirate on the seven seas, with a reputation for his wit, charm, and daring exploits.

Captain Jack Sparrow was born on the island of Tortuga, where he grew up learning the ways of the sea from his father, Captain Teague Sparrow. As a young boy, Jack was fascinated by the stories of pirates and their adventures, and he dreamed of one day setting sail on his own ship.

One day, when Jack was just a teenager, his father was captured by the Spanish Navy. Jack was determined to rescue his father, and he set out on a quest to find the Spanish galleon that had taken him.

After a long and treacherous journey, Jack finally found the Spanish galleon. He boarded the ship and fought his way to his father's cell, where he rescued him and escaped the ship with his father.

From that day on, Jack Sparrow became a pirate, sailing the high seas and seeking his fortune. He encountered many strange and exotic creatures, including mermaids, sea monsters, and even the legendary Kraken.

One of Captain Jack Sparrow's most famous adventures was his encounter with the Black Pearl, a legendary pirate ship that was said to be cursed. Captain Jack Sparrow stole the Black Pearl from a group of Spanish pirates and set out on a quest to find the legendary Fountain of Youth.

Along the way, Jack encountered many challenges, including a group of pirates led by Captain Barbossa, who was determined to reclaim the Black Pearl. Captain Jack Sparrow and his crew fought bravely against Barbossa's pirates, and in the end, they were able to reclaim the Black Pearl and escape with their lives.

Captain Jack Sparrow was a skilled navigator and a master of disguise. He was able to fool many of his enemies, including the Spanish Navy, by using his wit and charm to outsmart them.

In the end, Captain Jack Sparrow was a legendary pirate, and his adventures will be remembered for generations to come. He was a true hero, and his legacy will continue to inspire people to seek their fortune and adventure on the high seas.

Would you like to hear more tales of adventure and bravery?

Streaming#

Using stream_complete endpoint

from llama_index.llms import Bedrock

llm = Bedrock(model="amazon.titan-text-express-v1")
resp = llm.stream_complete("Paul Graham is ")
for r in resp:
    print(r.delta, end="")
son of John and Mary Graham.

Using stream_chat endpoint

from llama_index.llms import Bedrock

llm = Bedrock(
    model="amazon.titan-text-express-v1",
)
messages = [
    ChatMessage(
        role="system", content="You are a pirate with a colorful personality"
    ),
    ChatMessage(role="user", content="Tell me a story"),
]
resp = llm.stream_chat(messages)
for r in resp:
    print(r.delta, end="")
 Ahoy, matey! Here's a tale for you:

Once upon a time, there was a pirate named Captain Jack Sparrow. He was a notorious pirate on the high seas, known for his wit, his charm, and his love of adventure.

Captain Jack Sparrow was born on the Caribbean island of Tortuga. His father, Captain Teague Sparrow, was a notorious pirate himself, and Jack grew up learning the ways of the sea from his father.

At a young age, Jack showed a natural talent for sailing and navigation. He could navigate his ship through the toughest storms and find his way through the most treacherous waters.

One day, Jack's father was captured by the Spanish Navy. The Spanish were determined to capture all the pirates on the Caribbean, and they were particularly interested in Captain Teague Sparrow.

Jack was determined to rescue his father. He gathered a crew of his most trusted friends, and they set out on a daring mission to rescue Captain Teague Sparrow from the Spanish prison ship.

The mission was perilous, and the crew faced many challenges. They had to navigate through treacherous waters, avoid the Spanish Navy, and deal with a host of other pirates and sea creatures.

But Jack was a skilled captain, and he led his crew through the challenges with courage and determination. They finally reached the Spanish prison ship, and Jack and his crew attacked the guards.

They managed to rescue Captain Teague Sparrow, but they were soon surrounded by the Spanish Navy. Jack and his crew fought bravely, but they were outnumbered and outgunned.

Just when it seemed like all was lost, Jack had an idea. He used his wit and charm to distract the Spanish Navy, while his crew sneaked aboard their ship.

They managed to steal the Spanish ship's treasure, and they set sail for Tortuga, their home island.

Captain Jack Sparrow and his crew were hailed as heroes on Tortuga. They were celebrated for their bravery and their cunning, and they were known as the most feared pirates on the high seas.

But Jack was not content with just being a hero. He was determined to find the legendary treasure of the Spanish Main. He knew that it was hidden somewhere on the ocean, and he was determined to find it.

Jack and his crew set out on a new adventure, and they encountered many challenges along the way. They faced storms,

Configure Model#

from llama_index.llms import Bedrock

llm = Bedrock(
    model="amazon.titan-text-express-v1", temperature=0.5, max_tokens=2048
)
resp = llm.complete("Paul Graham is ")
print(resp)
an English singer, songwriter, and guitarist who is widely regarded as one of the most influential and important musicians of the 20th century. He is best known for his work with the Beatles, which he joined in 1962, and for his solo career, which began in 1965.
Paul McCartney is known for his unique songwriting style, which combines elements of pop, rock, classical, and folk music. He is also known for his versatile guitar playing, which has inspired many musicians throughout the world.

One of Paul McCartney's most famous songs is "Yesterday," which he wrote and performed with the Beatles in 1965. The song has become one of the most covered songs in history and has been praised for its beautiful melody and clever lyrics.

Paul McCartney is also known for his activism and humanitarian work. He has been a champion for environmental causes, peace, and social justice throughout his career, and has used his platform to raise awareness about these issues. He has been awarded numerous honors, including the Nobel Peace Prize in 2019, for his contributions to music and society.

Paul McCartney was born on September 18, 1942, in Liverpool, England. He grew up in a working-class family and showed an early interest in music. He began playing the guitar at the age of 12 and formed his first band, the Quarrymen, in 1957. The Quarrymen later became the Beatles, and Paul McCartney became the band's lead singer and guitarist.

The Beatles' first hit song, "Love Me Do," was released in 1962, and the band quickly became one of the most popular bands in the world. The Beatles' music was characterized by its catchy melodies, innovative production techniques, and clever lyrics, and the band's popularity continued to grow throughout the 1960s.

Paul McCartney's solo career began in 1965 with the release of his debut album, "McCartney." The album was a huge success, and Paul McCartney became known as a solo artist in his own right. He continued to release successful albums throughout the 1970s, including "Band on the Run," "Let It Be," and "Abbey Road."

In addition to his music career, Paul McCartney has been involved in many other ventures. He has written books, directed films, and even designed a line of clothing. He is also a member of the band Wings, which he formed in 1971 with his wife Linda McCartney.

Paul McCartney is widely regarded as one of the greatest musicians of all time. His unique songwriting style, versatile guitar playing, and activism have made him a beloved figure in the music industry and a respected figure in society. He continues to perform and record new music, and his legacy as a musician and humanitarian will continue to inspire generations to come.

Paul McCartney is an English singer, songwriter, and guitarist who is widely regarded as one of the most influential and important musicians of the 20th century. He is best known for his work with the Beatles, which he joined in 1962, and for his solo career, which began in 1965.

One of Paul McCartney's most famous songs is "Yesterday," which he wrote and performed with the Beatles in 1965. The song has become one of the most covered songs in history and has been praised for its beautiful melody and clever lyrics.

Paul McCartney is also known for his activism and humanitarian work. He has been a champion for environmental causes, peace, and social justice throughout his career, and has used his platform to raise awareness about these issues. He has been awarded numerous honors, including the Nobel Peace Prize in 2019, for his contributions to music and society.

Paul McCartney was born on September 18, 1942, in Liverpool, England. He grew up in a working-class family and showed an early interest in music. He began playing the guitar at the age of 12 and formed his first band, the Quarrymen, in 1957. The Quarrymen later became the Beatles, and Paul McCartney became the band's lead singer and guitarist.

The Beatles' first hit song, "Love Me Do," was released in 1962, and the band quickly became one of the most popular bands in the world. The Beatles' music was characterized by its catchy melodies, innovative production techniques, and clever lyrics, and the band's popularity continued to grow throughout the 1960s.

Paul McCartney's solo career began in 1965 with the release of his debut album, "McCartney." The album was a huge success, and Paul McCartney became known as a solo artist in his own right. He continued to release successful albums throughout the 1970s, including "Band on the Run," "Let It Be," and "Abbey Road."

In addition to his music career, Paul McCartney has been involved in many other ventures. He has written books, directed films, and even designed a line of clothing. He is also a member of the band Wings, which he formed in 1971 with his wife Linda McCartney.

Paul McCartney is widely regarded as one of the greatest musicians of all time. His unique songwriting style, versatile guitar playing, and activism have made him a beloved figure in the music industry and a respected figure in society. He continues to perform and record new music, and his legacy as a musician and humanitarian will continue to inspire generations to come.

Paul McCartney is an English singer, songwriter, and guitarist who is widely regarded as one of the most influential and important musicians of the 20th century. He is best known for his work with the Beatles, which he joined in 1962, and for his solo career, which began in 1965.

One of Paul McCartney's most famous songs is "Yesterday," which he wrote and performed with the Beatles in 1965. The song has become one of the most covered songs in history and has been praised for its beautiful melody and clever lyrics.

Paul McCartney is also known for his activism and humanitarian work. He has been a champion for environmental causes, peace, and social justice throughout his career, and has used his platform to raise awareness about these issues. He has been awarded numerous honors, including the Nobel Peace Prize in 2019, for his contributions to music and society.

Paul McCartney was born on September 18, 1942, in Liverpool, England. He grew up in a working-class family and showed an early interest in music. He began playing the guitar at the age of 12 and formed his first band, the Quarrymen, in 1957. The Quarrymen later became the Beatles, and Paul McCartney became the band's lead singer and guitarist.

The Beatles' first hit song, "Love Me Do," was released in 1962, and the band quickly became one of the most popular bands in the world. The Beatles' music was characterized by its catchy melodies, innovative production techniques, and clever lyrics, and the band's popularity continued to grow throughout the 1960s.

Paul McCartney's solo career began in 1965 with the release of his debut album, "McCartney." The album was a huge success, and Paul McCartney became known as a solo artist in his own right. He continued to release successful albums throughout the 1970s, including "Band on the Run," "Let It Be," and "Abbey Road."

In addition to his music career, Paul McCartney has been involved in many other ventures. He has written books, directed films, and even designed a line of clothing. He is also a member of the band Wings, which he formed in 1971 with his wife Linda McCartney.

Paul McCartney is widely regarded as one of the greatest musicians of all time. His unique songwriting style, versatile guitar playing, and activism have made him a beloved figure in the music industry and a respected figure in society. He continues to perform and record new music, and his legacy as a musician and humanitarian will continue to inspire generations to come.

Connect to Bedrock with Access Keys#

from llama_index.llms import Bedrock

llm = Bedrock(
    model="amazon.titan-text-express-v1",
    aws_access_key_id="AWS Access Key ID to use",
    aws_secret_access_key="AWS Secret Access Key to use",
    aws_session_token="AWS Session Token to use",
    region_name="AWS Region to use, eg. us-east-1",
    profile_name="AWS Profile to use (if not using aws_access_key_id and aws_secret_access_key)",
)

resp = llm.complete("Paul Graham is ")
print(resp)
son of John and Mary Graham.