lakshmivairamani commited on
Commit
27d0e0a
1 Parent(s): 0bb9c80

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -36
app.py CHANGED
@@ -20,12 +20,8 @@ from PyPDF2 import PdfReader
20
 
21
  memory = ConversationBufferMemory(return_messages=True, memory_key="chat_history")
22
 
23
-
24
-
25
  open_api_key_token = os.environ['OPEN_AI_API']
26
 
27
- open_weather_api_key = os.environ['OPEN_WEATHER_API']
28
-
29
  os.environ['OPENAI_API_KEY'] = open_api_key_token
30
  db_uri = 'mysql+mysqlconnector://redmindgen:51([email protected]:3306/collegedb'
31
  #db_uri = 'postgresql+psycopg2://postgres:[email protected]:5432/warehouse'
@@ -35,12 +31,10 @@ db = SQLDatabase.from_uri(db_uri)
35
 
36
  # LLM setup
37
  llm = ChatOpenAI(model="gpt-3.5-turbo-0125")
38
- #llm = OpenAI(temperature=0, api_key= os.environ['OPEN_AI_API'], model_name='gpt-3.5-turbo')
39
 
40
  # Define the SQL query generation tool
41
  template_query_generation = """Based on the table schema below, write a SQL query that would answer the user's question:
42
  {schema}
43
-
44
  Question: {question}
45
  SQL Query:"""
46
  prompt_query_generation = ChatPromptTemplate.from_template(template_query_generation)
@@ -66,26 +60,60 @@ def database_tool(question):
66
  sql_query = generate_sql_query(question)
67
  return run_query(sql_query)
68
 
69
- # Define the weather data retrieval tool
70
- def get_weather_data(location="United Arab Emirates"):
71
- api_key = open_weather_api_key
72
- base_url = "http://api.openweathermap.org/data/2.5/weather?"
73
-
74
- if location is None or location.strip() == "":
75
- location = "United Arab Emirates"
76
-
77
- complete_url = f"{base_url}q={location}&appid={api_key}&units=metric"
78
-
79
- response = requests.get(complete_url)
80
- data = response.json()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
 
82
- if data["cod"] != "404":
83
- main = data["main"]
84
- weather_description = data["weather"][0]["description"]
85
- temperature = main["temp"]
86
- return f"The current temperature in {location} is {temperature}°C with {weather_description}."
87
- else:
88
- return "Weather data is not found."
89
  #get_weather_data("United Arab Emirates")
90
 
91
  # Define the document data tool
@@ -109,11 +137,7 @@ def query_vector_store(vector_store, query):
109
  return '\n\n'.join([doc.page_content for doc in docs])
110
 
111
  # Load and process the PDF (ensure the PDF is accessible from your Colab environment)
112
- #pdf_path = "The Magic of Analysing Customers Experience in Freight Forwarding Industry -BLOG.pdf"
113
  pdf_path = "Inbound.pdf"
114
- # Check if the user has the necessary permissions to access the directory
115
- # if not os.path.isdir(pdf_path):
116
- # raise ValueError(f"Directory {pdf_path} does not exist or you do not have permission to access it.")
117
 
118
  texts = load_and_split_pdf(pdf_path)
119
  vector_store = create_vector_store(texts)
@@ -124,29 +148,29 @@ def document_data_tool(query):
124
  # Initialize the agent with the tools
125
  tools = [
126
 
127
- Tool(name="WeatherData", func=get_weather_data, description="Tool to get current weather data for a city or country. Handles questions related to current weather conditions in specific cities or countries.", tool_choice="required"),
128
  Tool(name="DocumentData", func=document_data_tool, description="Tool to search and retrieve information from the uploaded document. Provide responses with the maximum of 150 words.", tool_choice="required"),
129
  #Tool(name="DatabaseQuery", func=database_tool, description="Tool to query the database based on the user's question. Only handles questions related to the collegedb schema, including tables such as buildings, classrooms, college, course, faculty, interns, person, section, student, and textbook. Ensure to use only the available fields in these tables.Provide responses with the maximum of 150 words.", tool_choice="required"),
130
  ]
131
 
