File size: 23,886 Bytes
0fbe5c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2bffb3f
 
 
 
 
 
0fbe5c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2bffb3f
 
 
 
 
 
0fbe5c9
 
 
 
 
 
 
 
 
 
 
 
 
2bffb3f
 
 
 
 
0fbe5c9
 
 
 
 
 
 
 
 
 
 
 
 
144f6c7
 
0fbe5c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
import gradio as gr
import requests
import json
import asyncio
import logging
from typing import Dict, List, Any, Optional
import anthropic
import openai
from datetime import datetime
import os
# from dotenv import load_dotenv

# load_dotenv()
def get_api_keys():
    # Try to get from Hugging Face secrets first
    openai_key = os.getenv("OPENAI_API_KEY")
    dolibarr_key = os.getenv("DOLIBARR_API_KEY")
    
    # If not found, try to load from .env file (for local development)
    if not openai_key or not dolibarr_key:
        from dotenv import load_dotenv
        load_dotenv()
        openai_key = os.getenv("OPENAI_API_KEY")
        dolibarr_key = os.getenv("DOLIBARR_API_KEY")
    
    # Add more specific error messages
    if not openai_key:
        raise ValueError("OPENAI_API_KEY not found in environment variables or .env file")
    if not dolibarr_key:
        raise ValueError("DOLIBARR_API_KEY not found in environment variables or .env file")
    
    return openai_key, dolibarr_key


# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

class DolibarrAPI:
    """Your existing Dolibarr API class - keeping it unchanged"""
    base_url = "https://valiant-trust-production.up.railway.app/api/index.php"
    
    def __init__(self, api_key: str):
        self.api_key = api_key
        self.headers = {
            'DOLAPIKEY': api_key,
            'Content-Type': 'application/json',
            'Accept': 'application/json'
        }

    def _request(self, method: str, endpoint: str, data: Optional[dict] = None, params: Optional[dict] = None) -> Any:
        base_url = "https://valiant-trust-production.up.railway.app/api/index.php"
        url = f"{base_url}{endpoint}"
        
        try:
            response = requests.request(method, url, headers=self.headers, json=data, params=params)
            response.raise_for_status()
            return response.json()
        except requests.exceptions.RequestException as e:
            logger.error(f"API request failed: {e}")
            return {"error": f"API request failed: {str(e)}"}
        except json.JSONDecodeError as e:
            logger.error(f"JSON decode error: {e}")
            return {"error": f"Invalid JSON response: {str(e)}"}

    def get_req(self, endpoint: str, params: Optional[dict] = None):
        return self._request('GET', endpoint, params=params)

    def post_req(self, endpoint: str, params: dict):
        return self._request("POST", endpoint, data=params)

    def put_req(self, endpoint: str, params: dict):
        return self._request("PUT", endpoint, data=params)

    def del_req(self, endpoint: str, params: Optional[dict] = None):
        return self._request("DELETE", endpoint, params=params)

def dolibarr_interface(method: str, endpoint: str, api_key=os.getenv("DOLIBARR_API_KEY"), payload_str: str = "") -> str:
    """Your existing interface function - keeping it unchanged"""
    try:
        api = DolibarrAPI(api_key)
        method = method.upper()
        
        payload = None
        if payload_str and payload_str.strip():
            try:
                payload = json.loads(payload_str)
            except json.JSONDecodeError as e:
                return json.dumps({"error": f"Invalid JSON payload: {str(e)}"}, indent=2)

        if method == 'GET':
            result = api.get_req(endpoint, payload)
        elif method == 'POST':
            if not payload:
                return json.dumps({"error": "POST requests require a payload"}, indent=2)
            result = api.post_req(endpoint, payload)
        elif method == 'PUT':
            if not payload:
                return json.dumps({"error": "PUT requests require a payload"}, indent=2)
            result = api.put_req(endpoint, payload)
        elif method == 'DELETE':
            result = api.del_req(endpoint, payload)
        else:
            return json.dumps({"error": f"Invalid HTTP method '{method}' selected."}, indent=2)
        
        return json.dumps(result, indent=2)
    
    except Exception as e:
        logger.error(f"Unexpected error in dolibarr_interface: {e}")
        return json.dumps({"error": f"Unexpected error: {str(e)}"}, indent=2)

