File size: 9,206 Bytes
edc7f30
 
 
 
 
 
 
 
 
a7b94a3
1fab82e
edc7f30
 
 
 
1fab82e
 
 
 
edc7f30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1fab82e
edc7f30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1fab82e
edc7f30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a7b94a3
edc7f30
 
 
 
 
1fab82e
 
 
 
3320e05
edc7f30
 
1fab82e
edc7f30
 
 
 
80d52a8
a7b94a3
edc7f30
a7b94a3
 
 
 
3767364
a7b94a3
edc7f30
 
 
6268937
edc7f30
 
a7b94a3
 
 
 
 
 
edc7f30
 
 
 
a7b94a3
 
edc7f30
a7b94a3
 
80d52a8
a7b94a3
80d52a8
 
 
 
 
 
 
 
40d03b3
80d52a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e822e70
80d52a8
 
edc7f30
80d52a8
 
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
import json
import os
from datetime import datetime
from enum import Enum
from typing import List

import gradio as gr
from instructor import OpenAISchema
from openai import OpenAI
from openai.types.chat import ChatCompletionMessage
from pydantic import BaseModel

OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")


class ZipCode(BaseModel):
    value: int = 30328


class Bodystyles(Enum):
    CAB = "CAB"
    CPE = "CPE"
    RDS = "RDS"
    SDN = "SDN"
    SUV = "SUV"
    WGN = "WGN"


class Brands(Enum):
    AMG = "AMG"
    MAYBACH = "MAYBACH"


class ColorsExterior(Enum):
    BLK = "BLK"
    BLU = "BLU"
    BWN = "BWN"
    GRN = "GRN"
    GRY = "GRY"
    OTR = "OTR"
    RED = "RED"
    SLV = "SLV"
    WHT = "WHT"
    YLW = "YLW"


class ColorsInterior(Enum):
    BGE = "BGE"
    BLK = "BLK"
    BWN = "BWN"
    GRY = "GRY"
    OTR = "OTR"
    RED = "RED"


class DistanceFilter(Enum):
    _10 = "10"
    _100 = "100"
    _1000 = "1000"
    _200 = "200"
    _25 = "25"
    _50 = "50"
    _500 = "500"
    ANY = "ANY"


class Engines(Enum):
    _20LINLINE4TURBO = "20LINLINE4TURBO"
    _20LINLINE4TURBOWITHMILDHYBRIDDRIVE = "20LINLINE4TURBOWITHMILDHYBRIDDRIVE"
    _20LINLINE4TURBOWITHPLUGINHYBRIDTECHNOLOGY = (
        "20LINLINE4TURBOWITHPLUGINHYBRIDTECHNOLOGY"
    )
    _30LINLINE6TURBOENGINEWITHHYBRIDASSIST = "30LINLINE6TURBOENGINEWITHHYBRIDASSIST"
    _30LINLINE6TURBOENGINEWITHMILDHYBRIDDRIVE = (
        "30LINLINE6TURBOENGINEWITHMILDHYBRIDDRIVE"
    )
    _30LINLINE6TURBOENGINEWITHMILDHYBRIDDRIVEANDELECTRICAUXILIARYCOMPRESSOR = (
        "30LINLINE6TURBOENGINEWITHMILDHYBRIDDRIVEANDELECTRICAUXILIARYCOMPRESSOR"
    )
    _30LINLINE6TURBOENGINEWITHMILDHYBRIDDRIVEPLUSPLUGINHYBRIDELECTRICMOTOR = (
        "30LINLINE6TURBOENGINEWITHMILDHYBRIDDRIVEPLUSPLUGINHYBRIDELECTRICMOTOR"
    )
    _30LINLINE6TURBOWITHHYBRIDASSIST = "30LINLINE6TURBOWITHHYBRIDASSIST"
    _30LINLINE6TURBOWITHMILDHYBRIDDRIVE = "30LINLINE6TURBOWITHMILDHYBRIDDRIVE"
    _40LV8BITURBO = "40LV8BITURBO"
    _40LV8BITURBOWITHMILDHYBRIDDRIVE = "40LV8BITURBOWITHMILDHYBRIDDRIVE"
    _AMGENHANCED20LINLINE4TURBO = "AMGENHANCED20LINLINE4TURBO"
    _AMGENHANCED20LINLINE4TURBOWITHHYBRIDASSIST = (
        "AMGENHANCED20LINLINE4TURBOWITHHYBRIDASSIST"
    )
    _AMGENHANCED30LINLINE6TURBOWITHHYBRIDASSIST = (
        "AMGENHANCED30LINLINE6TURBOWITHHYBRIDASSIST"
    )
    _AMGENHANCED30LINLINE6TURBOWITHHYBRIDASSISTANDELECTRICAUXILIARYCOMPRESSOR = (
        "AMGENHANCED30LINLINE6TURBOWITHHYBRIDASSISTANDELECTRICAUXILIARYCOMPRESSOR"
    )
    _AMGENHANCED30LV6BITURBO = "AMGENHANCED30LV6BITURBO"
    _DUALPERMANENTLYEXCITEDSYNCHRONOUSPSM = "DUALPERMANENTLYEXCITEDSYNCHRONOUSPSM"
    _FRONTASYNCHRONOUSASMREARPERMANENTLYEXCITEDSYNCHRONOUSPSM = (
        "FRONTASYNCHRONOUSASMREARPERMANENTLYEXCITEDSYNCHRONOUSPSM"
    )
    _FRONTMOUNTEDASYNCHRONOUSASM = "FRONTMOUNTEDASYNCHRONOUSASM"
    _HANDCRAFTED20LINLINE4TURBOWITHHYBRIDASSIST = (
        "HANDCRAFTED20LINLINE4TURBOWITHHYBRIDASSIST"
    )
    _HANDCRAFTED60LV12BITURBO = "HANDCRAFTED60LV12BITURBO"
    _HANDCRAFTEDAMG20LINLINE4TURBO = "HANDCRAFTEDAMG20LINLINE4TURBO"
    _HANDCRAFTEDAMG20LINLINE4TURBOWITHHYBRIDASSISTANDELECTRICEXHAUSTGASTURBOCHARGER = (
        "HANDCRAFTEDAMG20LINLINE4TURBOWITHHYBRIDASSISTANDELECTRICEXHAUSTGASTURBOCHARGER"
    )
    _HANDCRAFTEDAMG40LV8BITURBO = "HANDCRAFTEDAMG40LV8BITURBO"
    _HANDCRAFTEDAMG40LV8BITURBOWITHHYBRIDASSIST = (
        "HANDCRAFTEDAMG40LV8BITURBOWITHHYBRIDASSIST"
    )
    _PERMANENTLYEXCITEDSYNCHRONOUSPSM = "PERMANENTLYEXCITEDSYNCHRONOUSPSM"


