-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathscraper.py
286 lines (210 loc) · 9.23 KB
/
scraper.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import os
from langchain.agents import AgentExecutor, create_openai_tools_agent
from langchain.output_parsers.openai_functions import JsonOutputFunctionsParser
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_openai import ChatOpenAI
from langchain.tools import tool
from langchain_core.messages import HumanMessage, SystemMessage
import streamlit as st
from langchain_core.runnables import Runnable
import operator
from typing import Annotated, Sequence, TypedDict , List
from langchain_core.messages import BaseMessage
from langchain_community.document_loaders import WebBaseLoader
from langgraph.graph import StateGraph, END
def main():
st.title("LangGraph + Function Call + YahooFinance 👾")
# Add a sidebar for model selection
OPENAI_MODEL = st.sidebar.selectbox(
"Select Model",
["gpt-4-turbo-preview", "gpt-3.5-turbo", "gpt-3.5-turbo-instruct"] # Add your model options here
)
api_key = st.sidebar.text_input("Enter your OpenAI API Key", type="password")
if api_key:
os.environ["OPENAI_API_KEY"] = api_key
user_input = st.text_input("Enter your input here:")
# Run the workflow
if st.button("Run Workflow"):
with st.spinner("Running Workflow..."):
def create_agent(llm: ChatOpenAI, tools: list, system_prompt: str):
prompt = ChatPromptTemplate.from_messages(
[
("system", system_prompt),
MessagesPlaceholder(variable_name="messages"),
MessagesPlaceholder(variable_name="agent_scratchpad"),
]
)
agent = create_openai_tools_agent(llm, tools, prompt)
return AgentExecutor(agent=agent, tools=tools)
def create_supervisor(llm: ChatOpenAI, agents: list[str]):
system_prompt = (
"You are the supervisor over the following agents: {agents}."
" You are responsible for assigning tasks to each agent as requested by the user."
" Each agent executes tasks according to their roles and responds with their results and status."
" Please review the information and answer with the name of the agent to which the task should be assigned next."
" Answer 'FINISH' if you are satisfied that you have fulfilled the user's request."
)
options = ["FINISH"] + agents
function_def = {
"name": "supervisor",
"description": "Select the next agent.",
"parameters": {
"type": "object",
"properties": {
"next": {
"anyOf": [
{"enum": options},
],
}
},
"required": ["next"],
},
}
prompt = ChatPromptTemplate.from_messages(
[
("system", system_prompt),
MessagesPlaceholder(variable_name="messages"),
(
"system",
"In light of the above conversation, please select one of the following options for which agent should be act or end next: {options}."
),
]
).partial(options=str(options), agents=", ".join(agents))
return (
prompt
| llm.bind_functions(functions=[function_def], function_call="supervisor")
| JsonOutputFunctionsParser()
)
@tool
def researcher(urls: List[str]) -> str:
"""Use requests and bs4 to scrape the provided web pages for detailed information."""
loader = WebBaseLoader(urls)
docs = loader.load()
return "\n\n".join(
[
f'<Document name="{doc.metadata.get("title", "")}">\n{doc.page_content}\n</Document>'
for doc in docs
]
)
@tool("Market Analyser")
def analyze(content: str) -> str:
"""Market Analyser"""
chat = ChatOpenAI()
messages = [
SystemMessage(
content="You are a market analyst specializing in e-commerce trends, tasked with identifying a winning product to sell on Amazon. "
"Your goal is to leverage market analysis datas and your expertise to pinpoint a product that meets specific criteria for "
"success in the highly competitive online marketplace "
),
HumanMessage(
content=content
),
]
response = chat(messages)
return response.content
@tool("DropShipping_expert")
def expert(content: str) -> str:
"""Execute a trade"""
chat = ChatOpenAI()
messages = [
SystemMessage(
content="Act as an experienced DropShopping assistant.Your task is to identify Wining Product"
"thorough examination of the product range and pricing"
"Provide insights to help decide whether to start selling this product or not"
),
HumanMessage(
content=content
),
]
response = chat(messages)
return response.content
llm = ChatOpenAI(model=OPENAI_MODEL)
def scraper_agent() -> Runnable:
prompt = (
"You are an amazon scraper."
)
return create_agent(llm, [researcher], prompt)
def analyzer_agent() -> Runnable:
prompt = (
"you are analyser data that scrape from amazaon scraper i want you to help to find wining product"
)
return create_agent(llm, [analyze], prompt)
def Expert_agent() -> Runnable:
prompt = (
"You are a Buyer, your job is to help me decide wthere i start selling product or not"
)
return create_agent(llm, [expert], prompt)
RESEARCHER = "RESEARCHER"
ANALYZER = "Analyzer"
EXPERT = "Expet"
SUPERVISOR = "SUPERVISOR"
agents = [RESEARCHER , ANALYZER, EXPERT]
class AgentState(TypedDict):
messages: Annotated[Sequence[BaseMessage], operator.add]
next: str
def scraper_node(state: AgentState) -> dict:
result = scraper_agent().invoke(state)
return {"messages": [HumanMessage(content=result["output"], name=RESEARCHER)]}
def Analyzer_node(state: AgentState) -> dict:
result = analyzer_agent().invoke(state)
return {"messages": [HumanMessage(content=result["output"], name=ANALYZER)]}
def Expert_node(state: AgentState) -> dict:
result = Expert_agent().invoke(state)
return {"messages": [HumanMessage(content=result["output"], name=EXPERT)]}
def supervisor_node(state: AgentState) -> Runnable:
return create_supervisor(llm, agents)
workflow = StateGraph(AgentState)
workflow.add_node(RESEARCHER, scraper_node)
workflow.add_node(ANALYZER,Analyzer_node)
workflow.add_node(EXPERT,Expert_node)
workflow.add_node(SUPERVISOR, supervisor_node)
workflow.add_edge(RESEARCHER, SUPERVISOR)
workflow.add_edge(ANALYZER,SUPERVISOR)
workflow.add_edge(EXPERT,SUPERVISOR)
workflow.add_conditional_edges(
SUPERVISOR,
lambda x: x["next"],
{
RESEARCHER : RESEARCHER,
ANALYZER: ANALYZER,
EXPERT : EXPERT,
"FINISH" : END
}
)
workflow.set_entry_point(SUPERVISOR)
graph = workflow.compile()
for s in graph.stream({"messages": [HumanMessage(content=user_input)]}):
if "__end__" not in s:
st.write(s)
st.write("-----")
workflow = StateGraph(AgentState)
workflow.add_node(RESEARCHER, scraper_node)
workflow.add_node(ANALYZER, Analyzer_node)
workflow.add_node(EXPERT, Expert_node)
workflow.add_node(SUPERVISOR, supervisor_node)
workflow.add_edge(RESEARCHER, SUPERVISOR)
workflow.add_edge(ANALYZER, SUPERVISOR)
workflow.add_edge(EXPERT, SUPERVISOR)
workflow.add_conditional_edges(
SUPERVISOR,
lambda x: x["next"],
{
RESEARCHER : RESEARCHER ,
ANALYZER: ANALYZER,
EXPERT: EXPERT,
"FINISH": END
}
)
workflow.set_entry_point(SUPERVISOR)
graph = workflow.compile()
#what are some of the most popular stocks for 2024 should i invest in or stock that might have the biggest gains in the future
#What are some of the stocks that had the greatest performance recently And are also the most liquid and highly traded ?
# User_input = (
# "sCRAPE THIS game laptop and help me to find wining product"
# )
for s in graph.stream({"messages": [HumanMessage(content=user_input)]}):
if "__end__" not in s:
st.write(s)
st.write("----")
if __name__ == "__main__":
main()