0% found this document useful (0 votes)
29 views24 pages

Session 5 Getting Started With Cohere

Cohere provides an API for large-scale language models that can be tailored for various use cases, including chat applications and text classification. The document outlines the setup process, available models, and API endpoints like Chat, Generate, and Embed, along with examples of their usage. Additionally, it introduces the Cohere Playground, a visual interface for experimenting with AI models without coding.

Uploaded by

Nameless Wonder
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views24 pages

Session 5 Getting Started With Cohere

Cohere provides an API for large-scale language models that can be tailored for various use cases, including chat applications and text classification. The document outlines the setup process, available models, and API endpoints like Chat, Generate, and Embed, along with examples of their usage. Additionally, it introduces the Cohere Playground, a visual interface for experimenting with AI models without coding.

Uploaded by

Nameless Wonder
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Getting Started with Cohere

• Getting Started with Cohere


• Understanding of Cohere Models
Ram N Sangwan • Temperature, Token length, Penalties, Top P.
• Getting Started with Cohere API
• Authentication and Access Keys
• The Chat endpoint.
• The Summarize Endpoint (Deprecated)
• Using the Cohere Playground on OCI
Getting Started with Cohere
What is Cohere?
Cohere provides a powerful API for its models that integrates
language processing into any system.

Cohere develops large-scale language models and encapsulates


them within an intuitive API.

You can tailor these models to suit your use cases.

Cohere provides a range of models that can be trained and tailored


to suit specific use cases.
Cohere Baseline Models - Command
command-light - 4096 Tokens
A smaller, faster version of command.

Takes a user command-light-nightly – 8192 Tokens


instruction and
generates text Latest, experimental, and unstable version of command-light.
Updated regularly, without warning.
following the
instruction.
They are well-suited command
for chat applications.
An instruction-following conversational model that
performs language tasks with high quality, more reliably.

command-nightly
Updating environments, pushing to production
Cohere Baseline Models - Embed
embed-english-light-v3.0 - 512 Tokens
A smaller, faster version of embed-english-v3.0

Used to generate embeddings


from text or classify it. embed-multilingual-v3.0 - 512 Tokens
Used for estimating semantic
similarity between two Provides multilingual classification and embedding support.
sentences.
Outputs from the Classify
endpoint can be used for any
classification or analysis task.
embed-multilingual-light-v3.0 - 512 Tokens
A smaller, faster version of embed-multilingual-v3.0.
Cohere Models - Rerank
rerank-english-v2.0
A model that allows for re-ranking English language
documents.

Can improve created


models by re-organizing
their results based on
certain parameters.
Used to improve search
algorithms.

rerank-multilingual-v2.0
• A model for documents that are not in English.
• Supports the same languages as embed-multilingual-v3.0.

• Rerank not only surpasses the quality of results obtained through


embedding-based search but also requires just a single line of code
alteration in your application.
Cohere Models – Command-R
Command-R
With 35 billion parameter, it is highly performant
generative model.

Command-R is an
LLM with open Aya
weights optimized Aya is a multilingual model from Cohere For AI.
for reasoning, Is trained to support 21 languages:
summarization, and • Arabic, Chinese (simplified, traditional), Czech,
question answering. Dutch, French, German, Greek, Hindi, Indonesian,
Italian, Japanese, Korean, Persian, Polish,
Portuguese, Romanian, Russian, Spanish, Turkish,
Ukrainian, Vietnamese.

• Command-R has the capability for multilingual generation evaluated in 10


languages and highly performant RAG capabilities.
Setting Up Cohere

Register for a Cohere account and


Install the Python SDK.
get a free to use trial API key.

• There is no credit or time pip install cohere