class FeaturesOptions(Enum):
    _X_OPT_810 = "X_OPT_810"
    _X_OPT_811 = "X_OPT_811"
    _X_OPT_APA = "X_OPT_APA"
    _X_OPT_APE = "X_OPT_APE"
    _X_OPT_CAM = "X_OPT_CAM"
    _X_OPT_CFC = "X_OPT_CFC"
    _X_OPT_DIC = "X_OPT_DIC"
    _X_OPT_HSW = "X_OPT_HSW"
    _X_OPT_HTS = "X_OPT_HTS"
    _X_OPT_HVS = "X_OPT_HVS"
    _X_OPT_IDS = "X_OPT_IDS"
    _X_OPT_LSW = "X_OPT_LSW"
    _X_OPT_MCS = "X_OPT_MCS"
    _X_OPT_MSC = "X_OPT_MSC"
    _X_OPT_PAR = "X_OPT_PAR"
    _X_OPT_RSS = "X_OPT_RSS"
    _X_OPT_RWS = "X_OPT_RWS"
    _X_OPT_SBC = "X_OPT_SBC"
    _X_OPT_WLSW = "X_OPT_WLSW"
    _X_OPT_WPC = "X_OPT_WPC"


class FeaturesPackages(Enum):
    _X_PKG_318 = "X_PKG_318"
    _X_PKG_ALE = "X_PKG_ALE"
    _X_PKG_ANP = "X_PKG_ANP"
    _X_PKG_AP = "X_PKG_AP"
    _X_PKG_APS = "X_PKG_APS"
    _X_PKG_DIP = "X_PKG_DIP"
    _X_PKG_E321 = "X_PKG_E321"
    _X_PKG_EIP = "X_PKG_EIP"
    _X_PKG_PAP = "X_PKG_PAP"
    _X_PKG_PMP = "X_PKG_PMP"
    _X_PKG_S325 = "X_PKG_S325"


class FeaturesWheels(Enum):
    _X_WHL_17i = "X_WHL_17i"
    _X_WHL_18i = "X_WHL_18i"
    _X_WHL_192i = "X_WHL_192i"
    _X_WHL_19i = "X_WHL_19i"
    _X_WHL_20i = "X_WHL_20i"
    _X_WHL_21i = "X_WHL_21i"
    _X_WHL_22i = "X_WHL_22i"
    _X_WHL_23i = "X_WHL_23i"


