rohankaran commited on
Commit
35d9ec2
1 Parent(s): b5c1225

Add GPT-4o and GPT-4o-mini integration with NeMo Guardrails

Browse files

Implemented support for GPT-4o and GPT-4o-mini models, including their integration with NeMo Guardrails, and updated model references across the project files. Also added the new library dependence in requirements.txt and improved code formatting for readability.

Files changed (3) hide show
  1. app.py +3 -1
  2. guardrails_models.py +138 -14
  3. requirements.txt +0 -0
app.py CHANGED
@@ -451,7 +451,9 @@ with gr.Blocks(
451
 
452
  with gr.Tab(label="🏆 Leaderboard", elem_id="leaderboard") as leaderboard_tab:
453
  gr.Markdown("## 🏆 Guardrails Leaderboard")
454
- rankings = gr.Markdown("We will launch the guardrails leaderboard once enough votes are collected. Ranking will be calculated based on ELO ratings. Keep playing so that we can collect enough data.")
 
 
455
  # leaderboard_tab.select(get_rankings, None, [rankings])
456
 
457
  gr.Markdown(
 
451
 
452
  with gr.Tab(label="🏆 Leaderboard", elem_id="leaderboard") as leaderboard_tab:
453
  gr.Markdown("## 🏆 Guardrails Leaderboard")
454
+ rankings = gr.Markdown(
455
+ "We will launch the guardrails leaderboard once enough votes are collected. Ranking will be calculated based on ELO ratings. Keep playing so that we can collect enough data."
456
+ )
457
  # leaderboard_tab.select(get_rankings, None, [rankings])
458
 
459
  gr.Markdown(
guardrails_models.py CHANGED
@@ -29,7 +29,57 @@ def gpt35_turbo(
29
  llm = ChatOpenAI(
30
  temperature=temperature,
31
  max_retries=6,
32
- model_name="gpt-3.5-turbo-1106",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  metadata={"top_p": top_p, "max_output_tokens": max_output_tokens},
34
  )
35
  history_langchain_format = []
@@ -149,7 +199,7 @@ def gpt35_turbo_llamaguard(
149
  llm = ChatOpenAI(
150
  temperature=temperature,
151
  max_retries=6,
152
- model_name="gpt-3.5-turbo-1106",
153
  metadata={"top_p": top_p, "max_output_tokens": max_output_tokens},
154
  )
155
  history_langchain_format = []
@@ -294,7 +344,65 @@ def gpt35_turbo_nemoguardrails(
294
  rails = LLMRails(
295
  config,
296
  llm=ChatOpenAI(
297
- model_name="gpt-3.5-turbo-1106",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
298
  temperature=temperature,
299
  max_retries=6,
300
  metadata={"top_p": top_p, "max_output_tokens": max_output_tokens},
@@ -402,23 +510,31 @@ def gemini_pro_nemoguardrails(
402
  def get_all_models():
403
  return [
404
  {
405
- "name": "gpt3.5-turbo-1106",
406
  "model": gpt35_turbo,
407
  },
408
  {
409
- "name": "Llama-2-70b-chat-hf",
410
- "model": llama70B,
411
  },
412
  {
413
- "name": "Mixtral-8x7B-Instruct-v0.1",
414
- "model": mixtral7x8,
415
  },
416
  {
417
  "name": "Gemini-Pro",
418
  "model": gemini_pro,
419
  },
420
  # {
421
- # "name": "gpt3.5-turbo-1106 + Llama Guard",
 
 
 
 
 
 
 
 
422
  # "model": gpt35_turbo_llamaguard,
423
  # },
424
  # {
@@ -434,21 +550,29 @@ def get_all_models():
434
  # "model": gemini_pro_llamaguard,
435
  # },
436
  {
437
- "name": "gpt3.5-turbo-1106 + NeMo Guardrails",
438
  "model": gpt35_turbo_nemoguardrails,
439
  },
440
  {
441
- "name": "Llama-2-70b-chat-hf + NeMo Guardrails",
442
- "model": llama70B_nemoguardrails,
443
  },
444
  {
445
- "name": "Mixtral-8x7B-Instruct-v0.1 + NeMo Guardrails",
446
- "model": mixtral7x8_nemoguardrails,
447
  },
448
  {
449
  "name": "Gemini-Pro + NeMo Guardrails",
450
  "model": gemini_pro_nemoguardrails,
451
  },
 
 
 
 
 
 
 
 
452
  ]
453
 
454
 
 
29
  llm = ChatOpenAI(
30
  temperature=temperature,
31
  max_retries=6,
32
+ model_name="gpt-3.5-turbo",
33
+ metadata={"top_p": top_p, "max_output_tokens": max_output_tokens},
34
+ )
35
+ history_langchain_format = []
36
+ history_langchain_format.append(SystemMessage(system_prompt))
37
+ for human, ai in history:
38
+ history_langchain_format.append(HumanMessage(human))
39
+ if ai:
40
+ history_langchain_format.append(AIMessage(ai))
41
+
42
+ ai_message = llm.stream(history_langchain_format)
43
+ for message in ai_message:
44
+ yield message.content
45
+
46
+
47
+ def gpt4o(
48
+ history: List[List[Optional[str]]],
49
+ system_prompt: str,
50
+ temperature: float = 1,
51
+ top_p: float = 0.9,
52
+ max_output_tokens: int = 2048,
53
+ ):
54
+ llm = ChatOpenAI(
55
+ temperature=temperature,
56
+ max_retries=6,
57
+ model_name="gpt-4o",
58
+ metadata={"top_p": top_p, "max_output_tokens": max_output_tokens},
59
+ )
60
+ history_langchain_format = []
61
+ history_langchain_format.append(SystemMessage(system_prompt))
62
+ for human, ai in history:
63
+ history_langchain_format.append(HumanMessage(human))
64
+ if ai:
65
+ history_langchain_format.append(AIMessage(ai))
66
+
67
+ ai_message = llm.stream(history_langchain_format)
68
+ for message in ai_message:
69
+ yield message.content
70
+
71
+
72
+ def gpt4o_mini(
73
+ history: List[List[Optional[str]]],
74
+ system_prompt: str,
75
+ temperature: float = 1,
76
+ top_p: float = 0.9,
77
+ max_output_tokens: int = 2048,
78
+ ):
79
+ llm = ChatOpenAI(
80
+ temperature=temperature,
81
+ max_retries=6,
82
+ model_name="gpt-4o-mini",
83
  metadata={"top_p": top_p, "max_output_tokens": max_output_tokens},
84
  )
85
  history_langchain_format = []
 
199
  llm = ChatOpenAI(
200
  temperature=temperature,
201
  max_retries=6,
202
+ model_name="gpt-3.5-turbo",
203
  metadata={"top_p": top_p, "max_output_tokens": max_output_tokens},
204
  )
205
  history_langchain_format = []
 
344
  rails = LLMRails(
345
  config,
346
  llm=ChatOpenAI(
347
+ model_name="gpt-3.5-turbo",
348
+ temperature=temperature,
349
+ max_retries=6,
350
+ metadata={"top_p": top_p, "max_output_tokens": max_output_tokens},
351
+ ),
352
+ )
353
+ completion = rails.generate(messages=messages)
354
+ response = completion.get("content", "")
355
+ for message in response:
356
+ yield message
357
+
358
+
359
+ def gpt4o_nemoguardrails(
360
+ history: List[List[str]],
361
+ system_prompt: str,
362
+ temperature: float = 1,
363
+ top_p: float = 0.9,
364
+ max_output_tokens: int = 2048,
365
+ ):
366
+ messages = []
367
+ messages.append({"role": "system", "content": system_prompt})
368
+ for human, ai in history:
369
+ messages.append({"role": "user", "content": human})
370
+ if ai:
371
+ messages.append({"role": "assistant", "content": ai})
372
+ config = RailsConfig.from_path("./nemoguardrails_config")
373
+ rails = LLMRails(
374
+ config,
375
+ llm=ChatOpenAI(
376
+ model_name="gpt-4o",
377
+ temperature=temperature,
378
+ max_retries=6,
379
+ metadata={"top_p": top_p, "max_output_tokens": max_output_tokens},
380
+ ),
381
+ )
382
+ completion = rails.generate(messages=messages)
383
+ response = completion.get("content", "")
384
+ for message in response:
385
+ yield message
386
+
387
+
388
+ def gpt4o_mini_nemoguardrails(
389
+ history: List[List[str]],
390
+ system_prompt: str,
391
+ temperature: float = 1,
392
+ top_p: float = 0.9,
393
+ max_output_tokens: int = 2048,
394
+ ):
395
+ messages = []
396
+ messages.append({"role": "system", "content": system_prompt})
397
+ for human, ai in history:
398
+ messages.append({"role": "user", "content": human})
399
+ if ai:
400
+ messages.append({"role": "assistant", "content": ai})
401
+ config = RailsConfig.from_path("./nemoguardrails_config")
402
+ rails = LLMRails(
403
+ config,
404
+ llm=ChatOpenAI(
405
+ model_name="gpt-4o-mini",
406
  temperature=temperature,
407
  max_retries=6,
408
  metadata={"top_p": top_p, "max_output_tokens": max_output_tokens},
 
510
  def get_all_models():
511
  return [
512
  {
513
+ "name": "gpt3.5-turbo",
514
  "model": gpt35_turbo,
515
  },
516
  {
517
+ "name": "gpt4o",
518
+ "model": gpt4o,
519
  },
520
  {
521
+ "name": "gpt4o-mini",
522
+ "model": gpt4o_mini,
523
  },
524
  {
525
  "name": "Gemini-Pro",
526
  "model": gemini_pro,
527
  },
528
  # {
529
+ # "name": "Llama-2-70b-chat-hf",
530
+ # "model": llama70B,
531
+ # },
532
+ # {
533
+ # "name": "Mixtral-8x7B-Instruct-v0.1",
534
+ # "model": mixtral7x8,
535
+ # }, "model": gemini_pro,
536
+ # {
537
+ # "name": "gpt3.5-turbo + Llama Guard",
538
  # "model": gpt35_turbo_llamaguard,
539
  # },
540
  # {
 
550
  # "model": gemini_pro_llamaguard,
551
  # },
552
  {
553
+ "name": "gpt3.5-turbo + NeMo Guardrails",
554
  "model": gpt35_turbo_nemoguardrails,
555
  },
556
  {
557
+ "name": "gpt4o + NeMo Guardrails",
558
+ "model": gpt4o_nemoguardrails,
559
  },
560
  {
561
+ "name": "gpt4o-mini + NeMo Guardrails",
562
+ "model": gpt4o_mini_nemoguardrails,
563
  },
564
  {
565
  "name": "Gemini-Pro + NeMo Guardrails",
566
  "model": gemini_pro_nemoguardrails,
567
  },
568
+ # {
569
+ # "name": "Llama-2-70b-chat-hf + NeMo Guardrails",
570
+ # "model": llama70B_nemoguardrails,
571
+ # },
572
+ # {
573
+ # "name": "Mixtral-8x7B-Instruct-v0.1 + NeMo Guardrails",
574
+ # "model": mixtral7x8_nemoguardrails,
575
+ # },
576
  ]
577
 
578
 
requirements.txt CHANGED
Binary files a/requirements.txt and b/requirements.txt differ