from langchain.tools import tool import requests from pydantic import BaseModel, Field import datetime from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder @tool def filter_funds(text: str) -> str: """ This function filters the dataframe to find funds located in the specia region. Parameters: df (pandas.DataFrame): The dataframe containing fund data. Returns: pandas.DataFrame: A dataframe with only {text} funds. """ return data[data['Region'] == text] @tool def max_min_profit_margin_funds() -> str: """ Return the fund names with the highest and lowest profit margins. This function handles potential errors and ensures data consistency. """ try: # Convert Profit Margin column to numeric, handling errors gracefully data['Profit Margin'] = pd.to_numeric(data['Profit Margin'].str.replace('%', ''), errors='coerce') # Drop rows with NaN values in Profit Margin column clean_data = data.dropna(subset=['Profit Margin']) if clean_data.empty: return "No valid profit margin data available." # Find the funds with the highest and lowest profit margins max_profit_margin_fund = clean_data.loc[clean_data['Profit Margin'].idxmax(), 'Fund Name'] min_profit_margin_fund = clean_data.loc[clean_data['Profit Margin'].idxmin(), 'Fund Name'] return f"Highest Profit Margin Fund: {max_profit_margin_fund}, Lowest Profit Margin Fund: {min_profit_margin_fund}" except Exception as e: return f"An error occurred: {str(e)}" import requests from bs4 import BeautifulSoup import random # List of different headers to mimic various browser requests user_agents = [ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Safari/605.1.15", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.101 Safari/537.36", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0", "Mozilla/5.0 (iPhone; CPU iPhone OS 14_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Mobile/15E148 Safari/604.1" ] @tool def gresb(query: str) -> str: """Search for the query on the GRESB website and extract article content if available.""" base_url = "https://www.gresb.com/nl-en?s=" search_url = f"{base_url}{query.replace(' ', '+')}" # Select a random User-Agent header headers = { "User-Agent": random.choice(user_agents) } # Make a request to the search URL with headers response = requests.get(search_url, headers=headers) # Check if the request was successful if response.status_code == 200: # Parse the HTML content soup = BeautifulSoup(response.content, 'html.parser') # Extract search results (adjust the selector based on the website structure) results = soup.find_all('a', class_='overlay-link z-index-1') # Check if there are any results if results: # Get the first result's link article_url = results[0]['href'] # Fetch the HTML content of the article article_response = requests.get(article_url, headers=headers) if article_response.status_code == 200: # Extract and return the article text return extract_article_text(article_response.content) else: return f"Failed to retrieve the article page. Status code: {article_response.status_code}" else: return "No search results found." else: return f"Failed to retrieve search results. Status code: {response.status_code}" def extract_article_text(html_content): soup = BeautifulSoup(html_content, 'html.parser') # Look for common article structures on GRESB's website article = soup.find('div', class_='wysiwyg') if article: paragraphs = article.find_all(['p', 'ul', 'blockquote', 'h2', 'h4']) # Includes
,
,,
tags return ' '.join(p.get_text() for p in paragraphs).strip() return "Article content not found in the provided structure." # Example usage #query = "london office" #article_text = search_and_extract_gresb(query) #print(article_text) # This will print the extracted article content or any status messages import pandas as pd # Assuming you need pandas for DataFrame operations # Load the CSV file file_path = 'HW 1 newest version.csv' data = pd.read_csv(file_path) import os import openai openai.api_key = os.environ['OPENAI_API_KEY'] tools = [gresb, filter_funds, max_min_profit_margin_funds] from langchain.chat_models import ChatOpenAI from langchain.prompts import ChatPromptTemplate from langchain.tools.render import format_tool_to_openai_function from langchain.agents.output_parsers import OpenAIFunctionsAgentOutputParser functions = [format_tool_to_openai_function(f) for f in tools] model = ChatOpenAI(temperature=0).bind(functions=functions) def run_agent(user_input): # 初始化一個空列表,用於存放中間步驟的結果和觀察值 intermediate_steps = [] max_iterations = 20 # 設置最大迭代次數,以避免無限循環 iteration_count = 0 # 進入循環,直到代理完成任務或者達到最大迭代次數 while iteration_count < max_iterations: iteration_count += 1 # 調用處理鏈 (agent_chain) 並傳遞用戶輸入和中間步驟數據 result = agent_chain.invoke({ "input": user_input, # 傳遞用戶輸入,這裡是用戶查詢 "intermediate_steps": intermediate_steps # 傳遞中間步驟,初始為空列表 }) # 如果結果是 AgentFinish 類型,說明代理已經完成任務,返回結果 if isinstance(result, AgentFinish): return result.return_values # 返回代理的最終輸出 # Now it's safe to print the message log print(result.message_log) # 根據結果中的工具名稱選擇合適的工具函數 tool = { "gresb": gresb, "filter_funds": filter_funds, "max_min_profit_margin_funds": max_min_profit_margin_funds, }.get(result.tool) # 如果工具函數存在,則運行工具函數 if tool: observation = tool.run(result.tool_input) # 將當前步驟的結果和觀察值加入 intermediate_steps 列表中 intermediate_steps.append((result, observation)) else: print(f"未找到合適的工具: {result.tool}") break # 如果迭代次數超過最大限制,返回錯誤信息 return "無法完成任務,請稍後再試。" from langchain.prompts import MessagesPlaceholder, ChatPromptTemplate prompt = ChatPromptTemplate.from_messages([ ("system", """You are a helpful assistant. There are three tools to use based on different scenarios. GRESB Tool: Usage Scenario: Use this tool when you need to search for fund information related to a specific area, city, or keyword on the GRESB website. It is ideal for searching fund details in specific locations such as "London office" or "Paris commercial real estate." Prompt Example: - "Use the GRESB tool to search for fund information related to the London office." - "Use the GRESB tool to search for fund information related to what is GRESB." Filter Funds Tool: Usage Scenario: Use this tool when you already have a set of fund data and want to filter funds based on specific criteria, such as region, asset type, or ESG scores. This tool helps you narrow down the funds that meet your requirements. Prompt Example: - "Use the Filter Funds tool to filter funds located in the Asia region." Max Min Profit Margin Funds Tool: Usage Scenario: Use this tool when you need to find funds with the highest or lowest profit margins. This tool is suitable for comparing the profitability of different funds and identifying the extreme values (maximum and minimum profit margins). Prompt Example: - "Use the Max Min Profit Margin Funds tool to find the fund with the highest profit margin." - "Please use the Max Min Profit Margin Funds tool to find the fund with the lowest profit margin." """), MessagesPlaceholder(variable_name="chat_history"), ("user", "{input}"), MessagesPlaceholder(variable_name="agent_scratchpad") ]) from langchain.agents.format_scratchpad import format_to_openai_functions from langchain.schema.runnable import RunnablePassthrough from langchain.schema.agent import AgentFinish agent_chain = RunnablePassthrough.assign( agent_scratchpad= lambda x: format_to_openai_functions(x["intermediate_steps"]) ) | prompt | model | OpenAIFunctionsAgentOutputParser() from langchain.memory import ConversationBufferMemory memory = ConversationBufferMemory(return_messages=True,memory_key="chat_history") from langchain.agents import AgentExecutor agent_executor = AgentExecutor(agent=agent_chain, tools=tools, verbose=True, memory=memory) import gradio as gr # 處理函數,提取 AIMessage 的內容 def process_input(user_input): # 使用 agent_executor.invoke 來處理輸入 result = agent_executor.invoke({"input": user_input}) # 從結果中提取 AIMessage 的內容 if 'output' in result: return result['output'] else: return "No output found." # 建立 Gradio 介面 iface = gr.Interface( fn=process_input, # 處理函數 inputs="text", # 使用者輸入類型 outputs="text", # 輸出類型 title="GRESB chatbot", # 介面標題 description="Input your question, i answer." # 介面描述 ) # 啟動介面 iface.launch()