Open in GitHub

In this tutorial, we’ll explore text generation using Cohere’s Command model on Azure AI Foundry.

Text generation is a fundamental capability that enables LLMs to generate text for various applications, such as providing detailed responses to questions, helping with writing and editing tasks, creating conversational responses, and assisting with code generation and documentation.

In this tutorial, we’ll cover:

  • Setting up the Cohere client
  • Basic text generation
  • Other typical use cases
  • Building a chatbot

We’ll use Cohere’s Command model deployed on Azure to demonstrate these capabilities and help you understand how to effectively use text generation in your applications.

Setup

First, you will need to deploy the Command model on Azure via Azure AI Foundry. The deployment will create a serverless API with pay-as-you-go token based billing. You can find more information on how to deploy models in the Azure documentation.

In the example below, we are deploying the Command R+ (August 2024) model.

Once the model is deployed, you can access it via Cohere’s Python SDK. Let’s now install the Cohere SDK and set up our client.

To create a client, you need to provide the API key and the model’s base URL for the Azure endpoint. You can get these information from the Azure AI Foundry platform where you deployed the model.

PYTHON
1# %pip install cohere
2import cohere
3
4co = cohere.ClientV2(
5 api_key="AZURE_API_KEY_CHAT",
6 base_url="AZURE_ENDPOINT_CHAT", # example: "https://cohere-command-r-plus-08-2024-xyz.eastus.models.ai.azure.com/"
7)

Creating some contextual information

Before we begin, let’s create some context to use in our text generation tasks. In this example, we’ll use a set of technical support frequently asked questions (FAQs) as our context.

PYTHON
1# Technical support FAQ
2faq_tech_support = """- Question: How do I set up my new smartphone with my mobile plan?
3- Answer:
4 - Insert your SIM card into the device.
5 - Turn on your phone and follow the on-screen setup instructions.
6 - Connect to your mobile network and enter your account details when prompted.
7 - Download and install any necessary apps or updates.
8 - Contact customer support if you need further assistance.
9
10- Question: My internet connection is slow. How can I improve my mobile data speed?
11- Answer:
12 - Check your signal strength and move to an area with better coverage.
13 - Restart your device and try connecting again.
14 - Ensure your data plan is active and has sufficient data.
15 - Consider upgrading your plan for faster speeds.
16
17- Question: I can't connect to my mobile network. What should I do?
18- Answer:
19 - Check your SIM card is inserted correctly and not damaged.
20 - Restart your device and try connecting again.
21 - Ensure your account is active and not suspended.
22 - Check for any network outages in your area.
23 - Contact customer support for further assistance.
24
25- Question: How do I set up my voicemail?
26- Answer:
27 - Dial your voicemail access number (usually provided by your carrier).
28 - Follow the prompts to set up your voicemail greeting and password.
29 - Record your voicemail greeting and save it.
30 - Test your voicemail by calling your number and leaving a message.
31
32- Question: I'm having trouble sending text messages. What could be the issue?
33- Answer:
34 - Check your signal strength and move to an area with better coverage.
35 - Ensure your account has sufficient credit or an active plan.
36 - Restart your device and try sending a message again.
37 - Check your message settings and ensure they are correct.
38 - Contact customer support if the issue persists."""

Helper function to generate text

Now, let’s define a function to generate text using the Command R+ model on Bedrock. We’ll use this function a few times throughout.

This function takes a user message and generates the response via the chat endpoint. Note that we don’t need to specify the model as we have already set it up in the client.

PYTHON
1def generate_text(message):
2 response = co.chat(
3 model="model", # Pass a dummy string
4 messages=[{"role": "user", "content": message}],
5 )
6 return response

Text generation

Let’s explore basic text generation as our first use case. The model takes a prompt as input and produces a relevant response as output.

Consider a scenario where a customer support agent uses an LLM to help draft responses to customer inquiries. The agent provides technical support FAQs as context along with the customer’s question. The prompt is structured to include three components: the instruction, the context (FAQs), and the specific customer inquiry.

After passing this prompt to our generate_text function, we receive a response object. The actual generated text can be accessed through the response.text attribute.

