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 systems 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
2
3import cohere
4
5co = cohere.Client(
6 api_key="AZURE_API_KEY_CHAT",
7 base_url="AZURE_ENDPOINT_CHAT" # example: "https://cohere-command-r-plus-08-2024-xyz.eastus.models.ai.azure.com/"
8)

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 message=message
4 )
5 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.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.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.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
2preamble = """## Task and Context
3You are a helpful customer support agent that assists customers of a mobile network service."""
4
5# Run the chatbot
6def run_chatbot(message, chat_history=None):
7 if chat_history is None:
8 chat_history = []
9
10 response = co.chat(
11 message=message,
12 preamble=preamble,
13 chat_history=chat_history
14 )
15
16 print(response.text)
17
18 chat_history = response.chat_history
19
20 return chat_history
PYTHON
1chat_history = run_chatbot("Hi. I've noticed some fluctuations in my mobile network's performance recently.")
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 when the network seems slower or less reliable? Any additional details about your location, device, or recent changes to your plan would be helpful for troubleshooting.
4
5We can work together to get to the bottom of this and find a solution to improve your network performance.
PYTHON
1chat_history = run_chatbot("At times, the data speed is very poor. What should I do?", chat_history)
1Here are some steps you can take to address the issue of poor data speeds:
2
3- **Check Network Coverage:** First, confirm that you are in an area with good network coverage. You can check your service provider's coverage map to see if there are any known weak signal areas in your location. If you're in a spot with low coverage, 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 cycling your device can clear any temporary glitches that might be affecting performance.
6
7- **Check for Network Updates:** Ensure that 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- **Monitor Data Usage:** Keep an eye on your data usage to ensure you aren't exceeding any plan limits, as this can result in throttled speeds. Most devices have data usage monitors in the settings menu. If you're close to your limit, consider upgrading your plan or waiting for the billing cycle to reset.
10
11- **Contact Customer Support:** If the issue persists, it's a good idea to contact your mobile network's customer support team. They can run diagnostics and check for any known network issues in your area. They might also provide specific advice based on your plan and device.
12
13- **Evaluate Network Congestion:** Data speeds can be affected by network congestion during peak usage hours. If possible, try using data-intensive applications at off-peak times to see if that makes a difference.
14
15- **Consider Network Optimization Settings:** Some devices offer network mode settings (like 4G/3G/2G preferences) that can be adjusted. Experimenting with these settings might help find a more stable connection.
16
17- **Check for Device-Specific Issues:** If other devices on the same network perform better, the issue might be device-specific. Try testing with another phone to isolate the problem.
18
19- **Network Troubleshooting Tools:** Various apps and online tools can help test and diagnose network speed issues. These tools can provide insights into potential problems.
20
21If you've tried the basic troubleshooting steps and are still experiencing poor data speeds, contacting your service provider's technical support team will be the next best step to identify any underlying issues.
PYTHON
1chat_history = run_chatbot("Thanks. What else can I check?", chat_history)
1You're welcome! Let's explore a few more advanced troubleshooting steps and potential factors:
2
3- **Device Antenna:** Check if your device's antenna is damaged or obstructed in any way. A compromised antenna can weaken the signal and data connection. Ensure there are no physical obstructions like phone cases or accessories blocking the antenna.
4
5- **Network Bands and Frequencies:** Different mobile networks operate on various frequency bands. Ensure that your device supports the specific bands and frequencies used by your carrier. This information is often available on the network provider's website.
6
7- **Network Settings Reset:** Resetting your device's network settings can help eliminate any custom configurations that might be causing issues. This will reset WiFi, cellular, and Bluetooth settings to default.
8
9- **Device Software Updates:** Keep your device's operating system and apps up to date. Updates often include performance improvements and bug fixes that could enhance network stability.
10
11- **Network Signal Boosters:** In areas with weak signals, some users opt for signal boosters or repeaters. Ensure that any such devices are properly set up and compatible with your network and device.
12
13- **Network Plan and Throttling:** Review the terms of your mobile plan to understand any data speed caps or throttling policies. Some plans have different speeds for peak and off-peak hours.
14
15- **Network Diagnostics Apps:** Download and run specialized network diagnostics apps that provide detailed information about your connection, signal strength, and potential issues. These apps can offer insights that might help pinpoint the problem.
16
17- **Location-Specific Interference:** Certain locations, like dense urban areas or buildings with thick walls, can experience signal interference. Try moving to different locations to see if the signal improves.
18
19- **Network Provider Support Channels:** Explore all available support channels offered by your network provider, including online forums, community pages, or social media support handles. These platforms often provide solutions to common issues.
20
21If you've exhausted these troubleshooting steps and still experience poor data speeds, it's time to engage with your mobile network's customer support team, who can provide further guidance based on your specific situation. They might also offer network optimization tips tailored to your area.

View the chat history

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