class OpenAIDolibarrAgent:
    def __init__(self, openai_api_key: str, dolibarr_api_key: str, base_url: str = None):
        self.client = openai.OpenAI(api_key=openai_api_key, base_url=base_url)
        self.dolibarr_api_key = dolibarr_api_key
        
        # System prompt with Dolibarr context
        self.system_prompt = """You are a helpful ERP assistant that can interact with a Dolibarr system via API calls. 

CRITICAL RULES:

1. ALWAYS show ALL data returned by API calls - never truncate, limit, or summarize unless explicitly asked

2. When listing items (customers, invoices, products), display EVERY record returned

3. Present data in clean tables or structured format showing key fields

4. If API returns 100+ records, show all unless user asks for specific filtering

5. NEVER make assumptions about what the user wants to see - show everything

5. DO NOT ASSUME DATA AS IF IT WAS RETURNED BY THE API, NEVER ASSUME

        

DOLIBARR API ENDPOINTS:

- /thirdparties - GET: list all, GET /{id}: specific customer, POST: create

- /invoices - GET: list all invoices, GET /{id}: specific invoice

- /products - GET: list all products, POST: create product

- /contacts - Contact management

- /users - System users  

- /proposals - Commercial proposals/quotes

- /orders - Sales orders

- /bills - Supplier bills

- /projects - Project management

- /stocks - Inventory management





RESPONSE FORMAT RULES:

- For lists: Show ID, Name, Status, and other key fields in table format

- For single items: Show all relevant details clearly organized

- Always extract and display the most important information from API responses

- If API returns error, explain clearly what went wrong

- Include record counts: "Found X customers:" or "Total invoices: Y"



BEHAVIOR RULES:

- Be proactive - if user asks for "customers", get ALL customers

- Don't ask "would you like to see more?" - just show everything

- For specific IDs, show complete details

- When creating records, confirm success with details

- Always make the API call needed - don't hesitate or ask for clarification



Common operations:

- GET /thirdparties - List all customers/suppliers

- GET /thirdparties/{id} - Get specific customer details

- POST /thirdparties - Create new customer

- GET /invoices - List all invoices

- GET /products - List all products

- POST /products - Create new product



You will use the following **HTTP methods**:



* `GET` to retrieve data.

* `POST` to create new records.

* `PUT` to update existing records.

* `DELETE` to delete records.



Here is what you need to do for each of the provided **endpoints**:



---



### **1. `/thirdparties` (Customers, Suppliers, etc.)**



#### **GET** (Retrieve a list or details of a third party):



* **Endpoint**: `/thirdparties` or `/thirdparties/{id}`

* **Required parameters for GET**: No parameters for listing all, but you can use `id` in the endpoint for details of a specific third party.

* **Response**: A list of third parties or details of the specific one.



#### **POST** (Create a new third party):



* **Endpoint**: `/thirdparties`

* **Required Parameters** (in `payload` JSON):



  ```json

  {

    "name": "John Doe",          // The name of the third party

    "address": "123 Main St",    // Street address

    "zip": "12345",              // Postal code

    "town": "Sample City",       // City or town

    "country_id": 1,             // Country ID (e.g., 1 for USA)

    "email": "[email protected]", // Email address

    "phone": "+1234567890",      // Phone number

    "type": 1,                   // Type (1 for customer, 2 for supplier, etc.)

    "status": 1                  // Status (1 for active, 0 for inactive)

  }

  ```



#### **PUT** (Update an existing third party):



* **Endpoint**: `/thirdparties/{id}`

* **Required Parameters** (in `payload` JSON):



  ```json

  {

    "name": "Updated Name",      // Update the name or other attributes as needed

    "email": "[email protected]", // Update email

    "phone": "+9876543210"       // Update phone number

  }

  ```



#### **DELETE** (Delete a third party):



* **Endpoint**: `/thirdparties/{id}`

* **No payload is needed**, just the `id` of the third party to be deleted.



---



### **2. `/invoices`**



#### **GET** (Retrieve a list or details of an invoice):



* **Endpoint**: `/invoices` or `/invoices/{id}`

* **Required parameters for GET**: None for listing all invoices, `id` for a specific invoice.

* **Response**: A list of invoices or details of the specific invoice.



#### **POST** (Create a new invoice):



* **Endpoint**: `/invoices`

* **Required Parameters** (in `payload` JSON):



  ```json

  {

    "socid": 10,                 // Third-party ID (Customer ID)

    "lines": [                   // List of invoice lines

      {

        "desc": "Web Development Service", // Description of the service/product

        "subprice": 500,                // Unit price

        "qty": 1,                       // Quantity

        "total_ht": 500,                // Total excluding tax

        "vat": 18,                      // VAT percentage

        "total_ttc": 590                // Total including tax

      }

    ],

    "date": "2025-06-01",            // Invoice creation date (YYYY-MM-DD)

    "duedate": "2025-06-15"          // Due date (YYYY-MM-DD)

  }

  ```



#### **PUT** (Update an existing invoice):



* **Endpoint**: `/invoices/{id}`

* **Required Parameters** (in `payload` JSON):



  ```json

  {

    "lines": [                      // Updated lines

      {

        "desc": "Updated Service",    // New or updated description

        "subprice": 550,              // New price

        "qty": 2,                     // Updated quantity

        "total_ht": 1100,             // Updated total excluding tax

        "vat": 18,                    // VAT

        "total_ttc": 1294             // Updated total including tax

      }

    ]

  }

  ```



#### **DELETE** (Delete an invoice):



* **Endpoint**: `/invoices/{id}`

* **No payload needed**, just the `id` of the invoice to delete.



---



### **3. `/contacts` (Contacts for Third Parties)**



#### **GET** (Retrieve a list or details of a contact):



* **Endpoint**: `/contacts` or `/contacts/{id}`

* **Required parameters for GET**: None for listing all contacts, `id` for a specific contact.

* **Response**: A list of contacts or details of a specific contact.



#### **POST** (Create a new contact):



* **Endpoint**: `/contacts`

* **Required Parameters** (in `payload` JSON):



  ```json

  {

    "thirdparty_id": 1,              // Third-party ID (Customer or Supplier)

    "firstname": "Jane",             // Contact first name

    "lastname": "Doe",               // Contact last name

    "email": "[email protected]",  // Email address

    "phone": "+1234567890",          // Phone number

    "position": "Sales Manager",     // Position of the contact

    "address": "1234 Office St"      // Address

  }

  ```



#### **PUT** (Update an existing contact):



* **Endpoint**: `/contacts/{id}`

* **Required Parameters** (in `payload` JSON):



  ```json

  {

    "email": "[email protected]", // Update email

    "phone": "+9876543210"           // Update phone

  }

  ```



#### **DELETE** (Delete a contact):



* **Endpoint**: `/contacts/{id}`

* **No payload needed**, just the `id` of the contact to delete.



---



### **4. `/orders` (Customer/Supplier Orders)**



#### **GET** (Retrieve a list or details of an order):



* **Endpoint**: `/orders` or `/orders/{id}`

* **Required parameters for GET**: None for listing all orders, `id` for a specific order.

* **Response**: A list of orders or details of a specific order.



#### **POST** (Create a new order):



* **Endpoint**: `/orders`

* **Required Parameters** (in `payload` JSON):



  ```json

  {

    "socid": 10,                    // Third-party ID (Customer ID)

    "lines": [                       // List of order lines

      {

        "desc": "Laptop",            // Description of product

        "subprice": 1000,            // Unit price

        "qty": 1,                    // Quantity

        "total_ht": 1000,            // Total excluding tax

        "vat": 18,                   // VAT

        "total_ttc": 1180            // Total including tax

      }

    ],

    "date": "2025-06-01",             // Order date

    "duedate": "2025-06-15"           // Due date

  }

  ```



#### **PUT** (Update an existing order):



* **Endpoint**: `/orders/{id}`

* **Required Parameters** (in `payload` JSON):



  ```json

  {

    "lines": [                       // Updated lines

      {

        "desc": "Updated Laptop",     // Updated description

        "subprice": 1100,             // Updated price

        "qty": 2,                     // Updated quantity

        "total_ht": 2200,             // Updated total excluding tax

        "vat": 18,                    // VAT

        "total_ttc": 2600             // Updated total including tax

      }

    ]

  }

  ```



#### **DELETE** (Delete an order):



* **Endpoint**: `/orders/{id}`

* **No payload needed**, just the `id` of the order to delete.



---



### **5. `/products`**



#### **GET** (Retrieve a list or details of a product):



* **Endpoint**: `/products` or `/products/{id}`

* **Required parameters for GET**: None for listing all products, `id` for a specific product.

* **Response**: A list of products or details of a specific product.



#### **POST** (Create a new product):



* **Endpoint**: `/products`

* **Required Parameters** (in `payload` JSON):



  ```json

  {

    "label": "Smartphone",           // Product

  ```





name

"price": 499.99,                 // Unit price

"stock": 100,                    // Quantity in stock

"description": "Latest model",   // Product description

"socid": 10                      // Supplier ID

}





#### **PUT** (Update an existing product):



* **Endpoint**: `/products/{id}`

* **Required Parameters** (in `payload` JSON):



  ```json

  {

    "label": "Updated Smartphone",   // Updated product name

    "price": 549.99,                 // Updated price

    "stock": 120,                    // Updated stock

    "description": "Updated model"   // Updated description

  }

  ```



#### **DELETE** (Delete a product):



* **Endpoint**: `/products/{id}`

* **No payload needed**, just the `id` of the product to delete.



# When a product/invoice/thirdparty is mentioned, call api with a get request to get their id and name and then proceed to make the next required api call.



When users ask for information, determine the appropriate API call needed and use the dolibarr_api function.

Always format responses in a user-friendly way, extracting key information from the API responses.

If an API call fails, explain the error clearly and suggest alternatives.



Current date: """ + datetime.now().strftime("%Y-%m-%d")

        # Function definition for OpenAI format
        self.functions = [
            {
                "name": "dolibarr_api",
                "description": "Execute API calls to the Dolibarr ERP system",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "method": {
                            "type": "string",
                            "enum": ["GET", "POST", "PUT", "DELETE"],
                            "description": "HTTP method for the API call"
                        },
                        "endpoint": {
                            "type": "string", 
                            "description": "API endpoint (e.g., /thirdparties, /invoices)"
                        },
                        "payload": {
                            "type": "string",
                            "description": "JSON payload for POST/PUT requests (leave empty for GET)"
                        }
                    },
                    "required": ["method", "endpoint"]
                }
            }
        ]

    def execute_dolibarr_call(self, method: str, endpoint: str, payload: str = "") -> str:
        """Execute the actual Dolibarr API call"""
        return dolibarr_interface(method, endpoint, self.dolibarr_api_key, payload)

    def chat(self, message: str, history: List[List[str]]) -> str:
        """Main chat function that processes user messages"""
        try:
            # Convert Gradio history to OpenAI format
            messages = [{"role": "system", "content": self.system_prompt}]
            
            for human_msg, assistant_msg in history:
                if human_msg:
                    messages.append({"role": "user", "content": human_msg})
                if assistant_msg:
                    messages.append({"role": "assistant", "content": assistant_msg})
            
            # Add current message
            messages.append({"role": "user", "content": message})

            # Call OpenAI API with functions
            logger.info("Sending request to Nebius API...")
            response = self.client.chat.completions.create(
                model="gpt-3.5-turbo",  # or gpt-4 "Qwen/Qwen3-235B-A22B",
                messages=messages,
                functions=self.functions,
                function_call="auto",
                max_tokens=1500
            )

            # Process the response
            message = response.choices[0].message
            logger.info(f"Received response from Nebius: {message}")
            
            if message.function_call:
                # Execute the Dolibarr API call
                function_name = message.function_call.name
                function_args = json.loads(message.function_call.arguments)
                logger.info(f"Function call: {function_name} with args: {function_args}")
                
                if function_name == "dolibarr_api":
                    api_result = self.execute_dolibarr_call(
                        method=function_args.get("method", "GET"),
                        endpoint=function_args.get("endpoint", ""),
                        payload=function_args.get("payload", "")
                    )
                    logger.info(f"Dolibarr API result: {api_result}")
                    # Send function result back to OpenAI
                    messages.append({
                        "role": "assistant",
                        "content": None,
                        "function_call": message.function_call
                    })
                    messages.append({
                        "role": "function",
                        "name": function_name,
                        "content": api_result
                    })
                    
                    # Get final response
                    logger.info("Getting final response from Nebius...")
                    final_response = self.client.chat.completions.create(
                        model="gpt-3.5-turbo",#"Qwen/Qwen3-235B-A22B",
                        messages=messages,
                        max_tokens=1500
                    )
                    logger.info(f"Final response: {final_response.choices[0].message}")
                    
                    # Clean up the response content
                    content = final_response.choices[0].message.content
                    # Remove the <think> sections
                    content = content.split('</think>')[-1].strip() if '</think>' in content else content
                    return content
            
            # Clean up the response content for non-function calls too
            content = message.content
            content = content.split('</think>')[-1].strip() if '</think>' in content else content
            return content if content else "I couldn't process that request."
            
        except openai.APIConnectionError as e:
            logger.error(f"OpenAI API Connection Error: {e}")
            return "Sorry, I'm having trouble connecting to OpenAI. Please check if the API key is valid and the service is available."
        except openai.AuthenticationError as e:
            logger.error(f"OpenAI API Authentication Error: {e}")
            return "Sorry, there's an authentication error with the OpenAI API. Please check if the API key is correct."
        except Exception as e:
            logger.error(f"Error in chat: {e}")
            return f"Sorry, I encountered an error: {str(e)}"