132
  prompt_template = f"""You are an assistant that helps with database queries, weather information, and document retrieval.
133
 
134
- For weather-related questions, if the user specifies a city, provide the weather information for that city. If the user specifies a country or does not specify a city, provide the weather information for the specified country or the default location of 'United Arab Emirates'.
135
  For document-related questions, search and retrieve information from the uploaded document.
136
 
137
  {{agent_scratchpad}}
138
  Question: {{input}}
139
-
140
  """
141
- #{memory.buffer}
142
  #For SQL database-related questions, only use the fields available in the collegedb schema, which includes tables such as buildings, classrooms, college, course, faculty, interns, person, section, student, and textbook.
143
  prompt = ChatPromptTemplate.from_template(prompt_template)
144
 
145
  # Initialize the agent with memory
146
  llm_with_memory = llm.bind(memory=memory)
147
- #llm_with_memory = llm.bind()
148
  agent = create_tool_calling_agent(llm_with_memory, tools, prompt)
149
- #agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
150
  agent_executor = AgentExecutor(agent=agent, tools=tools, memory= memory, verbose=True)
151
 
152
  # Define the interface function
@@ -154,6 +178,17 @@ max_iterations = 5
154
  iterations = 0
155
 
156
  def answer_question(user_question, city="United Arab Emirates"):
 
 
 
 
 
 
 
 
 
 
 
157
  global iterations
158
  iterations = 0
159
 
@@ -175,7 +210,6 @@ def answer_question(user_question, city="United Arab Emirates"):
175
  # Print memory buffer for debugging
176
  print("Memory Buffer11:", memory.load_memory_variables({}))
177
 
178
-
179
  # Format the response text
180
  response_text = response_text.replace('\n', ' ').replace(' ', ' ').strip()
181
 
 
20
 
21
  memory = ConversationBufferMemory(return_messages=True, memory_key="chat_history")
22
 
 
 
23
  open_api_key_token = os.environ['OPEN_AI_API']
24
 
 
 
25
  os.environ['OPENAI_API_KEY'] = open_api_key_token
26
  db_uri = 'mysql+mysqlconnector://redmindgen:51([email protected]:3306/collegedb'
27
  #db_uri = 'postgresql+psycopg2://postgres:[email protected]:5432/warehouse'
 
31
 
32
  # LLM setup
33
  llm = ChatOpenAI(model="gpt-3.5-turbo-0125")
 
34
 
35
  # Define the SQL query generation tool
36
  template_query_generation = """Based on the table schema below, write a SQL query that would answer the user's question:
37
  {schema}
 
38
  Question: {question}
39
  SQL Query:"""
40
  prompt_query_generation = ChatPromptTemplate.from_template(template_query_generation)
 
60
  sql_query = generate_sql_query(question)
61
  return run_query(sql_query)
62
 
63
+ # Define the ASN API data retrieval tool
64
+ def get_ASN_data(asn_id):
65
+
66
+ base_url = "http://193.203.162.39:9090/nxt-wms/trnHeader?"
67
+
68
+ if asn_id is None or asn_id.strip() == "":
69
+ asn_id = "ASN24070100015"
70
+
71
+ complete_url = f"{base_url}branchMaster.id=343&transactionUid={asn_id}&userId=164&transactionType=ASN"
72
+ try:
73
+ response = requests.get(complete_url)
74
+ data = response.json()
75
+ response.raise_for_status() # Raises an HTTPError if the response was an error
76
+
77
+ if data["httpStatus"] != "200":
78
+ # Assuming the first content item and first party item are what we're interested in
79
+ content = data['result']['content'][0]
80
+ trnHeaderAsn = content['trnHeaderAsn']
81
+ party = content['party'][0]
82
+
83
+ # Extracting the required information
84
+ transactionUid = trnHeaderAsn['transactionUid']
85
+ customerOrderNo = trnHeaderAsn.get('customerOrderNo', 'N/A') # Using .get() for potentially missing keys
86
+ orderDate = trnHeaderAsn.get('orderDate', 'N/A')
87
+ customerInvoiceNo = trnHeaderAsn.get('customerInvoiceNo', 'N/A')
88
+ invoiceDate = trnHeaderAsn.get('invoiceDate', 'N/A')
89
+ expectedReceivingDate = trnHeaderAsn['expectedReceivingDate']
90
+ transactionStatus = trnHeaderAsn['transactionStatus']
91
+ shipper_code = party['shipper']['code'] if party['shipper'] else 'N/A'
92
+ shipper_name = party['shipper']['name'] if party['shipper'] else 'N/A'
93
+
94
+ # Assuming the variables are already defined as per previous context
95
+ data = [
96
+ ["Transaction UID", transactionUid],
97
+ ["Customer Order No", customerOrderNo],
98
+ ["Order Date", orderDate],
99
+ ["Customer Invoice No", customerInvoiceNo],
100
+ ["Invoice Date", invoiceDate],
101
+ ["Expected Receiving Date", expectedReceivingDate],
102
+ ["Transaction Status", transactionStatus],
103
+ ["Shipper Code", shipper_code],
104
+ ["Shipper Name", shipper_name]
105
+ ]
106
+ return f"The ASN details of {asn_id} is {data}."
107
+ else:
108
+ return "ASN Details are not found. Please contact system administrator."
109
+
110
+ except requests.exceptions.HTTPError as http_err:
111
+ print(f"HTTP error occurred: {http_err}")
112
+ except Exception as err:
113
+ print(f"An error occurred: {err}")
114
+
115
 