PYTHON
1print("Chat history:")
2for turn in chat_history:
3 print(turn, "\n")
1Chat history:
2role='USER' message="Hi. I've noticed some fluctuations in my mobile network's performance recently." tool_calls=None
3
4role='CHATBOT' message="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 when the network seems slower or less reliable? Any additional details about your location, device, or recent changes to your plan would be helpful for troubleshooting. \n\nWe can work together to get to the bottom of this and find a solution to improve your network performance." tool_calls=None
5
6role='USER' message='At times, the data speed is very poor. What should I do?' tool_calls=None
7
8role='CHATBOT' message="Here are some steps you can take to address the issue of poor data speeds:\n\n- **Check Network Coverage:** First, confirm that you are in an area with good network coverage. You can check your service provider's coverage map to see if there are any known weak signal areas in your location. If you're in a spot with low coverage, 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 cycling your device can clear any temporary glitches that might be affecting performance.\n\n- **Check for Network Updates:** Ensure that 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- **Monitor Data Usage:** Keep an eye on your data usage to ensure you aren't exceeding any plan limits, as this can result in throttled speeds. Most devices have data usage monitors in the settings menu. If you're close to your limit, consider upgrading your plan or waiting for the billing cycle to reset.\n\n- **Contact Customer Support:** If the issue persists, it's a good idea to contact your mobile network's customer support team. They can run diagnostics and check for any known network issues in your area. They might also provide specific advice based on your plan and device.\n\n- **Evaluate Network Congestion:** Data speeds can be affected by network congestion during peak usage hours. If possible, try using data-intensive applications at off-peak times to see if that makes a difference.\n\n- **Consider Network Optimization Settings:** Some devices offer network mode settings (like 4G/3G/2G preferences) that can be adjusted. Experimenting with these settings might help find a more stable connection.\n\n- **Check for Device-Specific Issues:** If other devices on the same network perform better, the issue might be device-specific. Try testing with another phone to isolate the problem.\n\n- **Network Troubleshooting Tools:** Various apps and online tools can help test and diagnose network speed issues. These tools can provide insights into potential problems.\n\nIf you've tried the basic troubleshooting steps and are still experiencing poor data speeds, contacting your service provider's technical support team will be the next best step to identify any underlying issues." tool_calls=None
9
10role='USER' message='Thanks. What else can I check?' tool_calls=None
11
12role='CHATBOT' message="You're welcome! Here are some additional points to consider and troubleshoot the network speed issue further:\n\n- **WiFi or Mobile Data:** Determine whether the speed issue is specific to your mobile data or if it also occurs when connected to WiFi. If WiFi speeds are fine, the problem might be related to the mobile network or your device's cellular connection.\n\n- **Network Mode:** Check the network mode settings on your device. It might be set to a power-saving mode or a mode that prioritizes voice quality over data speed. Changing it to a performance-oriented mode could help.\n\n- **SIM Card:** Ensure that your SIM card is properly inserted and clean. A damaged or loose SIM card might cause connectivity issues. Try removing and reinserting the SIM card, and if possible, test with a different SIM card in your device to isolate the problem.\n\n- **Device Settings:** Review your device's cellular settings for any customizations. For example, some devices allow you to set data limits or restrict background data usage, which could impact speed.\n\n- **Background Applications:** Certain apps running in the background can consume data and resources, impacting overall performance. Close any unnecessary apps and see if that makes a difference.\n\n- **Network Diagnostics Tools:** Utilize the network diagnostics and optimization tools provided by your device manufacturer. These tools can help identify and fix common connectivity issues.\n\n- **Network Congestion at Specific Times:** Note the times when you experience slow speeds. If it consistently happens during certain hours, it might be due to network congestion. Contacting your service provider can help confirm this.\n\n- **Device Age and Specifications:** Older devices or those with lower specifications might struggle with newer network technologies. Ensure your device is compatible with the network's capabilities.\n\n- **Network Provider Status:** Check your network provider's website or social media for any reported outages or maintenance in your area.\n\nIf you've gone through these checks and the issue persists, it's advisable to contact your mobile network's customer support, as they can provide real-time network status updates and further assistance tailored to your account." tool_calls=None
13
14role='USER' message='Thanks. What else can I check?' tool_calls=None
15
16role='CHATBOT' message="You're welcome! Let's explore a few more advanced troubleshooting steps and potential factors:\n\n- **Device Antenna:** Check if your device's antenna is damaged or obstructed in any way. A compromised antenna can weaken the signal and data connection. Ensure there are no physical obstructions like phone cases or accessories blocking the antenna.\n\n- **Network Bands and Frequencies:** Different mobile networks operate on various frequency bands. Ensure that your device supports the specific bands and frequencies used by your carrier. This information is often available on the network provider's website.\n\n- **Network Settings Reset:** Resetting your device's network settings can help eliminate any custom configurations that might be causing issues. This will reset WiFi, cellular, and Bluetooth settings to default.\n\n- **Device Software Updates:** Keep your device's operating system and apps up to date. Updates often include performance improvements and bug fixes that could enhance network stability.\n\n- **Network Signal Boosters:** In areas with weak signals, some users opt for signal boosters or repeaters. Ensure that any such devices are properly set up and compatible with your network and device.\n\n- **Network Plan and Throttling:** Review the terms of your mobile plan to understand any data speed caps or throttling policies. Some plans have different speeds for peak and off-peak hours.\n\n- **Network Diagnostics Apps:** Download and run specialized network diagnostics apps that provide detailed information about your connection, signal strength, and potential issues. These apps can offer insights that might help pinpoint the problem.\n\n- **Location-Specific Interference:** Certain locations, like dense urban areas or buildings with thick walls, can experience signal interference. Try moving to different locations to see if the signal improves.\n\n- **Network Provider Support Channels:** Explore all available support channels offered by your network provider, including online forums, community pages, or social media support handles. These platforms often provide solutions to common issues.\n\nIf you've exhausted these troubleshooting steps and still experience poor data speeds, it's time to engage with your mobile network's customer support team, who can provide further guidance based on your specific situation. They might also offer network optimization tips tailored to your area." tool_calls=None

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