Repository navigation
Expand file tree
/
Copy pathtoolkit.py
More file actions
285 lines (252 loc) · 10.7 KB
/
Copy pathtoolkit.py
File metadata and controls
285 lines (252 loc) · 10.7 KB
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
# %%
from agents import function_tool, set_tracing_disabled
import os
import requests
import time
from unitycatalog.ai.core.databricks import (
DatabricksFunctionClient,
FunctionExecutionResult,
)
from databricks.sdk import WorkspaceClient
from dotenv import load_dotenv
from openai import AsyncOpenAI
from openai import OpenAI
from census import Census
from us import states
from rich import print
import streamlit as st
# %%
# Load environment variables
load_dotenv("/Users/sathish.gangichetty/Documents/openai-agents/apps/.env-local")
# Initialize environment variables
BASE_URL = os.getenv("DATABRICKS_BASE_URL") or ""
# API_KEY = st.context.headers.get('X-Forwarded-Access-Token')
API_KEY = os.getenv("DATABRICKS_TOKEN") or ""
MODEL_NAME = os.getenv("DATABRICKS_MODEL") or ""
# OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") or ""
set_tracing_disabled(True)
# Initialize clients
client = AsyncOpenAI(base_url=BASE_URL, api_key=API_KEY)
w = WorkspaceClient(
host=os.getenv("DATABRICKS_HOST"),
token=os.getenv("DATABRICKS_TOKEN"),
auth_type="pat",
)
dbclient = DatabricksFunctionClient(client=w)
@function_tool
def get_store_performance_info(user_query: str):
"""
For us, we use this to get information about the store location, store performance, returns, BOPIS(buy online pick up in store) etc.
"""
st.write(
"<span style='color:green;'>[🛠️TOOL-CALL]: the <a href='https://adb-984752964297111.11.azuredatabricks.net/genie/rooms/01f023ae84651418a1203b194dff21a9?o=984752964297111' target='_blank'>get_store_performance_info</a> tool was called</span>",
unsafe_allow_html=True,
)
print("INFO: `get_store_performance_info` tool called")
databricks_instance = os.getenv("DATABRICKS_HOST")
space_id = os.getenv("GENIE_SPACE_ID")
access_token = os.getenv("DATABRICKS_TOKEN")
headers = {
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/json",
}
poll_interval = 2.0
timeout = 60.0
# Step 1: Start a new conversation
start_url = (
f"{databricks_instance}/api/2.0/genie/spaces/{space_id}/start-conversation"
)
payload = {"content": user_query}
resp = requests.post(start_url, headers=headers, json=payload)
resp.raise_for_status()
data = resp.json()
print(data)
conversation_id = data["conversation_id"]
message_id = data["message_id"]
# Step 2: Poll for completion
poll_url = f"{databricks_instance}/api/2.0/genie/spaces/{space_id}/conversations/{conversation_id}/messages/{message_id}"
start_time = time.time()
while True:
poll_resp = requests.get(poll_url, headers=headers)
poll_resp.raise_for_status()
poll_data = poll_resp.json()
status = poll_data.get("status")
if status == "COMPLETED":
attachment_id = poll_data["attachments"][0]["attachment_id"]
url = f"{databricks_instance}/api/2.0/genie/spaces/{space_id}/conversations/{conversation_id}/messages/{message_id}/attachments/{attachment_id}/query-result"
response = requests.get(url, headers=headers)
response.raise_for_status()
return response.json()["statement_response"]
if time.time() - start_time > timeout:
raise TimeoutError("Genie API query timed out.")
time.sleep(poll_interval)
@function_tool
def get_product_inventory_info(user_query: str):
"""
For us, we use this to get information about products and the current inventory snapshot across stores
"""
st.write(
"<span style='color:green;'>[🛠️TOOL-CALL]: the <a href='https://adb-984752964297111.11.azuredatabricks.net/genie/rooms/01f02c2c29211c388b9b5b9b6f5a80c9?o=984752964297111' target='_blank'>get_product_inventory_info</a> tool was called</span>",
unsafe_allow_html=True,
)
print("INFO: `get_product_inventory_info` tool called")
databricks_instance = os.getenv("DATABRICKS_HOST")
space_id = os.getenv("GENIE_SPACE_PRODUCT_INV_ID")
access_token = os.getenv("DATABRICKS_TOKEN")
headers = {
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/json",
}
poll_interval = 2.0
timeout = 60.0
# Step 1: Start a new conversation
start_url = (
f"{databricks_instance}/api/2.0/genie/spaces/{space_id}/start-conversation"
)
payload = {"content": user_query}
resp = requests.post(start_url, headers=headers, json=payload)
resp.raise_for_status()
data = resp.json()
print(data)
conversation_id = data["conversation_id"]
message_id = data["message_id"]
# Step 2: Poll for completion
poll_url = f"{databricks_instance}/api/2.0/genie/spaces/{space_id}/conversations/{conversation_id}/messages/{message_id}"
start_time = time.time()
while True:
poll_resp = requests.get(poll_url, headers=headers)
poll_resp.raise_for_status()
poll_data = poll_resp.json()
status = poll_data.get("status")
if status == "COMPLETED":
attachment_id = poll_data["attachments"][0]["attachment_id"]
url = f"{databricks_instance}/api/2.0/genie/spaces/{space_id}/conversations/{conversation_id}/messages/{message_id}/attachments/{attachment_id}/query-result"
response = requests.get(url, headers=headers)
response.raise_for_status()
return response.json()["statement_response"]
if time.time() - start_time > timeout:
raise TimeoutError("Genie API query timed out.")
time.sleep(poll_interval)
@function_tool
def get_business_conduct_policy_info(search_query: str) -> FunctionExecutionResult:
st.write(
"<span style='color:green;'>[🛠️TOOL-CALL]: the <a href='https://adb-984752964297111.11.azuredatabricks.net/explore/data/main/sgfs/retail_conduct_policy?o=984752964297111&activeTab=overview' target='_blank'>get_business_conduct_policy_info</a> tool was called</span>",
unsafe_allow_html=True,
)
print("INFO: `get_business_conduct_policy_info` tool called")
return dbclient.execute_function(
function_name="main.sgfs.retail_club_conduct",
parameters={"search_query": search_query},
)
@function_tool
def do_research_and_reason(user_query: str):
"""
Get a response from sythesized intelligence from the web including the "thinking" process highligted in the <think></think> tags
Args:
user_query: The user's question or request
Returns:
The sythesized intelligence from the web including the "thinking"
process inside <think></think> tags
"""
print("INFO: `do_research_and_reason` tool called")
messages = [
{
"role": "system",
"content": (
"You are an artificial intelligence assistant and you need to "
"engage in a helpful, detailed, polite conversation with a user."
),
},
{
"role": "user",
"content": f"today is {time.strftime('%d %B %Y')} + " + user_query,
},
]
client = OpenAI(
api_key=os.getenv("PERPLEXITY_API_KEY"), base_url="https://api.perplexity.ai"
)
# chat completion without streaming
# response = client.chat.completions.create(
# model="sonar-pro",
# messages=messages,
# )
# return response.choices[0].message.content
# chat completion with streaming
response_stream = client.chat.completions.create(
model="sonar-reasoning-pro", #o3 comp
messages=messages,
stream=True,
)
full_response = ""
for response in response_stream:
if response.choices and response.choices[0].delta.content:
content = response.choices[0].delta.content
full_response += content
return full_response
# from langchain_community.tools import BraveSearch
# tool = BraveSearch.from_api_key(
# api_key=os.getenv("BRAVE_API_KEY"), search_kwargs={"count": 5}
# )
# print(tool.invoke("prices of blue tack"))
# %%
# https://downloads.esri.com/esri_content_doc/dbl/us/Var_List_ACS_Winter_2021.pdf
@function_tool
def get_state_census_data(state_code: str) -> str:
"""
Get census data for a specific state.
Args:
state_code: The two-letter state code (e.g., 'MD' for Maryland)
Returns:
A formatted string with the state name, population, and median household income
"""
print("INFO: `get_state_census_data` tool called")
c = Census(os.getenv("CENSUS_API_KEY"))
state_obj = getattr(states, state_code.upper())
results = c.acs5.get(
(
"NAME",
"B11016_001E", # total households
"B25081_001E", # owner occupied households
"B01003_001E", # total population
"B19013_001E", # median household income
"B15003_022E", # bachelors degree graduates
"B15003_023E", # masters degree graduates
"B15003_025E", # doctorate degree graduates
),
{"for": "state:{}".format(state_obj.fips)},
)
if results:
state_name = results[0]["NAME"]
total_households = int(results[0]["B11016_001E"] or 0)
owner_occupied_households = int(results[0]["B25081_001E"] or 0)
population = int(results[0]["B01003_001E"] or 0)
median_income = int(results[0]["B19013_001E"] or 0)
bachelors_degree_graduates = int(results[0]["B15003_022E"] or 0)
masters_degree_graduates = int(results[0]["B15003_023E"] or 0)
doctorate_degree_graduates = int(results[0]["B15003_025E"] or 0)
# Calculate percentages and ratios
owner_occupied_percent = (
(owner_occupied_households / total_households * 100)
if total_households
else 0
)
people_per_household = (
(population / total_households) if total_households else 0
)
bachelors_percent = (
(bachelors_degree_graduates / population * 100) if population else 0
)
masters_percent = (
(masters_degree_graduates / population * 100) if population else 0
)
doctorate_percent = (
(doctorate_degree_graduates / population * 100) if population else 0
)
return (
f"{state_name} has an estimated population of {round(population)} and a median household income of ${round(median_income)}. "
f"{round(owner_occupied_percent, 1)}% of households are owner-occupied with an average of {round(people_per_household, 2)} people per household. "
f"Education levels: {round(bachelors_percent, 1)}% have a bachelor's degree, {round(masters_percent, 1)}% have a master's degree, "
f"and {round(doctorate_percent, 1)}% have a doctorate degree."
)
else:
return f"No census data found for state code {state_code}."