-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathagent_example.py
57 lines (47 loc) · 1.62 KB
/
agent_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
Remember to install the additional dependencies for this example to work:
pip install langchain-openai langchain
"""
from dotenv import load_dotenv
from langchain.agents import AgentExecutor, create_openai_functions_agent
from langchain_core.messages import SystemMessage
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_openai import ChatOpenAI
from langchain_scrapegraph.tools import (
GetCreditsTool,
SearchScraperTool,
SmartScraperTool,
)
load_dotenv()
# Initialize the tools
tools = [
SmartScraperTool(),
GetCreditsTool(),
SearchScraperTool(),
]
# Create the prompt template
prompt = ChatPromptTemplate.from_messages(
[
SystemMessage(
content=(
"You are a helpful AI assistant that can analyze websites and extract information. "
"You have access to tools that can help you scrape and process web content. "
"Always explain what you're doing before using a tool."
)
),
MessagesPlaceholder(variable_name="chat_history", optional=True),
("user", "{input}"),
MessagesPlaceholder(variable_name="agent_scratchpad"),
]
)
# Initialize the LLM
llm = ChatOpenAI(temperature=0)
# Create the agent
agent = create_openai_functions_agent(llm, tools, prompt)
# Create the executor
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
# Example usage
query = """Extract the main products from https://www.scrapegraphai.com/"""
print("\nQuery:", query, "\n")
response = agent_executor.invoke({"input": query})
print("\nFinal Response:", response["output"])