ANTHROPIC - CHATBOT
Simon-Pierre Boucher
2024-09-14
This Python script defines a ChatBot
class that interacts with the Anthropic API to generate responses based on user input. Here's a detailed explanation of how it works:
1. Environment Setup¶
- The script uses the
dotenv
library to load environment variables, including the Anthropic API key (ANTHROPIC_API_KEY
), from a.env
file.
2. Class ChatBot
¶
Initialization (
__init__
):The
ChatBot
class is initialized with the API key, the model (claude-3-5-sonnet-20240620
), and parameters such asmax_tokens
,temperature
, andtop_p
. These parameters control the text generation behavior:max_tokens
: Limits the length of the generated text.temperature
: Controls randomness in the output.top_p
: Affects diversity via nucleus sampling.
A conversation history list (
self.conversation_history
) is used to maintain the ongoing chat context between the user and the assistant.
add_message()
:- This method adds a message to the conversation history, either from the user or the assistant.
- It accepts two parameters: the
role
(either "user" or "assistant") and thecontent
(the message text).
get_response()
:- This method handles the user input, sending it to the Anthropic API and returning the assistant's response.
- The method first adds the user’s message to the conversation history, then calls the
generate_anthropic_text()
method to send the conversation history to the Anthropic API. - It extracts the assistant’s reply from the API response, adds the reply to the conversation history, and returns it.
generate_anthropic_text()
:- This method interacts with the Anthropic API. It sends the conversation history along with the model,
max_tokens
,temperature
, andtop_p
to the API. - The method sends a POST request to
https://api.anthropic.com/v1/messages
with the necessary headers and data. - It returns the API's response as a dictionary or prints an error message if the request fails.
- This method interacts with the Anthropic API. It sends the conversation history along with the model,
3. Usage¶
- An instance of the
ChatBot
class is created (bot
) using the API key and default parameters for the model,max_tokens
,temperature
, andtop_p
. - The user’s input (
"Can you suggest 5 dinner ideas for this week?"
) is passed to theget_response()
method, which returns the assistant’s generated response.
Summary:¶
- Modular ChatBot: The class handles both user and assistant messages, maintains a conversation history, and interacts with Anthropic’s API.
- Parameters: Allows control over token limits, randomness, and text diversity using
max_tokens
,temperature
, andtop_p
. - Error Handling: Gracefully handles request exceptions by printing errors if the API call fails.
This structure allows for multiple interactions, keeping a continuous conversation context for better responses from the API.
In [1]:
import os
import requests
from dotenv import load_dotenv
from IPython.display import display, HTML
import re
# Charger les variables d'environnement depuis le fichier .env
load_dotenv()
# Obtenir la clé API depuis les variables d'environnement
api_key = os.getenv("ANTHROPIC_API_KEY")
In [3]:
import requests
class ChatBot:
def __init__(self, api_key, model="claude-3-5-sonnet-20240620", max_tokens=1024, temperature=0.7, top_p=0.9):
"""
Initialize the ChatBot with API key and parameters.
Parameters:
- api_key (str): The API key for Anthropic.
- model (str): The model to use for text generation.
- max_tokens (int): The maximum number of tokens to generate in the completion.
- temperature (float): Controls randomness in the output (0-1).
- top_p (float): Controls the diversity via nucleus sampling (0-1).
"""
self.api_key = api_key
self.model = model
self.max_tokens = max_tokens
self.temperature = temperature
self.top_p = top_p
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 "assistant").
- content (str): The content of the message.
"""
self.conversation_history.append({"role": role, "content": content})
def get_response(self, user_input):
"""
Get a response from the Anthropic 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
self.add_message("user", user_input)
# Call the Anthropic API to get a response
response = self.generate_anthropic_text(
self.api_key,
self.model,
self.conversation_history,
max_tokens=self.max_tokens,
temperature=self.temperature,
top_p=self.top_p
)
if response:
# Extract the assistant's reply correctly based on response structure
content = response.get("content", [])
if content and isinstance(content, list) and len(content) > 0 and "text" in content[0]:
assistant_reply = content[0]["text"]
else:
assistant_reply = "Sorry, I couldn't generate a response."
# Add assistant's reply to conversation history
self.add_message("assistant", assistant_reply)
return assistant_reply
else:
return "Sorry, I couldn't generate a response."
def generate_anthropic_text(self, api_key, model, messages, max_tokens=1024, temperature=0.7, top_p=0.9):
"""
Generate text using Anthropic's API.
Parameters:
- api_key (str): The API key for Anthropic.
- model (str): The model to use for text generation.
- messages (list): A list of messages to pass to the API in a conversation format.
- max_tokens (int): The maximum number of tokens to generate in the completion.
- temperature (float): Controls randomness in the output (0-1).
- top_p (float): Controls the diversity via nucleus sampling (0-1).
Returns:
- response (dict): The API response as a dictionary.
"""
url = "https://api.anthropic.com/v1/messages"
headers = {
"Content-Type": "application/json",
"x-api-key": api_key,
"anthropic-version": "2023-06-01"
}
data = {
"model": model,
"max_tokens": max_tokens,
"temperature": temperature,
"top_p": top_p,
"messages": messages
}
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
In [4]:
bot = ChatBot(api_key, model="claude-3-5-sonnet-20240620", max_tokens=1024, temperature=0.7, top_p=0.9)
In [5]:
user_input = "Can you suggest 5 dinner ideas for this week?"
response = bot.get_response(user_input)
print("Assistant:", response)
In [6]:
user_input = "Can you give me the recipe for the first idea?"
response = bot.get_response(user_input)
print("Assistant:", response)
In [7]:
user_input = "Can you give me the recipe for the second idea?"
response = bot.get_response(user_input)
print("Assistant:", response)