COHERE - CHATBOT
Simon-Pierre Boucher
2024-09-14
This Python code defines a ChatBotCohere
class that interacts with the Cohere API to generate conversational responses using a history of conversation and a user-provided prompt. Here’s a detailed explanation of how the code works:
1. Environment Setup¶
- API Key: The script loads environment variables from a
.env
file usingload_dotenv()
and retrieves the Cohere API key (COHERE_API_KEY
) withos.getenv()
.
2. Text Generation Function (generate_cohere_text
)¶
This function sends a request to Cohere’s API for text generation, using the specified parameters:
- API key: Required for authentication.
- Model: Specifies the model to use (e.g.,
"command-r-plus"
). - Message: The current user input to which the assistant should respond.
- Chat History: A list of previous messages exchanged between the user and the assistant.
- Preamble: Provides context or instructions for the assistant (e.g.,
"You are an AI-assistant chatbot."
). - Temperature: Controls randomness in the output. A value of
1.0
gives balanced creativity and coherence. - Max Tokens: Specifies the maximum number of tokens (words/subwords) to generate in the response.
The API request is made via a POST request to the Cohere API's
/v1/chat
endpoint.The function handles exceptions and returns the API response in JSON format if successful.
3. ChatBotCohere
Class¶
- Initialization: The class is initialized with the API key and parameters such as:
- Model: The Cohere model used for text generation (default:
"command-r-plus"
). - Preamble: A context for the assistant's role (default:
"You are an AI-assistant chatbot."
). - Temperature: Controls randomness (default:
1.0
). - Max Tokens: The maximum number of tokens the assistant can generate (default:
2000
). - Conversation History: A list to keep track of the conversation's messages (including roles of
"USER"
or"CHATBOT"
).
- Model: The Cohere model used for text generation (default:
4. Adding Messages to Conversation History¶
- The
add_message()
method adds each new message (user input or assistant response) to the conversation history, assigning it the appropriate role ("USER"
or"CHATBOT"
).
5. Generating a Response (get_response
)¶
- The
get_response()
method takes the user's input, appends it to the conversation history as a"USER"
message, and sends a request to Cohere’s API using thegenerate_cohere_text()
function. - Once the API returns a response, the assistant’s generated reply is extracted and added to the conversation history as a
"CHATBOT"
message. - The assistant’s response is then returned to the user. If there’s an issue with the response (e.g., missing keys in the API response), an error message is displayed.
6. Example Workflow¶
- A
ChatBotCohere
instance is created using the API key and default settings. - The user inputs the message:
"Can you suggest 5 dinner ideas for this week?"
. - The bot sends the user input to the Cohere API, which generates a response.
- The response is printed, and the conversation continues with the assistant replying based on both the user’s input and the conversation history.
Key Flow of Execution:¶
- User Input: The user enters a query (e.g., asking for dinner suggestions).
- Conversation History: The query is added to the conversation history.
- API Request: The query, conversation history, and preamble are sent to the Cohere API to generate a response.
- Response: The assistant’s response is extracted and added to the conversation history.
- Output: The response is printed for the user.
This chatbot maintains a history of the conversation and uses that history to produce contextually appropriate responses using Cohere's language models.
In [2]:
import os
import requests
from dotenv import load_dotenv
# Charger les variables d'environnement depuis le fichier .env
load_dotenv()
# Obtenir la clé API depuis les variables d'environnement
api_key = os.getenv("COHERE_API_KEY")
def generate_cohere_text(api_key, model, message, chat_history, preamble, temperature=1.0, max_tokens=2000):
"""
Generate text using Cohere's API.
Parameters:
- api_key (str): The API key for Cohere.
- model (str): The model to use for text generation.
- message (str): The user's message.
- chat_history (list): A list of previous messages in the conversation.
- preamble (str): A preamble that sets the context for the chatbot.
- temperature (float): Controls randomness in the output (0-1).
- max_tokens (int): The maximum number of tokens to generate in the completion.
Returns:
- response (dict): The API response as a dictionary.
"""
url = "https://api.cohere.com/v1/chat"
headers = {
"accept": "application/json",
"content-type": "application/json",
"Authorization": f"Bearer {api_key}"
}
data = {
"model": model,
"message": message,
"chat_history": chat_history,
"preamble": preamble,
"temperature": temperature,
"max_tokens": max_tokens
}
# Supprimer ou commenter la ligne d'impression des données
# print("Sending data to Cohere API:", data)
try:
response = requests.post(url, headers=headers, json=data)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
return None
class ChatBotCohere:
def __init__(self, api_key, model="command-r-plus", preamble="You are an AI-assistant chatbot.", temperature=1.0, max_tokens=2000):
"""
Initialize the ChatBotCohere with API key and parameters.
Parameters:
- api_key (str): The API key for Cohere.
- model (str): The model to use for text generation.
- preamble (str): A preamble that sets the context for the chatbot.
- temperature (float): Controls randomness in the output (0-1).
- max_tokens (int): The maximum number of tokens to generate in the completion.
"""
self.api_key = api_key
self.model = model
self.preamble = preamble
self.temperature = temperature
self.max_tokens = max_tokens
self.conversation_history = []
def add_message(self, role, content):
"""
Add a message to the conversation history.
Parameters:
- role (str): The role of the sender ("USER" or "CHATBOT").
- content (str): The content of the message.
"""
self.conversation_history.append({"message": content, "role": role})
def get_response(self, user_input):
"""
Get a response from the Cohere API based on the user input.
Parameters:
- user_input (str): The user's input message.
Returns:
- assistant_reply (str): The assistant's generated response.
"""
# Add user input to conversation history with role "USER"
self.add_message("USER", user_input)
# Call the Cohere API to get a response
response = generate_cohere_text(
self.api_key,
self.model,
user_input,
self.conversation_history,
self.preamble,
temperature=self.temperature,
max_tokens=self.max_tokens
)
if response:
# Extract and return only the message (text) generated by the chatbot
try:
assistant_reply = response["text"]
# Add assistant's reply to conversation history with role "CHATBOT"
self.add_message("CHATBOT", assistant_reply)
return assistant_reply
except KeyError:
return "Sorry, I couldn't find the expected response key in the API response."
else:
return "Sorry, I couldn't generate a response."
In [3]:
bot = ChatBotCohere(api_key)
In [4]:
user_input = "Can you suggest 5 dinner ideas for this week?"
response = bot.get_response(user_input)
print("Assistant:", response)
In [5]:
user_input = "Can you give me the recipe for the first idea?"
response = bot.get_response(user_input)
print("Assistant:", response)
In [6]:
user_input = "Can you give me the recipe for the second idea?"
response = bot.get_response(user_input)
print("Assistant:", response)