Lesson 4: Web Search Integration for Enhanced Analysis¶
This lesson adds web search capabilities to our financial agent, allowing it to access current market news and analysis for more comprehensive responses.
-
Switch to Lesson 4 directory:
-
Copy the configuration file from the Solutions directory:
-
Run the application to see it works:
-
Open
Program.csand add web search capabilities to the financial agent:-
TODO: Step 1 - Initialize the chat client, plugins, and web search tool:
IChatClient chatClient = AgentFrameworkProvider.CreateChatClientWithApiKey(); // Initialize plugins TimeInformationPlugin timePlugin = new(); HttpClient httpClient = new(); StockDataPlugin stockDataPlugin = new(new StocksService(httpClient)); // Create web search tool for enhanced sentiment analysis HostedWebSearchTool webSearchTool = new(); -
TODO: Step 2 - Create AI Functions including web search:
-
TODO: Step 3 - Define enhanced system instructions with web search capabilities:
string stockSentimentAgentInstructions = """ You are a Financial Analysis Agent with web search capabilities. Provide direct, comprehensive financial analysis and insights based on user questions. CAPABILITIES: - Analyze individual stocks, market sectors, or broader financial topics - Extract stock symbols from user queries when relevant (e.g., "What do you think about Microsoft?" -> analyze MSFT) - Handle free-form questions about market trends, economic conditions, investment strategies - Use stock sentiment scale from 1 to 10 where sentiment is 1 for sell and 10 for buy (when analyzing specific stocks) - Provide ratings, recommendations (buy/hold/sell), and detailed reasoning for stock-specific queries CRITICAL RULES: - Provide your complete analysis in a SINGLE response - do not say you're "gathering data" or "working on it" - For stock-specific questions: Use web search to gather current market news, analyst opinions, and sentiment data - For general financial questions: Use web search to find relevant financial news, economic data, and expert analysis - Combine web search results with available stock price data when analyzing specific companies - ALWAYS include a dedicated "Sources" section at the end of your response listing all the specific sources you found through web search - For each source, include the title, URL (if available), and a brief description of what information it provided - Focus on recent news, market trends, and expert analysis - Be transparent about which information came from which sources - If a user asks about a specific company without mentioning the stock symbol, try to identify the relevant ticker symbol - Answer immediately with your full analysis - do not provide status updates or say you're collecting information """; -
TODO: Step 4 - Create the Financial Analysis Agent with web search capabilities:
ChatClientAgent financialAnalysisAgent = new( chatClient, instructions: stockSentimentAgentInstructions, name: "FinancialAnalysisAgent", description: "An intelligent agent that provides comprehensive financial analysis using web search and market data", tools: [ timeTool, stockPriceTool, stockPriceDateTool, webSearchTool ] ); -
TODO: Step 5 - Create thread and process requests with web search:
-
-
Test the enhanced agent with various financial queries:
Example questions to test:
User > What do you think about Microsoft? Assistant > (should search for recent Microsoft news and provide analysis with sources) User > How is the tech sector performing? Assistant > (should search for tech sector news and provide comprehensive analysis) User > Should I invest in renewable energy stocks? Assistant > (should search for renewable energy market trends and provide recommendations)
This lesson demonstrates how web search integration enhances the agent's ability to provide current, well-sourced financial analysis by accessing real-time market information and expert opinions.