File size: 1,753 Bytes
e521af9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fbb910a
 
 
 
 
 
 
 
 
e521af9
 
 
 
 
 
 
 
fbb910a
 
 
 
 
 
 
 
e521af9
 
 
 
 
7d1e393
026a398
e521af9
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""
Module for configuring and creating tools for the LangGraph agent.
"""
from typing import Dict, Any, List, Tuple

from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_community.tools.arxiv.tool import ArxivQueryRun
from langchain_community.tools import DuckDuckGoSearchRun
from langchain_openai import ChatOpenAI


def create_tools(config: Dict[str, Any]) -> Tuple[List, ChatOpenAI]:
    """
    Create LangChain tools and model for use in the agent.
    
    Args:
        config: Configuration dictionary
        
    Returns:
        Tuple containing:
        - List of tools 
        - ChatOpenAI model
    """
     # Initialize tools with error handling
    try:
        tavily_tool = TavilySearchResults(
            max_results=config["tavily_max_results"],
            api_key=config["tavily_api_key"]
        )
    except Exception as e:
        print(f"Error initializing Tavily: {e}")
        tavily_tool = None
    
    # Initialize ArXiv tool
    arxiv_tool = ArxivQueryRun()

    # Initialize DuckDuckGo search tool
    duckduckgo_tool = DuckDuckGoSearchRun()
    
    # Create the tool belt
    tool_belt = []
    if tavily_tool:
        tool_belt.append(tavily_tool)
    if arxiv_tool:
        tool_belt.append(arxiv_tool)
    if duckduckgo_tool:
        tool_belt.append(duckduckgo_tool)
    
    
    # Initialize the OpenAI model
    model = ChatOpenAI(
        model=config["model_name"], 
        temperature=config["temperature"],
        api_key=config["openai_api_key"],
        callbacks=None  # Disable default callbacks that might cause tracing issues
    )
    
    # Bind tools to the model
    model_with_tools = model.bind_tools(tool_belt)
    
    return tool_belt, model_with_tools