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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import os
|
3 |
+
from datetime import datetime
|
4 |
+
from enum import Enum
|
5 |
+
from typing import List
|
6 |
+
|
7 |
+
import gradio as gr
|
8 |
+
from instructor import OpenAISchema
|
9 |
+
from openai import OpenAI
|
10 |
+
from pydantic import BaseModel
|
11 |
+
|
12 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
13 |
+
|
14 |
+
|
15 |
+
class Bodystyles(Enum):
|
16 |
+
CAB = "CAB"
|
17 |
+
CPE = "CPE"
|
18 |
+
RDS = "RDS"
|
19 |
+
SDN = "SDN"
|
20 |
+
SUV = "SUV"
|
21 |
+
WGN = "WGN"
|
22 |
+
|
23 |
+
|
24 |
+
class Brands(Enum):
|
25 |
+
AMG = "AMG"
|
26 |
+
MAYBACH = "MAYBACH"
|
27 |
+
|
28 |
+
|
29 |
+
class ColorsExterior(Enum):
|
30 |
+
BLK = "BLK"
|
31 |
+
BLU = "BLU"
|
32 |
+
BWN = "BWN"
|
33 |
+
GRN = "GRN"
|
34 |
+
GRY = "GRY"
|
35 |
+
OTR = "OTR"
|
36 |
+
RED = "RED"
|
37 |
+
SLV = "SLV"
|
38 |
+
WHT = "WHT"
|
39 |
+
YLW = "YLW"
|
40 |
+
|
41 |
+
|
42 |
+
class ColorsInterior(Enum):
|
43 |
+
BGE = "BGE"
|
44 |
+
BLK = "BLK"
|
45 |
+
BWN = "BWN"
|
46 |
+
GRY = "GRY"
|
47 |
+
OTR = "OTR"
|
48 |
+
RED = "RED"
|
49 |
+
|
50 |
+
|
51 |
+
class DistanceFilter(Enum):
|
52 |
+
_10 = "10"
|
53 |
+
_100 = "100"
|
54 |
+
_1000 = "1000"
|
55 |
+
_200 = "200"
|
56 |
+
_25 = "25"
|
57 |
+
_50 = "50"
|
58 |
+
_500 = "500"
|
59 |
+
ANY = "ANY"
|
60 |
+
|
61 |
+
|
62 |
+
class Engines(Enum):
|
63 |
+
# Adding only a few options for brevity
|
64 |
+
_20LINLINE4TURBO = "20LINLINE4TURBO"
|
65 |
+
_20LINLINE4TURBOWITHMILDHYBRIDDRIVE = "20LINLINE4TURBOWITHMILDHYBRIDDRIVE"
|
66 |
+
_20LINLINE4TURBOWITHPLUGINHYBRIDTECHNOLOGY = (
|
67 |
+
"20LINLINE4TURBOWITHPLUGINHYBRIDTECHNOLOGY"
|
68 |
+
)
|
69 |
+
_30LINLINE6TURBOENGINEWITHHYBRIDASSIST = "30LINLINE6TURBOENGINEWITHHYBRIDASSIST"
|
70 |
+
_30LINLINE6TURBOENGINEWITHMILDHYBRIDDRIVE = (
|
71 |
+
"30LINLINE6TURBOENGINEWITHMILDHYBRIDDRIVE"
|
72 |
+
)
|
73 |
+
_30LINLINE6TURBOENGINEWITHMILDHYBRIDDRIVEANDELECTRICAUXILIARYCOMPRESSOR = (
|
74 |
+
"30LINLINE6TURBOENGINEWITHMILDHYBRIDDRIVEANDELECTRICAUXILIARYCOMPRESSOR"
|
75 |
+
)
|
76 |
+
_30LINLINE6TURBOENGINEWITHMILDHYBRIDDRIVEPLUSPLUGINHYBRIDELECTRICMOTOR = (
|
77 |
+
"30LINLINE6TURBOENGINEWITHMILDHYBRIDDRIVEPLUSPLUGINHYBRIDELECTRICMOTOR"
|
78 |
+
)
|
79 |
+
_30LINLINE6TURBOWITHHYBRIDASSIST = "30LINLINE6TURBOWITHHYBRIDASSIST"
|
80 |
+
_30LINLINE6TURBOWITHMILDHYBRIDDRIVE = "30LINLINE6TURBOWITHMILDHYBRIDDRIVE"
|
81 |
+
_40LV8BITURBO = "40LV8BITURBO"
|
82 |
+
_40LV8BITURBOWITHMILDHYBRIDDRIVE = "40LV8BITURBOWITHMILDHYBRIDDRIVE"
|
83 |
+
_AMGENHANCED20LINLINE4TURBO = "AMGENHANCED20LINLINE4TURBO"
|
84 |
+
_AMGENHANCED20LINLINE4TURBOWITHHYBRIDASSIST = (
|
85 |
+
"AMGENHANCED20LINLINE4TURBOWITHHYBRIDASSIST"
|
86 |
+
)
|
87 |
+
_AMGENHANCED30LINLINE6TURBOWITHHYBRIDASSIST = (
|
88 |
+
"AMGENHANCED30LINLINE6TURBOWITHHYBRIDASSIST"
|
89 |
+
)
|
90 |
+
_AMGENHANCED30LINLINE6TURBOWITHHYBRIDASSISTANDELECTRICAUXILIARYCOMPRESSOR = (
|
91 |
+
"AMGENHANCED30LINLINE6TURBOWITHHYBRIDASSISTANDELECTRICAUXILIARYCOMPRESSOR"
|
92 |
+
)
|
93 |
+
_AMGENHANCED30LV6BITURBO = "AMGENHANCED30LV6BITURBO"
|
94 |
+
_DUALPERMANENTLYEXCITEDSYNCHRONOUSPSM = "DUALPERMANENTLYEXCITEDSYNCHRONOUSPSM"
|
95 |
+
_FRONTASYNCHRONOUSASMREARPERMANENTLYEXCITEDSYNCHRONOUSPSM = (
|
96 |
+
"FRONTASYNCHRONOUSASMREARPERMANENTLYEXCITEDSYNCHRONOUSPSM"
|
97 |
+
)
|
98 |
+
_FRONTMOUNTEDASYNCHRONOUSASM = "FRONTMOUNTEDASYNCHRONOUSASM"
|
99 |
+
_HANDCRAFTED20LINLINE4TURBOWITHHYBRIDASSIST = (
|
100 |
+
"HANDCRAFTED20LINLINE4TURBOWITHHYBRIDASSIST"
|
101 |
+
)
|
102 |
+
_HANDCRAFTED60LV12BITURBO = "HANDCRAFTED60LV12BITURBO"
|
103 |
+
_HANDCRAFTEDAMG20LINLINE4TURBO = "HANDCRAFTEDAMG20LINLINE4TURBO"
|
104 |
+
_HANDCRAFTEDAMG20LINLINE4TURBOWITHHYBRIDASSISTANDELECTRICEXHAUSTGASTURBOCHARGER = (
|
105 |
+
"HANDCRAFTEDAMG20LINLINE4TURBOWITHHYBRIDASSISTANDELECTRICEXHAUSTGASTURBOCHARGER"
|
106 |
+
)
|
107 |
+
_HANDCRAFTEDAMG40LV8BITURBO = "HANDCRAFTEDAMG40LV8BITURBO"
|
108 |
+
_HANDCRAFTEDAMG40LV8BITURBOWITHHYBRIDASSIST = (
|
109 |
+
"HANDCRAFTEDAMG40LV8BITURBOWITHHYBRIDASSIST"
|
110 |
+
)
|
111 |
+
_PERMANENTLYEXCITEDSYNCHRONOUSPSM = "PERMANENTLYEXCITEDSYNCHRONOUSPSM"
|
112 |
+
|
113 |
+
|
114 |
+
class FeaturesOptions(Enum):
|
115 |
+
_X_OPT_810 = "X_OPT_810"
|
116 |
+
_X_OPT_811 = "X_OPT_811"
|
117 |
+
_X_OPT_APA = "X_OPT_APA"
|
118 |
+
_X_OPT_APE = "X_OPT_APE"
|
119 |
+
_X_OPT_CAM = "X_OPT_CAM"
|
120 |
+
_X_OPT_CFC = "X_OPT_CFC"
|
121 |
+
_X_OPT_DIC = "X_OPT_DIC"
|
122 |
+
_X_OPT_HSW = "X_OPT_HSW"
|
123 |
+
_X_OPT_HTS = "X_OPT_HTS"
|
124 |
+
_X_OPT_HVS = "X_OPT_HVS"
|
125 |
+
_X_OPT_IDS = "X_OPT_IDS"
|
126 |
+
_X_OPT_LSW = "X_OPT_LSW"
|
127 |
+
_X_OPT_MCS = "X_OPT_MCS"
|
128 |
+
_X_OPT_MSC = "X_OPT_MSC"
|
129 |
+
_X_OPT_PAR = "X_OPT_PAR"
|
130 |
+
_X_OPT_RSS = "X_OPT_RSS"
|
131 |
+
_X_OPT_RWS = "X_OPT_RWS"
|
132 |
+
_X_OPT_SBC = "X_OPT_SBC"
|
133 |
+
_X_OPT_WLSW = "X_OPT_WLSW"
|
134 |
+
_X_OPT_WPC = "X_OPT_WPC"
|
135 |
+
|
136 |
+
|
137 |
+
class FeaturesPackages(Enum):
|
138 |
+
_X_PKG_318 = "X_PKG_318"
|
139 |
+
_X_PKG_ALE = "X_PKG_ALE"
|
140 |
+
_X_PKG_ANP = "X_PKG_ANP"
|
141 |
+
_X_PKG_AP = "X_PKG_AP"
|
142 |
+
_X_PKG_APS = "X_PKG_APS"
|
143 |
+
_X_PKG_DIP = "X_PKG_DIP"
|
144 |
+
_X_PKG_E321 = "X_PKG_E321"
|
145 |
+
_X_PKG_EIP = "X_PKG_EIP"
|
146 |
+
_X_PKG_PAP = "X_PKG_PAP"
|
147 |
+
_X_PKG_PMP = "X_PKG_PMP"
|
148 |
+
_X_PKG_S325 = "X_PKG_S325"
|
149 |
+
|
150 |
+
|
151 |
+
class FeaturesWheels(Enum):
|
152 |
+
_X_WHL_17i = "X_WHL_17i"
|
153 |
+
_X_WHL_18i = "X_WHL_18i"
|
154 |
+
_X_WHL_192i = "X_WHL_192i"
|
155 |
+
_X_WHL_19i = "X_WHL_19i"
|
156 |
+
_X_WHL_20i = "X_WHL_20i"
|
157 |
+
_X_WHL_21i = "X_WHL_21i"
|
158 |
+
_X_WHL_22i = "X_WHL_22i"
|
159 |
+
_X_WHL_23i = "X_WHL_23i"
|
160 |
+
|
161 |
+
|
162 |
+
class FuelType(Enum):
|
163 |
+
_D = "D"
|
164 |
+
_E = "E"
|
165 |
+
_G = "G"
|
166 |
+
_H = "H"
|
167 |
+
|
168 |
+
|
169 |
+
class HighwayFuelEconomy(Enum):
|
170 |
+
_1 = "1"
|
171 |
+
_2 = "2"
|
172 |
+
_3 = "3"
|
173 |
+
_4 = "4"
|
174 |
+
|
175 |
+
|
176 |
+
class PassengerCapacity(Enum):
|
177 |
+
_4 = "4"
|
178 |
+
_5 = "5"
|
179 |
+
_7 = "7"
|
180 |
+
|
181 |
+
|
182 |
+
class PriceRanges(Enum):
|
183 |
+
_1 = "50000_70000"
|
184 |
+
_2 = "70000_90000"
|
185 |
+
_3 = "90000_120000"
|
186 |
+
_4 = "120000_999000"
|
187 |
+
_5 = "0_50000"
|
188 |
+
|
189 |
+
|
190 |
+
class Years(Enum):
|
191 |
+
_2023 = "2023"
|
192 |
+
_2024 = "2024"
|
193 |
+
|
194 |
+
|
195 |
+
class CarParameters(OpenAISchema):
|
196 |
+
"""
|
197 |
+
defines various query parameters used
|
198 |
+
to search vehicle inventory on https://mbusa.com/en/inventory/search.
|
199 |
+
"""
|
200 |
+
|
201 |
+
bodyStyle: List[Bodystyles] = []
|
202 |
+
brand: List[Brands] = []
|
203 |
+
exteriorColor: List[ColorsExterior] = []
|
204 |
+
interiorColor: List[ColorsInterior] = []
|
205 |
+
distance: List[DistanceFilter] = []
|
206 |
+
engine: List[Engines] = []
|
207 |
+
interiorFeature: List[FeaturesOptions] = []
|
208 |
+
performanceSafetyFeature: List[FeaturesOptions] = []
|
209 |
+
wheel: List[FeaturesWheels] = []
|
210 |
+
fuelType: List[FuelType] = []
|
211 |
+
highwayFuelEconomy: List[HighwayFuelEconomy] = []
|
212 |
+
passengerCapacity: List[PassengerCapacity] = []
|
213 |
+
price: List[PriceRanges] = []
|
214 |
+
year: List[Years] = []
|
215 |
+
|
216 |
+
|
217 |
+
def format_url(response) -> str:
|
218 |
+
tool_call = response.choices[0].message.tool_calls[0]
|
219 |
+
|
220 |
+
arguments_string = tool_call.function.arguments
|
221 |
+
arguments = json.loads(arguments_string)
|
222 |
+
|
223 |
+
query_parameters = [f'{key}={",".join(value)}' for key, value in arguments.items()]
|
224 |
+
query = "&".join(query_parameters)
|
225 |
+
|
226 |
+
endpoint = "https://www.mbusa.com/en/inventory/search"
|
227 |
+
url = f"{endpoint}?zip=30328&{query}"
|
228 |
+
|
229 |
+
return url
|
230 |
+
|
231 |
+
|
232 |
+
def main(prompt: str):
|
233 |
+
client = OpenAI(api_key=OPENAI_API_KEY)
|
234 |
+
tool = [{"type": "function", "function": CarParameters.openai_schema}]
|
235 |
+
|
236 |
+
system_prompt = f"""
|
237 |
+
You are an AI assistant who specializes in generating relevant query parameters for the Mercedes-Benz USA website.
|
238 |
+
current date: {datetime.now().strftime("%Y-%m-%d")}
|
239 |
+
""".strip()
|
240 |
+
|
241 |
+
user_prompt = "Im looking for a 2022 Mercedes-Benz AMG SUV \
|
242 |
+
with a black exterior and interior, \
|
243 |
+
and 17 inch wheels. \
|
244 |
+
Can you help me with that?"
|
245 |
+
|
246 |
+
messages = [
|
247 |
+
{"role": "system", "content": system_prompt},
|
248 |
+
{"role": "user", "content": user_prompt},
|
249 |
+
]
|
250 |
+
|
251 |
+
response = client.chat.completions.create(
|
252 |
+
model="gpt-4-turbo",
|
253 |
+
messages=messages,
|
254 |
+
temperature=0,
|
255 |
+
tools=tool,
|
256 |
+
tool_choice="required",
|
257 |
+
)
|
258 |
+
return format_url(response)
|
259 |
+
|
260 |
+
|
261 |
+
interface = gr.Interface(fn=main, inputs="text", outputs="text")
|
262 |
+
interface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
pydantic==2.7.1
|
2 |
+
openai==1.4.0
|
3 |
+
instructor==1.2.5
|