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) 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.completions.create( prompt=query, max_tokens_to_sample=500, model="claude-v1", stop_sequences=[], ) return response.completion except Exception as e: # Handle any exceptions here print(f"Error: {str(e)}") return "Error: An unexpected error occurred. Please try again later." @tool("Anthropic search news on the internet") def anthropic_search_news(query): """ Searches for news 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.completions.create( prompt=f"Search for news about: {query}", max_tokens_to_sample=500, model="claude-v1", stop_sequences=[], ) return response.completion except Exception as e: # Handle any exceptions here print(f"Error: {str(e)}") return "Error: An unexpected error occurred. Please try again later."