<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ashmi Banerjee on Medium]]></title>
        <description><![CDATA[Stories by Ashmi Banerjee on Medium]]></description>
        <link>https://medium.com/@ashmi_banerjee?source=rss-13be101669ab------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*zGSRCAH2OZfvS55_P9G-xQ.jpeg</url>
            <title>Stories by Ashmi Banerjee on Medium</title>
            <link>https://medium.com/@ashmi_banerjee?source=rss-13be101669ab------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 11:01:05 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ashmi_banerjee/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[[Tutorial] Building a Multi-Agent Travel Planner with Google ADK]]></title>
            <link>https://medium.com/google-developer-experts/tutorial-building-a-multi-agent-travel-planner-with-google-adk-4409ac4e47b5?source=rss-13be101669ab------2</link>
            <guid isPermaLink="false">https://medium.com/p/4409ac4e47b5</guid>
            <category><![CDATA[google-adk]]></category>
            <category><![CDATA[agentic-ai-architecture]]></category>
            <category><![CDATA[agentic-ai]]></category>
            <category><![CDATA[agentic-workflow]]></category>
            <category><![CDATA[multi-agent-systems]]></category>
            <dc:creator><![CDATA[Ashmi Banerjee]]></dc:creator>
            <pubDate>Mon, 29 Jun 2026 13:08:25 GMT</pubDate>
            <atom:updated>2026-07-02T01:43:13.572Z</atom:updated>
            <content:encoded><![CDATA[<p>Most travel apps optimize for popularity and convenience, often directing users toward overcrowded, carbon-intensive destinations.</p><p>In this tutorial, we’ll learn how to <strong>build a multi-agent travel planner with Google ADK </strong>that instead <em>recommends highly relevant, more sustainable alternatives as inviting possibilities rather than prescriptive choices.</em></p><p>The implementation is based on <a href="https://arxiv.org/abs/2604.14223"><strong>TRACE</strong>, our research prototype accepted at <strong>SIGIR 2026</strong> (Banerjee et al.)</a>, bridging research with a production-ready engineering workflow.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*9ZDWpAHmIGHekx3oEx7mGQ.png" /><figcaption>TRACE architecture overview</figcaption></figure><p>The backend is built using <strong>Google’s Agent Development Kit (ADK)</strong> and powered by <strong>Gemini 2.5 Flash</strong>, with <strong>Chainlit used for the frontend</strong>.</p><p>In this article, we focus exclusively on the backend architecture, as it is the most interesting component. <br>Below, we break down the core multi-agent design and provide a lightweight, runnable prototype to illustrate the concept.</p><blockquote><strong>Note:</strong> The original GCP demo of the paper has moved to Hugging Face Spaces due to a Chainlit security update. See the end of this post for details.</blockquote><h3>The Architecture</h3><p>To influence user choices without restricting autonomy, TRACE generates two parallel recommendations and evaluates the trade-offs:</p><ol><li><strong>A Baseline Recommendation:</strong> Optimizes solely for user relevance.</li><li><strong>A Sustainability-Aware Recommendation:</strong> Balanced for carbon footprint and crowd density.</li></ol><p>A <strong>Counterfactual Explanation (CFE) Agent</strong> then compares the two options. It highlights the sustainable choice as the primary recommendation while keeping the popular baseline visible as an alternative. This transparent comparison serves as a gentle cognitive nudge.</p><pre>                  ┌──▶ Baseline RecSys ──┐<br>                  │   (Relevance Only) <br>                  │                      │<br>User Query ───────┤                      ├──▶ CFE Agent (Compare &amp; Nudge)<br>                  │                      │                 <br>                  └──▶ Context RecSys ───┘ <br>                       (Sustainable) </pre><h3>The Production Pipeline</h3><p>This orchestration is handled efficiently in the TRACE backend via parallel agent execution. Here is a simplified view of the pipeline orchestration (backend/adk/assembly/pipeline.py):</p><pre>import asyncio<br>from google.adk.agents import ParallelAgent, LlmAgent, SequentialAgent<br><br>async def create_pipeline():<br>    # Load agents in parallel<br>    ic_agent, rec_baseline_agent, ca_recsys_agent = await asyncio.gather(<br>        get_ic_agent(),<br>        get_recsys_agent(has_context=False),   # Baseline: Relevance only<br>        get_recsys_agent(has_context=True),    # Context-aware: Sustainable<br>    )<br>    <br>    # Sequence intent classification followed by context-aware recommendation<br>    sequential = SequentialAgent(<br>        name=&quot;SequentialPipeline&quot;,<br>        sub_agents=[ic_agent, ca_recsys_agent]<br>    )<br>    <br>    # Run the sustainable recommendation path and the baseline path concurrently<br>    parallel = ParallelAgent(<br>        name=&quot;ParallelRecAgents&quot;,<br>        sub_agents=[sequential, rec_baseline_agent]<br>    )<br>    <br>    # Consolidate both recommendations into a single counterfactual output<br>    cfe = LlmAgent(<br>        name=&quot;CFEAgent&quot;,<br>        model=&quot;gemini-2.5-flash&quot;,<br>        instruction=ENV.get_template(&quot;cfe_combination.jinja2&quot;).render(),<br>        output_schema=CFEOutput<br>    )<br>    <br>    return SequentialAgent(name=&quot;CRSPipeline&quot;, sub_agents=[parallel, cfe])</pre><p>The design relies entirely on three foundational ADK concepts: SequentialAgent, ParallelAgent, and structured data validation via output_schema (powered by Pydantic to ensure strict JSON schemas).</p><h3>Building a Lightweight Prototype</h3><p>To demonstrate the structural logic of TRACE, we can build a minimal, local version that runs with a standard Gemini API key (without requiring external databases such as Firestore).</p><h4>Project Setup</h4><p>Acquire a free API key from Google AI Studio, then initialize your local environment:</p><pre>mkdir trace-mini &amp;&amp; cd trace-mini<br>python -m venv .venv &amp;&amp; source .venv/bin/activate   # On Windows: .venv\Scripts\activate<br>pip install google-adk<br>mkdir trace_agent<br>echo &quot;from . import agent&quot; &gt; trace_agent/__init__.py<br>printf &quot;GOOGLE_GENAI_USE_VERTEXAI=FALSE\nGOOGLE_API_KEY=your_key_here\n&quot; &gt; .env</pre><h4>The Agent Implementation</h4><p>Save the following agent configuration as trace_agent/agent.py:</p><pre>from __future__ import annotations<br>from typing import Optional<br>from pydantic import BaseModel, Field<br>from google.adk.agents import Agent, ParallelAgent, SequentialAgent<br><br>MODEL: str = &quot;gemini-2.5-flash&quot;<br><br>class Recommendation(BaseModel):<br>    recommendation: str = Field(..., description=&quot;List of cities&quot;)<br>    explanation: str = Field(..., description=&quot;A single-sentence justification for the choice.&quot;)<br><br>class CFEOutput(BaseModel):<br>    recommendation_shown: str<br>    is_recommendation_sustainable: bool<br>    explanation_shown: str<br>    alternative_recommendation: Optional[str] = None<br># Agent 1: Relevance-focused baseline<br>baseline = Agent(<br>    model=MODEL, <br>    name=&quot;baseline_recsys&quot;, <br>    output_schema=Recommendation, <br>    output_key=&quot;baseline_rec&quot;,<br>    description=&quot;Recommends a city strictly matching user preferences.&quot;,<br>    instruction=f&quot;Recommend the best city for the user&#39;s request, ignoring sustainability. Available options: {CITIES}&quot;,<br>) # this is a sample instruction which can be updated on a need basis<br><br># Agent 2: Sustainability-focused recommender<br>green = Agent(<br>    model=MODEL, <br>    name=&quot;context_recsys&quot;, <br>    output_schema=Recommendation, <br>    output_key=&quot;green_rec&quot;,<br>    description=&quot;Recommends a city balancing relevance and environmental/social impact.&quot;,<br>    instruction=f&quot;Recommend the best city that fits the user&#39;s intent but is historically less crowded and lower-carbon. Available options: {CITIES}&quot;,<br>) # this is a sample instruction which can be updated on a need basis<br><br># Agent 3: The CFE synthesis agent<br>cfe = Agent(<br>    model=MODEL, <br>    name=&quot;CFEAgent&quot;, <br>    output_schema=CFEOutput,<br>    description=&quot;Compares both recommendations and formulates a gentle behavioral nudge.&quot;,<br>    instruction=(<br>        &quot;Relevance-only recommendation: {baseline_rec}\nSustainable recommendation: {green_rec}\n\n&quot;<br>        &quot;Present the sustainable option (recommendation_shown) if it meets the user&#39;s core intent. &quot;<br>        &quot;Explain it warmly as a comparative alternative: &#39;what if you chose X instead of Y&#39;-highlighting &quot;<br>        &quot;a similar cultural experience with fewer crowds and lower carbon impact. &quot;<br>        &quot;Populate alternative_recommendation with the baseline city. Do not lecture the user.&quot;<br>    ),<br>)# this is a sample instruction which can be updated on a need basis; for exact prompts used in the paper, check resources<br><br># Root pipeline orchestration<br>root_agent = SequentialAgent(<br>    name=&quot;CRSPipeline&quot;,<br>    sub_agents=[<br>        ParallelAgent(name=&quot;ParallelRecAgents&quot;, sub_agents=[baseline, green]), <br>        cfe<br>    ],<br>)</pre><h4>Running the Agent</h4><p>Launch the native ADK web playground to interact with your agents:</p><pre>adk web</pre><p>In the UI, select trace_agent and try entering a query like:</p><blockquote>“A lively canal city with rich art, historical architecture, and great food — I am thinking Amsterdam.”</blockquote><p>You will see both recommenders execute concurrently. The CFE agent will evaluate both and gently suggest <em>Ghent</em> or <em>Bruges</em> as the greener “what-if” alternative, keeping <em>Amsterdam</em> transparently available as the baseline option you originally weighed it against.</p><h4>Why This Paradigm Succeeds</h4><p>Forcing sustainable choices upon users by omitting popular options entirely is a paternalistic approach that often prompts users to look elsewhere. TRACE succeeds by validating the user’s initial preference and offering a direct, side-by-side comparison. By reducing the friction of finding greener alternatives, behavioral science and AI agents work together to make the sustainable choice the easy choice.</p><h3>📢 Live Sandbox Migration Note</h3><p><em>The original GCP-hosted demo referenced in the paper is currently offline due to a vulnerability in the Chainlit UI framework. To maintain zero-trust security, the entire system has been migrated to </em><a href="https://huggingface.co/spaces/ashmib/trace-crs-chatbot"><em>Hugging Face Spaces</em></a><em>.</em></p><p><em>Out of the box, the space utilizes </em><em>Gemma-3-27b to serve zero-shot, lightweight agent decisions. To experience the full, production-grade ADK parallel agent pipeline as detailed in the paper, you can securely input your own </em><strong><em>Gemini API Key</em></strong><em> directly into the UI. Read the full </em><a href="https://huggingface.co/spaces/ashmib/trace-crs-chatbot/blob/main/README.md"><em>Hugging Face README</em></a><em> for deeper implementation details.</em></p><h3>Resources</h3><ul><li><strong>Research Paper:</strong> <a href="https://arxiv.org/pdf/2604.14223">TRACE (SIGIR ’26)</a></li><li><strong>Interactive Sandbox:</strong> <a href="https://huggingface.co/spaces/ashmib/trace-crs-chatbot">TRACE Chatbot on Hugging Face Spaces</a></li><li><strong>Code Repository:</strong> <a href="https://github.com/ashmibanerjee/trace-crs-chatbot">Source code</a></li><li><strong>Project Website: </strong><a href="https://ashmibanerjee.github.io/trace-chatbot/">https://ashmibanerjee.github.io/trace-chatbot</a></li></ul><p>✨ <em>If you like the article, please </em><a href="https://medium.com/@ashmi_banerjee"><strong><em>subscribe</em></strong></a><em> to my latest ones.<br>To get in touch, contact me on </em><a href="https://www.linkedin.com/in/ashmi-banerjee/"><strong><em>LinkedIn</em></strong></a><em> or via </em><a href="https://ashmibanerjee.com/"><strong><em>ashmibanerjee.com</em></strong></a><em>.</em></p><p><strong><em>GenAI usage disclosure:</em></strong><em> GenAI models were used to check for grammar inconsistencies in the blog and to refine the text for greater clarity. The authors accept full responsibility for the content presented in this blog.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4409ac4e47b5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/google-developer-experts/tutorial-building-a-multi-agent-travel-planner-with-google-adk-4409ac4e47b5">[Tutorial] Building a Multi-Agent Travel Planner with Google ADK</a> was originally published in <a href="https://medium.com/google-developer-experts">Google Developer Experts</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[Tutorial] Running Google’s Gemma-4b locally with Google ADK and dual A40 GPUs]]></title>
            <link>https://medium.com/google-developer-experts/tutorial-running-googles-gemma-4b-locally-with-google-adk-and-dual-a40-gpus-5a421fbc6ef9?source=rss-13be101669ab------2</link>
            <guid isPermaLink="false">https://medium.com/p/5a421fbc6ef9</guid>
            <category><![CDATA[agentic-ai]]></category>
            <category><![CDATA[hugging-face]]></category>
            <category><![CDATA[google-adk]]></category>
            <category><![CDATA[vllm]]></category>
            <dc:creator><![CDATA[Ashmi Banerjee]]></dc:creator>
            <pubDate>Sat, 21 Mar 2026 11:06:17 GMT</pubDate>
            <atom:updated>2026-03-23T00:10:30.777Z</atom:updated>
            <content:encoded><![CDATA[<p>The <a href="https://google.github.io/adk-docs/">Google Agent Development Kit (ADK)</a> is a modular framework for building agentic workflows. While it integrates deeply with Gemini, its model-agnostic design allows you to plug in local models served via <strong>vLLM</strong>.</p><p>Getting the <a href="https://models.litellm.ai/"><strong>LiteLLM configuration</strong> </a>right for this setup, however, can be surprisingly tricky. To save you the trial and error, this guide provides a battle-tested approach to connecting local inference with Google’s agent tooling.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5jnaK-dmTqIHuzPveUysLQ.png" /><figcaption><em>The complete local agent stack — harnessing Gemma-3–4b and Google ADK on NVIDIA A40 hardware.</em></figcaption></figure><blockquote>In this tutorial, you’ll learn how to serve <strong>Gemma 3 (4B)</strong> <strong>with vLLM on high-performance GPUs (e.g., single or dual NVIDIA A40s)</strong> and integrate it with the Google ADK using a custom LiteLLM configuration.</blockquote><p>I’ve also used this approach in a more complex project, whose <a href="https://github.com/ashmibanerjee/collab-rec"><strong>source code can be found here</strong></a>; what we cover in this tutorial is just the prototype version.</p><h3>🛠️ Phase 0: Installation &amp; Environment Setup (using uv)</h3><p>To handle the heavy-duty dependencies for vLLM and Google ADK efficiently, we’ll use uv, the extremely fast Python package manager and resolver.</p><h4>1. Install uv and create the Environment</h4><p>First, ensure uv is installed and create a synchronized virtual environment from the terminal.</p><pre># Install uv if you haven&#39;t already<br>curl -LsSf [https://astral.sh/uv/install.sh](https://astral.sh/uv/install.sh) | sh<br><br># Create a new project environment<br>uv venv --python 3.11<br>source .venv/bin/activate</pre><h4>2. Install Dependencies</h4><p>We need the Google ADK for the agent logic and vllm for the inference server.</p><pre># Install the core stack<br>uv pip install google-adk vllm huggingface_hub pydantic</pre><h3>🏗️ Phase 1: Serving Gemma 3 with vLLM</h3><p>Gemma 3 4B is a highly capable multimodal model. To use it for agentic tasks (such as tool calling), we need to serve it via an OpenAI-compatible API and specify the tool parser.</p><h4>1. Environment Preparation</h4><p>Before launching the server, configure your environment to optimize for the A40s. On enterprise GPUs like the A40, standard P2P/IB communications can sometimes cause NCCL timeouts depending on your PCIe topology. We disable these for maximum stability.</p><pre>export VLLM_USE_V1=0 <br>export NCCL_P2P_DISABLE=1 <br>export NCCL_IB_DISABLE=1</pre><h4>2. Download the Model Weights</h4><p>As per the official <a href="https://huggingface.co/google/gemma-3-4b-it?local-app=vllm">Hugging Face documentation for Gemma 3</a>, you should use the huggingface-cli to fetch the weights.</p><blockquote>💡 Since Gemma 3 is a gated model, ensure you have accepted the license agreement on the model card and are authenticated in your terminal.</blockquote><h4>3. Launching the Inference Server</h4><p>We will use the downloaded weights. If you are running on two A40s, we set tensor-parallel-size to 2 to split the model across both GPUs.</p><blockquote>💡 To ensure the server keeps running even if your SSH connection breaks, we will use nohup to run the process in the background. However, of course this is optional.</blockquote><pre>nohup vllm serve &quot;google/gemma-3-4b-it&quot; \<br>--port 8000 \<br>--gpu-memory-utilization 0.90 \<br>--max-model-len 32768 \<br>--tensor-parallel-size 2 \<br>--trust-remote-code \<br>--enable-auto-tool-choice \<br>--tool-call-parser openai \<br>--distributed-executor-backend mp &gt; gemma-4b.log 2&gt;&amp;1</pre><p><em>Note: You can monitor the logs in real-time using </em><em>tail -f vllm.log.</em></p><h4>4. Verification</h4><p>To ensure everything is wired correctly, you can run a quick test curl from your terminal to the vLLM server independently of the ADK:</p><pre>curl http://localhost:8000/v1/chat/completions \<br>    -H &quot;Content-Type: application/json&quot; \<br>    -d &#39;{<br>        &quot;model&quot;: &quot;google/gemma-3-4b-it&quot;,<br>        &quot;messages&quot;: [{&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: &quot;What is the best city for tech in Germany?&quot;}],<br>        &quot;temperature&quot;: 0.7<br>    }&#39;</pre><h3>🐍 Phase 2: Connecting Google ADK to Local vLLM</h3><p>The Google ADK uses LiteLlm as its bridge to various backends. To force the agent to use your local vLLM instance instead of a cloud API, we use a helper function to configure the model parameters, including <strong>Guided JSON</strong> for structured outputs.</p><h4>1. The Model Configuration Function</h4><p>Create a file named agent.py.</p><blockquote>💡 Note the use of extra_body to enforce schema adherence via guided_json and is completely optional if you’re just building a prototype.</blockquote><pre>import asyncio<br>from google.adk.agents import LlmAgent<br>from google.adk.models.lite_llm import LiteLlm<br>from pydantic import BaseModel<br><br># 1. Define the expected response structure for Guided Decoding<br>class AgentResponse(BaseModel):<br>    answer: str<br>    confidence: float<br><br># 2. Custom LiteLLM configuration function<br>def _litellm(<br>    model: str,<br>    api_base: str,<br>    max_tokens: int,<br>    temperature: float = 0.7<br>) -&gt; LiteLlm:<br>    return LiteLlm(<br>        model=model,<br>        api_base=api_base,<br>        custom_llm_provider=&quot;openai&quot;,<br>        api_key=&quot;not-needed&quot;, # vLLM doesn&#39;t require a key by default<br>        max_tokens=max_tokens,<br>        temperature=temperature,<br>        top_p=0.95,<br>        extra_body={<br>            &quot;guided_json&quot;: AgentResponse.model_json_schema(),<br>        },<br>    )<br><br># 3. Initialize the Model<br># Point this to your local vLLM instance<br>local_model = _litellm(<br>    model=&quot;google/gemma-3-4b-it&quot;,<br>    api_base=&quot;http://localhost:8000/v1&quot;,<br>    max_tokens=8192,<br>)<br><br># 4. Initialize the Agent<br># Note: Use &#39;instruction&#39; (singular) for the system prompt<br>root_agent = LlmAgent(<br>    name=&quot;GemmaLocalAgent&quot;,<br>    model=local_model,<br>    instruction=&quot;You are travel expert. Answer the user&#39;s query.&quot; # this is a placeholder instruction<br>)<br><br># 5. Simple Runner for testing<br>async def main():<br>    print(&quot;Querying local Gemma 3...&quot;)<br>    # ADK uses async streaming or simple run<br>    async for event in root_agent.run_async(&quot;What is the capital of Germany?&quot;):<br>        if event.is_final_response():<br>             print(f&quot;\nResponse: {event.content.parts[0].text}&quot;)<br><br>if __name__ == &quot;__main__&quot;:<br>    asyncio.run(main())</pre><h4>Troubleshooting Tips</h4><ul><li><strong>OOM (Out of Memory):</strong> If you are running on a single A40, set --tensor-parallel-size 1. If the model still doesn&#39;t fit, reduce --max-model-len.</li><li><strong>Tool Calling Issues:</strong> If the agent fails to trigger tools, check the vLLM logs. Gemma 3 requires specific prompt formatting; ensure your --tool-call-parser matches the model&#39;s expected format.</li><li><strong>Port Conflicts:</strong> If port 8000 is taken, use --port 8080 in the vLLM command and update the api_base in your Python code accordingly.</li></ul><p><strong>Ready to build the API layer?</strong> Head over to my <a href="https://medium.com/google-developer-experts/tutorial-from-agents-to-apis-building-production-ready-ai-systems-with-google-adk-fastapi-48a72dd03df6">FastAPI + ADK deep dive</a> to see how to connect everything we built today to a production-grade backend. <br><em>Although this tutorial focuses on </em><strong><em>Gemma-4b</em></strong><em>, you can use any open-sourced HuggingFace model </em><a href="https://models.litellm.ai/"><em>supported by LiteLLM</em></a><em>. </em><br>I’ve also used this approach in a more complex project, whose source code can be found <a href="https://github.com/ashmibanerjee/collab-rec/tree/main">here</a>; what we cover in this tutorial is just the prototype version.</p><p>✨ <em>If you like the article, please </em><a href="https://medium.com/@ashmi_banerjee"><strong><em>subscribe</em></strong></a><em> to my latest ones.<br>To get in touch, contact me on </em><a href="https://www.linkedin.com/in/ashmi-banerjee/"><strong><em>LinkedIn</em></strong></a><em> or via </em><a href="https://ashmibanerjee.com/"><strong><em>ashmibanerjee.com</em></strong></a><em>.</em></p><p><strong><em>GenAI usage disclosure:</em></strong><em> GenAI models were used to check for grammar inconsistencies in the blog and to refine the text for greater clarity. The authors accept full responsibility for the content presented in this blog.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5a421fbc6ef9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/google-developer-experts/tutorial-running-googles-gemma-4b-locally-with-google-adk-and-dual-a40-gpus-5a421fbc6ef9">[Tutorial] Running Google’s Gemma-4b locally with Google ADK and dual A40 GPUs</a> was originally published in <a href="https://medium.com/google-developer-experts">Google Developer Experts</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[Tutorial] From Agents to APIs: Building Production-Ready AI Systems with Google ADK & FastAPI]]></title>
            <link>https://medium.com/google-developer-experts/tutorial-from-agents-to-apis-building-production-ready-ai-systems-with-google-adk-fastapi-48a72dd03df6?source=rss-13be101669ab------2</link>
            <guid isPermaLink="false">https://medium.com/p/48a72dd03df6</guid>
            <category><![CDATA[ai-agent]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[google-adk]]></category>
            <category><![CDATA[fastapi]]></category>
            <dc:creator><![CDATA[Ashmi Banerjee]]></dc:creator>
            <pubDate>Thu, 19 Mar 2026 01:44:02 GMT</pubDate>
            <atom:updated>2026-03-21T10:58:41.161Z</atom:updated>
            <content:encoded><![CDATA[<p>Building production-ready AI systems requires a shift from simple prompting to structured orchestration. Using the <strong>Google Agent Development Kit (ADK)</strong> and <strong>Python</strong>, you can build robust agents that handle complex reasoning and state management.</p><p>In this post, I’ll walk you through the core concepts of the ADK: from defining your first agent to exposing it as a production-ready API using FastAPI.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zxvZPIK0woFdfCbfxJyC_w.png" /><figcaption>Our tech-stack for this tutorial.</figcaption></figure><h3>What is an AI Agent?</h3><p>Unlike a standard LLM, which simply generates text based on a prompt, an AI agent includes <strong>reasoning and decision-making logic</strong>. It can interpret user requests, decide which actions to take, and interact with tools or other components to complete a task.<br>In practice, an agent can:</p><ul><li><strong>Reason:</strong> Interpret the user’s intent and understand constraints in the request.</li><li><strong>Act:</strong> Call external tools, APIs, or functions to retrieve information or perform operations.</li><li><strong>Collaborate:</strong> Work together with other specialized agents to solve more complex tasks.</li></ul><p>This makes agents particularly useful for building <strong>structured, multi-step AI applications</strong>.</p><h3>Our Use-Case</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/639/1*fBB3JB00tyEwA3qwEP4I1A.png" /><figcaption>Our use-case for this example project.</figcaption></figure><p>In this example, we build a <strong>travel planning agent</strong> that can respond to user queries about trips and destinations. The agent processes the user’s request, generates relevant travel suggestions, and structures the response in a helpful way.</p><p>To make the system accessible to other applications, we expose the agent via a <strong>REST API built with FastAPI</strong>, enabling integration with web applications, chat interfaces, and other services.</p><h3>Step 0: Setting Up Your Environment</h3><p>To follow along, you’ll need Python 3.10+ and a package manager like pipx or uv (which is significantly faster than standard pip).</p><p>If you don&#39;t have uv yet, you can install it with a simple curl command:</p><pre># Install uv<br>curl -LsSf [https://astral.sh/uv/install.sh](https://astral.sh/uv/install.sh) | sh</pre><h4>Project Structure</h4><p>For this project, we expect the following project structure:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/797/1*va2p346FsWI9Q2w8WkBaMQ.png" /><figcaption>Expected project structure for this project.</figcaption></figure><h4>Initializing Your Project</h4><p>Once uv is installed, from the root directory of your repository, install the relevant libraries for both the agent &amp; the API layers, and activate the virtual environment:</p><pre># Install the Google ADK, FastAPI, and Uvicorn (the ASGI server)<br>uv add google-adk fastapi uvicorn<br><br># Sync dependencies and activate your environment<br>uv sync<br>source .venv/bin/activate</pre><p>The next step is to initialize your agent through the ADK CLI, which creates the necessary directory structure for your agent’s logic.</p><pre># Create a new agent project<br>adk create my_travel_planner</pre><h4>The .env Configuration</h4><p>Handling API keys securely is paramount. You should create a .env file inside the my_travel_planner directory (or fill up the existing .env template file created from the above step) to manage credentials.</p><blockquote><strong>It is important that you never commit this file to version control. Make sure you add this to the </strong><strong>.gitignore file (if it&#39;s not already there).</strong></blockquote><ul><li><strong>Option 1 (Google AI Studio):</strong> If you are using a standard Gemini API key from <a href="https://aistudio.google.com/prompts/new_chat">Google AI Studio</a>, set GOOGLE_GENAI_USE_VERTEXAI=False and add your key.</li><li><strong>Option 2 (Vertex AI):</strong> If you are working within Google Cloud, set GOOGLE_GENAI_USE_VERTEXAI=True and point to your application credentials. You can follow this <a href="https://developers.google.com/workspace/guides/create-credentials">link</a> for the exact documentation.</li></ul><pre># .env template<br>GOOGLE_API_KEY=your_gemini_api_key_here<br>GOOGLE_GENAI_USE_VERTEXAI=False<br># Optional for Vertex AI:<br># GOOGLE_APPLICATION_CREDENTIALS=path/to/your/credentials.json</pre><h3>Step 1: Defining the Agent</h3><p>The “Brain” of your system is defined in my_travel_planner/agent.py. Here, we give the agent a name, a specific model (like gemini-2.5-flash), and clear instructions on its persona.</p><pre>from google.adk.agents import Agent<br><br><br>def get_root_agent():<br>    # The Root Agent (The &quot;Brain&quot;)<br>    travel_planner = Agent(<br>        name=&quot;travel_planner&quot;,<br>        model=&quot;gemini-2.5-flash&quot;,<br>        description=&quot;A comprehensive travel planning assistant.&quot;,<br>        instruction=&quot;&quot;&quot;You are a world-class travel planner.<br>        Recommend places to visit based on the user&#39;s query.&quot;&quot;&quot;,<br>    )<br>    return travel_planner<br><br><br>root_agent = get_root_agent()</pre><h3>Step 2: Managing Memory with Sessions and Runners</h3><p>Stateless LLMs forget context as soon as the request ends. For production systems, agents need <strong>memory</strong>. ADK manages this through <strong>Sessions</strong> and the <strong>Runner</strong>.</p><ul><li><strong>Sessions:</strong> Store conversation history, user info, and intermediate agent states.</li><li><strong>Runner:</strong> Orchestrates the full interaction — receiving input, passing it to the brain, and maintaining the state.</li></ul><p>Here is how you might execute that pipeline asynchronously. In your repository’s root, create run.py as follows:</p><pre>from google.adk.sessions import InMemorySessionService<br>from google.adk.agents.llm_agent import Agent<br>from google.adk.runners import Runner<br>from google.genai import types  # For creating message Content/Parts<br>import asyncio<br>from pathlib import Path<br>from dotenv import load_dotenv<br>load_dotenv(Path(__file__).parent / &quot;my_travel_planner&quot; / &quot;.env&quot;)  # Load environment variables from the .env file<br>from my_travel_planner.agent import get_root_agent<br><br>APP_NAME = &quot;travel-planner_app&quot;<br>USER_ID = &quot;user_1&quot;<br>SESSION_ID = &quot;session_001&quot;<br><br><br>async def setup_session_and_runner(root_agent: Agent = None, session_id: str = SESSION_ID):<br>    # Setup Runner for execution<br>    session_service = InMemorySessionService()<br>    session = await session_service.create_session(app_name=APP_NAME, user_id=USER_ID, session_id=session_id)<br>    runner = Runner(agent=root_agent, app_name=APP_NAME, session_service=session_service)<br>    return session, runner<br><br><br>async def call_agent_async(query: str, root_agent: Agent = None, session_id: str = SESSION_ID) -&gt; str:<br>    content = types.Content(role=&#39;user&#39;, parts=[types.Part(text=query)])<br>    session, runner = await setup_session_and_runner(root_agent=root_agent, session_id=session_id)<br>    events = runner.run_async(user_id=USER_ID, session_id=session_id, new_message=content)<br>    final_response_text = &quot;No response received.&quot;<br>    async for event in events:<br>        # Key Concept: is_final_response() marks the concluding message for the turn.<br>        if event.is_final_response():<br>            if event.content and event.content.parts:<br>                final_response_text = event.content.parts[0].text<br>            elif event.actions and event.actions.escalate:<br>                final_response_text = f&quot;Agent escalated: {event.error_message or &#39;No specific message.&#39;}&quot;<br>            break<br><br>    print(f&quot;&lt;&lt;&lt; Agent Response: {final_response_text}&quot;)<br>    return final_response_text<br><br><br>async def run_agent_pipeline(query: str) -&gt; str:<br>    root_agent = get_root_agent()<br>    return await call_agent_async(query=query, root_agent=root_agent, session_id=SESSION_ID)<br><br><br>if __name__ == &quot;__main__&quot;:<br>    user_query = (&quot;I&#39;m planning a trip to Paris in the spring. What are some must-see attractions and local events &quot;<br>                  &quot;during that time?&quot;)<br>    asyncio.run(run_agent_pipeline(query=user_query))</pre><h3>Step 3: Turning Your Agent into an API</h3><p>To make your agent useful in the real world, we need to expose it as an API. <strong>FastAPI</strong> is the modern standard for this because it’s high-performance, uses type hints for validation, and generates automatic Swagger documentation.</p><p>By wrapping our ADK pipeline in a POST endpoint, we can connect our agent to frontends, mobile apps, or other microservices.</p><p>In the repository root directory, we create api.py, where we define a simple /ask endpoint:</p><pre>from fastapi import FastAPI<br>from pydantic import BaseModel<br>from pathlib import Path<br>from dotenv import load_dotenv<br><br>load_dotenv(Path(__file__).parent / &quot;my_travel_planner&quot; / &quot;.env&quot;)<br><br>from run import run_agent_pipeline<br><br>app = FastAPI(title=&quot;Travel Planner AI&quot;, description=&quot;Powered by Google ADK + Gemini&quot;)<br><br><br>class QueryRequest(BaseModel):<br>    query: str<br><br><br>class QueryResponse(BaseModel):<br>    query: str<br>    response: str<br><br><br>@app.post(&quot;/ask&quot;, response_model=QueryResponse)<br>async def ask_agent(request: QueryRequest):<br>    response = await run_agent_pipeline(query=request.query)<br>    return QueryResponse(query=request.query, response=response)</pre><p>Run your server with: uv run uvicorn api:app --reload <br>The server starts at <a href="http://localhost:8000.">http://localhost:8000</a> and Interactive docs (Swagger UI): <a href="http://localhost:8000/docs">http://localhost:8000/docs</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/767/1*IrvzNxVXFUU8ow6h2fyUCw.png" /><figcaption><a href="http://localhost:8000/docs">http://localhost:8000/docs</a> should render something like this!</figcaption></figure><h3>Final Thoughts: The Road to Production</h3><p>A local script is a great start, but production readiness involves a few more steps:</p><ol><li><strong>Test:</strong> Write unit tests to verify that the agent logic handles edge cases.</li><li><strong>Containerize:</strong> Use Docker to ensure that “it works on my machine” translates to “it works on the server.”</li><li><strong>Deploy:</strong> Ship your container to a cloud provider like Google Cloud (Cloud Run) or AWS.</li></ol><p><strong>Ready to build?</strong> Grab the code from the <a href="https://github.com/ashmibanerjee/google-adk-workshop">GitHub repo</a> and start building your own specialized agentic workflows today!</p><p>✨ <em>We thank the </em><strong><em>Google AI/ML Developer Programs</em></strong><em> team for supporting us with Google Cloud Credits.</em></p><p>✨ <em>The source code on GitHub can be accessed </em><a href="https://github.com/ashmibanerjee/google-adk-workshop"><strong><em>here</em></strong></a><strong><em>.</em></strong></p><p>✨ <em>If you like the article, please </em><a href="https://medium.com/@ashmi_banerjee"><strong><em>subscribe</em></strong></a><em> to my latest ones.<br>To get in touch, contact me on </em><a href="https://www.linkedin.com/in/ashmi-banerjee/"><strong><em>LinkedIn</em></strong></a><em> or via </em><a href="https://ashmibanerjee.com/"><strong><em>ashmibanerjee.com</em></strong></a><em>.</em></p><p><strong><em>GenAI usage disclosure:</em></strong><em> GenAI models were used to check for grammar inconsistencies in the blog and to refine the text for greater clarity. The authors accept full responsibility for the content presented in this blog.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=48a72dd03df6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/google-developer-experts/tutorial-from-agents-to-apis-building-production-ready-ai-systems-with-google-adk-fastapi-48a72dd03df6">[Tutorial] From Agents to APIs: Building Production-Ready AI Systems with Google ADK &amp; FastAPI</a> was originally published in <a href="https://medium.com/google-developer-experts">Google Developer Experts</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[One Small Change That Makes OpenAI Work Perfectly in ADK Parallel Pipelines]]></title>
            <link>https://medium.com/google-developer-experts/one-small-change-that-makes-openai-work-perfectly-in-adk-parallel-pipelines-c37cd2858396?source=rss-13be101669ab------2</link>
            <guid isPermaLink="false">https://medium.com/p/c37cd2858396</guid>
            <category><![CDATA[ai-agent]]></category>
            <category><![CDATA[llm-agent]]></category>
            <category><![CDATA[google-adk]]></category>
            <category><![CDATA[litellm]]></category>
            <dc:creator><![CDATA[Ashmi Banerjee]]></dc:creator>
            <pubDate>Sat, 28 Feb 2026 11:46:05 GMT</pubDate>
            <atom:updated>2026-03-03T00:17:35.784Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*5eE1EE3oCTynjuMkbfgvUQ.png" /><figcaption>ADK `ParallelAgent` behaves differently across models: Google models handle sub-agents cleanly, while non-Google models, when invoked through LiteLLM, risk interleaved messages and broken outputs. Base image from A<a href="https://google.github.io/adk-docs/agents/workflow-agents/parallel-agents/#independent-execution-and-state-management">DK Parallel Agents</a> documentation.</figcaption></figure><p>If you’re following Google ADK’s <a href="https://google.github.io/adk-docs/agents/workflow-agents/parallel-agents/">ParallelAgent documentation</a> to build a multi-agent system with non-Google models like OpenAI (via LiteLLM or custom adapters), be warned: the standard approach can lead to serious headaches.</p><p>Conversation histories often get <strong>interleaved</strong>, message ordering can break, random API errors can occur, and subtle <strong>race conditions</strong> can surface during parallel execution. <br>🔺 <strong>Even if the workflow runs perfectly with Google-native models, non-Google LLMs need careful handling to avoid unreliable results.</strong></p><p>In this article, we’ll explain why this happens, how ADK manages context internally, and why Google models handle parallelism seamlessly while OpenAI-based agents often fail.</p><p>Most importantly, we’ll show a clean, practical workaround using a custom GPTParallelAgent that isolates context for each sub-agent.</p><blockquote>You can copy and paste the implementation directly into your multi-agent pipeline. 😉</blockquote><h3>⚠️ The Root Problem: Shared Context in Parallel Execution</h3><p>In Google ADK, agents run using a shared ctx (context) object that stores the <strong>session state</strong>, <strong>conversation history</strong>, <strong>memory</strong>, <strong>tool bindings</strong>, and <strong>intermediate execution state</strong>. When multiple sub-agents run in parallel, ADK passes this same context instance to each of them.</p><p>With Gemini models, this works because Google’s infrastructure safely handles concurrent mutations. <strong><em>However, with OpenAI or LiteLLM models, it doesn’t.</em></strong></p><p>If multiple agents append to the same conversation history at the same time, <strong>they concurrently mutate shared state, leading to mixed turns, out-of-order messages, and, sometimes, malformed payload errors from OpenAI. These failures are often subtle and non-deterministic, making them difficult to debug.</strong></p><blockquote>The problem isn’t parallelism itself — it’s shared mutable state.</blockquote><h3>🛠️ The Fix: Context Isolation Per Agent</h3><p>The solution is simple:</p><p>Give every parallel agent its own copy of the context.</p><p>Instead of:</p><pre>agent.run_async(ctx)</pre><p>We do:</p><pre>local_ctx = copy.copy(ctx)<br>agent.run_async(local_ctx)</pre><blockquote>Each agent runs in an isolated “branch” of history. No interleaving. No corruption.</blockquote><h3>🙌 The Workaround: GPTParallelAgent</h3><p>Here is a production-ready drop-in replacement for ADK’s parallel behavior when using non-Google models.</p><p><strong>You can copy this directly.</strong></p><pre>import asyncio<br>import copy<br>from google.adk.agents import BaseAgent<br>from google.adk.events import Event<br><br><br>class GPTParallelAgent(BaseAgent):<br>    &quot;&quot;&quot;<br>    Runs sub-agents in parallel but isolates session history to prevent<br>    OpenAI/LiteLLM interleaving errors.<br>    &quot;&quot;&quot;<br><br>    def __init__(self, name: str, sub_agents: list, description: str = &quot;&quot;):<br>        super().__init__(name=name, description=description)<br>        self.sub_agents = sub_agents<br><br>    async def _run_async_impl(self, ctx):<br>        # Helper to run an agent and collect its events independently<br>        async def run_isolated(agent):<br>            # Clone the context so each agent has its own private history branch<br>            local_ctx = copy.copy(ctx)<br><br>            events = []<br>            async for event in agent.run_async(local_ctx):<br>                events.append(event)<br><br>            return events<br><br>        # Run all agents concurrently<br>        all_results = await asyncio.gather(<br>            *[run_isolated(agent) for agent in self.sub_agents]<br>        )<br><br>        # Flatten results and yield events upstream<br>        for agent_events in all_results:<br>            for event in agent_events:<br>                yield event</pre><h3>How to use this in your Project?</h3><p>This is super easy with the implementation above!</p><h4>✍️ <strong>Step 1: Define your sub-agents</strong></h4><pre>accommodation_agent = SomeLLMAgent(name=&quot;accommodation_selector&quot;)<br>activity_agent = SomeLLMAgent(name=&quot;activity_recommender&quot;)<br>transport_agent = SomeLLMAgent(name=&quot;transport_planner&quot;)</pre><h4>🤖<strong> Step 2: Wrap Them in </strong><strong>GPTParallelAgent</strong></h4><pre>parallel_agent = GPTParallelAgent(<br>    name=&quot;parallel_agent&quot;,<br>    sub_agents=[<br>        accommodation_agent,<br>        activity_agent,<br>        transport_agent,<br>    ],<br>    description=&quot;Runs agents in isolation&quot;<br>)</pre><p>You can now run them in parallel, even with non-Google models — saving valuable execution time and significantly improving overall efficiency. 🚀</p><p>✨ <em>If you like the article, please </em><a href="https://medium.com/@ashmi_banerjee"><strong><em>subscribe</em></strong></a><em> to my latest ones.<br>To get in touch, contact me on </em><a href="https://www.linkedin.com/in/ashmi-banerjee/"><strong><em>LinkedIn</em></strong></a><em> or via </em><a href="https://ashmibanerjee.com/"><strong><em>ashmibanerjee.com</em></strong></a><em>.</em></p><p><strong><em>GenAI usage disclosure:</em></strong><em> GenAI models were used to check for grammar inconsistencies in the blog and to refine the text for greater clarity. The authors accept full responsibility for the content presented in this blog.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c37cd2858396" width="1" height="1" alt=""><hr><p><a href="https://medium.com/google-developer-experts/one-small-change-that-makes-openai-work-perfectly-in-adk-parallel-pipelines-c37cd2858396">One Small Change That Makes OpenAI Work Perfectly in ADK Parallel Pipelines</a> was originally published in <a href="https://medium.com/google-developer-experts">Google Developer Experts</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Generating Personalized Travel Queries with Gemini 2.5 Flash on Vertex AI]]></title>
            <link>https://medium.com/@ashmi_banerjee/generating-personalized-travel-queries-with-gemini-2-5-flash-on-vertex-ai-30c2456c03d6?source=rss-13be101669ab------2</link>
            <guid isPermaLink="false">https://medium.com/p/30c2456c03d6</guid>
            <category><![CDATA[vertex-ai]]></category>
            <category><![CDATA[gemini]]></category>
            <category><![CDATA[genai]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <dc:creator><![CDATA[Ashmi Banerjee]]></dc:creator>
            <pubDate>Mon, 02 Jun 2025 18:23:55 GMT</pubDate>
            <atom:updated>2026-02-28T11:02:36.653Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*1WemtBfVjLld2_Rj.png" /><figcaption><a href="https://arxiv.org/pdf/2504.09277">SynthTRIPS workflow</a> for generating synthetic data using LLMs for personalized, sustainable city trips.</figcaption></figure><p>In the age of personalization, one-size-fits-all tourism recommendations just don’t cut it. That’s where <strong>SynthTRIPs</strong> comes in — a framework designed to generate rich, grounded, and personalized synthetic travel queries at scale using Google’s <strong>Gemini 2.5 Flash</strong> on <strong>Vertex AI</strong>.</p><p>This blog post builds on the upcoming paper:</p><blockquote><a href="https://ashmibanerjee.com/static/media/SIGIR25_banerjee.bc71f0118321ee2a247f.pdf"><strong>SynthTRIPs: A Knowledge-Grounded Framework for Benchmark Query Generation for Personalized Tourism Recommenders</strong></a><strong><br></strong><em>Ashmi Banerjee</em>, Adithi Satish, Fitri Nur Aisyah, Wolfgang Wörndl, and Yashar Deldjoo,<a href="https://doi.org/10.1145/3726302.3730321"> [To appear] In Proceedings of the 48th International ACM SIGIR Conference on Research and Development in Information Retrieval (SIGIR ’25)</a> July 2025.</blockquote><p>While the original study used <strong>Gemini 1.5 Pro</strong>, we’ll use the <strong>newer Gemini 2.5 Flash</strong> model in this walkthrough.</p><h3>What You’ll Learn</h3><p>In this post, you’ll learn how to replicate the SynthTRIPs pipeline — from persona definition to prompt generation — with step-by-step code examples.</p><p>The core idea is simple yet powerful:<br> ✅ Feed Gemini with <strong>contextual metadata</strong> about cities (budget, popularity, activities, etc.)<br> ✅ Supply it with <strong>user personas</strong> from the <a href="https://huggingface.co/datasets/proj-persona/PersonaHub">PersonaHub dataset</a><br> ✅ Prompt the model to generate <strong>natural, personalized travel queries</strong> grounded in real-world facts</p><p>Let’s dive in. Below is the code you’ll need to bring this to life.</p><h3>🧠 Step 1: Define Personas and Travel Filters</h3><p>We start by defining a user persona and a set of travel filters (budget, interests, sustainability constraints).</p><p>You can also get the personas from the <a href="https://huggingface.co/datasets/proj-persona/PersonaHub">PersonaHub dataset</a>. Here, we use a sample one.</p><pre>persona = &quot;A digital nomad couple passionate about eco-conscious travel and jazz clubs.&quot;<br><br>filters = {<br>    &quot;budget&quot;: &quot;medium&quot;,<br>    &quot;interests&quot;: [&quot;nightlife&quot;, &quot;arts&quot;],<br>    &quot;walkability&quot;: &quot;high&quot;,<br>    &quot;AQI&quot;: &quot;good&quot;,<br>    &quot;popularity&quot;: &quot;low&quot;<br>}</pre><h3>🗺️ Step 2: Ground the Prompt with a Knowledge-Base (KB) Context</h3><p>The KB is a curated collection of real city attributes — sourced from Wikivoyage, TripAdvisor, and Nomadlist. Cities matching the filters are retrieved to ground the LLM generation.</p><p>We use the <a href="https://huggingface.co/datasets/ashmib/SynthTRIPs/blob/main/kb/eu-cities-database.csv">KB from here</a> to filter out the cities to be used as input to the query. However, SynthTRIPs framework is storage-agnostic — you can implement the KB in <strong>any format</strong> that suits your tech stack:</p><p>✅ A <strong>relational database</strong> (e.g., SQLite, PostgreSQL) for structured SQL-style filtering</p><p>✅ A <strong>vector database</strong> (e.g., FAISS, Weaviate, ChromaDB) for embedding-based semantic retrieval</p><p>✅ Even a <strong>Knowledge Graph</strong> if you want to represent cities and attributes as interconnected entities</p><p>The key idea is to <strong>retrieve only the cities and attributes</strong> that meet the persona’s preferences and constraints — so the LLM generates realistic, context-aware queries without hallucinating details.</p><p>Here’s where we load and filter the KB before prompt generation using a relational database.</p><pre>def filter_kb(filters, kb_df):<br>    filtered = kb_df.copy()<br>    if &quot;budget&quot; in filters:<br>        filtered = filtered[filtered[&quot;budget&quot;] == filters[&quot;budget&quot;]]<br>    if &quot;walkability&quot; in filters:<br>        filtered = filtered[filtered[&quot;walkability&quot;] &gt;= 80]<br>    if &quot;AQI&quot; in filters:<br>        filtered = filtered[filtered[&quot;AQI&quot;] == filters[&quot;AQI&quot;]]<br>    if &quot;popularity&quot; in filters:<br>        filtered = filtered[filtered[&quot;popularity&quot;] == filters[&quot;popularity&quot;]]<br>    return filtered</pre><p>A sample response from the KB for the following filters {“popularity”: “High”, “budget”: “Low”, “interests”: “Outdoors &amp; Recreation”} would look like this:</p><pre>&quot;cities&quot;: [<br>        &quot;Zagreb&quot;,<br>        &quot;Tirana&quot;,<br>        &quot;Tbilisi&quot;,<br>        &quot;Sofia&quot;,<br>        &quot;Samara&quot;,<br>        &quot;Riga&quot;,<br>        &quot;Perm&quot;,<br>        &quot;Moscow&quot;,<br>        &quot;Minsk&quot;,<br>        &quot;Krasnodar&quot;,<br>        &quot;Kazan&quot;,<br>        &quot;Kaliningrad&quot;,<br>        &quot;Izmir&quot;,<br>        &quot;Baku&quot;,<br>        &quot;Ankara&quot;<br>    ],<br>    &quot;context&quot;: [<br>        &quot;Zagreb has High popularity.\nZagreb has Low budget.\nIn Zagreb you can see Maksimir Park &amp; Zagreb Zoo.\nIn Zagreb you can see Jarun.\nIn Zagreb you can see Ribnjak Park.\nIn Zagreb you can drink at Melin: A cheap bar brimming with young people and students. Offers indoors, a roofed terraced leveled seating and an open terrace facing Tkal\u010di\u0107eva street. Known for crowds of people often drinking even around the bar, in the park and playground in front of it..\nIn Zagreb you can drink at Portal: Next to Melin, offers same service, the Portal and Melin crowds often mix at the outdoors terraces..\nIn Zagreb you can do Snow Queen Trophy: Ladies Night Slalom : Zagreb is the only capital in the world that hosts a Ski World Cup race. The slalom event - known as the \&quot;Snow Queen Trophy\&quot; (Snje\u017ena kraljica) - usually takes place in January. It is the most expensive race on the women&#39;s world cup tour and an exceptionally popular sporting event in Croatia that attracts a crowd of around 25 thousand people. As of 2007 best skiers in men&#39;s competition are also expected to compete on Mount Sljeme that overlooks the city. If you fail to secure your ticket, you can watch the whole event on a giant screen in the Ban Jela\u010di\u0107 Square..\nIn Zagreb you can drink at Golf: Popular golf-themed cafe/bar..\n&quot;,<br>        &quot;Tirana has High popularity.\nTirana has Low budget.\nIn Tirana you can see Persqopi Archeological Site.\nIn Tirana you can do Grand Park along the Artificial Lake: People have picnics and have fun, and the natural beauty allows it because of the huge trees, open spaces, and mountains surrounding them. An artificial lake lies in the middle of the Park. It is popular with locals and tourists as a place to walk around and enjoy a lunch or coffee at one of the restaurants or caf\u00e9s. Over 120 types of plants. A popular swimming pool complex is located nearby..\nIn Tirana you can do Mount Dajti: 1,612m high (getting to the topmost point is not possible because of a restricted area with antennas and a military zone). Offers great views of Tirana. A very relaxing area, mount Dajti can be reached through an Austrian-built express cable car (one ride takes more than 15 minutes so is worth the cost even if you don&#39;t stay atop the mountain). To get to the bottom station of the cable car, take the bus line \&quot;Qender - Linze\&quot; that starts east beside the Skanderbeg place behind the mosque. In the bus tell the conductor \&quot;Teleferik\&quot; and he will show you the station to get out of the bus. From there it is a short 5-minute walk guided by direction signs (the cable car is closed on Tuesdays). In the top station of the cable car, there is a hotel (30\u20ac for a double room [2013]) and a tourist agency offering various activities like horse riding, trekking, excursions, sky sports, ski etc. The area has a limited number of hotels and restaurants, but it is great for taking a breath of fresh air, mountain sports, or to just get out of the city for a while. The &#39;&#39;&#39;Pellumba Cave&#39;&#39;&#39; in the area is worth a visit. Unfortunately, litter is becoming a problem on Mount Dajti. This detracts from the experience somewhat, but the beautiful scenery still makes the trip worthwhile. Don&#39;t forget to visit the spectacular &#39;&#39;&#39;Erzeni Canyon&#39;&#39;&#39; along the Erzeni River south of the park near the dam..\nIn Tirana you can do Rally Albania: The event features an international competition in the rough Albanian countryside roads..\nIn Tirana you can see Ndroqit Castle ruins.\nIn Tirana you can see Preze Castle.\n&quot;,<br>        &quot;Tbilisi has High popularity.\nTbilisi has Low budget.\nIn Tbilisi you can do Alexander&#39;s Garden: A part of Park April..\nIn Tbilisi you can see Gudauri.\nIn Tbilisi you can see Telavi.\nIn Tbilisi you can see Ananuri.\nIn Tbilisi you can do Botanical garden: National botanic garden of Georgia, founded 1845. Lying in the Tsavkisis-Tskali Gorge near Narikala Fortres,  is actually more of a large badly maintained park with scenic waterfall than a \&quot;botanical garden\&quot; as there is no scientific signposting available. Very little maintenance is apparent..\nIn Tbilisi you can do HeidelbergCement TbilisiMarathon: Every year at the end of October a big, international event officially recognized by the International Association of Athletic Federations takes place. There is a half marathon, a 10-km-race and kids&#39; runs. The start and finish is at Rustaveli Avenue. The racecourse guides the runners through this great city to its most interesting sights. The  event is professionally organized by the German company HeidelbergCement Caucasus and a German race director. HeidelbergCement Caucasus is one of the biggest investors in the region. The participation fee goes 100% to charity..\n&quot;,<br>        &quot;Sofia has High popularity.\nSofia has Low budget.\nIn Sofia you can do Sofia Bike Tour: Visit the greenest capital of Europe on a bike. Every day Free Bike Tour (12 April\u2013November) at 11AM &amp; 5PM. Escape from the city and join to the Bike Countryside Tour around Sofia. Start Point: National Theater \&quot;Ivan Vazov\&quot;.\nIn Sofia you can see Borisova Gradina (Boris Garden).\nIn Sofia you can see Yuzhen Park (South Park).\nIn Sofia you can do Playgrounds: If you&#39;re traveling with kids, they might love playgrounds. The \&quot;rich\&quot; one is in the park that holds Vasil Levski stadium (close to the corner with Orlov Most; there are actually two playgrounds close to this corner and you&#39;d look for the one more inside the park)..\nIn Sofia you can see Boyana Church.\nIn Sofia you can drink at Lodkite: In the huge \&quot;Borisova gradina\&quot; park near the stadium (just ask you way around) you&#39;ll find this open-air place, located in an old leisure-park parcour (you know, that type of small water-channels where kid can drive around in little gondolas). The boats have gone and the channels serve as improvised sitting places (basic tables made out of wood planks put between the two sides of the channel). There are also some tables on the court and lights decorating the trees illuminate the scene. The public sound system has experimental electronic music, ambiance, progressive rock, or whatever the DJs feel like. The later it gets, the more you&#39;ll find people sitting everywhere on the floor on the trees. On warm summer nights, this place is a must-be..\n&quot;,<br>        &quot;Samara has High popularity.\nSamara has Low budget.\nIn Samara you can see Krasnaya Glinka Ski complex.\nIn Samara you can see Viewpoint &#39;Helipad&#39;.\nIn Samara you can see Dacha Golovkin.\n&quot;,<br>        &quot;Riga has High popularity.\nRiga has Low budget.\nIn Riga you can do R\u012bga City Day: Takes place on 14-16 Aug.\nIn Riga you can do Latviabeerfest: Takes place annually at the end of May. The largest international beer festival in the Baltics..\n&quot;,<br>        &quot;Perm has High popularity.\nPerm has Low budget.\nIn Perm you can see Motovilikha open-air museum.\nIn Perm you can see Khohlovka Museum.\n&quot;,<br>        &quot;Moscow has High popularity.\nMoscow has Low budget.\nIn Moscow you can do Pokrovskie Baths: Has a famous steam room with horseradish-flavored steam. Wide selection of brooms from birch tree, tatarian maple, lime tree, and eucalyptus. Prices are cheaper than other banyas..\nIn Moscow you can do Rzehvskye Baths: 120 years old but recently renovated..\nIn Moscow you can do Kva-Kva Water Park: Water Park affiliated with Maxima Hotels (discounts for guests). There are 7 high trills (90-120 meters length) and a pleasant surprise for extremers \u2013 Tsunami trill \u2013 unique in Russia. There are also 4-line trills \u2013 Multislide and a special area for kids \u2013 a small tropical town with shallow pool. - Kva-Kva Lagoone offers hydromassage. There\u2019s also pure Russian bath, Finnish sauna, Turkish bath (hamam) and Kva-Kva SPA-salon. - Night discos take place every weekend, with free admission for Maxima Hotel guests..\n&quot;,<br>        &quot;Minsk has High popularity.\nMinsk has Low budget.\nIn Minsk you can do Skiing resorts: These are the most popular places to have a rest. They provide wide range of winter activities: skiing, snowboarding, skating, tubing etc..\nIn Minsk you can see Ozertso - Open Air Museum of Rural Architecture and Life.\nIn Minsk you can do Children&#39;s Railroad: This is a 750 mm (2 ft 5 1\u20442 in) narrow gauge railroad loop passing through the Chelyuskinites Park. The railway line counts 3 stations (listed from north to south): Zaslonovo, Pionerskaya (or Pionerskaja) and Sosnovy Bor (or Sosnovyj Bor); and a pair of rail loops after the end stops..\nIn Minsk you can do Minsk Sea: This  is an artificial reservoir.  There&#39;s a free public beach, and pedal-boat and catamaran rental..\nIn Minsk you can see Khatyn Memorial Complex.\nIn Minsk you can see Mound of Glory.\n&quot;,<br>        &quot;Krasnodar has High popularity.\nKrasnodar has Low budget.\nIn Krasnodar you can see Museum of Military Technologies Oruzhie Pobedy.\n&quot;,<br>        &quot;Kazan has High popularity.\nKazan has Low budget.\nIn Kazan you can do Victory Park: The park includes a memorial to the heroes of the Great Patriotic War. The Victory Column is 42m high..\nIn Kazan you can see Suyumbike Tower.\nIn Kazan you can see Kazan Kremlin.\nIn Kazan you can see Kul-Sharif Mosque.\n&quot;,<br>        &quot;Kaliningrad has High popularity.\nKaliningrad has Low budget.\nIn Kaliningrad you can do Trek in the forests: Soak up nature in the untouched forests.\nIn Kaliningrad you can see Fishing Village.\nIn Kaliningrad you can see The old cathedral of K\u00f6nigsberg.\n&quot;,<br>        &quot;Izmir has High popularity.\nIzmir has Low budget.\nIn Izmir you can see \u0130zmir Agora Open Air Museum.\nIn Izmir you can see Konak Square.\nIn Izmir you can see Kadifekale.\n&quot;,<br>        &quot;Baku has High popularity.\nBaku has Low budget.\nIn Baku you can see Oil Rocks.\nIn Baku you can do Naftalan Bath: Oil baths are famous for Naftalan, a city close to Ganca. However you can also get an oil treatment in Baku, with Naftalan oil imported from there. It&#39;s like a small hospital and it&#39;s not a recreactional thing to do usually. However it&#39;s great fun and positively strange to dip for 15min into hot crude oil. Choose yourself whether this is a save and healthy thing to do..\nIn Baku you can see Yanar Dagh.\nIn Baku you can see Hajji Gayyib bathhouse.\nIn Baku you can do Boat tours of Baku: Offered from the Baku Blvd. Lasts 40 minutes..\nIn Baku you can do AVIS: AVIS Rent a Car provides cars, SUV and 4WD. Airport pick up and delivery service is also available..\n&quot;,<br>        &quot;Ankara has High popularity.\nAnkara has Low budget.\nIn Ankara you can do Goksu Park: Enjoy the scenic Susuz Lake (G\u00f6lu).\nIn Ankara you can do Atat\u00fcrk Forest Farm and ZOO: This is an expansive recreational farming area, which houses a zoo, several small agricultural farms, greenhouses, restaurants, a dairy farm and a brewery. It was started by Kemal Atat\u00fcrk, who wanted to prove that the neglected steppes around Ankara could be converted into a lush, fertile agricultural land..\nIn Ankara you can do Guvenpark: A small park surrounding the Security Monument.\nIn Ankara you can see Arslanhane Mosque.\nIn Ankara you can see Roman Bath.\nIn Ankara you can see Roman Road of Ankara.\n&quot;<br>    ]</pre><h3>✍️ Step 3: Build the Prompt</h3><p>Use a structured prompt that includes the persona, filters, and KB-derived context. We use the following code to build our prompt.</p><pre>def build_user_prompt(example_config, config):<br>    &quot;&quot;&quot;<br>    Constructs a Gemini-compatible ICL prompt using an example config and a new config.<br>    <br>    Args:<br>        example_config (dict): Contains the sample persona, filters, context, and cities.<br>        config (dict): Contains the new persona, filters, context, and cities.<br><br>    Returns:<br>        str: A formatted user prompt string for Gemini query generation.<br>    &quot;&quot;&quot;<br>    <br>    USER_PROMPT_ICL_DK = &quot;&quot;&quot;<br>    Here is an example of a good query for a user who is a {sample_persona},<br>    the provided city information and associated travel filters.<br>    <br>    City Information: {sample_context}<br>    Travel filters: {sample_travel_filters}<br>    Generated Query: {sample_generated_query}<br>    <br>    Here are the following for the new user who is a {persona}:<br>    City Information: {new_context}<br>    Travel filters: {new_travel_filters}<br>    <br>    Your task: Guess a query, similar to the example above, that the following person may ask you to generate: {persona}<br>    &quot;&quot;&quot;<br><br>    prompt = USER_PROMPT_ICL_DK.format(<br>        sample_persona=example_config[&quot;persona&quot;],<br>        sample_context=&quot;\n&quot;.join(example_config[&quot;context&quot;]),<br>        sample_travel_filters=example_config[&quot;kb_filters&quot;],<br>        sample_generated_query=&quot;What European cities are popular and budget-friendly with outdoor recreational activities and parks to explore?&quot;,<br>        persona=config[&quot;persona&quot;],<br>        new_context=&quot;\n&quot;.join(config[&quot;context&quot;]),<br>        new_travel_filters=config[&quot;kb_filters&quot;]<br>    )<br><br>    return prompt</pre><p>🔄 <em>To keep things simple, we’ve included sample configs that simulate KB output — no database setup is needed. You can plug them in directly or replace them with your own retrieval logic later.</em></p><pre>#hard coded example config to be provided to the LLM<br>example_config = {<br>    &quot;persona&quot;: &quot;A travel blogger known for their captivating storytelling and stunning photography, attracting thousands of followers to their blog and social media channels&quot;,<br>    &quot;kb_filters&quot;: {<br>        &#39;popularity&#39;: &#39;high&#39;,<br>        &#39;budget&#39;: &#39;medium&#39;,<br>        &#39;interests&#39;: &#39;Arts &amp; Entertainment&#39;<br>        },<br>    &quot;cities&quot;: [<br>        &quot;Vilnius&quot;,<br>        &quot;Tallinn&quot;,<br>        &quot;Sarajevo&quot;,<br>        &quot;Nicosia&quot;,<br>        &quot;Budapest&quot;,<br>        &quot;Bratislava&quot;,<br>        &quot;Belgrade&quot;<br>    ],<br>    &quot;context&quot;: [<br>        &quot;Vilnius has high popularity.\nVilnius has medium budget.\nIn Vilnius you can do Lithuanian Opera and Ballet Theatre: beautiful ornate building. Three or four weekly performances..\nIn Vilnius you can do Lithuanian Russian Drama Theatre: performances mostly done in Russian language..\nIn Vilnius you can eat at Balti Drambliai (White Elephants): Cozy vegetarian-vegan cafe, world/reggae music, live concerts in the evenings..\nIn Vilnius you can do Oskaras Kor\u0161unovas Theatre (OKT): one of the most original modern theatres in Europe.\nIn Vilnius you can drink at Artistai: The large popular courtyard is a great place to relax and watch the sunset. Friendly service. Rock music..\nIn Vilnius you can see Angel of Uzupis Statue.\nIn Vilnius you can drink at Lindyhop.lt Klubas: The coolest and oldest Lindy Hop club in Lithuania..\nIn Vilnius you can drink at Prie Universiteto: Rock/punk rock music, friendly stuff and cheap prices. Best place for students and other not so serious people..\nIn Vilnius you can see St. Peter&#39;s and Paul&#39;s Church.\nIn Vilnius you can eat at \u010cagino (Chagin&#39;s): Named after the famous Russian architect Nikolai Chagin. Live Russian music played on a guitar..\nIn Vilnius you can see St. Ann&#39;s Church.\n&quot;,<br>        &quot;Tallinn has high popularity.\nTallinn has medium budget.\nIn Tallinn you can do Birgitta Festival: Music and theatre festival, held at the ruins of the historical Pirita (St. Bridget&#39;s) convent..\nIn Tallinn you can drink at Von Krahli: An avant-garde theatre/bar..\nIn Tallinn you can see Song Festival Grounds.\nIn Tallinn you can do Saku Suurhall: Rocca al Mare. Estonia&#39;s largest concert and exhibition space, the venue for the 2002 Eurovision Song Contest. The hall and its facilities include an excellent shopping centre that can easily be reached by trolleybus 6..\nIn Tallinn you can see Town Hall.\nIn Tallinn you can drink at Parlament: Bubblegum pop and live events..\nIn Tallinn you can do Tallinn Music Week: Showcase festival, aiming to stage the best and most outstanding Estonian talent on two nights in Tallinn&#39;s most vibrant live venues, as well as a networking event for the music industry professionals..\nIn Tallinn you can drink at Teater: Basement club on Vabaduse square. Popular with late twens and smaller groups. Music focus is on 90s and 00s. Door policy is relaxed but most people are dressed up. Usually starts going at midnight..\nIn Tallinn you can see St. Nicholas church.\n&quot;,<br>        &quot;Sarajevo has high popularity.\nSarajevo has medium budget.\nIn Sarajevo you can do Sarajevo Jazz Festival: A very eclectic festival, and the largest of its kind in the Balkans..\nIn Sarajevo you can drink at Opera Bar/Caf\u00e9: Fast WiFi connection, but the waitstaff are often unfriendly and inattentive. It attracts the acting and musical community among the regulars, though this isn&#39;t an exclusive kind of place. A bit smoky..\nIn Sarajevo you can see Sarajevo Art Gallery.\nIn Sarajevo you can do Sarajevo Film Festival: One of the best film festivals in Europe and the largest of its kind in Southeastern Europe..\nIn Sarajevo you can see Svrzina kuca (Svrzo house).\nIn Sarajevo you can drink at Central Cafe: Cocktail bar with great music.  Get there early or call to reserve a table.  The place is very busy until midnight when people leave to hit the various nightclubs around town. The street is a whole promenade with many other cafes around..\nIn Sarajevo you can see St. Anthony\u00b4s Church.\n&quot;,<br>        &quot;Nicosia has high popularity.\nNicosia has medium budget.\nIn Nicosia you can see Famagusta Gate.\nIn Nicosia you can see The Nicosia Municipal Theatre.\nIn Nicosia you can do Ifantourgio: 67-71 Lefkonos Str., Phaneromeni, 1011 Old Nicosia. The name of this place translates as &#39;&#39;The Weaving Mill&#39;&#39; and is in fact a converted old factory. Very arty, no blockbusters here. You can sip your wine as you watch an alternative movie..\nIn Nicosia you can see Byzantine Museum.\nIn Nicosia you can drink at The Kush Bar: Eleftheria Square, 1521 Nicosia. Opened in 2006, decorated with ultramodern minimalistic furniture and overlooking the dry moat surrounding Nicosia&#39;s venetian walls..\nIn Nicosia you can drink at Scorpios Club: Probably the only &#39;&#39;discoth\u00e8que&#39;&#39; in Nicosia having survived the test of time. Opened in the early 1970s and has reinvented itself (after several facelifts) since..\nIn Nicosia you can drink at Svoura Caf\u00e9-Bar: 5 Thermopylon, Old town. Serves coffee, snacks and alcohol; dance all night long. Free Wi-fi..\n&quot;,<br>        &quot;Budapest has high popularity.\nBudapest has medium budget.\nIn Budapest you can do Budapest Spring Festival: A dazzling variety of cultural events mainly revolving around classical music and performing arts - including folklore..\nIn Budapest you can do Firework above Danube River: National Holiday.  Sound &amp; Light Show with firework. Around half hour open air program..\nIn Budapest you can do Jewish Summer Festival: Another array of cultural and music events, with a Jewish touch..\n&quot;,<br>        &quot;Bratislava has high popularity.\nBratislava has medium budget.\nIn Bratislava you can see St. Clare&#39;s Church.\nIn Bratislava you can see Slovak National Theatre.\nIn Bratislava you can see Bratislava Castle.\nIn Bratislava you can drink at BeAbout: Music club near the riverside popular with younger crowd. Good selection of beers including Belgian specialty beers..\nIn Bratislava you can drink at UFO: If you want to go for extraordinary, visit UFO. It looks like a clich\u00e9 flying saucer and it&#39;s a high on a New Bridge (the member of great-towers.com), and you have to use a lift to go there. There is a good restaurant and lounge bar and observation deck. The view is incomparable..\n&quot;,<br>        &quot;Belgrade has high popularity.\nBelgrade has medium budget.\nIn Belgrade you can drink at Inex Film: a squat on the North side of the center, on the shore of the Danube, with weekly film screenings, art exhibitions, and DJ parties..\nIn Belgrade you can see The Residence of Princess Ljubica.\nIn Belgrade you can see Terazije and Kralja Milana streets.\nIn Belgrade you can see Church Ru\u017eica.\n&quot;<br>    ]<br>}<br># hard-coded new config retrieved from KB and merged with persona information<br>config = { <br>    &quot;persona&quot;: &quot;A veteran travel agent specialized in Asian travel routes.&quot;,<br>     &quot;kb_filters&quot;: {<br>            &quot;popularity&quot;: &quot;High&quot;,<br>            &quot;budget&quot;: &quot;Low&quot;,<br>            &quot;interests&quot;: &quot;Outdoors &amp; Recreation&quot;<br>        },<br>    &quot;cities&quot;: [<br>        &quot;Zagreb&quot;,<br>        &quot;Tirana&quot;,<br>        &quot;Tbilisi&quot;,<br>        &quot;Sofia&quot;,<br>        &quot;Samara&quot;,<br>        &quot;Riga&quot;,<br>        &quot;Perm&quot;,<br>        &quot;Moscow&quot;,<br>        &quot;Minsk&quot;,<br>        &quot;Krasnodar&quot;,<br>        &quot;Kazan&quot;,<br>        &quot;Kaliningrad&quot;,<br>        &quot;Izmir&quot;,<br>        &quot;Baku&quot;,<br>        &quot;Ankara&quot;<br>    ],<br>    &quot;context&quot;: [<br>        &quot;Zagreb has High popularity.\nZagreb has Low budget.\nIn Zagreb you can see Maksimir Park &amp; Zagreb Zoo.\nIn Zagreb you can see Jarun.\nIn Zagreb you can see Ribnjak Park.\nIn Zagreb you can drink at Melin: A cheap bar brimming with young people and students. Offers indoors, a roofed terraced leveled seating and an open terrace facing Tkal\u010di\u0107eva street. Known for crowds of people often drinking even around the bar, in the park and playground in front of it..\nIn Zagreb you can drink at Portal: Next to Melin, offers same service, the Portal and Melin crowds often mix at the outdoors terraces..\nIn Zagreb you can do Snow Queen Trophy: Ladies Night Slalom : Zagreb is the only capital in the world that hosts a Ski World Cup race. The slalom event - known as the \&quot;Snow Queen Trophy\&quot; (Snje\u017ena kraljica) - usually takes place in January. It is the most expensive race on the women&#39;s world cup tour and an exceptionally popular sporting event in Croatia that attracts a crowd of around 25 thousand people. As of 2007 best skiers in men&#39;s competition are also expected to compete on Mount Sljeme that overlooks the city. If you fail to secure your ticket, you can watch the whole event on a giant screen in the Ban Jela\u010di\u0107 Square..\nIn Zagreb you can drink at Golf: Popular golf-themed cafe/bar..\n&quot;,<br>        &quot;Tirana has High popularity.\nTirana has Low budget.\nIn Tirana you can see Persqopi Archeological Site.\nIn Tirana you can do Grand Park along the Artificial Lake: People have picnics and have fun, and the natural beauty allows it because of the huge trees, open spaces, and mountains surrounding them. An artificial lake lies in the middle of the Park. It is popular with locals and tourists as a place to walk around and enjoy a lunch or coffee at one of the restaurants or caf\u00e9s. Over 120 types of plants. A popular swimming pool complex is located nearby..\nIn Tirana you can do Mount Dajti: 1,612m high (getting to the topmost point is not possible because of a restricted area with antennas and a military zone). Offers great views of Tirana. A very relaxing area, mount Dajti can be reached through an Austrian-built express cable car (one ride takes more than 15 minutes so is worth the cost even if you don&#39;t stay atop the mountain). To get to the bottom station of the cable car, take the bus line \&quot;Qender - Linze\&quot; that starts east beside the Skanderbeg place behind the mosque. In the bus tell the conductor \&quot;Teleferik\&quot; and he will show you the station to get out of the bus. From there it is a short 5-minute walk guided by direction signs (the cable car is closed on Tuesdays). In the top station of the cable car, there is a hotel (30\u20ac for a double room [2013]) and a tourist agency offering various activities like horse riding, trekking, excursions, sky sports, ski etc. The area has a limited number of hotels and restaurants, but it is great for taking a breath of fresh air, mountain sports, or to just get out of the city for a while. The &#39;&#39;&#39;Pellumba Cave&#39;&#39;&#39; in the area is worth a visit. Unfortunately, litter is becoming a problem on Mount Dajti. This detracts from the experience somewhat, but the beautiful scenery still makes the trip worthwhile. Don&#39;t forget to visit the spectacular &#39;&#39;&#39;Erzeni Canyon&#39;&#39;&#39; along the Erzeni River south of the park near the dam..\nIn Tirana you can do Rally Albania: The event features an international competition in the rough Albanian countryside roads..\nIn Tirana you can see Ndroqit Castle ruins.\nIn Tirana you can see Preze Castle.\n&quot;,<br>        &quot;Tbilisi has High popularity.\nTbilisi has Low budget.\nIn Tbilisi you can do Alexander&#39;s Garden: A part of Park April..\nIn Tbilisi you can see Gudauri.\nIn Tbilisi you can see Telavi.\nIn Tbilisi you can see Ananuri.\nIn Tbilisi you can do Botanical garden: National botanic garden of Georgia, founded 1845. Lying in the Tsavkisis-Tskali Gorge near Narikala Fortres,  is actually more of a large badly maintained park with scenic waterfall than a \&quot;botanical garden\&quot; as there is no scientific signposting available. Very little maintenance is apparent..\nIn Tbilisi you can do HeidelbergCement TbilisiMarathon: Every year at the end of October a big, international event officially recognized by the International Association of Athletic Federations takes place. There is a half marathon, a 10-km-race and kids&#39; runs. The start and finish is at Rustaveli Avenue. The racecourse guides the runners through this great city to its most interesting sights. The  event is professionally organized by the German company HeidelbergCement Caucasus and a German race director. HeidelbergCement Caucasus is one of the biggest investors in the region. The participation fee goes 100% to charity..\n&quot;,<br>        &quot;Sofia has High popularity.\nSofia has Low budget.\nIn Sofia you can do Sofia Bike Tour: Visit the greenest capital of Europe on a bike. Every day Free Bike Tour (12 April\u2013November) at 11AM &amp; 5PM. Escape from the city and join to the Bike Countryside Tour around Sofia. Start Point: National Theater \&quot;Ivan Vazov\&quot;.\nIn Sofia you can see Borisova Gradina (Boris Garden).\nIn Sofia you can see Yuzhen Park (South Park).\nIn Sofia you can do Playgrounds: If you&#39;re traveling with kids, they might love playgrounds. The \&quot;rich\&quot; one is in the park that holds Vasil Levski stadium (close to the corner with Orlov Most; there are actually two playgrounds close to this corner and you&#39;d look for the one more inside the park)..\nIn Sofia you can see Boyana Church.\nIn Sofia you can drink at Lodkite: In the huge \&quot;Borisova gradina\&quot; park near the stadium (just ask you way around) you&#39;ll find this open-air place, located in an old leisure-park parcour (you know, that type of small water-channels where kid can drive around in little gondolas). The boats have gone and the channels serve as improvised sitting places (basic tables made out of wood planks put between the two sides of the channel). There are also some tables on the court and lights decorating the trees illuminate the scene. The public sound system has experimental electronic music, ambiance, progressive rock, or whatever the DJs feel like. The later it gets, the more you&#39;ll find people sitting everywhere on the floor on the trees. On warm summer nights, this place is a must-be..\n&quot;,<br>        &quot;Samara has High popularity.\nSamara has Low budget.\nIn Samara you can see Krasnaya Glinka Ski complex.\nIn Samara you can see Viewpoint &#39;Helipad&#39;.\nIn Samara you can see Dacha Golovkin.\n&quot;,<br>        &quot;Riga has High popularity.\nRiga has Low budget.\nIn Riga you can do R\u012bga City Day: Takes place on 14-16 Aug.\nIn Riga you can do Latviabeerfest: Takes place annually at the end of May. The largest international beer festival in the Baltics..\n&quot;,<br>        &quot;Perm has High popularity.\nPerm has Low budget.\nIn Perm you can see Motovilikha open-air museum.\nIn Perm you can see Khohlovka Museum.\n&quot;,<br>        &quot;Moscow has High popularity.\nMoscow has Low budget.\nIn Moscow you can do Pokrovskie Baths: Has a famous steam room with horseradish-flavored steam. Wide selection of brooms from birch tree, tatarian maple, lime tree, and eucalyptus. Prices are cheaper than other banyas..\nIn Moscow you can do Rzehvskye Baths: 120 years old but recently renovated..\nIn Moscow you can do Kva-Kva Water Park: Water Park affiliated with Maxima Hotels (discounts for guests). There are 7 high trills (90-120 meters length) and a pleasant surprise for extremers \u2013 Tsunami trill \u2013 unique in Russia. There are also 4-line trills \u2013 Multislide and a special area for kids \u2013 a small tropical town with shallow pool. - Kva-Kva Lagoone offers hydromassage. There\u2019s also pure Russian bath, Finnish sauna, Turkish bath (hamam) and Kva-Kva SPA-salon. - Night discos take place every weekend, with free admission for Maxima Hotel guests..\n&quot;,<br>        &quot;Minsk has High popularity.\nMinsk has Low budget.\nIn Minsk you can do Skiing resorts: These are the most popular places to have a rest. They provide wide range of winter activities: skiing, snowboarding, skating, tubing etc..\nIn Minsk you can see Ozertso - Open Air Museum of Rural Architecture and Life.\nIn Minsk you can do Children&#39;s Railroad: This is a 750 mm (2 ft 5 1\u20442 in) narrow gauge railroad loop passing through the Chelyuskinites Park. The railway line counts 3 stations (listed from north to south): Zaslonovo, Pionerskaya (or Pionerskaja) and Sosnovy Bor (or Sosnovyj Bor); and a pair of rail loops after the end stops..\nIn Minsk you can do Minsk Sea: This  is an artificial reservoir.  There&#39;s a free public beach, and pedal-boat and catamaran rental..\nIn Minsk you can see Khatyn Memorial Complex.\nIn Minsk you can see Mound of Glory.\n&quot;,<br>        &quot;Krasnodar has High popularity.\nKrasnodar has Low budget.\nIn Krasnodar you can see Museum of Military Technologies Oruzhie Pobedy.\n&quot;,<br>        &quot;Kazan has High popularity.\nKazan has Low budget.\nIn Kazan you can do Victory Park: The park includes a memorial to the heroes of the Great Patriotic War. The Victory Column is 42m high..\nIn Kazan you can see Suyumbike Tower.\nIn Kazan you can see Kazan Kremlin.\nIn Kazan you can see Kul-Sharif Mosque.\n&quot;,<br>        &quot;Kaliningrad has High popularity.\nKaliningrad has Low budget.\nIn Kaliningrad you can do Trek in the forests: Soak up nature in the untouched forests.\nIn Kaliningrad you can see Fishing Village.\nIn Kaliningrad you can see The old cathedral of K\u00f6nigsberg.\n&quot;,<br>        &quot;Izmir has High popularity.\nIzmir has Low budget.\nIn Izmir you can see \u0130zmir Agora Open Air Museum.\nIn Izmir you can see Konak Square.\nIn Izmir you can see Kadifekale.\n&quot;,<br>        &quot;Baku has High popularity.\nBaku has Low budget.\nIn Baku you can see Oil Rocks.\nIn Baku you can do Naftalan Bath: Oil baths are famous for Naftalan, a city close to Ganca. However you can also get an oil treatment in Baku, with Naftalan oil imported from there. It&#39;s like a small hospital and it&#39;s not a recreactional thing to do usually. However it&#39;s great fun and positively strange to dip for 15min into hot crude oil. Choose yourself whether this is a save and healthy thing to do..\nIn Baku you can see Yanar Dagh.\nIn Baku you can see Hajji Gayyib bathhouse.\nIn Baku you can do Boat tours of Baku: Offered from the Baku Blvd. Lasts 40 minutes..\nIn Baku you can do AVIS: AVIS Rent a Car provides cars, SUV and 4WD. Airport pick up and delivery service is also available..\n&quot;,<br>        &quot;Ankara has High popularity.\nAnkara has Low budget.\nIn Ankara you can do Goksu Park: Enjoy the scenic Susuz Lake (G\u00f6lu).\nIn Ankara you can do Atat\u00fcrk Forest Farm and ZOO: This is an expansive recreational farming area, which houses a zoo, several small agricultural farms, greenhouses, restaurants, a dairy farm and a brewery. It was started by Kemal Atat\u00fcrk, who wanted to prove that the neglected steppes around Ankara could be converted into a lush, fertile agricultural land..\nIn Ankara you can do Guvenpark: A small park surrounding the Security Monument.\nIn Ankara you can see Arslanhane Mosque.\nIn Ankara you can see Roman Bath.\nIn Ankara you can see Roman Road of Ankara.\n&quot;<br>    ]}<br></pre><h3>🤖 Step 4: Generate Queries with Gemini via Vertex AI</h3><p>To generate grounded, persona-aligned queries, we use <strong>Google’s Gemini 2.5 Flash</strong> model via the google-genai SDK with Vertex AI integration. The model is invoked in <strong>reasoning mode</strong>, which allows it to expose its internal &quot;thinking&quot; process — helpful for debugging, transparency, or evaluation.</p><p>Here’s the basic setup:</p><pre>from google import genai<br>from dotenv import load_dotenv<br>from vertexai.preview.generative_models import GenerativeModel as GenerativeModelPreview, Tool, grounding, \<br>    GenerationConfig<br>import os<br><br>def generate():<br><br>  client = genai.Client(<br>        vertexai=True,<br>        project=os.environ[&quot;VERTEXAI_PROJECTID&quot;],<br>        location=os.environ[&quot;PROJECT_LOCATION&quot;],<br>    )<br>  model = &quot;gemini-2.5-flash-preview-05-20&quot;<br>  prompt = build_user_prompt(example_config, config)<br>  <br>  response = model.models.generate_content(<br>                  model=mid,<br>                  contents=content,<br>                  config=GenerateContentConfig(thinking_config=ThinkingConfig(include_thoughts=True))<br>              )<br>  <br>  print(response.text)<br>  return response.text</pre><p>✅ The thinking_config flag turns on <strong>reasoning traces</strong>, giving you insight into how the model interprets context and constraints before generating output.</p><p>Use this setup to run single-shot or few-shot examples, grounded in city metadata and customized by persona.</p><p>✨ <em>We thank the </em><strong><em>Google AI Developer Programs</em></strong><em> team for supporting us with Google Cloud Credits.</em></p><p>✨ <em>To cite our work please use:</em></p><pre>@inproceedings{banerjee2025synthTRIPS,<br>    title={SynthTRIPs: A Knowledge-Grounded Framework for Benchmark Query Generation for Personalized Tourism Recommenders},<br>    author={Ashmi Banerjee and Adithi Satish and Fitri Nur Aisyah and<br>    Wolfgang Wörndl and Yashar Deldjoo},<br>    year={2025},<br>    year={2025},<br>    booktitle={In Proceedings of the 48th International ACM SIGIR Conference on Research and Development in Information Retrieval (SIGIR ’25)},<br>    doi={https://doi.org/10.1145/3726302.3730321}<br>}</pre><p>✨ <em>Link to the </em><a href="https://arxiv.org/pdf/2504.09277"><strong><em>paper</em></strong></a><em>, </em><a href="https://ashmibanerjee.github.io/synthTRIPS-website/"><strong><em>project website</em></strong></a><em> &amp; the </em><a href="https://github.com/ashmibanerjee/synthTRIPS-code"><strong><em>source code</em></strong></a><em> on GitHub.</em></p><p>✨ <em>If you like the article, please </em><a href="https://medium.com/@ashmi_banerjee"><strong><em>subscribe</em></strong></a><em> to my latest ones.<br>To get in touch, contact me on </em><a href="https://www.linkedin.com/in/ashmi-banerjee/"><strong><em>LinkedIn</em></strong></a><em> or via </em><a href="https://ashmibanerjee.com/"><strong><em>ashmibanerjee.com</em></strong></a><em>.</em></p><p><strong><em>GenAI usage disclosure:</em></strong><em> GenAI models were used to check for grammar inconsistencies in the blog and to refine the text for greater clarity. The authors accept full responsibility for the content presented in this blog.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=30c2456c03d6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[Tutorial] Grounding Gemini Responses in VertexAI using Google Search]]></title>
            <link>https://medium.com/@ashmi_banerjee/tutorial-grounding-gemini-responses-in-vertexai-using-google-search-b595e012bd05?source=rss-13be101669ab------2</link>
            <guid isPermaLink="false">https://medium.com/p/b595e012bd05</guid>
            <category><![CDATA[gemini]]></category>
            <category><![CDATA[vertex-ai]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[llm]]></category>
            <dc:creator><![CDATA[Ashmi Banerjee]]></dc:creator>
            <pubDate>Thu, 20 Feb 2025 14:22:55 GMT</pubDate>
            <atom:updated>2025-02-20T14:40:17.630Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>In this step-by-step tutorial, we will learn how to use ground Gemini responses in VertexAI using Google Search.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/814/1*Oy2j9eQJB5ifxlc9G9PjXA.png" /><figcaption>Our techstack for the tutorial</figcaption></figure><p>In today’s rapidly evolving information landscape, ensuring that AI-generated responses are both accurate and up-to-date is paramount. <em>Google’s Vertex AI offers a solution by integrating its Gemini models with real-time data available on the web through grounding using Google Search.</em></p><p>This tutorial provides a comprehensive guide on how to set up and utilize this feature effectively.</p><p><strong>Understanding Grounding with Google Search</strong></p><p><em>Grounding refers to anchoring AI model outputs in current, real-world data.</em></p><p>Gemini models can access and retrieve the latest information by <strong>leveraging Google Search</strong>, ensuring responses reflect recent developments and data.</p><p>This dynamic retrieval capability is crucial for applications requiring up-to-date knowledge across various topics.</p><h3>Prerequisites</h3><p><strong>1. Google Cloud Setup</strong></p><ul><li>A <strong>Google Cloud Platform (GCP) account</strong> with billing enabled.</li><li><strong>Vertex AI API access</strong> is enabled in the GCP project.</li><li>A <strong>service account</strong> with the necessary permissions to use Vertex AI.</li><li>The <strong>Google Application Credentials JSON file</strong> for authentication.</li></ul><p><strong>2. Python and Environment Setup</strong></p><ul><li>Basic knowledge of <strong>Python (3.8+ recommended)</strong>.</li><li>Installed Python packages:</li></ul><pre>pip install google-cloud-aiplatform vertexai google-auth</pre><h3>Option 1: Prototyping using the VertexAI console</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MKiNODW6ujgfPice7NK1Ng.png" /><figcaption>The grounding process using Google Search and Gemini via the VertexAI console. This illustration includes: 1. The prompts used, 2. The model settings (highlighting the Google Search enablement), 3. The results generated from the query, and 4. The citations referenced by Google to produce these results.</figcaption></figure><p>You can streamline your setup by utilizing the “<em>Get code</em>” feature in the Vertex AI console. This option provides ready-to-use code snippets tailored to your configuration, expediting the initialization process.</p><p>Alternatively, if you prefer a more hands-on approach with detailed explanations, please continue with the following tutorial.</p><h3>Option 2: Develop your code</h3><h4><strong>Step 1: Setup and Initialization</strong></h4><p>Begin by setting up your environment and initializing the necessary parameters for Vertex AI.</p><pre>import os<br>from google.oauth2 import service_account<br>import vertexai<br><br>def initialize_vertexai_params(location: str = &quot;us-central1&quot;):<br>    # Path to your Google Application Credentials JSON<br>    creds_file_name = &quot;path/to/your/credentials.json&quot;<br>    os.environ[&quot;GOOGLE_APPLICATION_CREDENTIALS&quot;] = creds_file_name<br><br>    # Authenticate using the service account credentials<br>    credentials = service_account.Credentials.from_service_account_file(<br>        filename=os.environ[&quot;GOOGLE_APPLICATION_CREDENTIALS&quot;],<br>        scopes=[&quot;https://www.googleapis.com/auth/cloud-platform&quot;],<br>    )<br><br>    # Initialize Vertex AI with your project and location<br>    vertexai.init(project=&quot;YOUR_PROJECT_ID&quot;, location=location, credentials=credentials)</pre><p>In this code:</p><ul><li>We import the necessary modules and define a function initialize_vertexai_params to set up the environment.</li><li>The GOOGLE_APPLICATION_CREDENTIALS environment variable is set to point to your service account JSON file.</li><li>We authenticate using the service account credentials and initialize Vertex AI with your project details.</li></ul><h4><strong>Step 2: Model Initialization</strong></h4><p>Next, initialize the Gemini model you intend to use.</p><pre>from vertexai.generative_models import GenerativeModel<br><br># Initialize the Gemini model<br>model = GenerativeModel(model_name=&quot;gemini-2.0-flash-001&quot;)</pre><p>Here:</p><ul><li>We import the GenerativeModel class from Vertex AI&#39;s generative models&#39; module.</li><li>We create an instance of the model by specifying the desired model name.</li></ul><h4><strong>Step 3: Setting Up Grounding with Google Search</strong></h4><p>To enable the model to retrieve real-time information, configure grounding using Google Search.</p><pre>from vertexai.preview.generative_models import Tool, grounding, GenerationConfig<br><br># Create a Tool instance configured for Google Search retrieval<br>tool = Tool.from_google_search_retrieval(<br>    google_search_retrieval=grounding.GoogleSearchRetrieval()<br>)</pre><p>In this segment:</p><ul><li>We import necessary classes, including Tool, grounding, and GenerationConfig.</li><li>We create a Tool instance configured to use Google Search for information retrieval.</li></ul><h4><strong>Step 4: Generating Grounded Responses</strong></h4><p>You can now generate responses incorporating real-time data with the model and grounding tool.</p><pre># Define your prompt or input content<br>content = &quot;What are the latest advancements in quantum computing?&quot;<br><br># Generate a response using the model with the grounding tool<br>response = model.generate_content(<br>    content=content,<br>    tools=[tool],<br>    generation_config=GenerationConfig(<br>        temperature=1.0,<br>        max_output_tokens=8192,<br>        top_p=0.95,<br>    ),<br>)<br><br># Output the generated response<br>print(response)Of course you can try out with different models and configurations.</pre><p>In this code:</p><ul><li>We define the input prompt content that you want the model to respond to.</li><li>The generate_content method is called on the model, passing the prompt, the grounding tool, and a GenerationConfig object that specifies parameters like temperature, max_output_tokens, and top_p.</li><li>The generated response is then printed out.</li></ul><p><strong>Customization and Experimentation</strong></p><p>Feel free to experiment with different models and configurations to suit your use case. Adjusting parameters such as temperature and top_p can influence the creativity and diversity of the model&#39;s responses.</p><h4>Parsing the Response</h4><p>After generating a response using the Gemini model with Google Search grounding, the output is a GenerationResponse object. This object encapsulates various details about the generated content, including:</p><ul><li><strong>Candidates</strong>: A list of possible responses generated by the model.</li><li><strong>Citation Metadata</strong>: Information about sources cited in the response.</li><li><strong>Grounding Metadata</strong>: Details about the data used to ground the response.</li></ul><p>While this object is rich in information, it may not immediately suit all applications, especially those requiring JSON-formatted data. To address this, we can parse the GenerationResponse object into a JSON-serializable dictionary.</p><p><strong>Converting GenerationResponse to JSON</strong></p><p>Below is a Python function that transforms a GenerationResponse object into a JSON-serializable dictionary:</p><pre><br>from vertexai.generative_models import GenerationResponse<br>from typing import Dict, Any<br><br>def convert_to_serializable(obj):<br>    &quot;&quot;&quot;<br>    Recursively converts objects into JSON serializable types.<br>    &quot;&quot;&quot;<br>    if isinstance(obj, dict):<br>        return {k: convert_to_serializable(v) for k, v in obj.items()}<br>    elif isinstance(obj, list):<br>        return [convert_to_serializable(v) for v in obj]<br>    elif isinstance(obj, (int, float, str, bool)):<br>        return obj  # Primitive types are serializable as-is<br>    else:<br>        return str(obj)  # Convert non-serializable objects to strings<br><br>def generation_response_to_json(response: GenerationResponse) -&gt; Dict[str, Any]:<br>    &quot;&quot;&quot;<br>    Converts a GenerationResponse object into a JSON-serializable dictionary.<br><br>    Args:<br>        response: The GenerationResponse object to convert.<br><br>    Returns:<br>        A dictionary representation of the GenerationResponse object suitable for JSON serialization.<br>    &quot;&quot;&quot;<br>    response_dict = {<br>        &quot;candidates&quot;: [<br>            {<br>                &quot;content&quot;: candidate.text,<br>                &quot;index&quot;: candidate.index,<br>                &quot;avg_log_prob&quot;: candidate.avg_logprobs,<br>                &quot;citation_metadata&quot;: [<br>                    {<br>                        &quot;citation_sources&quot;: [<br>                            {<br>                                &quot;end_index&quot;: citation.end_index,<br>                                &quot;start_index&quot;: citation.start_index,<br>                                &quot;uri&quot;: citation.uri,<br>                            }<br>                        ]<br>                    }<br>                    for citation in candidate.citation_metadata.citations<br>                ],<br>                &quot;grounding_chunks&quot;: [<br>                    {<br>                        &quot;uri&quot;: chunk.web.uri,<br>                        &quot;title&quot;: chunk.web.title,<br>                    }<br>                    for chunk in candidate.grounding_metadata.grounding_chunks<br>                ],<br>                &quot;grounding_supports&quot;: [<br>                    {<br>                        &quot;grounding_chunk_index&quot;: support.grounding_chunk_indices[0],<br>                        &quot;confidence_score&quot;: support.confidence_scores[0],<br>                        &quot;text&quot;: support.segment.text,<br>                        &quot;start_index&quot;: support.segment.start_index,<br>                        &quot;end_index&quot;: support.segment.end_index,<br>                    }<br>                    for support in candidate.grounding_metadata.grounding_supports<br>                ],<br>            }<br>            for candidate in response.candidates<br>        ],<br>    }<br>    return convert_to_serializable(response_dict)</pre><p><strong>Explanation of the Code</strong></p><p><strong>convert_to_serializable()</strong></p><ul><li><strong>Purpose</strong>: Ensures all elements within the dictionary are JSON-serializable.</li><li><strong>Implementation</strong>: Recursively traverses the object, converting non-serializable types to strings.</li></ul><p><strong>generation_response_to_json()</strong></p><ul><li><strong>Purpose</strong>: Transforms the GenerationResponse object into a dictionary suitable for JSON serialization.</li><li><strong>Process</strong>:<br>Iterates over each candidate response, extracting content, index, average log probabilities, citation metadata, grounding chunks, and grounding supports.</li></ul><p><strong>Original Documentation</strong></p><p><a href="https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/ground-with-google-search#dynamic-retrieval">Ground with Google Search | Generative AI | Google Cloud</a></p><p>If you want a similar implementation in JavaScript this is a very good article:</p><p><a href="https://medium.com/@afirstenberg/grounding-results-with-google-search-gemini-and-langchainjs-b2ccacdbbc2d">Grounding Results with Google Search, Gemini, and LangChainJS</a></p><p>✨ <em>We thank the </em><strong><em>Google AI/ML Developer Programs</em></strong><em> team for supporting us with Google Cloud Credits.</em></p><p>✨ <em>The source code on GitHub can be accessed </em><a href="https://github.com/ashmibanerjee/synthTRIPS-code/blob/main/tests/eval/rec-llm/benchmark.py"><strong><em>here</em></strong></a><strong><em>.</em></strong></p><p>✨ <em>If you like the article, please </em><a href="https://medium.com/@ashmi_banerjee"><strong><em>subscribe</em></strong></a><em> to my latest ones.<br>To get in touch, contact me on </em><a href="https://www.linkedin.com/in/ashmi-banerjee/"><strong><em>LinkedIn</em></strong></a><em> or via </em><a href="https://ashmibanerjee.com/"><strong><em>ashmibanerjee.com</em></strong></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b595e012bd05" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[Tutorial] A Simple RAG System using Google Gemma to answer travel-related queries]]></title>
            <link>https://medium.com/@ashmi_banerjee/mlstory-a-simple-rag-system-using-google-gemma-to-answer-travel-related-queries-4b08b9bde1c6?source=rss-13be101669ab------2</link>
            <guid isPermaLink="false">https://medium.com/p/4b08b9bde1c6</guid>
            <category><![CDATA[travel]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[recsys]]></category>
            <category><![CDATA[retrieval-augmented-gen]]></category>
            <dc:creator><![CDATA[Ashmi Banerjee]]></dc:creator>
            <pubDate>Tue, 19 Mar 2024 14:22:31 GMT</pubDate>
            <atom:updated>2025-07-23T19:36:46.723Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>In this step-by-step tutorial, we will use a simple Retrieval Augmented Generation (RAG) with Google Gemma to help it answer travel-related queries.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*CqkLgVGW4bmP0RCNpetx6g.jpeg" /><figcaption>Our proposed architecture. Image: by the author.</figcaption></figure><p>Before we start, let’s look into what RAG is.</p><h3>What is RAG, and why do we need it?</h3><blockquote>Retrieval Augmented Generation, aka RAG, is often described as an “open-book” approach to answering domain-specific questions.</blockquote><p>Typically, Large Language Models (LLMs) are trained on vast amounts of data, but sometimes they need updates to provide accurate information for specific topics.</p><p>There are two main methods:</p><ol><li><strong>Fine-tuning</strong> the model with specific data or giving it extra context with user queries.</li><li>Using <strong>Retrieval Augmented Generation (RAG)</strong>: This is like an open-book test for the model, where it can find relevant answers. This approach is often preferred due to lower computational costs compared to fine-tuning.</li></ol><p>For more details, you can explore RAG <a href="https://arxiv.org/abs/2005.11401">here</a>.</p><p><em>Now, let’s talk about enhancing Google Gemma for travel-related questions.</em></p><h3>Methodology</h3><p>Below, we outline the steps in building a RAG for travel-related queries.</p><h4>Step 1: Building our Documents Database</h4><p>We assembled our Documents database using information from Wikivoyage, a freely accessible online travel guide authored by volunteers.</p><p>We selectively extracted abstracts by parsing the <a href="https://dumps.wikimedia.org/enwikivoyage/latest/"><strong><em>Wikivoyage XML data</em></strong></a>, filtering specifically for European cities, resulting in a curated list of <strong><em>160 cities</em></strong>.</p><p><strong><em>Note:</em></strong><em> While this initial iteration suffices for prototyping purposes, future iterations could benefit from additional data and chunking techniques for enhanced production readiness.</em></p><p>Although chunking was omitted due to limited data availability, the code for parsing the XML data is provided below, and the complete dataset is accessible <a href="https://huggingface.co/datasets/ashmib/wikivoyage-city-embeddings"><strong><em>here</em></strong></a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dcd7646e1894a7f7304c3af06a8353ff/href">https://medium.com/media/dcd7646e1894a7f7304c3af06a8353ff/href</a></iframe><h4>Step 2: Generate Embeddings</h4><p>The next step is to generate the embeddings that will be used to embed both the documents and the queries.</p><blockquote>Embeddings are vector representations of textual data, aiding in semantic understanding and similarity comparisons.</blockquote><p>The embeddings enhance the efficiency and accuracy of information retrieval systems by encoding semantic relationships between documents and queries.</p><p>In this case, we utilize the gte-large embedding model from the SentenceTransformer library to generate embeddings for documents and queries.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2c5cd8ec3221e3c365cab1c957d74bff/href">https://medium.com/media/2c5cd8ec3221e3c365cab1c957d74bff/href</a></iframe><h4>Step 3: Database Setup and Connection</h4><p>Here, we use MongoDB as the operational and vector database.</p><blockquote>Using the <a href="https://www.mongodb.com/products/tools/compass"><strong><em>MongoDB compass</em></strong></a>, we migrate our tabular data, including the embeddings, from our local machine to the MongoDB Atlas. The detailed process of setting up the MongoDB Atlas can be found <a href="https://www.mongodb.com/docs/atlas/"><strong><em>here</em></strong></a>.</blockquote><p>Once the data has been migrated, we utilize the pymongo library to establish database connectivity and access the migrated data from Python.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/60c3157ea5d65c4c275cbf3dbefd492c/href">https://medium.com/media/60c3157ea5d65c4c275cbf3dbefd492c/href</a></iframe><h4>Step 4: Retrieval</h4><p>In this step, we retrieve the most similar documents to the query from the vector database.</p><p>In the query_results function:</p><ul><li>[Lines 4–5] We connect to the mongo client created above by specifying the mongo_url</li><li>[Line 7] We retrieve the embedding for the input query using the get_embedding function.</li><li>[Line 8] The MongoDB aggregation framework is then used to perform a vector search on the collection named “EU_cities_collection.” The vector search is performed using a specified index (“vector_index”) and the path to the embedding field, using the query_embedding as the vector for the search. It specifies that up to 150 candidates should be retrieved but limits the final results to the top 5 candidates.</li></ul><p>In the get_search_result function:</p><p>We begin by calling the query_results function with the provided query and mongo_url. This returns the 5 top most similar documents from our database, i.e., the context that is then augmented with the prompt later.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/775473b1a70777784fce4663f86ca1de/href">https://medium.com/media/775473b1a70777784fce4663f86ca1de/href</a></iframe><h4>Step 5: Augmentation and Generation</h4><ul><li>We merge the context derived from the get_search_result function with the user query to create an augmented prompt.</li><li>This augmented prompt serves as input for our chosen LLM to generate a context-specific response.</li><li>Specifically, in this instance, we utilize the google/gemma-2b-it model from the <a href="https://huggingface.co/docs/huggingface_hub/en/package_reference/inference_client"><strong>HuggingFace</strong></a> InferenceClient for text generation.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dfcce7ed3e58d6ed1c9728fe0ed3f3ed/href">https://medium.com/media/dfcce7ed3e58d6ed1c9728fe0ed3f3ed/href</a></iframe><h3>Conclusion</h3><p>In this tutorial, we’ve explored the process of constructing a RAG system, leveraging Google Gemma to address inquiries related to European travel. This framework is adaptable and can be extended to diverse domains beyond travel.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jcFzh-crMX2ZvDc3UM7ZxQ.png" /><figcaption>Demo: <a href="https://huggingface.co/spaces/ashmib/gemma-eu-travels">Gemma Eu Travels — a Hugging Face Space by ashmib</a></figcaption></figure><p>If you want to experiment with the prototype, here’s the <a href="https://huggingface.co/spaces/ashmib/gemma-eu-travels"><strong><em>demo</em></strong></a> version built with <a href="https://www.gradio.app/guides/quickstart"><strong><em>Gradio</em></strong></a> on <a href="https://huggingface.co/"><strong><em>HuggingFace</em></strong></a>.</p><p>It’s still a work in progress, so please patiently approach it. 😉<br>🌍 <strong><em>A tip: the system performs best when queried about specific recommendations for European cities.</em></strong></p><p>Moving forward, the next phase involves evaluating the performance of our model within the RAG framework alongside a broader assessment of RAG systems in general.</p><p>✨ <em>This blog series is inspired by the Open-Source AI cookbook </em><a href="https://huggingface.co/learn/cookbook/en/rag_with_hugging_face_gemma_mongodb"><strong><em>blog post</em></strong><em> </em></a><em>from HuggingFace, which focused on building a movie recommender system using RAG.</em></p><p>✨ <em>The source code can be accessed </em><a href="https://huggingface.co/spaces/ashmib/green-city-finder"><strong><em>here</em></strong></a><strong><em>.<br></em></strong><em>The references and further readings on this topic have been summarised </em><a href="https://gist.github.com/ashmibanerjee/b4421b326cc88c3e702a1c9f673f374f"><strong><em>here</em></strong></a><em>.</em></p><p>✨ <em>If you like the article, please </em><a href="https://medium.com/@ashmi_banerjee"><strong><em>subscribe</em></strong></a><em> to my latest ones.<br>To get in touch, contact me on </em><a href="https://www.linkedin.com/in/ashmi-banerjee/"><strong><em>LinkedIn</em></strong></a><em> or via </em><a href="https://ashmibanerjee.com/"><strong><em>ashmibanerjee.com</em></strong></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4b08b9bde1c6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Pros and Cons of Collaborative Filtering]]></title>
            <link>https://medium.com/@ashmi_banerjee/pros-and-cons-of-collaborative-filtering-9c3aa4ce44f6?source=rss-13be101669ab------2</link>
            <guid isPermaLink="false">https://medium.com/p/9c3aa4ce44f6</guid>
            <category><![CDATA[recommendations]]></category>
            <category><![CDATA[recsys]]></category>
            <category><![CDATA[collaborative-filtering]]></category>
            <category><![CDATA[recommender-systems]]></category>
            <dc:creator><![CDATA[Ashmi Banerjee]]></dc:creator>
            <pubDate>Tue, 19 Mar 2024 14:11:12 GMT</pubDate>
            <atom:updated>2024-03-19T14:11:12.885Z</atom:updated>
            <content:encoded><![CDATA[<p><em>In this blog, we will learn the advantages and disadvantages of using collaborative filtering.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/649/0*xA2P_g7T4TOPRKJE.png" /><figcaption>Collaborative Filtering: How it works</figcaption></figure><p><strong><em>Collaborative filtering</em></strong> is a recommendation technique that predicts a user’s preferences based on past behavior and similarities with other users. It relies on patterns of interactions among users and items to suggest personalized recommendations.</p><p>In the <a href="https://medium.com/@ashmi_banerjee/understanding-collaborative-filtering-f1f496c673fd"><strong><em>previous</em></strong> </a>blog post, we delved into the mechanics of collaborative filtering. Now, in this one, we focus on examining the merits and drawbacks inherent to this method.</p><h3>Advantages</h3><h4>1. Works well in practice</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/896/1*zjC9IhyYTbSffeT0iE4XcQ.png" /></figure><p>Collaborative filtering consistently demonstrates its efficacy in real-world scenarios. Its reliance on user ratings directly correlates with recommendation quality. <br>Many e-commerce companies leverage this method to boost revenue streams by offering similar products to users with comparable tastes, enhancing user engagement and satisfaction.</p><h4>2. No user metadata is required</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/610/0*7E6mrHVmnMS3mJzh" /></figure><p>Unlike some recommendation systems, collaborative filtering operates efficiently with just user ratings, omitting the need for extensive user metadata. This simplicity streamlines the implementation process and reduces computational overhead.</p><h4>3. Diverse cross-domain recommendations</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*wkUl1b-Fkn8Ctwj6.jpg" /></figure><p>Collaborative filtering transcends domain boundaries, providing diverse recommendations across various sectors. Whether suggesting movies, books, or products, this method adapts seamlessly, catering to user preferences and interests.</p><h4>4. Implicit user feedback is often adequate</h4><p>Collaborative filtering harnesses implicit user feedback effectively. Unary ratings, such as clicks on links or interactions with content, serve as valuable indicators of user behavior. Leveraging this data, the system can infer user preferences and deliver relevant recommendations, even without explicit ratings.</p><h3>Disadvantages</h3><h4>1. Cold-start problem (serious issue)</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/372/0*SL7TKebk9e_ND9xZ.jpg" /><figcaption>Cold start and Latency are significant concerns for Collaborative Filtering Algorithms.</figcaption></figure><p>One of the most significant challenges with collaborative filtering is the cold-start problem.</p><blockquote>This issue arises when the system needs help to make accurate recommendations for new users or items with limited interaction history.</blockquote><p>With sufficient data, the system may provide relevant suggestions, hindering user engagement and satisfaction.</p><h4>2. Data sparseness</h4><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/0*6z8CS837mKLApsI4.jpg" /></figure><p>Collaborative filtering relies heavily on user-item interaction data.</p><blockquote>However, this data may be sparse in practice, especially for niche or less popular items.</blockquote><p>Sparse data can lead to accurate recommendations and improved user experience, particularly for items with limited user feedback.</p><h4>3. “Grey sheep” problem</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/0*7kMx2WJHM7yryV7m.gif" /></figure><p>It encounters difficulty when faced with users who possess unique or unconventional preferences.</p><blockquote>In such cases, finding similar users with comparable tastes becomes challenging, resulting in suboptimal recommendations.</blockquote><p>This limitation undermines the system’s ability to effectively cater to diverse user preferences.</p><h4>4. “Black sheep” problem</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/0*vsY7rise0DklH3HR.jpeg" /></figure><p>It is susceptible to manipulation and malicious behavior by users.</p><blockquote>In some instances, users may intentionally distort ratings, compromising the integrity and reliability of the recommendation system.</blockquote><p>These trust and robustness issues pose significant challenges, making the system vulnerable to manipulation and attacks.</p><h4>5. Domain-specific.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/336/1*es7X55ADnNvxsv6NMbwQYA.png" /></figure><p>It is only universally applicable across some domains.</p><blockquote>It is most effective in domains characterized by short-term user preferences and frequent interactions.</blockquote><p>Collaborative filtering may yield unreliable recommendations in domains with long-term or complex user behaviors, such as financial investments or healthcare, limiting its utility in specific contexts.</p><h3>Conclusion</h3><p>Despite its drawbacks, collaborative filtering remains widely used, with many companies relying on it for revenue generation.</p><p>Its ability to provide personalized recommendations and enhance user engagement makes it a go-to method in recommendation systems.</p><p>✨ <em>This blog series is inspired by the User Modeling and Recommender Systems course offered by Dr </em><a href="https://www.ce.cit.tum.de/cm/research-group/wolfgang-woerndl/"><em>Wolfgang Wörndl</em></a><em> at the Technical University of Munich, Germany.</em></p><p>✨ <em>The memes are taken from Google or Reddit.</em></p><p>✨ <em>If you like the article, please </em><a href="https://medium.com/@ashmi_banerjee"><strong><em>subscribe</em></strong></a><em> to my latest ones.<br>To get in touch, contact me on </em><a href="https://www.linkedin.com/in/ashmi-banerjee/"><strong><em>LinkedIn</em></strong></a><em> or via </em><a href="https://ashmibanerjee.com/"><strong><em>ashmibanerjee.com</em></strong></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9c3aa4ce44f6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[MLStory] A Guide to Using VertexAI and Google’s Embeddings for Generative Projects]]></title>
            <link>https://medium.com/google-developer-experts/mlstory-a-guide-to-using-vertexai-and-googles-embeddings-for-generative-projects-9954c9d4c004?source=rss-13be101669ab------2</link>
            <guid isPermaLink="false">https://medium.com/p/9954c9d4c004</guid>
            <category><![CDATA[text-embedding]]></category>
            <category><![CDATA[cosine-similarity]]></category>
            <category><![CDATA[vertex-ai]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[generative-ai-use-cases]]></category>
            <dc:creator><![CDATA[Ashmi Banerjee]]></dc:creator>
            <pubDate>Mon, 11 Sep 2023 07:51:37 GMT</pubDate>
            <atom:updated>2023-09-11T08:59:43.873Z</atom:updated>
            <content:encoded><![CDATA[<p>In this step-by-step tech tutorial, we’ll explore an alternative approach to tackle a common use case we discussed in a previous blog:</p><p><a href="https://medium.com/google-developer-experts/ml-story-leveraging-vertexai-and-clever-prompts-for-textual-data-insights-bf78efb08569">[ML Story] Leveraging VertexAI and “Clever” Prompts for Textual Data Insights</a></p><p>Imagine you own a bustling restaurant with a multitude of customer reviews. Your goal is to gain meaningful insights from this wealth of data. This includes <em>identifying menu favorites, understanding customer preferences, and finding areas for enhancement</em>.</p><p>In this tutorial, we’ll leverage the power of generative AI to extract these insights efficiently.</p><blockquote>By the end of this tutorial, you’ll be able to identify popular menu items, understand customer sentiments, and pinpoint areas for improvement without manually sifting through hundreds of reviews.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*dNvwXSBG47NxAG-b.png" /><figcaption>Problem Statement</figcaption></figure><p>Before we dive into the technical details, let’s briefly grasp the concept of embeddings.</p><blockquote>Embeddings are numerical representations of text, translating words and phrases into a format that computers can comprehend.</blockquote><ul><li>They’re crucial for various applications, including search and recommendation systems.</li><li>Similar texts share similar embeddings, making them ideal for our analysis.</li></ul><p>Our approach involves using VertexAI’s TextEmbeddingModel to generate embeddings for each review and then comparing them against a query using their dot product.</p><blockquote>The dot product’s value ranges from -1 to +1: -1 signifies dissimilarity, 0 indicates orthogonality, and +1 signifies similarity.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/931/1*GY3zDKracu7fa9mBmZltyQ.png" /><figcaption>Proposed Workflow using Text Embeddings</figcaption></figure><p>Let’s look at a sketch of our algorithm:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b000c9f9674833249f84fea1de627abd/href">https://medium.com/media/b000c9f9674833249f84fea1de627abd/href</a></iframe><h4>Advantages</h4><ul><li>Suitable for similarity-based recommendations and search methods.</li></ul><h4>Drawbacks</h4><ul><li>Doesn’t generate new text but rather filters the most similar reviews.</li><li>Embeddings must be computed on the fly or stored in memory, which is potentially computationally expensive, depending on your data volume.</li></ul><blockquote>By the end of this tutorial, you’ll have a practical understanding of how to apply VertexAI and embeddings to gain valuable insights from textual data efficiently. Let’s get started! <em>🚀</em></blockquote><p>✨ <em>The source code on GitHub can be accessed </em><a href="https://github.com/ashmibanerjee/review-analysis-genai/tree/main"><strong><em>here</em></strong></a><strong><em>.<br></em></strong><em>The references and further readings on this topic have been summarized </em><a href="https://gist.github.com/ashmibanerjee/1823c8c2aa55c9caf2447b3596a2d560"><strong><em>here</em></strong></a><em>.</em></p><p>✨ <em>If you like the article, please </em><a href="https://medium.com/@ashmi_banerjee"><strong><em>subscribe</em></strong></a><em> to my latest ones.<br>To get in touch, contact me on </em><a href="https://www.linkedin.com/in/ashmi-banerjee/"><strong><em>LinkedIn</em></strong></a><em> or via </em><a href="https://ashmibanerjee.com/"><strong><em>ashmibanerjee.com</em></strong></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9954c9d4c004" width="1" height="1" alt=""><hr><p><a href="https://medium.com/google-developer-experts/mlstory-a-guide-to-using-vertexai-and-googles-embeddings-for-generative-projects-9954c9d4c004">[MLStory] A Guide to Using VertexAI and Google’s Embeddings for Generative Projects</a> was originally published in <a href="https://medium.com/google-developer-experts">Google Developer Experts</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[ML Story] Leveraging VertexAI and “Clever” Prompts for Textual Data Insights]]></title>
            <link>https://medium.com/google-developer-experts/ml-story-leveraging-vertexai-and-clever-prompts-for-textual-data-insights-bf78efb08569?source=rss-13be101669ab------2</link>
            <guid isPermaLink="false">https://medium.com/p/bf78efb08569</guid>
            <category><![CDATA[generative-ai-tools]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[yelp-review]]></category>
            <category><![CDATA[text-generation]]></category>
            <category><![CDATA[vertex-ai]]></category>
            <dc:creator><![CDATA[Ashmi Banerjee]]></dc:creator>
            <pubDate>Thu, 07 Sep 2023 08:50:34 GMT</pubDate>
            <atom:updated>2023-09-07T08:58:28.786Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>A step-by-step tutorial to use the VertexAI Python APIs for restaurant review analysis.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/805/1*gtntR3JXMhXOAPhQpJ4T6A.png" /><figcaption>Problem Statement</figcaption></figure><p>Imagine you’re the owner of a popular restaurant with many reviews. You’d like to gain valuable insights from these reviews, such as <em>identifying the most popular menu items</em>, <em>understanding what customers appreciate most about your restaurant</em>, and <em>pinpointing areas for improvement</em>.</p><p>Instead of manually sifting through hundreds of reviews, you can harness the power of generative AI to extract insights from the data.</p><p>Let’s explore how to achieve this using <a href="https://cloud.google.com/vertex-ai/docs/python-sdk/use-vertex-ai-python-sdk">VertexAI Python APIs</a>.</p><p>Before we proceed further, it’s essential to clarify two fundamental concepts that will play a recurring role throughout this tutorial:</p><ul><li><strong>Prompt Engineering</strong>: It is a pivotal aspect of successful generative AI analysis. It involves crafting well-defined prompts that serve as instructions to guide the AI model in generating meaningful responses to your queries or directives.</li><li><strong>Token Limit Challenges:</strong> A finite input token limit exists in the context of VertexAI models. <br>⚠️ Combining <strong>ALL</strong> reviews in one go ➡️ <strong>exceeds</strong> the model&#39;s input token limit.<br>⚠️ Sending <strong>EACH</strong> review individually to the API ➡️ proves <strong>inefficient</strong>, as it underutilizes the token limit capacity.</li></ul><blockquote>To address this constraint, we will adopt a strategy of processing reviews in manageable batches or chunks.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/836/1*Z2ATTu1S-aI1XMUnkrajaw.png" /><figcaption>Original Workflow Proposal: Sending all reviews through the API.</figcaption></figure><h3>Step 1: Prepare the Dataset</h3><p>To start with this tutorial, you can access a subsample of the <a href="https://www.yelp.com/dataset">Yelp Business data</a>, which simulates restaurant review analysis. <br>You can find the sample data <a href="https://github.com/ashmibanerjee/review-analysis-genai/blob/main/data/yelp_academic_dataset_businesses_reviews_sample.csv">here</a>.</p><p><a href="https://github.com/ashmibanerjee/review-analysis-genai/blob/main/data/yelp_academic_dataset_businesses_reviews_sample.csv">review-analysis-genai/data/yelp_academic_dataset_businesses_reviews_sample.csv at main · ashmibanerjee/review-analysis-genai</a></p><p><a href="https://github.com/ashmibanerjee/review-analysis-genai/blob/main/data/yelp_academic_dataset_businesses_reviews_sample.csv">This dataset</a> is already cleaned and ready for analysis. 🎉</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/761/1*Gfbi0UnmlOlafJwyEv16HQ.png" /><figcaption>Our tech stack: sample reviews from the Yelp Business dataset [1], Python, and VertexAI</figcaption></figure><h3>Step 2: Set Up VertexAI on Google Cloud</h3><p>To access VertexAI locally through your Jupyter notebooks, you should set up VertexAI on Google Cloud. You can follow the setup tutorial <a href="https://medium.com/google-developer-experts/ml-story-exploring-the-future-of-text-generation-a-deep-dive-into-vertex-ai-using-jupyter-c414e524d99f">here</a>.</p><p><a href="https://medium.com/google-developer-experts/ml-story-exploring-the-future-of-text-generation-a-deep-dive-into-vertex-ai-using-jupyter-c414e524d99f">[ML Story] Exploring the Future of Text Generation: A Deep Dive into Vertex AI using Jupyter…</a></p><h3>Step 3: Process Reviews in Batches</h3><p>Now, let’s start processing the restaurant reviews in batches. We’ll use Python and the VertexAI API to accomplish this. Here’s a breakdown of the code, followed by its explanation.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/40bd4746bee886a8f0ad99864dbe4c21/href">https://medium.com/media/40bd4746bee886a8f0ad99864dbe4c21/href</a></iframe><ol><li>The code divides the text data into smaller chunks or batches based on the estimated token counts. It ensures that each chunk doesn’t exceed the token limit when combined with the prompt.</li><li>The prompt, a guiding statement or question, initiates the text generation. It’s designed to effectively elicit meaningful responses from the AI model.</li><li>The code iterates through the text data, adding individual items to a chunk until it approaches the token limit. The chunk is sent to the VertexAI model for text generation when the limit is reached.</li><li>The responses from the model for each chunk are collected and concatenated to create a cohesive output.</li><li>If the concatenated response still exceeds the token limit, the process is repeated recursively, ensuring that the token constraints are maintained.</li></ol><p>The <a href="https://github.com/openai/tiktoken">tiktoken</a> library is a valuable tool for estimating tokens without making API requests, optimizing the text processing workflow for AI-based analysis.</p><h3>Step 4: Utilizing “Clever” Prompt Engineering</h3><p>You can now call get_final_response_prompt_engg(prompt, items) with your desired prompt and restaurant reviews to generate insights based on your questions.</p><p>You can explore more <a href="https://www.deeplearning.ai/short-courses/chatgpt-prompt-engineering-for-developers/">here</a> if you’d like to delve deeper into prompt engineering strategies.</p><blockquote>So, let your creativity flow and craft prompts that spark innovation, allowing you to unearth previously undiscovered revelations within your data. <br>The possibilities are boundless, and the insights are waiting to be discovered! 🚀</blockquote><p>✨ <em>The source code on GitHub can be accessed </em><a href="https://github.com/ashmibanerjee/review-analysis-genai/tree/main"><strong><em>here</em></strong></a><strong><em>.<br></em></strong><em>The references and further readings on this topic have been summarized </em><a href="https://gist.github.com/ashmibanerjee/1823c8c2aa55c9caf2447b3596a2d560"><strong><em>here</em></strong></a><em>.</em></p><p>✨ <em>If you like the article, please </em><a href="https://medium.com/@ashmi_banerjee"><strong><em>subscribe</em></strong></a><em> to my latest ones.<br>To get in touch, contact me on </em><a href="https://www.linkedin.com/in/ashmi-banerjee/"><strong><em>LinkedIn</em></strong></a><em> or via </em><a href="https://ashmibanerjee.com/"><strong><em>ashmibanerjee.com</em></strong></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bf78efb08569" width="1" height="1" alt=""><hr><p><a href="https://medium.com/google-developer-experts/ml-story-leveraging-vertexai-and-clever-prompts-for-textual-data-insights-bf78efb08569">[ML Story] Leveraging VertexAI and “Clever” Prompts for Textual Data Insights</a> was originally published in <a href="https://medium.com/google-developer-experts">Google Developer Experts</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>