PYTHON
1inquiry = "I've noticed some fluctuations in my mobile network's performance recently. The connection seems stable most of the time, but every now and then, I experience brief periods of slow data speeds. It happens a few times a day and is quite inconvenient."
2
3prompt = f"""Use the FAQs below to provide a concise response to this customer inquiry.
4
5# Customer inquiry
6{inquiry}
7
8# FAQs
9{faq_tech_support}"""
10
11response = generate_text(prompt)
12
13print(response.message.content[0].text)
1It's quite common to experience occasional fluctuations in mobile network performance, and there are a few steps you can take to address this issue.
2
3First, check your signal strength and consider moving to a different location with better coverage. Sometimes, even a small change in position can make a difference. If you find that you're in an area with low signal strength, this could be the primary reason for the slow data speeds.
4
5Next, try restarting your device. A simple restart can often resolve temporary glitches and improve your connection. After restarting, ensure that your data plan is active and has enough data allocated for your usage. If you're close to reaching your data limit, this could also impact your speeds.
6
7If the issue persists, it might be worth checking for any network outages in your area. Occasionally, temporary network issues can cause intermittent slowdowns. Contact your mobile network's customer support to inquire about any known issues and to receive further guidance.
8
9Additionally, consider the age and condition of your device. Older devices or those with outdated software might struggle to maintain consistent data speeds. Ensuring your device is up-to-date and well-maintained can contribute to a better overall network experience.
10
11If the problem continues, you may want to explore the option of upgrading your data plan. Higher-tier plans often offer faster speeds and more reliable connections, especially during peak usage times. Contact your mobile provider to discuss the available options and find a plan that better suits your needs.

Text summarization

Another type of use case is text summarization. Now, let’s summarize the customer inquiry into a single sentence. We add an instruction to the prompt and then pass the inquiry to the prompt.

PYTHON
1prompt = f"""Summarize this customer inquiry into one short sentence.
2
3Inquiry: {inquiry}"""
4
5response = generate_text(prompt)
6
7print(response.message.content[0].text)
1A customer is experiencing intermittent slow data speeds on their mobile network several times a day.

Text rewriting

Text rewriting is a powerful capability that allows us to adapt content for different purposes while preserving the core message. This involves transforming the style, tone, or format of text to better suit the target audience or medium.

Let’s look at an example where we convert a customer support chat response into a formal email. We’ll construct the prompt by first stating our goal to rewrite the text, then providing the original chat response as context.

