<RETURN_TO_BASE

Building a Smart AI Assistant with LangChain, Gemini 2.0, and Jina Search for Real-Time Answers

Discover how to create an intelligent AI assistant using LangChain, Gemini 2.0, and Jina Search that provides real-time, well-sourced answers to user queries.

Integrating Powerful AI Tools for Real-Time Information

This tutorial demonstrates how to create an intelligent AI assistant by combining the LangChain framework, Gemini 2.0 Flash model, and Jina Search tool. The assistant is designed to provide accurate, up-to-date information by leveraging a large language model alongside external web search capabilities.

Setting Up the Environment and API Keys

Begin by installing the required Python packages, including LangChain and its community tools, as well as the integration with Google Gemini models:

%pip install --quiet -U "langchain-community>=0.2.16" langchain langchain-google-genai

Essential modules such as getpass, os, json, and typing are imported to handle secure API key entry, environment variable management, JSON data processing, and type annotations.

API keys for Jina and Google Gemini are requested securely if not already set in the environment variables:

if not os.environ.get("JINA_API_KEY"):
    os.environ["JINA_API_KEY"] = getpass.getpass("Enter your Jina API key: ")
 
if not os.environ.get("GOOGLE_API_KEY"):
    os.environ["GOOGLE_API_KEY"] = getpass.getpass("Enter your Google/Gemini API key: ")

Initializing Tools and Models

The JinaSearch tool and Gemini 2.0 Flash model are initialized to enable web search and generative AI capabilities:

search_tool = JinaSearch()
 
 
 
gemini_model = ChatGoogleGenerativeAI(
    model="gemini-2.0-flash",
    temperature=0.1,
    convert_system_message_to_human=True  
)

These components allow the assistant to retrieve fresh information dynamically and generate human-like responses.

Defining the AI Prompt and Binding Tools

A structured prompt template guides the assistant’s behavior, instructing it to use the search tool for recent or specific queries and to cite sources:

detailed_prompt = ChatPromptTemplate.from_messages([
    ("system", """You are an intelligent assistant with access to web search capabilities.
    When users ask questions, you can use the Jina search tool to find current information.
   
    Instructions:
    1. If the question requires recent or specific information, use the search tool
    2. Provide comprehensive answers based on the search results
    3. Always cite your sources when using search results
    4. Be helpful and informative in your responses"""),
    ("human", "{user_input}"),
    ("placeholder", "{messages}"),
])
 
 
 
# Bind the search tool to the Gemini model
 
gemini_with_tools = gemini_model.bind_tools([search_tool])
 
main_chain = detailed_prompt | gemini_with_tools

Handling User Queries with Dynamic Tool Calls

An enhanced chain is defined to process user inputs, detect when web searches are necessary, execute those searches, and integrate the results back into the AI’s responses:

@chain
def enhanced_search_chain(user_input: str, config: RunnableConfig):
    print(f"\n Processing query: '{user_input}'")
   
    input_data = {"user_input": user_input}
   
    print(" Sending to Gemini...")
    ai_response = main_chain.invoke(input_data, config=config)
   
    if ai_response.tool_calls:
        print(f"  AI requested {len(ai_response.tool_calls)} tool call(s)")
       
        tool_messages = []
        for i, tool_call in enumerate(ai_response.tool_calls):
            print(f"    Executing search {i+1}: {tool_call['args']['query']}")
           
            tool_result = search_tool.invoke(tool_call)
           
            tool_msg = ToolMessage(
                content=tool_result,
                tool_call_id=tool_call['id']
            )
            tool_messages.append(tool_msg)
       
        print(" Getting final response with search results...")
        final_input = {
            **input_data,
            "messages": [ai_response] + tool_messages
        }
        final_response = main_chain.invoke(final_input, config=config)
       
        return final_response
    else:
        print("  No tool calls needed")
        return ai_response

Testing and Interactive Usage

The setup includes a test function running example queries to ensure the assistant correctly uses the search tool and generates relevant answers:

def test_search_chain():
    test_queries = [
        "what is langgraph",
        "latest developments in AI for 2024",
        "how does langchain work with different LLMs"
    ]
   
    for query in test_queries:
        response = enhanced_search_chain.invoke(query)
        print(f" Response: {response.content[:300]}...")

An interactive mode allows users to ask questions in real-time, receiving AI-generated answers supported by live search results. The interaction continues until the user decides to quit.

Expanding AI Assistant Capabilities

This implementation showcases a hybrid AI assistant that extends static LLM knowledge with real-time web information, providing accurate, cited, and current answers. It serves as a foundation for more advanced applications by integrating additional tools, customizing prompts, or deploying in various environments like APIs or web apps.

🇷🇺

Сменить язык

Читать эту статью на русском

Переключить на Русский