Commit 4ed8111

benny-dou <60535774+benny-dou@users.noreply.github.com>
2025-08-18 14:17:12
fix(gemini): fix grounding indices
1 parent f2ca4be
Changed files (1)
src
llm
gemini
src/llm/gemini/utils.py
@@ -56,16 +56,18 @@ async def add_grounding_results(answers: str, grounding_chunks: list[dict], grou
     except Exception as e:
         logger.warning(e)
         index2url = urls
+    logger.trace(f"Grounding URLs: {index2url}")
     for support in grounding_supports:
         indices: list[int] = support.get("grounding_chunk_indices", [])
-        indices_with_url = " ".join([f"[[{idx + 1}]]({index2url[idx]})" for idx in indices])
+        logger.trace(f"Add grounding indices: {indices}")
+        indices_with_url = " ".join([f"[[{idx + 1}]]({glom(index2url, str(idx), default='https://www.google.com')})" for idx in indices])
         if segment := glom(support, "segment.text", default=""):
             answers = answers.replace(segment, f"{segment}{indices_with_url}", 1)
     for idx, grounding in enumerate(grounding_chunks):
         if idx > 9:
             break
         title = glom(grounding, "web.title", default="Web")
-        url = index2url[idx]
+        url = glom(index2url, str(idx), default="https://www.google.com")
         if url in answers:
             answers += f"\n{number_to_emoji(idx + 1)}[{title}]({url})"
     return answers