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

      Designing For TV: Principles, Patterns And Practical Guidance (Part 2)

      September 5, 2025

      Neo4j introduces new graph architecture that allows operational and analytics workloads to be run together

      September 5, 2025

      Beyond the benchmarks: Understanding the coding personalities of different LLMs

      September 5, 2025

      Top 10 Use Cases of Vibe Coding in Large-Scale Node.js Applications

      September 3, 2025

      Building smarter interactions with MCP elicitation: From clunky tool calls to seamless user experiences

      September 4, 2025

      From Zero to MCP: Simplifying AI Integrations with xmcp

      September 4, 2025

      Distribution Release: Linux Mint 22.2

      September 4, 2025

      Coded Smorgasbord: Basically, a Smorgasbord

      September 4, 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

      Drupal 11’s AI Features: What They Actually Mean for Your Team

      September 5, 2025
      Recent

      Drupal 11’s AI Features: What They Actually Mean for Your Team

      September 5, 2025

      Why Data Governance Matters More Than Ever in 2025?

      September 5, 2025

      Perficient Included in the IDC Market Glance for Digital Business Professional Services, 3Q25

      September 5, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      How DevOps Teams Are Redefining Reliability with NixOS and OSTree-Powered Linux

      September 5, 2025
      Recent

      How DevOps Teams Are Redefining Reliability with NixOS and OSTree-Powered Linux

      September 5, 2025

      Distribution Release: Linux Mint 22.2

      September 4, 2025

      ‘Cronos: The New Dawn’ was by far my favorite experience at Gamescom 2025 — Bloober might have cooked an Xbox / PC horror masterpiece

      September 4, 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("<img src="https://s.w.org/images/core/emoji/15.1.0/72x72/1f527.png" alt="🔧" class="wp-smiley" /> 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"<img src="https://s.w.org/images/core/emoji/15.1.0/72x72/2705.png" alt="✅" class="wp-smiley" /> Jina Search tool initialized: {search_tool.name}")
    
    
    print("n<img src="https://s.w.org/images/core/emoji/15.1.0/72x72/1f50d.png" alt="🔍" class="wp-smiley" /> 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("<img src="https://s.w.org/images/core/emoji/15.1.0/72x72/2705.png" alt="✅" class="wp-smiley" /> 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("<img src="https://s.w.org/images/core/emoji/15.1.0/72x72/2705.png" alt="✅" class="wp-smiley" /> 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<img src="https://s.w.org/images/core/emoji/15.1.0/72x72/1f916.png" alt="🤖" class="wp-smiley" /> Processing query: '{user_input}'")
       
        input_data = {"user_input": user_input}
       
        print("<img src="https://s.w.org/images/core/emoji/15.1.0/72x72/1f4e4.png" alt="📤" class="wp-smiley" /> Sending to Gemini...")
        ai_response = main_chain.invoke(input_data, config=config)
       
        if ai_response.tool_calls:
            print(f"<img src="https://s.w.org/images/core/emoji/15.1.0/72x72/1f6e0.png" alt="🛠" class="wp-smiley" />  AI requested {len(ai_response.tool_calls)} tool call(s)")
           
            tool_messages = []
            for i, tool_call in enumerate(ai_response.tool_calls):
                print(f"   <img src="https://s.w.org/images/core/emoji/15.1.0/72x72/1f50d.png" alt="🔍" class="wp-smiley" /> 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("<img src="https://s.w.org/images/core/emoji/15.1.0/72x72/1f4e5.png" alt="📥" class="wp-smiley" /> 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("<img src="https://s.w.org/images/core/emoji/15.1.0/72x72/2139.png" alt="ℹ" class="wp-smiley" />  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("<img src="https://s.w.org/images/core/emoji/15.1.0/72x72/1f9ea.png" alt="🧪" class="wp-smiley" /> TESTING ENHANCED SEARCH CHAIN")
        print("="*60)
       
        for i, query in enumerate(test_queries, 1):
            print(f"n<img src="https://s.w.org/images/core/emoji/15.1.0/72x72/1f4dd.png" alt="📝" class="wp-smiley" /> Test {i}: {query}")
            print("-" * 50)
           
            try:
                response = enhanced_search_chain.invoke(query)
                print(f"<img src="https://s.w.org/images/core/emoji/15.1.0/72x72/2705.png" alt="✅" class="wp-smiley" /> Response: {response.content[:300]}...")
               
                if hasattr(response, 'tool_calls') and response.tool_calls:
                    print(f"<img src="https://s.w.org/images/core/emoji/15.1.0/72x72/1f6e0.png" alt="🛠" class="wp-smiley" />  Used {len(response.tool_calls)} tool call(s)")
                   
            except Exception as e:
                print(f"<img src="https://s.w.org/images/core/emoji/15.1.0/72x72/274c.png" alt="❌" class="wp-smiley" /> 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<img src="https://s.w.org/images/core/emoji/15.1.0/72x72/1f680.png" alt="🚀" class="wp-smiley" /> Starting enhanced LangChain + Gemini + Jina Search demo...")
        test_search_chain()
       
        print("n" + "="*60)
        print("<img src="https://s.w.org/images/core/emoji/15.1.0/72x72/1f4ac.png" alt="💬" class="wp-smiley" /> INTERACTIVE MODE - Ask me anything! (type 'quit' to exit)")
        print("="*60)
       
        while True:
            user_query = input("n<img src="https://s.w.org/images/core/emoji/15.1.0/72x72/1f5e3.png" alt="🗣" class="wp-smiley" />  Your question: ").strip()
            if user_query.lower() in ['quit', 'exit', 'bye']:
                print("<img src="https://s.w.org/images/core/emoji/15.1.0/72x72/1f44b.png" alt="👋" class="wp-smiley" /> Goodbye!")
                break
           
            if user_query:
                try:
                    response = enhanced_search_chain.invoke(user_query)
                    print(f"n<img src="https://s.w.org/images/core/emoji/15.1.0/72x72/1f916.png" alt="🤖" class="wp-smiley" /> Response:n{response.content}")
                except Exception as e:
                    print(f"<img src="https://s.w.org/images/core/emoji/15.1.0/72x72/274c.png" alt="❌" class="wp-smiley" /> 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

    September 3, 2025
    Machine Learning

    Announcing the new cluster creation experience for Amazon SageMaker HyperPod

    September 3, 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

    A Customer-Centric Shoptalk Spring 2025

    Development

    CVE-2025-7749 – Code-projects Online Appointment Booking System SQL Injection Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    Flowtly- All in one business management system. Ready for AI agents.

    Web Development

    6 Best Free and Open Scheme Color Scheme Generators

    Linux

    Highlights

    Linux

    Introduction to Terraform: Infrastructure as Code Basics

    June 7, 2025

    What is Terraform? Let’s Start Simple Hey there, Infra coders! Imagine you’re building a house,…

    How Top CISOs Save Their SOCs from Alert Chaos to Never Miss Real Incidents

    August 5, 2025

    CVE-2025-20308 – Cisco Spaces Connector Privilege Escalation Vulnerability

    July 2, 2025

    CVE-2025-4458 – Code-projects Patient Record Management System SQL Injection Vulnerability

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

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