Notebooks
C
Cohere
Tutorial Pt2 V2

Tutorial Pt2 V2

guidesv2notebookscohere-developer-experiencegetting-started

Text Generation

Command is Cohere’s flagship LLM. It generates a response based on a user message or prompt. It is trained to follow user commands and to be instantly useful in practical business applications, like summarization, copywriting, extraction, and question-answering.

Command R and Command R+ are the most recent models in the Command family. They are the market-leading models that balance high efficiency with strong accuracy to enable enterprises to move from proof of concept into production-grade AI.

You'll use Chat, the Cohere endpoint for accessing the Command models.

In this tutorial, you'll learn about:

  • Basic text generation
  • Prompt engineering
  • Parameters for controlling output
  • Structured output generation
  • Streamed output

You'll learn these by building an onboarding assistant for new hires.

Setup

To get started, first we need to install the cohere library and create a Cohere client.

[4]

Basic text generation

To get started with Chat, we need to pass two parameters, model for the LLM model ID and messages, which we add a single user message. We then call the Chat endpoint through the client we created earlier.

The response contains several objects. For simplicity, what we want right now is the message.content[0].text object.

Here's an example of the assistant responding to a new hire's query asking for help to make introductions.

[4]
Absolutely! Here’s a warm and professional introduction message you can use to connect with your new teammates at Co1t:

---

**Subject:** Excited to Join the Co1t Team!  

Hi everyone,  

My name is [Your Name], and I’m thrilled to officially join Co1t starting today! I’m really excited to be part of such an innovative and dynamic team, and I’m looking forward to contributing to our shared mission.  

A little about me: [Share a brief background about your professional experience, skills, or what excites you about the startup world]. Outside of work, I enjoy [hobbies or interests, e.g., hiking, reading, or coding side projects].  

I’m eager to get to know all of you, learn from your expertise, and collaborate on the amazing work happening here. Please feel free to reach out if you’d like to chat or grab a virtual coffee—I’d love to connect!  

Looking forward to an exciting journey together at Co1t. Let’s make it happen!  

Best regards,  
[Your Name]  
[Your Role]  
Co1t  

---

Feel free to customize it further to match your personality and style! Welcome to Co1t! 🚀

Prompt engineering

Prompting is at the heart of working with LLMs. The prompt provides context for the text that we want the model to generate. The prompts we create can be anything from simple instructions to more complex pieces of text, and they are used to encourage the model to produce a specific type of output.

In this section, we'll look at a couple of prompting techniques.

The first is to add more specific instructions to the prompt. The more instructions you provide in the prompt, the closer you can get to the response you need.

The limit of how long a prompt can be is dependent on the maximum context length that a model can support (in the case Command R/R+, it's 128k tokens).

Below, we'll add one additional instruction to the earlier prompt: the length we need the response to be.

[5]
"Hi everyone, I’m thrilled to join the Co1t team and excited to collaborate with all of you to drive innovation and success together!"

All our prompts so far use what is called zero-shot prompting, which means that provide instruction without any example. But in many cases, it is extremely helpful to provide examples to the model to guide its response. This is called few-shot prompting.

Few-shot prompting is especially useful when we want the model response to follow a particular style or format. Also, it is sometimes hard to explain what you want in an instruction, and easier to show examples.

Below, we want the response to be similar in style and length to the convention, as we show in the examples.

[6]
Server Access Issue: Potential Permissions Problem

Parameters for controlling output

The Chat endpoint provides developers with an array of options and parameters.

For example, you can choose from several variations of the Command model. Different models produce different output profiles, such as quality and latency.

[7]
"Excited to join the Co1t team today, and I’m looking forward to collaborating with all of you to drive innovation and success!"

Often, you’ll need to control the level of randomness of the output. You can control this using a few parameters.

The most commonly used parameter is temperature, which is a number used to tune the degree of randomness. You can enter values between 0.0 to 1.0.

A lower temperature gives more predictable outputs, and a higher temperature gives more "creative" outputs.

Here's an example of setting temperature to 0.

[8]
1: "Industrial Revolution Enthusiast, Shaping Modernity."

2: "Industrial Revolution Enthusiast, Shaping Modernity."

3: "Industrial Revolution Enthusiast, Shaping Modernity."

And here's an example of setting temperature to 1.

[11]
1: "Industrial Revolution Enthusiast, History Buff."

2: "Industrial Revolution Enthusiast Here!"

3: "History Buff: Industrial Revolution Enthusiast"

Structured output generation

By adding the response_format parameter, you can get the model to generate the output as a JSON object. By generating JSON objects, you can structure and organize the model's responses in a way that can be used in downstream applications.

The response_format parameter allows you to specify the schema the JSON object must follow. It takes the following parameters:

  • message: The user message
  • response_format: The schema of the JSON object
[6]
The `response_format.schema` parameter is an experimental feature and may change in future releases.
To suppress this warning, set `log_warning_experimental_features=False` when initializing the client.
{'title': 'Unable to Access Server - Possible Permissions Issue', 'category': 'access', 'status': 'open'}

Streaming responses

All the previous examples above generate responses in a non-streamed manner. This means that the endpoint would return a response object only after the model has generated the text in full.

The Chat endpoint also provides streaming support. In a streamed response, the endpoint would return a response object for each token as it is being generated. This means you can display the text incrementally without having to wait for the full completion.

To activate it, use co.chat_stream() instead of co.chat().

In streaming mode, the endpoint will generate a series of objects. To get the actual text contents, we take objects whose event_type is content-delta.

[7]
"Excited to join the Co1t team today, and I'm looking forward to collaborating with all of you to drive innovation and growth!"

Conclusion

In this tutorial, you learned about:

  • How to get started with a basic text generation
  • How to improve outputs with prompt engineering
  • How to control outputs using parameter changes
  • How to generate structured outputs
  • How to stream text generation outputs

However, we have only done all this using direct text generations. As its name implies, the Chat endpoint can also support building chatbots, which require features to support multi-turn conversations and maintain the conversation state.

In Part 3, you'll learn how to build chatbots with the Chat endpoint.