Tafazzul-Nadeeem commited on
Commit
41c607d
·
1 Parent(s): d705d38
Files changed (1) hide show
  1. app.py +31 -9
app.py CHANGED
@@ -26,6 +26,7 @@ with gr.Blocks() as demo:
26
  )
27
  clear = gr.ClearButton([chat_input, chatbot])
28
 
 
29
  # Agent1 - RAG Decision Agent (whether RAG is needed for the user's query)
30
  def agent1_rag_decision(query):
31
  decision = rag_decision(query)
@@ -36,6 +37,15 @@ with gr.Blocks() as demo:
36
  results = get_top_k(query, k=k)
37
  return results
38
 
 
 
 
 
 
 
 
 
 
39
  def encode_image(image_path):
40
  with open(image_path, "rb") as f:
41
  return base64.b64encode(f.read()).decode("utf-8")
@@ -46,9 +56,6 @@ with gr.Blocks() as demo:
46
  if message["text"] is not None:
47
  history.append({"role": "user", "content": message["text"]})
48
  return history, gr.MultimodalTextbox(value=None, interactive=False)
49
-
50
- # def user(user_message, history: list):
51
- # return "", history + [{"role": "user", "content": user_message}]
52
 
53
  def respond(history):
54
  # print("history", history)
@@ -80,12 +87,27 @@ with gr.Blocks() as demo:
80
  }
81
  clean_messages.append(clean_msg)
82
  # print("First messages", messages[0])
83
- response = client.chat.completions.create(
84
- model="gpt-4o-mini",
85
- messages=clean_messages,
86
- temperature=0.7
87
- )
88
- response = response.choices[0].message.content.strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  # print("response:", response)
90
  # history.append({"role": "assistant", "content": response})
91
  history.append({"role": "assistant", "content": ""})
 
26
  )
27
  clear = gr.ClearButton([chat_input, chatbot])
28
 
29
+ ################################ AGENTS ###################################
30
  # Agent1 - RAG Decision Agent (whether RAG is needed for the user's query)
31
  def agent1_rag_decision(query):
32
  decision = rag_decision(query)
 
37
  results = get_top_k(query, k=k)
38
  return results
39
 
40
+ # Agent3 - LLM Agent (get query response from LLM)
41
+ def agent3_llm_agent(messages):
42
+ response = client.chat.completions.create(
43
+ model="gpt-4o-mini",
44
+ messages=messages,
45
+ temperature=0.7
46
+ )
47
+ return response.choices[0].message.content.strip()
48
+ ###########################################################################
49
  def encode_image(image_path):
50
  with open(image_path, "rb") as f:
51
  return base64.b64encode(f.read()).decode("utf-8")
 
56
  if message["text"] is not None:
57
  history.append({"role": "user", "content": message["text"]})
58
  return history, gr.MultimodalTextbox(value=None, interactive=False)
 
 
 
59
 
60
  def respond(history):
61
  # print("history", history)
 
87
  }
88
  clean_messages.append(clean_msg)
89
  # print("First messages", messages[0])
90
+
91
+ ########################### AGENTIC WORKFLOW ##########################
92
+ # Call Agent1- the RAG Decision Agent
93
+ rag_decision = agent1_rag_decision(messages[-1]["content"])
94
+
95
+ if rag_decision == True:
96
+ #Call Agent2 - the RAG Retrieval Agent
97
+ top_k_results = agent2_use_rag(messages[-1]["content"][0]["text"], k=3)
98
+ # Append the top k results to the messages
99
+ for i, result in enumerate(top_k_results):
100
+ clean_messages.append({
101
+ "role": "system",
102
+ "content": f"RAG Retrieved Result-{i+1}: " + result["content"]
103
+ })
104
+ # Call Agent3 - the LLM Agent to get query response
105
+ response = agent3_llm_agent(clean_messages)
106
+ else:
107
+ # Call Agent3 - the LLM Agent to get query response
108
+ response = agent3_llm_agent(messages)
109
+ #######################################################################
110
+
111
  # print("response:", response)
112
  # history.append({"role": "assistant", "content": response})
113
  history.append({"role": "assistant", "content": ""})