Build a Real-Time AI Assistant with LangChain, Jina Search and Gemini 2.0 Flash

A team of developers has unveiled an advanced AI assistant that brings together the LangChain framework, Google’s Gemini 2.0 Flash model, and Jina Search. By connecting a large language model (LLM) to a live web-search API, the prototype delivers real-time data complete with citations. A detailed walkthrough shows how to set API keys, install libraries, integrate tools into Gemini, and build a flexible LangChain pipeline that fetches fresh information on demand.

To start, engineers install required Python packages. Using pip, they add langchain, langchain-community-tools (version 0.2.16 or higher), and google-generative-ai. These dependencies allow seamless access to Gemini models and external utilities within a single pipeline. A typical command—

pip install langchain langchain-community-tools google-generative-ai

pulls in the core framework, community integrations, and Gemini bindings for LangChain.

Next, the script imports essential Python modules. The getpass library prompts users to enter API keys without echoing them on screen. The os module reads and sets environment variables. JSON handles serialization and deserialization of data structures. The typing package provides type hints for dictionaries and function signatures, improving code clarity and maintainability.

Security measures place Jina and Google credentials in environment variables. If JINA_API_KEY or GOOGLE_API_KEY aren’t already defined, getpass requests input at runtime. The script then assigns os.environ['JINA_API_KEY'] and os.environ['GOOGLE_API_KEY'], avoiding hardcoded secrets. This pattern keeps keys out of source control and secures access to both search and language services.

With credentials in place, developers load key classes from LangChain. JinaSearch bridges web queries, while ChatGoogleGenerativeAI enables Gemini-based chat. Core utilities include ChatPromptTemplate for crafting structured messages, RunnableConfig for pipeline settings, and message types—HumanMessage, AIMessage, and ToolMessage. A simple print notification marks the start of setup and confirms that imports succeed.

An instance of JinaSearch initializes the search tool. Once

jina = JinaSearch()

executes, a print statement indicates readiness. A quick test invokes

jina.invoke("what is langgraph")

returning a snippet of search results. This preliminary check confirms that the connection to Jina Search works before full integration.

The next step spins up the Gemini model. Developers create

chat_model = ChatGoogleGenerativeAI(temperature=0.1, convert_system_message_to_human=True)

The low temperature favors consistent outputs. The convert_system_message_to_human parameter causes system prompts to appear in a human-friendly format. A print command confirms that the Gemini 2.0 Flash instance is active and ready to handle queries.

To guide the assistant, a prompt template is defined via

ChatPromptTemplate.from_messages([ ... ])

It includes a system prompt describing the assistant’s role, a placeholder for user input, and slots for tool-related messages. This design helps guarantee that when the AI answers questions, context from web searches integrates naturally into its replies, giving users clear and sourced information.

Binding the search tool occurs through

chat_model.bind_tools([jina])

This step gives Gemini the ability to call JinaSearch during conversations. The main_chain merges the prompt template with the enhanced chat_model into a single Runnable pipeline. Developers also set up a helper, format_tool_result, to transform raw search hits into readable AI messages that blend seamlessly into the dialogue.

An @chain decorator wraps a function named enhanced_search_chain. It accepts two arguments: a user query and a RunnableConfig object. When called, it passes the query through main_chain. If the AI suggests a web search, the function executes the tool calls, wraps results in ToolMessage instances, then reinvokes main_chain for a final answer. If no tool call emerges, it simply returns the model’s output.

To verify behavior, a test_search_chain() function runs sample prompts. The list covers topics such as defining LangGraph, exploring AI toolchains, and comparing LLM integrations. Each query routes through enhanced_search_chain, then prints the response and notes whether a web search occurred. This validation confirms that the assistant triggers external lookups appropriately and delivers accurate replies.

When the script runs directly, it first invokes test_search_chain() to perform those checks. It then opens an interactive loop where users type questions freely. Input persists until it matches “quit,” “exit,” or “bye.” As users submit new queries, the assistant responds in real time, enriched with live search citations.

The final pipeline can be wrapped as an API or integrated into a web interface. For example, developers might build a FastAPI endpoint or use Streamlit for a simple chat UI. By packaging enhanced_search_chain behind a lightweight server, organizations gain a production-grade assistant that scales from prototypes to full applications, all while maintaining dynamic access to the latest online information.

Combining LangChain’s modular design, Gemini 2.0 Flash’s generative powers, and Jina Search’s real-time lookup creates a versatile, citation-ready assistant. Teams can customize prompts, add more tools, or tweak pipeline settings to meet domain-specific needs. The result extends LLMs beyond static training data, letting AI-driven applications tap into current online resources and raise the overall reliability of automated answers.

Going forward, integrating additional data sources—such as vector databases or custom APIs—can broaden the assistant’s scope. Organizations might implement authentication layers, auditing, or rate-limiting for enterprise use. Since the core pipeline remains flexible, new tools plug in with minimal code changes, offering a scalable blueprint for building AI systems that balance generative models with live information retrieval.

Similar Posts