class FuelType(Enum):
    _D = "D"
    _E = "E"
    _G = "G"
    _H = "H"


class HighwayFuelEconomy(Enum):
    _1 = "1"
    _2 = "2"
    _3 = "3"
    _4 = "4"


class PassengerCapacity(Enum):
    _4 = "4"
    _5 = "5"
    _7 = "7"


class PriceRanges(Enum):
    _1 = "50000_70000"
    _2 = "70000_90000"
    _3 = "90000_120000"
    _4 = "120000_999000"
    _5 = "0_50000"


class Years(Enum):
    _2023 = "2023"
    _2024 = "2024"


class CarParameters(OpenAISchema):
    """
    defines various query parameters used
    to search vehicle inventory on https://mbusa.com/en/inventory/search.
    """

    zip: ZipCode
    bodyStyle: List[Bodystyles] = []
    brand: List[Brands] = []
    exteriorColor: List[ColorsExterior] = []
    interiorColor: List[ColorsInterior] = []
    distance: List[DistanceFilter] = []
    engine: List[Engines] = []
    interiorFeature: List[FeaturesOptions] = []
    performanceSafetyFeature: List[FeaturesOptions] = []
    wheel: List[FeaturesWheels] = []
    fuelType: List[FuelType] = []
    highwayFuelEconomy: List[HighwayFuelEconomy] = []
    passengerCapacity: List[PassengerCapacity] = []
    price: List[PriceRanges] = []
    year: List[Years] = []


def format_url(response: ChatCompletionMessage) -> str:
    tool_call = response.choices[0].message.tool_calls[0]

    arguments_string = tool_call.function.arguments
    arguments = json.loads(arguments_string)

    query_parameters = [
        f'{key}={",".join(value)}' for key, value in arguments.items() if key != "zip"
    ]
    query_string = "&".join(query_parameters)
    zip_code = arguments.get("zip", {"value": 30328})

    endpoint = "https://www.mbusa.com/en/inventory/search"
    url = f"{endpoint}?zip={zip_code['value']}&{query_string}"

    return url


def main(openai_api_key: str, prompt: str) -> str:
    """Returns a valid URL for the Mercedes-Benz USA website based on the user's query."""

    system_prompt = (
        "You are an AI assistant "
        "who specializes in generating "
        "relevant query parameters for the Mercedes-Benz USA website."
        f'\ncurrent date: {datetime.now().strftime("%Y-%m-%d")}'
    )

    messages = [
        {"role": "system", "content": system_prompt},
        {"role": "user", "content": prompt},
    ]

    # Connect to OpenAI with the provided API key
    client = OpenAI(api_key=openai_api_key)
    # Define an OpenAI tool for guaranteeing the schema of the response
    tool = [{"type": "function", "function": CarParameters.openai_schema}]

    # Call the OpenAI API
    response = client.chat.completions.create(
        model="gpt-4-turbo",
        messages=messages,
        temperature=0,
        tools=tool,  # Ensure the response schema is correct
        tool_choice="required",  # Ensure the tool is used
    )

    # Parse the response and construct the URL
    url = format_url(response)
    markdown_link = f"[{url}]({url})"  # Return clickable link for convenience
    return url, markdown_link


with gr.Blocks() as demo:
    with gr.Column():
        with gr.Row():
            openai_api_key = gr.Textbox(
                label="api-key",
                placeholder="Set your OpenAI API key here",
                lines=1,
                type="password",
            )
        with gr.Row():
            with gr.Column():
                prompt = gr.Textbox(
                    label="prompt",
                    placeholder="Enter your query here and press Enter",
                    lines=1,
                )
            with gr.Column():
                url = gr.Textbox(
                    label="url",
                    placeholder="URL will appear here",
                    lines=1,
                    type="text",
                )
        with gr.Row():
            link = gr.Markdown()
        with gr.Row():
            submit = gr.Button(value="Submit")

    submit.click(fn=main, inputs=[openai_api_key, prompt], outputs=[url, link])

    examples = [
        "I need a car for my growing family",
        "I frequently travel in snowy conditions",
        "I'm an eco-conscious city dweller",
        "I love high-performance sports cars",
        "I live in the snowy tundra's of Alaska",
        "Im looking for a 2024 Mercedes-Benz AMG SUV with a black exterior and interior, and 17 inch wheels. Can you help me with that?",
    ]

    gr.Examples(examples=examples, inputs=prompt, outputs=url)
demo.launch(inline=True, debug=True)