This repository has been archived on 2025-04-28. You can view files and clone it, but cannot push or open issues or pull requests.
gensec-westgate-djw2/hw2/app.py

55 lines
1.9 KiB
Python

from langchain import hub
from langchain.agents import AgentExecutor, create_react_agent, load_tools
from langchain.tools import tool
from langchain_openai import ChatOpenAI
from langchain_community.utilities.dalle_image_generator import DallEAPIWrapper
from langchain_community.tools.google_jobs import GoogleJobsQueryRun
from langchain_community.utilities.google_jobs import GoogleJobsAPIWrapper
from dotenv import load_dotenv
from tools import lookup_ip, lookup_name, search_ksp
# from langsmith import Client
"""
This is the main runner of the custom agent. Custom agent tools are defined seperatly and imported from tools.py
Adapted from https://github.com/wu4f/cs410g-src/blob/main/04_Agents/07_tools_custom_agent.py
Langsmith code can be uncommeted for testing/debugging
"""
load_dotenv()
# os.environ["LANGCHAIN_TRACING_V2"] = "true"
# os.environ["LANGCHAIN_PROJECT"] = f"LangSmith Introduction"
# os.environ["LANGCHAIN_ENDPOINT"] = "https://api.smith.langchain.com"
# client = Client()
llm = ChatOpenAI(model_name="gpt-4-turbo", temperature=0)
tools = load_tools(
["serpapi", "terminal", "dalle-image-generator", "google-jobs"],
allow_dangerous_tools=True,
llm=llm,
)
tools.extend([lookup_name, lookup_ip, search_ksp])
base_prompt = hub.pull("langchain-ai/react-agent-template")
prompt = base_prompt.partial(
instructions="Answer the user's request utilizing at most 8 tool calls"
)
agent = create_react_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
print("Welcome to my application. I am configured with these tools:")
for tool in agent_executor.tools:
print(f" Tool: {tool.name} = {tool.description}")
while True:
line = input("llm>> ")
try:
if line:
result = agent_executor.invoke({"input": line})
print(result)
else:
break
except Exception as e:
print(e)