116
+
 
 
 
 
 
 
117
  #get_weather_data("United Arab Emirates")
118
 
119
  # Define the document data tool
 
137
  return '\n\n'.join([doc.page_content for doc in docs])
138
 
139
  # Load and process the PDF (ensure the PDF is accessible from your Colab environment)
 
140
  pdf_path = "Inbound.pdf"
 
 
 
141
 
142
  texts = load_and_split_pdf(pdf_path)
143
  vector_store = create_vector_store(texts)
 
148
  # Initialize the agent with the tools
149
  tools = [
150
 
151
+ Tool(name="ASNData", func=get_ASN_data, description="Tool to get the status of ASN with ASN id given as input. Handles questions related to ASN id which starts with ASN followed by 11 numeric digits. For example, ASN24070100015 ", tool_choice="required"),
152
  Tool(name="DocumentData", func=document_data_tool, description="Tool to search and retrieve information from the uploaded document. Provide responses with the maximum of 150 words.", tool_choice="required"),
153
  #Tool(name="DatabaseQuery", func=database_tool, description="Tool to query the database based on the user's question. Only handles questions related to the collegedb schema, including tables such as buildings, classrooms, college, course, faculty, interns, person, section, student, and textbook. Ensure to use only the available fields in these tables.Provide responses with the maximum of 150 words.", tool_choice="required"),
154
  ]
155
 
156
  prompt_template = f"""You are an assistant that helps with database queries, weather information, and document retrieval.
157
 
158
+ For ASN-related questions, if the user specifies ASN id. Provide the information like ASN status, expected Receiving Date etc.
159
  For document-related questions, search and retrieve information from the uploaded document.
160
 
161
  {{agent_scratchpad}}
162
  Question: {{input}}
163
+ {{memory.buffer}}
164
  """
165
+
166
  #For SQL database-related questions, only use the fields available in the collegedb schema, which includes tables such as buildings, classrooms, college, course, faculty, interns, person, section, student, and textbook.
167
  prompt = ChatPromptTemplate.from_template(prompt_template)
168
 
169
  # Initialize the agent with memory
170
  llm_with_memory = llm.bind(memory=memory)
171
+
172
  agent = create_tool_calling_agent(llm_with_memory, tools, prompt)
173
+
174
  agent_executor = AgentExecutor(agent=agent, tools=tools, memory= memory, verbose=True)
175
 
176
  # Define the interface function
 
178
  iterations = 0
179
 
180
  def answer_question(user_question, city="United Arab Emirates"):
181
+ """
182
+ This function takes a user question as input and generates a response using an agent executor.
183
+ It iterates until a valid response is generated or the maximum number of iterations is reached.
184
+
185
+ Parameters:
186
+ - user_question (str): The question asked by the user.
187
+ - city (str): The city to use for the agent executor. Default is "United Arab Emirates".
188
+
189
+ Returns:
190
+ - str: The generated response or an error message if no valid response is generated within the iteration limit.
191
+ """
192
  global iterations
193
  iterations = 0
194
 
 
210
  # Print memory buffer for debugging
211
  print("Memory Buffer11:", memory.load_memory_variables({}))
212
 
 
213
  # Format the response text
214
  response_text = response_text.replace('\n', ' ').replace(' ', ' ').strip()
215