haikal/haikalbot/chatbot_logic.py
Marwan Alwali 231c9b5638 update
2024-12-22 15:51:35 +03:00

46 lines
1.8 KiB
Python

from openai import OpenAI
from inventory import models
from car_inventory import settings
def fetch_data(dealer):
try:
# Annotate total cars by make, model, and trim
cars = models.Car.objects.filter(dealer=dealer).count()
print(cars)
if cars:
return f"إجمالي عدد السيارات في المخزون الخاص بـ {dealer.get_local_name}) هو {cars}"
# return f"The total cars in {dealer} inventory is ( {cars} )."
else:
return "No cars found in the inventory."
except Exception as e:
return f"An error occurred while fetching car data: {str(e)}"
def get_gpt4_response(user_input, dealer):
dealer = dealer
client = OpenAI(api_key=settings.OPENAI_API_KEY)
if "سيارة في المخزون" in user_input.lower():
# cars = user_input.split("how many cars")[-1].strip()
car_data = fetch_data(dealer)
user_input += f" Relevant car data: {car_data}"
try:
completion = client.chat.completions.create(
model="gpt-4o",
messages=[
{
"role": "system",
"content": (
"You are an assistant specialized in car inventory management for the Haikal system. "
"You can answer questions about the inventory, sales process, car transfers, invoices, "
"and other application-specific functionalities. Always provide responses aligned "
"with the Haikal system's structure and features."
)
},
{"role": "user", "content": user_input},
],
)
return completion.choices[0].message.content.strip()
except Exception as e:
return f"An error occurred while generating the response: {str(e)}"