Created
March 5, 2026 18:10
-
-
Save mbstacy/2f7d9a66c564b7f3be3c30fb5cca1ef2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Strands Agent with Web Search - Harvard Holiday Schedule Example | |
| Uses the Strands Agents SDK to create an AI agent backed by Claude on AWS Bedrock | |
| (via Harvard's Apigee gateway) with a local web_search tool powered by DDGS. | |
| The agent searches the web for Harvard's current holiday schedule and writes | |
| the results to a markdown file. | |
| Install | |
| ------- | |
| pip install strands-agents ddgs | |
| Optional tools package (not required here, provides pre-built tools like | |
| file_read, shell, use_browser, retrieve, etc.): | |
| pip install strands-agents-tools | |
| Environment variables | |
| --------------------- | |
| APIGEE_API_KEY Harvard Apigee API key for the Bedrock gateway | |
| Usage | |
| ----- | |
| python strand_agents.py | |
| """ | |
| from strands import Agent, tool | |
| from strands.models.bedrock import BedrockModel | |
| from ddgs import DDGS | |
| from datetime import date | |
| import os | |
| # function to add custom headers to the request | |
| def add_custom_headers(request, **kwargs): | |
| request.headers.add_header('api-key', os.environ[f'APIGEE_API_KEY']) | |
| bedrock_model = BedrockModel( | |
| model_id="us.anthropic.claude-opus-4-6-v1", | |
| region_name="us-east-1", | |
| endpoint_url=f"https://go.apis.huit.harvard.edu/ais-bedrock-llm/v2", | |
| ) | |
| # Register custom Apigee header on the client BedrockModel created internally | |
| bedrock_model.client.meta.events.register_first('before-sign.bedrock-runtime.*', add_custom_headers) | |
| @tool | |
| def web_search(query: str) -> str: | |
| """Search the web for current information using DuckDuckGo (no API key required). | |
| Args: | |
| query: The search query string. | |
| Returns: | |
| A string containing the top search results with titles, URLs, and snippets. | |
| Available backends (pass via backend param): brave, duckduckgo, google, grokipedia, | |
| mojeek, wikipedia, yahoo, yandex. Defaults to "auto" (rotates engines). | |
| """ | |
| results = list(DDGS().text(query, max_results=5, backend="google")) | |
| return "\n\n".join( | |
| f"[{r['title']}]({r['href']})\n{r['body']}" | |
| for r in results | |
| ) | |
| # 2. Initialize the agent, providing the model and the web_search tool | |
| agent = Agent( | |
| model=bedrock_model, | |
| tools=[web_search], | |
| system_prompt=f"You are a Human Resources specialist for Harvard University. Today's date is {date.today()}." | |
| ) | |
| # 3. Invoke the agent with a prompt that requires searching the web | |
| response = agent("""Provide information about the university's holiday schedule and identify the next upcoming holiday for staff members. | |
| Use the web_search tool to find the most current information on Harvard's official website or other reliable sources. | |
| What is the next upcoming holiday for staff members? List holidays with dates and whether they have already passed | |
| or are upcoming.""") | |
| # 4. Write the response to a markdown file | |
| with open("harvard_holiday_schedule.md", "w") as f: | |
| f.write(str(response)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment