import anthropicfrom linkup import LinkupClientimport json# Initialize clients with your API keysclaude_client = anthropic.Anthropic(api_key="your_anthropic_api_key")linkup_client = LinkupClient(api_key="your_linkup_api_key")
3
Define the Function Schema
Tell Claude about your search function:
Copy
Ask AI
tools = [{ "name": "search_web", "description": "Search the web in real time. Use this tool whenever the user needs trusted facts, news, or source-backed information. Returns comprehensive content from the most relevant sources.", "input_schema": { "type": "object", "properties": { "query": { "type": "string", "description": "The search query" } }, "required": ["query"] }}]
4
Handle the Conversation
Put it all together:
Copy
Ask AI
# Start a conversationmessages = [ {"role": "user", "content": "What are the latest AI developments?"}]# Get Claude's responseresponse = claude_client.messages.create( model="claude-3-5-sonnet-20241022", max_tokens=1000, messages=messages, tools=tools)# Check if Claude wants to call our functionif response.stop_reason == "tool_use": # Get the function call details tool_use = next(block for block in response.content if block.type == "tool_use") # Call Linkup to search the web linkup_response = linkup_client.search(query=tool_use.input["query"], depth="standard") search_results = json.dumps([{"content": result.content} for result in linkup_response.results]) # Continue the conversation with the function result messages = [ {"role": "user", "content": "What are the latest AI developments?"}, {"role": "assistant", "content": response.content}, { "role": "user", "content": [{ "type": "tool_result", "tool_use_id": tool_use.id, "content": search_results }] } ] # Get final response with search results final_response = claude_client.messages.create( model="claude-3-5-sonnet-20241022", max_tokens=1000, messages=messages, tools=tools ) # Extract text from response text_content = next((block.text for block in final_response.content if hasattr(block, "text")), None) print(text_content)else: # Extract text from response text_content = next((block.text for block in response.content if hasattr(block, "text")), None) print(text_content)
That’s it! Your AI can now search the web in real-time using Linkup whenever it needs current information.