limit associated with a trial
key.
• Calls are rate-limited to 100
calls per minute.
• This is typically enough for
an experimental project.
Define the Cohere client with the API key
import cohere
co = cohere.Client('your_api_key’)
response = co.generate(
model='command’,
prompt='Generate a product description
for the product: wireless earbuds.’,
max_tokens=100)
print(response.generations[0].text)

We defined a number of parameters.


• model — We selected command.
Setting Up Cohere • prompt — is an instruction to write a social ad copy.
• max_tokens — The maximum number of tokens to be
generated. One word is about three tokens.
Setting Up Cohere

Example response, with the text generated:

[
cohere.Generation {
id: 3d3ecb76-6435-4665-a323-6650eefc2429
prompt: Generate a concise product description for the
product: wireless earbuds.
text: These wireless earbuds are designed for comfort and
convenience, with a secure fit and easy-to-use controls. They're
perfect for when you're on the go, and provide clear sound and
reliable connectivity.
likelihood: None
token_likelihoods: None }
]

The Generate endpoint accepts a text input, that is


the prompt, and outputs a Generation object.
Cohere API Endpoints- Chat
https://api.cohere.ai/v1/chat
The endpoint has a number of
Generates a text response to a user message.
settings you can use to control
the kind of output it generates.
Create Prompt
Store the message you want to send into a variable
message = "Hello World!"

Define the Model Settings


model: command, command-light, command-nightly, and command-light-nightly.
temperature: Controls the randomness of the output.

Generate the Response


response = co.chat(
message,
model="command",
temperature=0.9
)
answer = response.text
Chat API Example

import cohere
co = cohere.Client('<<apiKey>>') A list of previous messages between the user and the
response = co.chat( model, meant to give the model conversational context for
chat_history=[ responding to the user's message.
{"role": "USER", "message": "Who discovered gravity?"},
{"role": "CHATBOT", "message": "The man who is widely
credited with discovering gravity is Sir Isaac Newton"} One of CHATBOT|USER to identify who the message is coming from.
],
message="What year was he born?",
# perform web search before answering the question. You can
also use your own custom connector.
connectors=[{"id": "web-search"}] Text input for the model to respond to.
)
print(response)
Summarize Endpoint (Deprecated)
Generates a succinct version of the original text that relays the most important information.

• Ideal use cases : news articles, blogs, chat transcripts, scientific articles, meeting
notes etc.
• The endpoint can:
• Summarize a single document
• Control output length
• Format chosen output
• Handle long documents
• Provide additional instructions to focus the summary
Summarize Endpoint – How to use?
Create prompt
text ="""It's an exciting day for the development community. Cohere's state-of-the-art language AI
is now available through Amazon SageMaker. This makes it easier for developers to deploy Cohere's
pre-trained generation language model to Amazon SageMaker, an end-to-end machine learning (ML)
service. Developers, data scientists, and business analysts use Amazon SageMaker to build, train,
and deploy ML models quickly and easily using its fully managed infrastructure, tools, and
workflows. At Cohere, the focus is on language. The company's mission is to enable developers and
businesses to add language AI to their technology stack and build game-changing applications with
it. Cohere helps developers and businesses automate a wide range of tasks, such as copywriting,
named entity recognition, paraphrasing, text summarization, and classification. The company builds
and continually improves its general-purpose large language models (LLMs), making them accessible
via a simple-to-use platform. Companies can use the models out of the box or tailor them to their
particular needs using their own custom data. Developers using SageMaker will have access to
Cohere's Medium generation language model. The Medium generation model excels at tasks that
require fast responses, such as question answering, copywriting, or paraphrasing. The Medium model
is deployed in containers that enable low-latency inference on a diverse set of hardware
accelerators available on AWS, providing different cost and performance advantages for SageMaker
customers. """
Summarize Endpoint – Define model settings. format

length • You can choose


model between paragraph a
short, medium and lon nd bullets.
g. short summaries
Generally, lite are roughly up to two
models are faster
sentences
while larger models temperature
will perform better. long, medium between
three and five,
extractiveness
and long might have
This parameter
more six or more This parameter can
ranges from 1 to 5.
sentences. be set
at low, medium, high
values.
Summarize Endpoint – How to use?
Generate the summary
• Call the endpoint via the co.summarize()
• Specifying the prompt and the rest of the model settings.
response = co.summarize(
text=text,
model='command’,
length='medium’,
extractiveness='medium’
)
summary = response.summary
Generate
https://api.cohere.ai/v1/generate
• This endpoint generates realistic text conditioned on a given input.

import cohere
co = cohere.Client('<<apiKey>>')

response = co.generate(
prompt='Please explain to me how LLMs work',
)
print(response)
Embed
{
"response_type": "embeddings_floats",
https://api.cohere.ai/v1/embed "id": "string",
"embeddings": [
[
0
• Returns text embeddings. ]
],
• An embedding is a list of floating point numbers that captures "texts": [
"string"
semantic information about the text that it represents. ],
"meta": {
• Embeddings can be used to create text classifiers as well as "api_version": {
"version": "string",
empower semantic search. "is_deprecated": true,
"is_experimental": true
},
import cohere "billed_units": {
co = cohere.Client('<<apiKey>>') "input_tokens": 0,
"output_tokens": 0,
"search_units": 0,
response = co.embed( "classifications": 0
texts=['hello', 'goodbye'], },
model='embed-english-v3.0', "warnings": [
input_type='classification' "string"
) ]
print(response) }
}
Cohere Playground

• A visual interface for users to test Cohere’s LLMs without writing a single line of
code.
Why Use Cohere Playground?

• Serves as a fantastic introduction to the incredible


capabilities of AI technology.
• By playing with different AI models, you can Introduction to AI Technology
experience first-hand their unique strengths and
discover how AI can benefit you.

• A haven for AI research and learning.


• By interacting with the pre-existing AI models, you can gain insights
into how these models respond to various prompts and how they
Research & Learning generate human-like text.
• Whether you’re a student trying to understand the nuances of AI for
your thesis or a business professional looking to leverage AI for
your operations, the Playground is an invaluable resource.
Innovation & Creativity

Innovation Be more Creative

• Allows you to push the • Whether you’re using AI to


boundaries of what’s possible with automate repetitive tasks,
AI, to imagine and realize new generate novel content, or
applications, and to contribute to understand complex data, the
the AI revolution in your own Playground is the canvas where
unique way. you bring your AI-powered ideas
to life.
Getting Started with Cohere Playground

Choose your Parameters


After registration, head over Try tinkering with different
to the Cohere Playground temperature and token-picking settings
to alter the model's output behaviour.

On the top you will see the 5 tabs: Pickup your Model
• Generate,
• Classify, Cohere Playground offers a variety of
models, each with its own set of
• Embed, strengths.
• Summarize and
• Chat. For example, command is a popular
choice for its superior text generation
abilities.
Chat API Example

You can search a specific website and ask questions about it.
Thank You

You might also like