-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolygon_app.py
91 lines (76 loc) · 2.75 KB
/
polygon_app.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
import os
from langchain import hub
from langchain.agents import create_openai_functions_agent
from langchain_openai.chat_models import ChatOpenAI
from langchain.tools import BaseTool, StructuredTool, Tool, tool
from langchain_community.utilities.polygon import PolygonAPIWrapper
from langchain_community.tools import PolygonLastQuote, PolygonTickerNews, PolygonFinancials, PolygonAggregates
from langchain_core.runnables import RunnablePassthrough
from langchain_core.agents import AgentFinish
from langgraph.graph import END, Graph
from dotenv import load_dotenv
import gradio as gr
load_dotenv(".env")
openAI = os.getenv("OPENAI_API_KEY")
polygon = os.getenv("POLYGON_API_KEY")
langsmith = os.getenv("LANGSMITH_API_KEY")
os.environ["LANGCHAIN_TRACING_V2"] = "true"
os.environ["LANGCHAIN_PROJECT"] = "langgraph-financial-bot"
os.environ["LANGCHAIN_ENDPOINT"] = "https://api.smith.langchain.com"
os.environ["LANGCHAIN_API_KEY"] = str(os.getenv("LANGCHAIN_API_KEY"))
prompt = hub.pull("hwchase17/openai-functions-agent")
llm = ChatOpenAI(model="gpt-4-0125-preview")
polygon = PolygonAPIWrapper()
tools = [
PolygonLastQuote(api_wrapper=polygon),
PolygonTickerNews(api_wrapper=polygon),
PolygonFinancials(api_wrapper=polygon),
PolygonAggregates(api_wrapper=polygon),
]
# DEFINE THE AGENT
agent_runnable = create_openai_functions_agent(llm, tools, prompt)
agent = RunnablePassthrough.assign(
agent_outcome = agent_runnable
)
# DEFINE THE FUNCTIONS TO USE
def execute_tools(data):
agent_action = data.pop('agent_outcome')
tool_to_use = {t.name: t for t in tools}[agent_action.tool]
observation = tool_to_use.invoke(agent_action.tool_input)
data['intermediate_steps'].append((agent_action, observation))
return data
# DEFINE LOGIC THAT WILL BE USED TO DETERMINE WHICH CONDITIONAL EDGE TO USE
def should_continue(data):
if isinstance(data['agent_outcome'], AgentFinish):
return "exit"
else:
return "continue"
workflow = Graph()
workflow.add_node("agent", agent)
workflow.add_node("tools", execute_tools)
workflow.set_entry_point("agent")
workflow.add_conditional_edges(
"agent",
should_continue,
{
"continue": "tools",
"exit": END
}
)
workflow.add_edge('tools', 'agent')
#
# COMPILE GRAPH
chain = workflow.compile()
def financial_agent(input_text):
result = chain.invoke({"input": input_text, "intermediate_steps": []})
output = result['agent_outcome'].return_values["output"]
return output
# CREATE INTERFACE
iface = gr.Interface(
fn=financial_agent,
inputs=gr.Textbox(lines=2, placeholder="Enter your query here..."),
outputs=gr.Markdown(),
title="Financial Agent",
description="Financial Data Explorer: Leveraging Advanced API Tools for Market Insights"
)
iface.launch()