# Start a conversation
messages = [
{"role": "user", "content": "What are the latest AI developments?"}
]
# Get Claude's response
response = claude_client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1000,
messages=messages,
tools=tools
)
# Check if Claude wants to call our function
if 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)