data-village / crewai /tools /anthropic_tools.py
eaglelandsonce's picture
Update crewai/tools/anthropic_tools.py
64d429b verified
import os
import anthropic
# Retrieve API Key from Environment Variable
ANTHROPIC_API_KEY = os.environ.get('ANTHROPIC_API_KEY')
# Ensure the API key is available
if not ANTHROPIC_API_KEY:
raise ValueError("API key not found. Please set the ANTHROPIC_API_KEY environment variable.")
from langchain.tools import tool
# Initialize the Anthropic client
#client = anthropic.Client(ANTHROPIC_API_KEY)
client = anthropic.Anthropic(api_key=ANTHROPIC_API_KEY)
class AnthropicSearchTools:
@tool("Anthropic search the internet")
def anthropic_search(query):
"""
Searches for content based on the provided query using the Anthropic model.
Args:
query (str): The search query.
Returns:
str: The response text from the Anthropic model or an error message.
"""
try:
response = client.messages.create(
model="claude-3-opus-20240229",
max_tokens=1024,
messages=[
{"role": "user", "content": query}]
)
return response.content
except Exception as e:
# Handle any exceptions here
print(f"Error: {str(e)}")
return "Error: An unexpected error occurred. Please try again later."