VectorV17 commited on
Commit
a9b3fd7
·
verified ·
1 Parent(s): 4ded160

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -45
app.py CHANGED
@@ -58,67 +58,47 @@ def retrieve_relevant_context(user_query):
58
 
59
  # 4. Agentic RAG Controller Logic (Compatible with Gradio 5.x / 6.0+)
60
  def chat_respond(user_message, chat_history):
61
- if not user_message.strip():
62
- return "", chat_history
63
-
64
- # RAG Step 1: Retrieve matching documents semantically
65
- retrieved_context, confidence_score = retrieve_relevant_context(user_message)
 
 
 
 
 
66
 
67
- # Set a smart similarity threshold; if query is irrelevant, bypass context injection
68
- if confidence_score < 0.25:
69
- system_instructions = (
70
- "You are Lane, a helpful AI Support Agent for Simplilearn's MOOC platform. "
71
- "The user is asking a general question outside your narrow knowledge scope. "
72
- "Politely inform them that you are specialized exclusively in Simplilearn course policies, "
73
- "certificates, prerequisites, and technical learning hub queries, then try to guide them back."
74
- )
75
- else:
76
- system_instructions = (
77
- "You are Lane, an official expert AI Support Agent for Simplilearn's MOOC educational pathways. "
78
- "Answer the student's question accurately, professionally, and naturally using ONLY the verified "
79
- "institutional data provided in the Context below. Do not invent details.\n\n"
80
- f"=== VERIFIED INSTITUTIONAL CONTEXT ===\n{retrieved_context}"
81
- )
82
-
83
- # Append the new user input using the modern role-content dictionary format
84
  chat_history.append({"role": "user", "content": user_message})
85
-
86
- # Compile messages sequence including system context guidelines
87
- api_messages = [{"role": "system", "content": system_instructions}]
88
- # Inject conversational window memory (last 4 turns) to maintain context
89
- for past_turn in chat_history[-4:]:
90
- api_messages.append(past_turn)
91
-
92
- # Append placeholder for streaming chatbot generation response
93
  chat_history.append({"role": "assistant", "content": ""})
94
 
 
 
 
95
  try:
96
- # Step 2: Stream generation via Serverless API to conserve local Space RAM
97
  stream_response = client.chat_completion(
98
  messages=api_messages,
99
- max_tokens=350,
100
- temperature=0.3,
101
  stream=True
102
  )
103
-
104
  running_text = ""
105
  for token_chunk in stream_response:
106
  token = token_chunk.choices[0].delta.content
107
  if token:
108
  running_text += token
109
- chat_history[-1]["content"] = running_text
 
 
 
 
110
  yield "", chat_history
111
-
112
- except Exception as api_error:
113
- # Dynamic local fallback if external server connection fluctuates
114
- print(f"API Connection Exception: {api_error}")
115
- fallback_text = (
116
- f"⚠️ [Session Routing Active] I am operating in Local Fallback mode due to server load. "
117
- f"Regarding your query, here is the official documentation: {retrieved_context.split(']: ')[-1]}"
118
- )
119
- chat_history[-1]["content"] = fallback_text
120
- yield "", chat_history
121
 
 
 
 
122
 
123
  # 5. UI Layout Styling Engine
124
  custom_css = """
 
58
 
59
  # 4. Agentic RAG Controller Logic (Compatible with Gradio 5.x / 6.0+)
60
  def chat_respond(user_message, chat_history):
61
+ # 1. Convert Gradio's ChatMessage objects into standard dictionaries for the API
62
+ api_messages = [{"role": "system", "content": SYSTEM_PROMPT}]
63
+ for msg in chat_history:
64
+ if isinstance(msg, dict):
65
+ api_messages.append(msg)
66
+ else:
67
+ api_messages.append({"role": msg.role, "content": msg.content})
68
+
69
+ # 2. Append the incoming user query to the API payload
70
+ api_messages.append({"role": "user", "content": user_message})
71
 
72
+ # 3. Update the Gradio UI history state
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  chat_history.append({"role": "user", "content": user_message})
 
 
 
 
 
 
 
 
74
  chat_history.append({"role": "assistant", "content": ""})
75
 
76
+ # Clear the input box and display the user query instantly
77
+ yield "", chat_history
78
+
79
  try:
80
+ # Pass the safely formatted api_messages list to the Inference Client
81
  stream_response = client.chat_completion(
82
  messages=api_messages,
83
+ max_tokens=1024,
 
84
  stream=True
85
  )
86
+
87
  running_text = ""
88
  for token_chunk in stream_response:
89
  token = token_chunk.choices[0].delta.content
90
  if token:
91
  running_text += token
92
+ # Safely modify the last item in chat_history regardless of its type
93
+ if isinstance(chat_history[-1], dict):
94
+ chat_history[-1]["content"] = running_text
95
+ else:
96
+ chat_history[-1].content = running_text
97
  yield "", chat_history
 
 
 
 
 
 
 
 
 
 
98
 
99
+ except Exception as e:
100
+ print(f"Error during streaming: {e}")
101
+
102
 
103
  # 5. UI Layout Styling Engine
104
  custom_css = """