Dama03 commited on
Commit
4cd8c82
·
1 Parent(s): 2844a43
Files changed (2) hide show
  1. app/ai_agent/agent.py +37 -6
  2. requirements.txt +4 -4
app/ai_agent/agent.py CHANGED
@@ -1,5 +1,10 @@
1
  from typing import List, Dict, Any, Optional
2
- from langchain.agents import create_react_agent, initialize_agent, AgentType
 
 
 
 
 
3
  from langchain_openai import ChatOpenAI
4
  from langchain_community.chat_models import ChatOllama
5
  from langchain_community.tools import Tool
@@ -482,7 +487,7 @@ N'oublie pas : tu es empathique, humain, et tu adaptes toujours ton niveau de la
482
  logger.info("Legacy/string agent created successfully")
483
  return legacy_agent
484
 
485
- # Messages-based ReAct agent with Pydantic output parser guidance
486
  logger.info("Creating messages-based ReAct agent")
487
  # Combine system prompt with ReAct-style instructions (no JSON)
488
  template = system_message + """
@@ -503,10 +508,36 @@ Begin!
503
  tool_names=", ".join([t.name for t in tools]) if tools else "none",
504
  tools="\n".join([f"- {t.name}: {t.description}" for t in tools]) if tools else "No tools available",
505
  )
506
- agent = create_react_agent(llm, tools, prompt)
507
- logger.info("Messages-based agent created successfully")
508
- # Return the runnable agent directly (compatible with newer LangChain versions)
509
- return agent
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
510
 
511
  except Exception as e:
512
  logger.error(f"Error creating agent: {str(e)}", exc_info=True)
 
1
  from typing import List, Dict, Any, Optional
2
+ try:
3
+ # Newer LC may or may not expose create_react_agent
4
+ from langchain.agents import create_react_agent, initialize_agent, AgentType # type: ignore
5
+ except Exception: # pragma: no cover - be resilient to LC API changes
6
+ from langchain.agents import initialize_agent, AgentType # type: ignore
7
+ create_react_agent = None # type: ignore
8
  from langchain_openai import ChatOpenAI
9
  from langchain_community.chat_models import ChatOllama
10
  from langchain_community.tools import Tool
 
487
  logger.info("Legacy/string agent created successfully")
488
  return legacy_agent
489
 
490
+ # Messages-based ReAct agent (fallbacks if create_react_agent is unavailable)
491
  logger.info("Creating messages-based ReAct agent")
492
  # Combine system prompt with ReAct-style instructions (no JSON)
493
  template = system_message + """
 
508
  tool_names=", ".join([t.name for t in tools]) if tools else "none",
509
  tools="\n".join([f"- {t.name}: {t.description}" for t in tools]) if tools else "No tools available",
510
  )
511
+ if create_react_agent is not None:
512
+ try:
513
+ agent = create_react_agent(llm, tools, prompt)
514
+ logger.info("Messages-based agent created successfully (create_react_agent)")
515
+ return agent
516
+ except Exception as e:
517
+ logger.warning(f"create_react_agent unavailable or failed: {e}; falling back to initialize_agent")
518
+
519
+ # Fallback: initialize_agent ZERO_SHOT_REACT_DESCRIPTION with our prompt
520
+ try:
521
+ legacy_agent = initialize_agent(
522
+ tools=tools,
523
+ llm=llm,
524
+ agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
525
+ prompt=prompt,
526
+ **common_kwargs,
527
+ )
528
+ logger.info("Messages-based agent created successfully via initialize_agent fallback")
529
+ return legacy_agent
530
+ except Exception as e:
531
+ logger.warning(f"initialize_agent with prompt failed: {e}; trying minimal initialize_agent")
532
+ # Last resort minimal init without prompt arg (for very new/old LC variants)
533
+ legacy_agent = initialize_agent(
534
+ tools=tools,
535
+ llm=llm,
536
+ agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
537
+ **common_kwargs,
538
+ )
539
+ logger.info("Messages-based agent created via minimal initialize_agent")
540
+ return legacy_agent
541
 
542
  except Exception as e:
543
  logger.error(f"Error creating agent: {str(e)}", exc_info=True)
requirements.txt CHANGED
@@ -10,7 +10,7 @@ PyPDF2
10
  httpx
11
  openai
12
  python-dotenv
13
- langchain-openai
14
  langchain-huggingface
15
  huggingface-hub
16
  cachetools
@@ -28,7 +28,7 @@ langdetect
28
  requests
29
  serpapi
30
  googlemaps
31
- langchain
32
- langchain-community
33
- langchain-core
34
  langsmith
 
10
  httpx
11
  openai
12
  python-dotenv
13
+ langchain-openai<0.3
14
  langchain-huggingface
15
  huggingface-hub
16
  cachetools
 
28
  requests
29
  serpapi
30
  googlemaps
31
+ langchain<0.3
32
+ langchain-community<0.3
33
+ langchain-core<0.3
34
  langsmith