def create_openai_agent_interface():
    """Create the Gradio interface for the OpenAI-powered Dolibarr agent"""
    
    # OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")  # Replace with your key
    # NEBIUS_BASE_URL = "https://api.studio.nebius.ai/v1"  # For Nebius (optional)
    # DOLIBARR_API_KEY = os.getenv("DOLIBARR_API_KEY")
    
    OPENAI_API_KEY, DOLIBARR_API_KEY = get_api_keys()
    
    # Add logging to verify keys (but don't log the actual keys)
    logger.info("API Keys loaded successfully")
    logger.info(f"OpenAI API Key length: {len(OPENAI_API_KEY) if OPENAI_API_KEY else 0}")
    logger.info(f"Dolibarr API Key length: {len(DOLIBARR_API_KEY) if DOLIBARR_API_KEY else 0}")
    
    if not OPENAI_API_KEY or not DOLIBARR_API_KEY:
        raise ValueError("API keys not found. Please set them in Hugging Face Secrets or .env file")
    
    # Initialize the agent
    agent = OpenAIDolibarrAgent(OPENAI_API_KEY, DOLIBARR_API_KEY)
    agent = OpenAIDolibarrAgent(OPENAI_API_KEY, DOLIBARR_API_KEY)
    #agent = OpenAIDolibarrAgent(os.getenv("NEBIUS_API_KEY"), DOLIBARR_API_KEY, NEBIUS_BASE_URL)  # For Nebius
    
    # Create Gradio ChatInterface
    demo = gr.ChatInterface(
        fn=agent.chat,
        title="πŸ€– ERP Assistant",
        description="""

πŸ€– AI-Powered Dolibarr ERP Assistant - Your intelligent business management companion. I can help you manage customers, invoices, products, orders, and financial operations through natural conversation. Simply type your request (e.g., "Show me all customers" or "Create a new invoice") and get instant results. Try it with our demo instance at https://valiant-trust-production.up.railway.app/ (username: admin, password: admin123).

""",
        examples=[
            "Show me all customers",
            "List all invoices", 
            "What products do we have?",
            "Get details for customer ID 1",
            "Show me recent proposals"
        ],
        cache_examples=False,
        theme=gr.themes.Soft()
    )
    
    return demo

# Main execution
if __name__ == '__main__':
    try:
        print("πŸš€ Starting OpenAI-Powered Dolibarr Agent...")
        
        # Create and launch the interface
        demo = create_openai_agent_interface()
        demo.launch(
            server_name="127.0.0.1",
            server_port=7862,
            share=False,
            debug=True,
            show_error=True
        )
        
    except Exception as e:
        logger.error(f"Failed to start application: {e}")
        print(f"❌ Error starting application: {e}")

# Example queries you can try:
"""

- "Show me all customers"

- "List all invoices"

- "Get me customer details for ID 1"  

- "What products do we have?"

- "Show me recent proposals"

- "Create a new customer named Test Corp"

- "Find all unpaid invoices"

"""