Anurag Prasad commited on
Commit
5c9e521
·
1 Parent(s): f7527e8

made changes in chatbot response

Browse files
Files changed (2) hide show
  1. app.py +66 -18
  2. requirements.txt +2 -1
app.py CHANGED
@@ -5,6 +5,10 @@ from typing import Optional, List, Dict
5
  from contextlib import AsyncExitStack
6
  from mcp import ClientSession, StdioServerParameters
7
  from mcp.client.stdio import stdio_client
 
 
 
 
8
  # Modify imports section to include all required tools
9
  from database_module import (
10
  init_db,
@@ -294,11 +298,11 @@ def enhance_prompt(original_prompt):
294
  gr.update(visible=True)
295
  ]
296
 
297
- def save_new_model(selected_model_name, original_prompt, enhanced_prompt, choice):
298
  """Save new model to database"""
299
- if not selected_model_name or not original_prompt.strip():
300
  return [
301
- "Please select a model and enter a system prompt",
302
  gr.update(visible=True),
303
  gr.update()
304
  ]
@@ -306,13 +310,16 @@ def save_new_model(selected_model_name, original_prompt, enhanced_prompt, choice
306
  final_prompt = enhanced_prompt if choice == "Keep Enhanced" else original_prompt
307
 
308
  try:
309
- # Save the model first
 
 
 
310
  status = save_model_to_db(selected_model_name, final_prompt)
311
 
312
  # Run initial diagnostics
313
  diagnostic_result = run_initial_diagnostics(
314
  selected_model_name,
315
- f"System Prompt: {final_prompt}\nCapabilities: General language model capabilities"
316
  )
317
 
318
  if diagnostic_result:
@@ -333,7 +340,7 @@ def save_new_model(selected_model_name, original_prompt, enhanced_prompt, choice
333
  ]
334
 
335
  def chatbot_response(message, history, dropdown_value):
336
- """Generate chatbot response"""
337
  if not message.strip() or not dropdown_value:
338
  return history, ""
339
 
@@ -341,10 +348,36 @@ def chatbot_response(message, history, dropdown_value):
341
  model_details = get_model_details(model_name)
342
  system_prompt = model_details.get("system_prompt", "")
343
 
344
- # Simulate response (replace with actual LLM call) //Work here
345
- response = f"[{model_name}] Response to: {message}\n(Using system prompt: {system_prompt[:50]}...)"
346
- history.append([message, response])
347
- return history, ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
348
 
349
  def calculate_drift(dropdown_value):
350
  """Calculate drift for selected model"""
@@ -388,13 +421,19 @@ def initialize_interface():
388
  global current_model_mapping
389
  current_model_mapping = model_mapping
390
 
391
- # Get available model names for create new model dropdown
392
- available_models = get_available_model_names()
 
 
 
 
 
 
393
 
394
  return (
395
  formatted_items, # model_dropdown choices
396
  formatted_items[0] if formatted_items else None, # model_dropdown value
397
- available_models, # new_model_name choices
398
  formatted_items[0].split(" (")[0] if formatted_items else "", # selected_model_display
399
  formatted_items[0].split(" (")[0] if formatted_items else "" # drift_model_display
400
  )
@@ -409,7 +448,7 @@ with gr.Blocks(title="AI Model Management & Interaction Platform") as demo:
409
  gr.Markdown("### Model Selection")
410
 
411
  model_dropdown = gr.Dropdown(
412
- choices=[],
413
  label="Select Model",
414
  interactive=True
415
  )
@@ -424,10 +463,19 @@ with gr.Blocks(title="AI Model Management & Interaction Platform") as demo:
424
  # Create New Model Section (Initially Hidden)
425
  with gr.Group(visible=False) as create_new_section:
426
  gr.Markdown("#### Create New Model")
427
- #work here to show options to select model
428
- new_model_name = gr.Dropdown(
429
- choices=[],
430
- label="Select Model Name",
 
 
 
 
 
 
 
 
 
431
  interactive=True
432
  )
433
  new_system_prompt = gr.Textbox(
 
5
  from contextlib import AsyncExitStack
6
  from mcp import ClientSession, StdioServerParameters
7
  from mcp.client.stdio import stdio_client
8
+
9
+ from database_module.db import SessionLocal
10
+ from database_module.models import ModelEntry
11
+ from langchain.chat_models import init_chat_model
12
  # Modify imports section to include all required tools
13
  from database_module import (
14
  init_db,
 
298
  gr.update(visible=True)
299
  ]
300
 
301
+ def save_new_model(selected_model_name, selected_llm, original_prompt, enhanced_prompt, choice):
302
  """Save new model to database"""
303
+ if not selected_model_name or not original_prompt.strip() or not selected_llm:
304
  return [
305
+ "Please provide model name, LLM selection, and system prompt",
306
  gr.update(visible=True),
307
  gr.update()
308
  ]
 
310
  final_prompt = enhanced_prompt if choice == "Keep Enhanced" else original_prompt
311
 
312
  try:
313
+ # Save the model with LLM capabilities
314
+ capabilities = f"{selected_llm}\nSystem Prompt: {final_prompt}"
315
+ register_model_with_capabilities(selected_model_name, capabilities)
316
+
317
  status = save_model_to_db(selected_model_name, final_prompt)
318
 
319
  # Run initial diagnostics
320
  diagnostic_result = run_initial_diagnostics(
321
  selected_model_name,
322
+ capabilities
323
  )
324
 
325
  if diagnostic_result:
 
340
  ]
341
 
342
  def chatbot_response(message, history, dropdown_value):
343
+ """Generate chatbot response using selected model"""
344
  if not message.strip() or not dropdown_value:
345
  return history, ""
346
 
 
348
  model_details = get_model_details(model_name)
349
  system_prompt = model_details.get("system_prompt", "")
350
 
351
+ try:
352
+ # Initialize LLM based on model details
353
+ # Get model configuration from database
354
+ with SessionLocal() as session:
355
+ model_entry = session.query(ModelEntry).filter_by(name=model_name).first()
356
+ if not model_entry:
357
+ return history + [[message, "Error: Model not found"]], ""
358
+
359
+ llm_name = model_entry.capabilities.split("\n")[0] if model_entry.capabilities else "groq-llama-3.1-8b-instant"
360
+
361
+ # Initialize the LLM using langchain
362
+ llm = init_chat_model(
363
+ llm_name,
364
+ model_provider='groq' if llm_name.startswith('groq') else 'google'
365
+ )
366
+
367
+ # Format the conversation with system prompt
368
+ formatted_prompt = f"System: {system_prompt}\nUser: {message}"
369
+
370
+ # Get response from LLM
371
+ response = llm.invoke(formatted_prompt)
372
+ response_text = response.content
373
+
374
+ history.append([message, response_text])
375
+ return history, ""
376
+
377
+ except Exception as e:
378
+ error_message = f"Error generating response: {str(e)}"
379
+ history.append([message, error_message])
380
+ return history, ""
381
 
382
  def calculate_drift(dropdown_value):
383
  """Calculate drift for selected model"""
 
421
  global current_model_mapping
422
  current_model_mapping = model_mapping
423
 
424
+ # Available LLM choices for new model creation
425
+ llm_choices = [
426
+ "gemini-1.0-pro",
427
+ "gemini-1.5-pro",
428
+ "groq-llama-3.1-8b-instant",
429
+ "groq-mixtral-8x7b",
430
+ "groq-gpt4"
431
+ ]
432
 
433
  return (
434
  formatted_items, # model_dropdown choices
435
  formatted_items[0] if formatted_items else None, # model_dropdown value
436
+ llm_choices, # new_llm choices
437
  formatted_items[0].split(" (")[0] if formatted_items else "", # selected_model_display
438
  formatted_items[0].split(" (")[0] if formatted_items else "" # drift_model_display
439
  )
 
448
  gr.Markdown("### Model Selection")
449
 
450
  model_dropdown = gr.Dropdown(
451
+ choices=[], #work here Here show the already created models (fetched from database using mcp functions defined above)
452
  label="Select Model",
453
  interactive=True
454
  )
 
463
  # Create New Model Section (Initially Hidden)
464
  with gr.Group(visible=False) as create_new_section:
465
  gr.Markdown("#### Create New Model")
466
+ new_model_name = gr.Textbox(
467
+ label="Model name",
468
+ placeholder="Model name"
469
+ )
470
+ new_llm = gr.Dropdown(
471
+ choices=[
472
+ "gemini-1.0-pro",
473
+ "gemini-1.5-pro",
474
+ "groq-llama-3.1-8b-instant",
475
+ "groq-mixtral-8x7b",
476
+ "groq-gpt4"
477
+ ], #work here to show options to select llms(available to use) like gemini-1.5-pro, etc google models, groq models (atleast 5 in total)
478
+ label="Select LLM Name",
479
  interactive=True
480
  )
481
  new_system_prompt = gr.Textbox(
requirements.txt CHANGED
@@ -6,4 +6,5 @@ typing
6
  sqlalchemy
7
  psycopg2-binary
8
  fast-agent-mcp
9
- langchain[groq]
 
 
6
  sqlalchemy
7
  psycopg2-binary
8
  fast-agent-mcp
9
+ langchain[groq]
10
+ langchain