AWS BedRock - Boto3 Demo - Cohere Model
Table of contents
- Github Link - Notebook
- Previous Blog on this Learning Series
- Environment Setup
- Install Latest Python
- Upgrade pip
- Install latest boto3,awscli, boto3-core
- Load the Library
- Cohere - Command Model - cohere.command-text-v14
- Set the Prompt
- Configure the Model configuration
- Invoke the Model
- Parse the Model
- Cohere - Command Model - Light Model
- Set the Prompt -
- Configure the Model configuration
- Invoke the Model
- Parse the response
- Cohere - Embedding
- Cohere Embedding Model as API
- Cohere Model for Prompt Engineering
Cohere offers a range of text generation and representation models designed for diverse business applications. The Command model, with 52 billion parameters, excels in tasks such as chat, text generation, and summarization. A lighter version, Command Light, with 6 billion parameters, provides a more resource-efficient alternative.
Additionally, Cohere provides Embed models, including Embed - English and Embed - Multilingual. Embed - English, with 1024 dimensions, supports English and is ideal for semantic search, retrieval-augmented generation (RAG), classification, and clustering. On the other hand, Embed - Multilingual supports over 100 languages, offering versatility for a wide range of linguistic contexts in similar applications."
Github Link - Notebook
https://github.com/jayyanar/learning-aws-bedrock/blob/main/blog6/Bedrock_Cohere.ipynb
Previous Blog on this Learning Series
Blog 1: dataopslabs.com/p/aws-bedrock-learning-seri..
Blog 2: dataopslabs.com/p/family-of-titan-text-mode..
Blog 3: dataopslabs.com/p/family-of-titan-text-mode..
Blog 4: https://blog.dataopslabs.com/aws-bedrock-boto3-demo-anthropic-claude
Blog 5: https://blog.dataopslabs.com/aws-bedrock-boto3-demo-ai21-labs
Environment Setup
I am using vscode local environment with AWS Credential configured.
Install Latest Python
! python --version
Python 3.11.5
Upgrade pip
! pip install --upgrade pip
Install latest boto3,awscli, boto3-core
! pip install --no-build-isolation --force-reinstall \
"boto3>=1.33.6" \
"awscli>=1.31.6" \
"botocore>=1.33.6"
Load the Library
import json
import os
import sys
import boto3
import botocore
bedrock = boto3.client(service_name="bedrock")
bedrock_runtime = boto3.client(service_name="bedrock-runtime")
Cohere - Command Model - cohere.command-text-v14
Cohere's flagship text generation model is known as Command. It is designed to seamlessly respond to user commands and provide immediate utility in real-world business scenarios. The model excels in various applications, including summarization, copywriting, dialogue, extraction, and question answering.
Key Model Features Command boasts attributes such as text generation and precise instruction following.
Set the Prompt
cohere_command_prompt = "Create a Story Similar to Marvel Antman story"
Configure the Model configuration
body = json.dumps({
"prompt": cohere_command_prompt,
"max_tokens":1024,
"temperature":0.2 #Temperature controls randomness; higher values increase diversity, lower values boost predictability.
})
Invoke the Model
response = bedrock_runtime.invoke_model(
body=body,
modelId="cohere.command-text-v14", # REPLACE WITH ai21.j2-mid-v1 lessthan powerful than Ultra but cost effective
accept= "*/*",
contentType="application/json"
)
Parse the Model
response_body = json.loads(response.get('body').read())
parse_text = response_body['generations'][0]['text']
parse_text
Response from Text Completion
Title: "Quantum Quest"
Once upon a time, in a world where the laws of physics were bent and broken, a new breed of superheroes emerged. These heroes were not born with superhuman abilities, but rather, they were ordinary people who had harnessed the power of quantum energy.
Our protagonist, Jake, was a brilliant scientist who had dedicated his life to unlocking the secrets of the quantum realm. He had always been fascinated by the potential of the microscopic world, and he dreamed of discovering a way to manipulate the energy that existed there.
One day, while working in his lab, Jake made a groundbreaking discovery. He developed a device that allowed him to enter the quantum realm and manipulate the energy there. At first, he was hesitant to use his newfound power, but when he witnessed a crime in progress, he knew he had to act.
Armed with his quantum device, Jake transformed into a new superhero, Quantum Quest. He leaped into action, using his control over quantum energy to shrink himself down to the size of an ant and battle the criminals.
As Quantum Quest, Jake encountered many challenges and enemies. He faced off against powerful villains who sought to exploit the quantum realm for their own gain, and he fought to protect the world from the dangers of quantum technology.
As he continued to use his powers, Jake discovered that he could not only shrink himself, but also grow to an enormous size, and even teleport short distances. He used his powers to defend the innocent and fight for justice, becoming a beloved hero in the process.
However, Jake's journey was not without its challenges. He struggled to balance his superhero duties with his personal life, and he often felt the weight of the responsibility he had taken on. Despite this, he remained dedicated to his mission, and he continued to fight for what was right.
In the end, Quantum Quest emerged as a true hero, a testament to the power of science and the strength of the human spirit. He had proven that anyone, even an ordinary person, could make a difference and become a force for good in the world.
The End.
Would you like me to expand on the story or provide a different ending?
Cohere - Command Model - Light Model
Cohere's Command-Light is a generative model optimized for instruction-like prompts, offering an exceptional balance of quality, cost-effectiveness, and low-latency inference. It excels in various applications, including summarization, copywriting, dialogue, extraction, and question answering.
The model is characterized by its text generation capabilities and adeptness at following instructions.
Set the Prompt -
cohere_command_light_prompt = "Give me benefits about Artificial Intelligence"
Configure the Model configuration
body = json.dumps({
"prompt": cohere_command_light_prompt,
"max_tokens":128,
"temperature":0.2 #Temperature controls randomness; higher values increase diversity, lower values boost predictability.
})
Invoke the Model
response = bedrock_runtime.invoke_model(
body=body,
modelId="cohere.command-light-text-v14", # REPLACE WITH ai21.j2-mid-v1 lessthan powerful than Ultra but cost effective
accept= "*/*",
contentType="application/json"
)
Parse the response
response_body = json.loads(response.get('body').read())
parse_text = response_body['generations'][0]['text']
parse_text
Cohere - Embedding
Cohere's Embed is a model designed to transform text into numerical vectors, facilitating comprehension by various machine learning models. Widely utilized in advanced generative AI applications, Embed plays a crucial role in deciphering user inputs, search results, and documents with nuanced understanding.
Its applications span semantic search, retrieval-augmented generation (RAG), classification, and clustering, and it is characterized by its 1024-dimensional vector representation."
Set the Prompt
embed_prompt = "AWS re:Invent 2023, our biggest cloud event of the year, in Las Vegas, Nevada, featured keynotes, innovation talks, builder labs, workshops, tech and sustainability demos"
Configure the Model configuration
corpus = [
"I love playing football",
"Football is my favorite sport",
"I like shoot three points in Basketball"
"Basketball is an exciting game",
"I like swimming in the ocean"
]
body = json.dumps({
"texts" : corpus,
"input_type" : 'search_query'
})
Invoke the Model
response = bedrock_runtime.invoke_model(
body=body,
modelId="cohere.embed-english-v3",
accept="application/json",
contentType="application/json"
)
Parse the Configuration
response_body = json.loads(response.get("body").read())
embedding_output = response_body.get("embeddings")
print (embedding_output)
Text within this block will maintain its original spacing when published [0.40429688, -0.38085938, 0.19726562, '...', 0.2109375, 0.012573242, 0.18847656]
Cohere Embedding Model as API
Practice Word Embedding using Cohere API - Step by Step Instruction - Follow Below notebook
https://github.com/jayyanar/genai-apps/blob/main/lab1_word_embedding.ipynb
Cohere Model for Prompt Engineering
Practice Prompt Engineering using Cohere API - Step by Step Instruction - Follow Below notebook
https://github.com/jayyanar/genai-apps/blob/main/lab2_prompt_engineering.ipynb
Thanks for Reading the Post -- Share your Feedback and Comments -- Stay Tuned for Next Blog
Subscribe to my newsletter
Read articles from DataOps Labs directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
DataOps Labs
DataOps Labs
I'm Ayyanar Jeyakrishnan ; aka AJ. With over 18 years in IT, I'm a passionate Multi-Cloud Architect specialising in crafting scalable and efficient cloud solutions. I've successfully designed and implemented multi-cloud architectures for diverse organisations, harnessing AWS, Azure, and GCP. My track record includes delivering Machine Learning and Data Platform projects with a focus on high availability, security, and scalability. I'm a proponent of DevOps and MLOps methodologies, accelerating development and deployment. I actively engage with the tech community, sharing knowledge in sessions, conferences, and mentoring programs. Constantly learning and pursuing certifications, I provide cutting-edge solutions to drive success in the evolving cloud and AI/ML landscape.