| import gradio as gr |
| import requests |
| import os |
| from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline |
|
|
| |
| model_id = "TinyLlama/TinyLlama-1.1B-Chat-v1.0" |
| tokenizer = AutoTokenizer.from_pretrained(model_id) |
| model = AutoModelForCausalLM.from_pretrained(model_id) |
| llm = pipeline("text-generation", model=model, tokenizer=tokenizer) |
|
|
| |
| def fetch_sales_order_headers(): |
| """Fetches the top 5 SAP sales orders from the sandbox API and returns as a list of dicts.""" |
| api_url = "https://sandbox.api.sap.com/s4hanacloud/sap/opu/odata/sap/API_SALES_ORDER_SRV/A_SalesOrder?$top=5&$inlinecount=allpages" |
| api_key = os.getenv("SAP_SANDBOX_API_KEY", "YOUR_API_KEY") |
| headers = { |
| "APIKey": api_key, |
| "Accept": "application/json" |
| } |
| try: |
| r = requests.get(api_url, headers=headers, timeout=10) |
| r.raise_for_status() |
| data = r.json() |
| results = data.get('d', {}).get('results', []) |
| if not results: |
| return [] |
| return results |
| except Exception as e: |
| return f"Error fetching Sales Orders: {e}" |
|
|
| def format_sales_orders_for_llm(orders): |
| """Converts the SAP sales order list to readable context for the LLM.""" |
| if isinstance(orders, str): |
| return orders |
| context = "Here are the latest SAP sales orders:\n" |
| for i, order in enumerate(orders, 1): |
| context += ( |
| f"{i}. Order: {order['SalesOrder']}, " |
| f"Type: {order['SalesOrderType']}, " |
| f"Org: {order['SalesOrganization']}, " |
| f"Date: {order['SalesOrderDate']}, " |
| f"SoldTo: {order['SoldToParty']}, " |
| f"Net: {order['TotalNetAmount']} {order['TransactionCurrency']}, " |
| f"Status: {order['OverallSDProcessStatus']}\n" |
| ) |
| return context |
|
|
| def chat_agent(message, history): |
| |
| sales_orders = fetch_sales_order_headers() |
| context = format_sales_orders_for_llm(sales_orders) |
| |
| prompt = ( |
| f"{context}\n" |
| f"User asked: {message}\n" |
| "Based on the above SAP sales orders, answer the user's question as accurately as possible. " |
| "If the question asks for sorting, filtering, maximum values, customer-specific info, or other details, do it based on the orders above. " |
| "If the question is unclear, summarize the available sales orders." |
| ) |
| |
| llm_output = llm(prompt, max_new_tokens=256)[0]["generated_text"] |
| |
| response = llm_output.replace(prompt, "").strip() |
| history = history or [] |
| history.append((message, response)) |
| return history, history |
|
|
| |
| with gr.Blocks() as demo: |
| gr.Markdown( |
| """ |
| # SAP Sales Order Chat Agent (Smart Reasoning) |
| Ask about SAP sales orders, values, filtering, sorting, etc. |
| Example questions: |
| - `Show me the latest SAP sales orders.` |
| - `Tell me about SAP order values.` |
| - `Show me top 2 sales orders with maximum value.` |
| - `Which order has the highest net value?` |
| - `List orders by customer.` |
| """ |
| ) |
| chatbot = gr.Chatbot() |
| txt = gr.Textbox(label="Your question") |
| clear = gr.Button("Clear chat") |
|
|
| txt.submit(chat_agent, [txt, chatbot], [chatbot, chatbot]) |
| clear.click(lambda: ([], []), None, [chatbot, chatbot]) |
|
|
| demo.launch() |
|
|