PYTHON
1prompt = f"""Rewrite this customer support agent response into an email format, ready to send to the customer.
2
3If you're experiencing brief periods of slow data speeds or difficulty sending text messages and connecting to your mobile network, here are some troubleshooting steps you can follow:
4
51. Check your signal strength - Move to an area with better coverage.
62. Restart your device and try connecting again.
73. Ensure your account is active and not suspended.
84. Contact customer support for further assistance. (This can include updating your plan for better network performance.)
9
10Did these steps help resolve the issue? Let me know if you need further assistance."""
11
12response = generate_text(prompt)
13
14print(response.message.content[0].text)
1Subject: Troubleshooting Slow Data Speeds and Network Connection Issues
2
3Dear [Customer's Name],
4
5I hope this email finds you well. I understand that you may be facing some challenges with your mobile network, including slow data speeds and difficulties sending text messages. Here are some recommended troubleshooting steps to help resolve these issues:
6
7- Signal Strength: Check the signal strength on your device and move to a different location if the signal is weak. Moving to an area with better coverage can often improve your connection.
8
9- Restart Your Device: Sometimes, a simple restart can resolve temporary glitches. Please restart your device and then try connecting to the network again.
10
11- Account Status: Verify that your account is active and in good standing. In some cases, service providers may temporarily suspend accounts due to various reasons, which can impact your network access.
12
13- Contact Customer Support: If the issue persists, please reach out to our customer support team for further assistance. Our team can help troubleshoot and provide additional guidance. We can also discuss your current plan and explore options to enhance your network performance if needed.
14
15I hope these steps will help resolve the issue promptly. Please feel free to reply to this email if you have any further questions or if the problem continues. We are committed to ensuring your satisfaction and providing a seamless network experience.
16
17Best regards,
18[Your Name]
19[Customer Support Agent]
20[Company Name]

Build a Chatbot

While our previous examples were single-turn interactions, the Chat endpoint enables us to create chatbots that maintain memory of past conversation turns. This capability allows developers to build conversational applications that preserve context throughout the dialogue.

Below, we implement a basic customer support chatbot that acts as a helpful service agent. We’ll create a function called run_chatbot that handles the conversation flow and displays messages and events. The function can take an optional chat history parameter to maintain conversational context across multiple turns.

For this, we introduce a couple of additional parameters to the Chat endpoint:

  • preamble: A preamble contains instructions to help steer a chatbot’s response toward specific characteristics, such as a persona, style, or format. Here, we are using a simple preamble: “You are a helpful customer support agent that assists customers of a mobile network service.”
  • chat_history: We store the history of a conversation between a user and the chatbot as a list, append every new conversation turn, and pass this information to the next endpoint call.
PYTHON
1# Define a preamble
2system_message = """## Task and Context
3You are a helpful customer support agent that assists customers of a mobile network service."""
4
5
6# Run the chatbot
7def run_chatbot(message, messages=None):
8 if messages is None:
9 messages = []
10
11 if "system" not in {m.get("role") for m in messages}:
12 messages.append({"role": "system", "content": system_message})
13
14 messages.append({"role": "user", "content": message})
15
16 response = co.chat(
17 model="model", # Pass a dummy string
18 messages=messages,
19 )
20
21 messages.append(
22 {
23 "role": "assistant",
24 "content": response.message.content[0].text,
25 }
26 )
27
28 print(response.message.content[0].text)
29
30 return messages
PYTHON
1messages = run_chatbot(
2 "Hi. I've noticed some fluctuations in my mobile network's performance recently."
3)
1Hello there! I'd be happy to assist you with this issue. Network performance fluctuations can be concerning, and it's important to identify the cause to ensure you have a smooth experience.
2
3Can you tell me more about the problems you've been experiencing? Are there specific times or locations where the network seems to perform poorly? Any details you can provide will help me understand the situation better and offer potential solutions.
PYTHON
1messages = run_chatbot(
2 "At times, the data speed is very poor. What should I do?",
3 messages,
4)
1I'm sorry to hear that you're experiencing slow data speeds. Here are some troubleshooting steps and tips to help improve your network performance:
2
3- **Check Network Coverage:** First, ensure that you are in an area with good network coverage. You can check the coverage map provided by your mobile network service on their website. If you're in a location with known weak signal strength, moving to a different area might improve your data speed.
4
5- **Restart Your Device:** Sometimes, a simple restart of your mobile device can help refresh the network connection. Power off your device, wait for a few moments, and then turn it back on.
6
7- **Check for Network Updates:** Make sure your device is running the latest software and carrier settings. Updates often include improvements and optimizations for network performance. You can check for updates in your device's settings.
8
9- **Manage Network Settings:**
10 - *Network Mode:* Try switching to a different network mode (e.g., 4G/LTE, 3G) to see if a specific network band provides better speed.
11 - *Airplane Mode:* Toggle Airplane mode on and off to reconnect to the network.
12 - *Network Reset:* If the issue persists, you can try resetting your network settings, but note that this will erase saved Wi-Fi passwords.
13
14- **Contact Customer Support:** If the problem continues, it might be beneficial to contact your mobile network's customer support team. They can check for any known issues in your area and provide further assistance. They might also guide you through advanced troubleshooting steps.
15
16- **Consider Network Congestion:** Slow data speeds can sometimes occur during peak usage hours when the network is congested. Try using data-intensive apps during off-peak hours to see if that makes a difference.
17
18- **Check Background Apps:** Certain apps running in the background can consume data and impact speed. Close any unnecessary apps to free up resources.
19
20If the slow data speed persists despite these measures, it's advisable to reach out to your mobile network provider for further investigation and assistance. They can provide network-specific solutions and ensure you're getting the service you expect.
PYTHON
1messages = run_chatbot("Thanks. What else can I check?", messages)
1ou're welcome! Here are some additional steps and factors to consider:
2
3- **Device Health:** Ensure your device is in good working condition. An older device or one with hardware issues might struggle to maintain a fast data connection. Consider checking for any pending system updates that could optimize your device's performance.
4
5- **SIM Card:** Try removing and reinserting your SIM card to ensure it is properly seated. A loose connection can impact network performance. If the issue persists, it might be worth asking your network provider for a SIM replacement.
6
7- **Network Congestion at Specific Times:** Network speed can vary depending on the time of day. If possible, monitor your data speed during different parts of the day to identify any patterns. This can help determine if network congestion during peak hours is the primary cause.
8
9- **Data Plan and Throttling:** Check your mobile data plan to ensure you haven't exceeded any data limits, which could result in reduced speeds. Some providers throttle speeds after a certain data threshold is reached.
10
11- **Background Updates and Downloads:** Certain apps might be set to update or download content in the background, consuming data and potentially slowing down your connection. Review your app settings and consider disabling automatic updates or background data usage for apps that don't require real-time updates.
12
13- **Network Diagnostics Tools:** Some mobile devices have built-in network diagnostics tools that can provide insights into your connection. These tools can help identify issues with signal strength, network latency, and more.
14
15- **Wi-Fi Calling and Data Usage:** If your device supports Wi-Fi calling, ensure it is enabled. This can offload some data usage from the cellular network, potentially improving speeds.
16
17- **Network Provider's App:** Download and install your mobile network provider's official app, if available. These apps often provide real-time network status updates and allow you to report issues directly.
18
19If you've gone through these checks and the problem persists, contacting your network provider's technical support team is the next best step. They can provide further guidance based on your specific situation.

View the chat history

Here’s what is contained in the chat history after a few turns.

PYTHON
1print("Chat history:")
2for message in messages:
3 print(message, "\n")
1Chat history:
2{'role': 'system', 'content': '## Task and Context\nYou are a helpful customer support agent that assists customers of a mobile network service.'}
3
4{'role': 'user', 'content': "Hi. I've noticed some fluctuations in my mobile network's performance recently."}
5
6{'role': 'assistant', 'content': "Hello there! I'd be happy to assist you with this issue. Network performance fluctuations can be concerning, and it's important to identify the cause to ensure you have a smooth experience. \n\nCan you tell me more about the problems you've been experiencing? Are there specific times or locations where the network seems to perform poorly? Any details you can provide will help me understand the situation better and offer potential solutions."}
7
8{'role': 'user', 'content': 'At times, the data speed is very poor. What should I do?'}
9
10{'role': 'assistant', 'content': "I'm sorry to hear that you're experiencing slow data speeds. Here are some troubleshooting steps and tips to help improve your network performance:\n\n- **Check Network Coverage:** First, ensure that you are in an area with good network coverage. You can check the coverage map provided by your mobile network service on their website. If you're in a location with known weak signal strength, moving to a different area might improve your data speed.\n\n- **Restart Your Device:** Sometimes, a simple restart of your mobile device can help refresh the network connection. Power off your device, wait for a few moments, and then turn it back on.\n\n- **Check for Network Updates:** Make sure your device is running the latest software and carrier settings. Updates often include improvements and optimizations for network performance. You can check for updates in your device's settings.\n\n- **Manage Network Settings:**\n - *Network Mode:* Try switching to a different network mode (e.g., 4G/LTE, 3G) to see if a specific network band provides better speed.\n - *Airplane Mode:* Toggle Airplane mode on and off to reconnect to the network.\n - *Network Reset:* If the issue persists, you can try resetting your network settings, but note that this will erase saved Wi-Fi passwords.\n\n- **Contact Customer Support:** If the problem continues, it might be beneficial to contact your mobile network's customer support team. They can check for any known issues in your area and provide further assistance. They might also guide you through advanced troubleshooting steps.\n\n- **Consider Network Congestion:** Slow data speeds can sometimes occur during peak usage hours when the network is congested. Try using data-intensive apps during off-peak hours to see if that makes a difference.\n\n- **Check Background Apps:** Certain apps running in the background can consume data and impact speed. Close any unnecessary apps to free up resources.\n\nIf the slow data speed persists despite these measures, it's advisable to reach out to your mobile network provider for further investigation and assistance. They can provide network-specific solutions and ensure you're getting the service you expect."}
11
12{'role': 'user', 'content': 'Thanks. What else can I check?'}
13
14{'role': 'assistant', 'content': "You're welcome! Here are some additional steps and factors to consider:\n\n- **Device Health:** Ensure your device is in good working condition. An older device or one with hardware issues might struggle to maintain a fast data connection. Consider checking for any pending system updates that could optimize your device's performance.\n\n- **SIM Card:** Try removing and reinserting your SIM card to ensure it is properly seated. A loose connection can impact network performance. If the issue persists, it might be worth asking your network provider for a SIM replacement.\n\n- **Network Congestion at Specific Times:** Network speed can vary depending on the time of day. If possible, monitor your data speed during different parts of the day to identify any patterns. This can help determine if network congestion during peak hours is the primary cause.\n\n- **Data Plan and Throttling:** Check your mobile data plan to ensure you haven't exceeded any data limits, which could result in reduced speeds. Some providers throttle speeds after a certain data threshold is reached.\n\n- **Background Updates and Downloads:** Certain apps might be set to update or download content in the background, consuming data and potentially slowing down your connection. Review your app settings and consider disabling automatic updates or background data usage for apps that don't require real-time updates.\n\n- **Network Diagnostics Tools:** Some mobile devices have built-in network diagnostics tools that can provide insights into your connection. These tools can help identify issues with signal strength, network latency, and more.\n\n- **Wi-Fi Calling and Data Usage:** If your device supports Wi-Fi calling, ensure it is enabled. This can offload some data usage from the cellular network, potentially improving speeds.\n\n- **Network Provider's App:** Download and install your mobile network provider's official app, if available. These apps often provide real-time network status updates and allow you to report issues directly.\n\nIf you've gone through these checks and the problem persists, contacting your network provider's technical support team is the next best step. They can provide further guidance based on your specific situation."}

Summary

In this tutorial, we learned about:

  • How to set up the Cohere client to use the Command model deployed on Azure AI Foundry
  • How to perform basic text generation
  • How to use the model for other types of use cases
  • How to build a chatbot using the Chat endpoint

In the next tutorial, we’ll explore how to use the Embed model in semantic search applications.

Built with