76 lines
3.4 KiB
Python
76 lines
3.4 KiB
Python
from openai import OpenAI
|
|
from inventory import models
|
|
from car_inventory import settings
|
|
|
|
def fetch_data(dealer):
|
|
"""
|
|
Fetches the total number of cars in the inventory for the specified dealer. If no cars are
|
|
found, returns a message indicating that fact. If an error occurs during the operation,
|
|
it returns an error message with details.
|
|
|
|
:param dealer: The dealer object for which the inventory information is required.
|
|
The dealer object must be an instance of a model that includes a
|
|
`get_local_name` method for formatting localized dealer names.
|
|
:type dealer: Dealer
|
|
:return: A string indicating either the total number of cars in the dealer's inventory,
|
|
that no cars exist in the inventory, or an error message detailing what went
|
|
wrong during the operation.
|
|
:rtype: str
|
|
"""
|
|
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):
|
|
"""
|
|
Generates a response from the GPT-4 model based on the provided user input
|
|
and the dealer's information. The function is tailored to assist with car
|
|
inventory management, including queries about inventory, sales processes,
|
|
car transfers, invoices, and other features specific to the Haikal system.
|
|
|
|
:param user_input: The text input or query provided by the user.
|
|
:type user_input: str
|
|
:param dealer: Dealer information or identifier used to fetch related car data
|
|
or contextual information.
|
|
:type dealer: Any
|
|
:return: The generated response from the GPT-4 model as a string.
|
|
:rtype: str
|
|
:raises Exception: In case of an error during the API call to generate the
|
|
GPT-4 response.
|
|
"""
|
|
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)}"
|