Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      June 6, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      June 6, 2025

      How To Prevent WordPress SQL Injection Attacks

      June 6, 2025

      AI is currently in its teenage years, battling raging hormones

      June 6, 2025

      4 ways your organization can adapt and thrive in the age of AI

      June 6, 2025

      Google’s new Search tool turns financial info into interactive charts – how to try it

      June 6, 2025

      This rugged Android phone has something I’ve never seen on competing models

      June 6, 2025

      Anthropic’s new AI models for classified info are already in use by US gov

      June 6, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Handling PostgreSQL Migrations in Node.js

      June 6, 2025
      Recent

      Handling PostgreSQL Migrations in Node.js

      June 6, 2025

      How to Add Product Badges in Optimizely Configured Commerce Spire

      June 6, 2025

      Salesforce Health Check Assessment Unlocks ROI

      June 6, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Microsoft: Run PS script now if you deleted “inetpub” on Windows 11, Windows 10

      June 6, 2025
      Recent

      Microsoft: Run PS script now if you deleted “inetpub” on Windows 11, Windows 10

      June 6, 2025

      Spf Permerror Troubleshooting Guide For Better Email Deliverability Today

      June 6, 2025

      Amap – Gather Info in Easy Way

      June 6, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Machine Learning»A Coding Implementation of an Intelligent AI Assistant with Jina Search, LangChain, and Gemini for Real-Time Information Retrieval

    A Coding Implementation of an Intelligent AI Assistant with Jina Search, LangChain, and Gemini for Real-Time Information Retrieval

    June 1, 2025

    In this tutorial, we demonstrate how to build an intelligent AI assistant by integrating LangChain, Gemini 2.0 Flash, and Jina Search tools. By combining the capabilities of a powerful large language model (LLM) with an external search API, we create an assistant that can provide up-to-date information with citations. This step-by-step tutorial walks through setting up API keys, installing necessary libraries, binding tools to the Gemini model, and building a custom LangChain that dynamically calls external tools when the model requires fresh or specific information. By the end of this tutorial, we will have a fully functional, interactive AI assistant that can respond to user queries with accurate, current, and well-sourced answers.

    Copy CodeCopiedUse a different Browser
    %pip install --quiet -U "langchain-community>=0.2.16" langchain langchain-google-genai

    We install the required Python packages for this project. It includes the LangChain framework for building AI applications, LangChain Community tools (version 0.2.16 or higher), and LangChain’s integration with Google Gemini models. These packages enable seamless use of Gemini models and external tools within LangChain pipelines.

    Copy CodeCopiedUse a different Browser
    import getpass
    import os
    import json
    from typing import Dict, Any

    We incorporate essential modules into the project. Getpass allows securely entering API keys without displaying them on the screen, while os helps manage environment variables and file paths. JSON is used for handling JSON data structures, and typing provides type hints for variables, such as dictionaries and function arguments, ensuring better code readability and maintainability.

    Copy CodeCopiedUse a different Browser
    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: ")

    We ensure that the necessary API keys for Jina and Google Gemini are set as environment variables. Suppose the keys are not already defined in the environment. In that case, the script prompts the user to enter them securely using the getpass module, keeping the keys hidden from view for security purposes. This approach enables seamless access to these services without requiring the hardcoding of sensitive information in the code.

    Copy CodeCopiedUse a different Browser
    from langchain_community.tools import JinaSearch
    from langchain_google_genai import ChatGoogleGenerativeAI
    from langchain_core.prompts import ChatPromptTemplate
    from langchain_core.runnables import RunnableConfig, chain
    from langchain_core.messages import HumanMessage, AIMessage, ToolMessage
    
    
    print("🔧 Setting up tools and model...")

    We import key modules and classes from the LangChain ecosystem. It introduces the JinaSearch tool for web search, the ChatGoogleGenerativeAI model for accessing Google’s Gemini, and essential classes from LangChain Core, including ChatPromptTemplate, RunnableConfig, and message structures (HumanMessage, AIMessage, and ToolMessage). Together, these components enable the integration of external tools with Gemini for dynamic, AI-driven information retrieval. The print statement confirms that the setup process has begun.

    Copy CodeCopiedUse a different Browser
    search_tool = JinaSearch()
    print(f"✅ Jina Search tool initialized: {search_tool.name}")
    
    
    print("n🔍 Testing Jina Search directly:")
    direct_search_result = search_tool.invoke({"query": "what is langgraph"})
    print(f"Direct search result preview: {direct_search_result[:200]}...")

    We initialize the Jina Search tool by creating an instance of JinaSearch() and confirming it’s ready for use. The tool is designed to handle web search queries within the LangChain ecosystem. The script then runs a direct test query, “what is langgraph”, using the invoke method, and prints a preview of the search result. This step verifies that the search tool is functioning correctly before integrating it into a larger AI assistant workflow.

    Copy CodeCopiedUse a different Browser
    gemini_model = ChatGoogleGenerativeAI(
        model="gemini-2.0-flash",
        temperature=0.1,
        convert_system_message_to_human=True  
    )
    print("✅ Gemini model initialized")

    We initialize the Gemini 2.0 Flash model using the ChatGoogleGenerativeAI class from LangChain. The model is set with a low temperature (0.1) for more deterministic responses, and the convert_system_message_to_human=True parameter ensures system-level prompts are properly handled as human-readable messages for Gemini’s API. The final print statement confirms that the Gemini model is ready for use.

    Copy CodeCopiedUse a different Browser
    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}"),
    ])
    

    We define a prompt template using ChatPromptTemplate.from_messages() that guides the AI’s behavior. It includes a system message outlining the assistant’s role, a human message placeholder for user queries, and a placeholder for tool messages generated during tool calls. This structured prompt ensures the AI provides helpful, informative, and well-sourced responses while seamlessly integrating search results into the conversation.

    Copy CodeCopiedUse a different Browser
    gemini_with_tools = gemini_model.bind_tools([search_tool])
    print("✅ Tools bound to Gemini model")
    
    
    main_chain = detailed_prompt | gemini_with_tools
    
    
    def format_tool_result(tool_call: Dict[str, Any], tool_result: str) -> str:
        """Format tool results for better readability"""
        return f"Search Results for '{tool_call['args']['query']}':n{tool_result[:800]}..."

    We bind the Jina Search tool to the Gemini model using bind_tools(), enabling the model to invoke the search tool when needed. The main_chain combines the structured prompt template and the tool-enhanced Gemini model, creating a seamless workflow for handling user inputs and dynamic tool calls. Additionally, the format_tool_result function formats search results for a clear and readable display, ensuring users can easily understand the outputs of search queries.

    Copy CodeCopiedUse a different Browser
    @chain
    def enhanced_search_chain(user_input: str, config: RunnableConfig):
        """
        Enhanced chain that handles tool calls and provides detailed responses
        """
        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

    We define the enhanced_search_chain using the @chain decorator from LangChain, enabling it to handle user queries with dynamic tool usage. It takes a user input and a configuration object, passes the input through the main chain (which includes the prompt and Gemini with tools), and checks if the AI suggests any tool calls (e.g., web search via Jina). If tool calls are present, it executes the searches, creates ToolMessage objects, and reinvokes the chain with the tool results for a final, context-enriched response. If no tool calls are made, it returns the AI’s response directly.

    Copy CodeCopiedUse a different Browser
    def test_search_chain():
        """Test the search chain with various queries"""
       
        test_queries = [
            "what is langgraph",
            "latest developments in AI for 2024",
            "how does langchain work with different LLMs"
        ]
       
        print("n" + "="*60)
        print("🧪 TESTING ENHANCED SEARCH CHAIN")
        print("="*60)
       
        for i, query in enumerate(test_queries, 1):
            print(f"n📝 Test {i}: {query}")
            print("-" * 50)
           
            try:
                response = enhanced_search_chain.invoke(query)
                print(f"✅ Response: {response.content[:300]}...")
               
                if hasattr(response, 'tool_calls') and response.tool_calls:
                    print(f"🛠  Used {len(response.tool_calls)} tool call(s)")
                   
            except Exception as e:
                print(f"❌ Error: {str(e)}")
           
            print("-" * 50)

    The function, test_search_chain(), validates the entire AI assistant setup by running a series of test queries through the enhanced_search_chain. It defines a list of diverse test prompts, covering tools, AI topics, and LangChain integrations, and prints results, indicating whether tool calls were used. This helps verify that the AI can effectively trigger web searches, process responses, and return useful information to users, ensuring a robust and interactive system.

    Copy CodeCopiedUse a different Browser
    if __name__ == "__main__":
        print("n🚀 Starting enhanced LangChain + Gemini + Jina Search demo...")
        test_search_chain()
       
        print("n" + "="*60)
        print("💬 INTERACTIVE MODE - Ask me anything! (type 'quit' to exit)")
        print("="*60)
       
        while True:
            user_query = input("n🗣  Your question: ").strip()
            if user_query.lower() in ['quit', 'exit', 'bye']:
                print("👋 Goodbye!")
                break
           
            if user_query:
                try:
                    response = enhanced_search_chain.invoke(user_query)
                    print(f"n🤖 Response:n{response.content}")
                except Exception as e:
                    print(f"❌ Error: {str(e)}")

    Finally, we run the AI assistant as a script when the file is executed directly. It first calls the test_search_chain() function to validate the system with predefined queries, ensuring the setup works correctly. Then, it starts an interactive mode, allowing users to type custom questions and receive AI-generated responses enriched with dynamic search results when needed. The loop continues until the user types ‘quit’, ‘exit’, or ‘bye’, providing an intuitive and hands-on way to interact with the AI system.

    In conclusion, we’ve successfully built an enhanced AI assistant that leverages LangChain’s modular framework, Gemini 2.0 Flash’s generative capabilities, and Jina Search’s real-time web search functionality. This hybrid approach demonstrates how AI models can expand their knowledge beyond static data, providing users with timely and relevant information from reliable sources. You can now extend this project further by integrating additional tools, customizing prompts, or deploying the assistant as an API or web app for broader applications. This foundation opens up endless possibilities for building intelligent systems that are both powerful and contextually aware.


    Check out the Notebook on GitHub. All credit for this research goes to the researchers of this project. Also, feel free to follow us on Twitter and don’t forget to join our 95k+ ML SubReddit and Subscribe to our Newsletter.

    The post A Coding Implementation of an Intelligent AI Assistant with Jina Search, LangChain, and Gemini for Real-Time Information Retrieval appeared first on MarkTechPost.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleDistroWatch Weekly, Issue 1124
    Next Article RLHF 101: A Technical Tutorial on Reinforcement Learning from Human Feedback

    Related Posts

    Machine Learning

    How to Evaluate Jailbreak Methods: A Case Study with the StrongREJECT Benchmark

    June 6, 2025
    Machine Learning

    Build a Text-to-SQL solution for data consistency in generative AI using Amazon Nova

    June 6, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    rio – hardware-accelerated GPU terminal emulator

    Linux

    Zorin OS 17.3 Released with New Default Browser

    Linux

    EncryptHub Exploits Windows Zero-Day to Deploy Rhadamanthys and StealC Malware

    Development

    OpenAI to launch AI models that can think up their own experiments, says report

    News & Updates

    Highlights

    Linux

    Ubuntu 20.04 LTS termina il supporto: cosa fare ora?

    May 31, 2025

    Ubuntu 20.04 LTS (Focal Fossa) ha ufficialmente concluso il suo ciclo di supporto standard il…

    Microsoft Teams Mobile will introduce a game-changing content share button

    April 9, 2025

    Collections in Java

    January 25, 2025

    Satya Nadella says Microsoft’s AI model performance is “doubling every 6 months”, despite the estranged OpenAI partnership

    May 2, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.