@@ -141,6 +141,13 @@ def from_response(cls, response_obj):
141
141
response_time = response_obj .usage .response_time
142
142
)
143
143
144
+ class ModelConfig (BaseModel ):
145
+ system_prompt : str
146
+ temperature : float
147
+ top_p : float
148
+ max_tokens : int
149
+ json_format : bool
150
+
144
151
145
152
class ModelResponse (BaseModel ):
146
153
id : str
@@ -155,6 +162,7 @@ class ModelResponse(BaseModel):
155
162
class MessageRequest (BaseModel ):
156
163
model : str = Field (..., description = "Name of the model to use." )
157
164
message : str = Field (..., description = "Message text to send to the model." )
165
+ config : ModelConfig
158
166
openai_api_key : Optional [str ] = Field (
159
167
None , description = "API key if required by the openai provider."
160
168
)
@@ -195,7 +203,7 @@ def redact_words(model_name, text):
195
203
return text
196
204
197
205
async def handle_completion (
198
- model_name : str , message : str , api_base : Optional [str ] = None
206
+ model_name : str , message : str , config : ModelConfig , api_base : Optional [str ] = None ,
199
207
):
200
208
try :
201
209
start_time = time .time ()
@@ -204,21 +212,48 @@ async def handle_completion(
204
212
logging .info (f"Using API base: { api_base } " )
205
213
response_obj = completion (
206
214
model = model_name ,
207
- messages = [{"content" : message , "role" : "user" }],
208
- api_base = api_base ,
215
+ messages = [
216
+ {
217
+ "content" : config .system_prompt + ". Please generate the response in JSON" if config .json_format else "" ,
218
+ "role" : "system" ,
219
+ },
220
+ {
221
+ "content" : message ,
222
+ "role" : "user" ,
223
+ }
224
+ ],
225
+ api_base = api_base + "/v1" ,
209
226
timeout = 180.00 ,
210
227
metadata = {
211
228
"generation_name" : model_name , # set langfuse generation name
212
- }
229
+ },
230
+ temperature = config .temperature ,
231
+ max_tokens = config .max_tokens ,
232
+ top_p = config .top_p ,
233
+ response_format = {"type" : "json_object" } if config .json_format else None ,
234
+ api_key = "None" ,
213
235
)
214
236
else :
215
237
response_obj = completion (
216
238
model = model_name ,
217
- messages = [{"content" : message , "role" : "user" }],
239
+ messages = [
240
+ {
241
+ "content" : config .system_prompt + ". Please generate the response in JSON" if config .json_format else "" ,
242
+ "role" : "system" ,
243
+ },
244
+ {
245
+ "content" : message ,
246
+ "role" : "user" ,
247
+ }
248
+ ],
218
249
timeout = 180.00 ,
219
250
metadata = {
220
251
"generation_name" : model_name , # set langfuse generation name
221
- }
252
+ },
253
+ temperature = config .temperature ,
254
+ max_tokens = config .max_tokens ,
255
+ top_p = config .top_p ,
256
+ response_format = {"type" : "json_object" } if config .json_format == True else None
222
257
)
223
258
224
259
end_time = time .time ()
@@ -275,7 +310,10 @@ async def messaging(request: MessageRequest):
275
310
message = request .message
276
311
openai_api_key = request .openai_api_key
277
312
anthropic_api_key = request .anthropic_api_key
313
+ config = request .config
314
+
278
315
logging .info (f"Requested model name: { model_name } " )
316
+ logging .info (f"Config: { config } " )
279
317
logging .info (f"Message: { message } " )
280
318
# Fetch models from Supabase
281
319
models = await fetch_models_from_supabase ()
@@ -285,36 +323,36 @@ async def messaging(request: MessageRequest):
285
323
if openai_model and openai_api_key :
286
324
logging .info (f"Using OpenAI provider with model_id: { openai_model ['model_id' ]} " )
287
325
with temporary_env_var ("OPENAI_API_KEY" , openai_api_key ):
288
- return await handle_completion (openai_model ['model_id' ], message )
326
+ return await handle_completion (openai_model ['model_id' ], message , config = config )
289
327
290
328
# Anthropic provider check
291
329
anthropic_model = next ((m for m in models if m ["model_name" ] == model_name and m ["provider" ] == "anthropic" ), None )
292
330
if anthropic_model and anthropic_api_key :
293
331
logging .info (f"Using Anthropic provider with model_id: { anthropic_model ['model_id' ]} " )
294
332
with temporary_env_var ("ANTHROPIC_API_KEY" , anthropic_api_key ):
295
- return await handle_completion (anthropic_model ['model_id' ], message )
333
+ return await handle_completion (anthropic_model ['model_id' ], message , config = config )
296
334
297
335
# GitHub provider check
298
336
github_model = next ((m for m in models if m ["model_name" ] == model_name and m ["provider" ] == "github" ), None )
299
337
if github_model :
300
338
logging .info (f"Using GitHub provider with model_id: { github_model ['model_id' ]} " )
301
339
model_id = f"{ github_model ['model_id' ]} "
302
- return await handle_completion (model_id , message )
340
+ return await handle_completion (model_id , message , config = config )
303
341
304
342
# Hugging Face provider check
305
343
huggingface_model = next ((m for m in models if m ["model_name" ] == model_name and m ["provider" ] == "huggingface" ), None )
306
344
if huggingface_model :
307
345
logging .info (f"Using Hugging Face provider with model_id: { huggingface_model ['model_id' ]} " )
308
346
model_id = f"{ huggingface_model ['model_id' ]} "
309
- return await handle_completion (model_id , message )
347
+ return await handle_completion (model_id , message , config = config )
310
348
311
349
# Ollama provider check
312
350
ollama_model = next ((m for m in models if m ["model_name" ] == model_name and m ["provider" ] == "ollama" ), None )
313
351
if ollama_model :
314
352
logging .info (f"Using Ollama provider with model_id: { ollama_model ['model_id' ]} " )
315
- model_id = f"ollama /{ ollama_model ['model_id' ]} "
316
- api_url = os . environ [ "OLLAMA_API_URL" ]
317
- return await handle_completion (model_id , message , api_base = api_url )
353
+ model_id = f"openai /{ ollama_model ['model_id' ]} "
354
+ api_url = "https://supa-dev--llm-comparison-api-ollama-api-dev.modal.run"
355
+ return await handle_completion (model_id , message , config = config , api_base = api_url )
318
356
319
357
# Error handling
320
358
model_info = next ((m for m in models if m ["model_name" ] == model_name ), None )
0 commit comments