Shipmaster1/Agent_Workout_531
0
1from typing import List, Dict, Any2from langchain.agents import create_openai_functions_agent, AgentExecutor3from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder4from langchain_core.tools import BaseTool5from langchain_openai import ChatOpenAI6 7class ResearchAgent:8 def __init__(self, tools: List[BaseTool], openai_api_key: str):9 self.tools = tools10 self.llm = ChatOpenAI(11 temperature=0,12 model="gpt-4-turbo-preview",13 openai_api_key=openai_api_key14 )15 16 # Define the system prompt17 system_prompt = """You are a specialized research assistant focused on scientific literature analysis.18 Your goal is to help users find, analyze, and understand scientific papers and research findings.19 You have access to tools that can:20 1. Search for relevant papers and research21 2. Analyze PDF documents22 3. Track citations and research impact23 24 Always be thorough in your analysis and provide clear, well-structured responses.25 If you're unsure about something, be honest and ask for clarification."""26 27 # Create the prompt template28 prompt = ChatPromptTemplate.from_messages([29 ("system", system_prompt),30 MessagesPlaceholder(variable_name="chat_history"),31 ("human", "{input}"),32 MessagesPlaceholder(variable_name="agent_scratchpad"),33 ])34 35 # Create the agent36 self.agent = create_openai_functions_agent(37 llm=self.llm,38 prompt=prompt,39 tools=self.tools40 )41 42 # Create the agent executor43 self.agent_executor = AgentExecutor(44 agent=self.agent,45 tools=self.tools,46 verbose=False47 )48 49 def run(self, query: str, chat_history: List[Dict[str, Any]] = None) -> str:50 """Run the agent with the given query and chat history."""51 if chat_history is None:52 chat_history = []53 54 result = self.agent_executor.invoke({55 "input": query,56 "chat_history": chat_history57 })58 59 return result["output"] 