Spaces:
Build error
Build error
Upload 3 files
Browse filesFiles changed (3)
- Inbound.pdfAdded
- app.pyAdded+460
- requirements.txtAdded+15
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460# -*- coding: utf-8 -*-"""Chat_with_agent_v4.ipynbAutomatically generated by Colab.Original file is located at https://colab.research.google.com/drive/1T5Buj_yHaAnfoO__2-gCFDSvBVheiHrF"""
from PIL import Imageimport base64from io import BytesIOimport osimport requestsimport gradio as gr#import nltkfrom langchain_core.prompts import ChatPromptTemplatefrom langchain_core.output_parsers import StrOutputParserfrom langchain_core.runnables import RunnableSequence, RunnableLambdafrom langchain_openai import ChatOpenAIfrom langchain_openai import OpenAIEmbeddingsfrom langchain_community.vectorstores import FAISSfrom langchain_community.utilities import SQLDatabasefrom langchain.agents import create_tool_calling_agent, AgentExecutor, Toolfrom langchain.text_splitter import RecursiveCharacterTextSplitterfrom langchain.tools import StructuredToolfrom langchain.pydantic_v1 import BaseModel, Fieldfrom PyPDF2 import PdfReaderfrom nltk.tokenize import sent_tokenizefrom sqlalchemy import create_enginefrom sqlalchemy.sql import textimport jsonimport nltknltk.download('punkt')
open_api_key_token = os.environ['OPEN_AI_API']
os.environ['OPENAI_API_KEY'] = open_api_key_tokendb_uri = 'postgresql+psycopg2://postgres:postpass@193.203.162.39:5432/warehouseAi'# Database setup
db = SQLDatabase.from_uri(db_uri)
# LLM setup#llm = ChatOpenAI(model="gpt-3.5-turbo-0125",max_tokens=150,temperature=0.1)llm = ChatOpenAI(model="gpt-4o-mini",max_tokens=250,temperature=0.1)
def get_schema(_): schema_info = db.get_table_info() # This should be a string of your SQL schema return schema_info
def generate_sql_query(question): schema = get_schema(None) template_query_generation = """ Schema: {schema} Question: {question} Provide a SQL query to answer the above question using the exact field names and table names specified in the schema. SQL Query (Please provide only the SQL statement without explanations or formatting): """ prompt_query_generation = ChatPromptTemplate.from_template(template_query_generation) schema_and_question = RunnableLambda(lambda _: {'schema': schema, 'question': question}) sql_chain = RunnableSequence( schema_and_question, prompt_query_generation, llm.bind(stop=["SQL Query End"]), # Adjust the stop sequence to your need StrOutputParser() ) sql_query = sql_chain.invoke({}) return sql_query.strip()
def run_query(query): # Clean the query by removing markdown symbols and trimming whitespace clean_query = query.replace("```sql", "").replace("```", "").strip() #print(f"Executing SQL Query: {clean_query}") try: result = db.run(clean_query) return result except Exception as e: print(f"Error executing query: {e}") return None
# Define the database query tool# The function that uses the above models# Define the function that will handle the database querydef database_tool(question): # print(question) sql_query = generate_sql_query(question) ##print(sql_query) return run_query(sql_query)
def get_ASN_data(question): #print(question) base_url = "http://193.203.162.39:9090/nxt-wms/trnHeader?" complete_url = f"{base_url}branchMaster.id=343&transactionUid={question}&userId=164&transactionType=ASN" #print("complete url") #print(complete_url) try: response = requests.get(complete_url) data = response.json() response.raise_for_status()
if 'result' in data and 'content' in data['result'] and data['result']['content']: content = data['result']['content'][0] trnHeaderAsn = content['trnHeaderAsn'] party = content['party'][0]
transactionUid = trnHeaderAsn['transactionUid'] customerOrderNo = trnHeaderAsn.get('customerOrderNo', 'N/A') orderDate = trnHeaderAsn.get('orderDate', 'N/A') customerInvoiceNo = trnHeaderAsn.get('customerInvoiceNo', 'N/A') invoiceDate = trnHeaderAsn.get('invoiceDate', 'N/A') expectedReceivingDate = trnHeaderAsn['expectedReceivingDate'] transactionStatus = trnHeaderAsn['transactionStatus'] shipper_code = party['shipper']['code'] if party['shipper'] else 'N/A' shipper_name = party['shipper']['name'] if party['shipper'] else 'N/A'
data = [ ["Transaction UID", transactionUid], ["Customer Order No", customerOrderNo], ["Order Date", orderDate], ["Customer Invoice No", customerInvoiceNo], ["Invoice Date", invoiceDate], ["Expected Receiving Date", expectedReceivingDate], ["Transaction Status", transactionStatus], ["Shipper Code", shipper_code], ["Shipper Name", shipper_name] ] return f"The ASN details of {question} is {data}." else: return "ASN Details are not found. Please contact system administrator."
except requests.exceptions.HTTPError as http_err: print(f"HTTP error occurred: {http_err}") except Exception as err: print(f"An error occurred: {err}")
get_ASN_data("ASN24072400001")
def load_and_split_pdf(pdf_path): reader = PdfReader(pdf_path) text = '' for page in reader.pages: text += page.extract_text() text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=50) texts = text_splitter.split_text(text) return textsdef create_vector_store(texts): embeddings = OpenAIEmbeddings() vector_store = FAISS.from_texts(texts, embeddings) return vector_store
def query_vector_store(vector_store, query): docs = vector_store.similarity_search(query, k=5) #print(f"Vector store return: {docs}") return docs
def summarize_document(docs): summarized_docs = [] for doc in docs: if isinstance(doc, list): doc_content = ' '.join([d.page_content for d in doc]) else: doc_content = doc.page_content
sentences = sent_tokenize(doc_content) if len(sentences) > 5: summarized_content = ' '.join(sentences[:5]) else: summarized_content = doc_content summarized_docs.append(summarized_content) return '\n\n'.join(summarized_docs)pdf_path = "Inbound.pdf"#pdf_path = r"D:\rajesh\python\chat_agent\Inbound.pdf"texts = load_and_split_pdf(pdf_path)vector_store = create_vector_store(texts)
def document_data_tool(question): #print(f"Document data tool enter: {question}") # query_string = question['tags'][0] if 'tags' in question and question['tags'] else "" query_response = query_vector_store(vector_store, question) print("query****") print(query_response) #summarized_response = summarize_document(query_response) #print("summary***") #print(summarized_response) return query_response
def make_api_request(url, params): import requests """Generic function to make API GET requests and return JSON data.""" try: response = requests.get(url, params=params) response.raise_for_status() # Raises an HTTPError if the response was an error return response.json() # Return the parsed JSON data except requests.exceptions.HTTPError as http_err: print(f"HTTP error occurred: {http_err}") except Exception as err: print(f"An error occurred: {err}")
name=""warehouse_id = ""apis = [ #fetch warehouse ID { "url": "http://193.203.162.39:9090/nxt-wms/userWarehouse/fetchWarehouseForUserId?", "params": {"query": name, "userId": "164"} }, #fetch customer id { "url": "http://193.203.162.39:9090/nxt-wms/userCustomer/fetchCustomerForUserId?", "params": {"query": "TESTING 123", "userId": "164", "status": "Active"} }, #Stock summary based on warehouse id { "url": "http://193.203.162.39:9090/nxt-wms/transactionHistory/stockSummary?", "params": {"branchId": "343", "onDate": "2024-08-06", "warehouseId" : warehouse_id } }]
def inventory_report(question):
name = question.split(":")[0] #print(question) question = question.split(":")[1] #print(name) import requests
data = make_api_request(apis[0]["url"], apis[0]["params"]) if data: #print(data) # Extracting the id for the warehouse with the name "WH" warehouse_id = next((item['id'] for item in data['result'] if item['name'] == name), None)
#print(f"The id for the warehouse named {name} is: {warehouse_id}") #Step 3: Update the placeholder with the actual warehouse_id for api in apis: if "warehouseId" in api["params"]: api["params"]["warehouseId"] = warehouse_id
#print(apis[2]["url"]) #print(apis[2]["params"]) data1 = make_api_request(apis[2]["url"], apis[2]["params"]) #if data1: #print(data1)
from tabulate import tabulate
headers = ["S.No","Warehouse Code", "Warehouse Name", "Customer Code", "Customer Name", "Item Code", "Item Name", "Currency", "EAN", "UOM", "Quantity", "Gross Weight", "Volume", "Total Value"] table_data = []
for index, item in enumerate(data1['result'], start=1): row = [ index, # Serial number item['warehouse']['code'], item['warehouse']['name'], item['customer']['code'], item['customer']['name'], item['skuMaster']['code'], item['skuMaster']['name'], item['currency']['code'], item['eanUpc'], item['uom']['code'], item['totalQty'], item['grossWeight'], item['volume'], item['totalValue'] ] table_data.append(row)
#if table_data: #print(tabulate(table_data, headers=headers, tablefmt="grid"))
# Convert to pandas DataFrame import pandas as pd df = pd.DataFrame(table_data, columns=headers) import pandas as pd from pandasai.llm.openai import OpenAI from pandasai import SmartDataframe #open api key import openai llm = OpenAI() sdf = SmartDataframe(df, config={"llm": llm}) #chart = sdf.chat("Can you draw a bar chart with all avaialble item name and quantity.") chart = sdf.chat(question) return chart#inventory_report("WH:can you give me a bar chart with item name and quantity for the warehouse WH")
# Define input and output models using Pydanticclass QueryInput(BaseModel): question: str = Field(description="The question to be answered by appropriate tool. Please follow the instructions. For API tool, do not send the question as it is. Please send the ASN id. Invoke datavisulaization tool by processing the user question and send two inputs to the tool. One input will be the warehouse name and another input to the tool will be the entire user_question itself. Please join those two strings and send them as a single input string with ':' as delimiter") # config: dict = Field(default={}, description="Optional configuration for the database query.")
# Define the output model for database queriesclass QueryOutput(BaseModel): result: str = Field(..., description="Display the answer based on the prompts given in each tool. For dataVisualization tool, it sends a image file as output. Please give the image file path only to the gr.Image. For DocumentData tool, Please provide a complete and concise response within 200 words and Ensure that the response is not truncated and covers the essential points.")
# Wrap the function with StructuredTool for better parameter handlingtools = [ StructuredTool( func=get_ASN_data, name="APIData", args_schema=QueryInput, output_schema=QueryOutput, description="Tool to get details of ASN api. ASN id will be in the input with the format of first three letters as ASN and it is followed by 11 digit numeral. Pass only the id as input. Do not send the complete user question to the tool. If there are any other queries related to ASN without ASN id, please use the document tool." ), StructuredTool( func=document_data_tool, name="DocumentData", args_schema=QueryInput, output_schema=QueryOutput, description="You are an AI assistant trained to help with warehouse management questions based on a detailed document about our WMS. The document covers various processes such as ASN handling, purchase orders, cross docking, appointment scheduling for shipments, and yard management. Please provide a complete and concise response within 200 words and Ensure that the response is not truncated and covers the essential points. " ), StructuredTool( func=database_tool, name="DatabaseQuery", args_schema=QueryInput, output_schema=QueryOutput, description="Tool to query the database based on structured input." ), StructuredTool( func=inventory_report, name="dataVisualization", args_schema=QueryInput, output_schema=QueryOutput, description="Tool to generate visual output for a particular warehouse. Invoke this tool if the user wants to create charts. Process the user question and send two inputs to the tool. One input will be the warehouse name and another input to the tool will be the entire user_question itself. " )]
prompt_template = f"""You are an assistant that helps with database queries, API information, and document retrieval. Your job is to provide clear, complete, and detailed responses to the following queries. Please give the output response in an user friendly way and remove "**" from the response. For example, document related queries can be answered in a clear and concise way with numbering and not as a paragraph. Database related queries should be answered with proper indentation and use numbering for the rows. ASN id related queries should be answered with proper indentation and use numbering for the rows.For ASN id related questions, if the user specifies an ASN id, provide the information from the api tool. Pass only the id as input to the tool. Do not pass the entire question as input to the tool. If the details are not found, say it in a clear and concise way.You are an AI assistant trained to help with warehouse management questions based on a detailed document about our WMS. The document covers various processes such as ASN handling, purchase orders, cross docking, appointment scheduling for shipments, and yard management. Please provide a complete and concise response within 200 words and Ensure that the response is not truncated and covers the essential points. When answering, focus on providing actionable insights and clear explanations related to the specific query. Please remove "**" from the response.For SQL database-related questions, only use the fields available in the warehouse schema, including tables such as customer_master, efs_company_master, efs_group_company_master, efs_region_master, party_address_detail, wms_warehouse_master.For datavisualization, user will ask for inventory report of a particular warehouse. Your job is to return the image path to chat interface and display the image as output.{{agent_scratchpad}}Here is the information you need to process:Question: {{input}}"""
llm = llm.bind()agent = create_tool_calling_agent(llm, tools, ChatPromptTemplate.from_template(prompt_template))agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
# Define the interface functionmax_iterations = 5iterations = 0
def answer_question(user_question,chatbot): global iterations iterations = 0
while iterations < max_iterations: #print(user_question) response = agent_executor.invoke({"input": user_question}) #print(response) if isinstance(response, dict): response_text = response.get("output", "") else: response_text = response if "invalid" not in response_text.lower(): break iterations += 1
if iterations == max_iterations: return "The agent could not generate a valid response within the iteration limit." if "chart" in user_question: # Open the image file img = Image.open('/home/user/app/exports/charts/temp_chart.png') # Convert the PIL Image to a base64 encoded string buffered = BytesIO() img.save(buffered, format="PNG") img_str = base64.b64encode(buffered.getvalue()).decode("utf-8") #print(img_str) img = f'<img src="data:image/png;base64,{img_str}">' #image = gr.Image(value=img_str) chatbot.append((user_question,img)) #print(chatbot) return gr.update(value=chatbot)
#return [(user_question,gr.Image("/home/user/app/exports/charts/temp_chart.png"))] # return "/home/user/app/exports/charts/temp_chart.png" else: chatbot.append((user_question, response_text)) #print(chatbot) return gr.update(value=chatbot) #response_text = response_text.replace('\n', ' ').replace(' ', ' ').strip() #return response_text
import gradio as gr
css = """
.gr-chatbot { /* Custom styles for the Chatbot component */ border: 1px solid #ccc; border-radius: 10px; padding: 10px; background-color: #f9f9f9; height: 300px; /* Adjust the height as needed */ /* Adjust the width as needed */ overflow-y: auto; /* Add scroll if the content exceeds the height */}
.gr-button { height: 40px; /* Adjust the height as needed */
"""
def submit_feedback(feedback, chatbot): feedback_response = "User feedback: " + feedback return chatbot + [(feedback_response,None)], gr.update(visible=False), gr.update(visible=False)
def handle_dislike(data: gr.LikeData): if not data.liked: print("downvote") return gr.update(visible=True), gr.update(visible=True) else: print("upvote") return gr.update(visible=False), gr.update(visible=False)
with gr.Blocks(css=css) as demo: gr.Markdown("<CENTER><h2 style='font-size: 20px; font-family: Calibri;'>NewageNXT GPT</h2></CENTER>") chatbot = gr.Chatbot(elem_classes="gr-chatbot", label="Ask a question about the API, Database, a Document or Warehouse inventory analysis.")#.style(color_map=["blue","grey","red"]) with gr.Row(): with gr.Column(scale=1): message = gr.Textbox(show_label=False) with gr.Column(scale=1): with gr.Row(): button = gr.Button("Submit", elem_classes="gr-button") gr.ClearButton(message, elem_classes="gr-button") with gr.Row(): with gr.Column(scale=1): feedback_textbox = gr.Textbox(visible=False, show_label=False) with gr.Column(scale=1): submit_feedback_button = gr.Button("Submit Feedback", visible=False, elem_classes="gr-button")
button.click(answer_question, [message, chatbot], [chatbot]) message.submit(answer_question, [message, chatbot], [chatbot]) message.submit(lambda x: gr.update(value=""), None, [message], queue=False) button.click(lambda x: gr.update(value=''), [], [message])
chatbot.like(handle_dislike,None, outputs=[feedback_textbox, submit_feedback_button]) submit_feedback_button.click(submit_feedback, [feedback_textbox, chatbot], [chatbot, feedback_textbox,submit_feedback_button]) submit_feedback_button.click(lambda x: gr.update(value=''), [], [feedback_textbox])
demo.launch()123456789101112131415huggingface_hub==0.22.2langchain mysql-connector-python langchain-community langchain-openai requests gradio PyPDF2 faiss-cpupsycopg2nltktabulatepandasnumpypandasaiInbound.pdfAdded
app.pyAdded+460
requirements.txtAdded+15