Skip to content

Commit 3b7ca5c

Browse files
committed
fix: Improve Chain of Thought handling in Gradio interface
- Remove global agent initiaization with use_cot - Create new agent instance per request with correct CoT setting - Add better error messages for missing configurations
1 parent 05836c2 commit 3b7ca5c

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

agentic_rag/gradio_app.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ def load_config():
3636
hf_token = load_config()
3737
openai_key = os.getenv("OPENAI_API_KEY")
3838

39-
local_agent = LocalRAGAgent(vector_store) if hf_token else None
40-
openai_agent = RAGAgent(vector_store, openai_api_key=openai_key) if openai_key else None
39+
# Initialize agents with use_cot=True to ensure CoT is available
40+
local_agent = LocalRAGAgent(vector_store, use_cot=True) if hf_token else None
41+
openai_agent = RAGAgent(vector_store, openai_api_key=openai_key, use_cot=True) if openai_key else None
4142

4243
def process_pdf(file: tempfile._TemporaryFileWrapper) -> str:
4344
"""Process uploaded PDF file"""
@@ -84,18 +85,22 @@ def chat(message: str, history: List[List[str]], agent_type: str, use_cot: bool,
8485
print(f"Agent: {agent_type}, CoT: {use_cot}, Language: {language}, Collection: {collection}")
8586
print("="*50 + "\n")
8687

87-
# Select appropriate agent
88-
agent = local_agent if agent_type == "Local (Mistral)" else openai_agent
89-
if not agent:
90-
response_text = "Agent not available. Please check your configuration."
91-
print(f"Error: {response_text}")
92-
return history + [[message, response_text]]
88+
# Select appropriate agent and reinitialize with correct settings
89+
if agent_type == "Local (Mistral)":
90+
if not hf_token:
91+
response_text = "Local agent not available. Please check your HuggingFace token configuration."
92+
print(f"Error: {response_text}")
93+
return history + [[message, response_text]]
94+
agent = LocalRAGAgent(vector_store, use_cot=use_cot)
95+
else:
96+
if not openai_key:
97+
response_text = "OpenAI agent not available. Please check your OpenAI API key configuration."
98+
print(f"Error: {response_text}")
99+
return history + [[message, response_text]]
100+
agent = RAGAgent(vector_store, openai_api_key=openai_key, use_cot=use_cot)
93101

94102
# Convert language selection to language code
95103
lang_code = "es" if language == "Spanish" else "en"
96-
97-
# Set CoT option and language
98-
agent.use_cot = use_cot
99104
agent.language = lang_code
100105

101106
# Process query and get response

0 commit comments

Comments
 (0)