Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: utilize reference url and markdown in attachments for Grounding #151

Merged
merged 3 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions aidial_adapter_vertexai/chat/gemini/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,16 +328,26 @@ async def create_function_calls(


async def create_grounding(candidate: Candidate, consumer: Consumer) -> None:
if (
not candidate.grounding_metadata
or not candidate.grounding_metadata.grounding_chunks
if not (metadata := candidate.grounding_metadata) or not (
supports := metadata.grounding_supports
):
return

for chunk in candidate.grounding_metadata.grounding_chunks:
if chunk.web and chunk.web.uri:
for support in supports:
if not (chunk_indices := support.grounding_chunk_indices):
continue

for chunk_index in chunk_indices:
chunk = metadata.grounding_chunks[chunk_index]
if not chunk.web or not chunk.web.uri:
continue
await consumer.add_attachment(
Attachment(url=chunk.web.uri, title=chunk.web.title)
Attachment(
reference_url=chunk.web.uri,
data=support.segment.text,
title=chunk.web.title,
type="text/markdown",
)
)


Expand Down
6 changes: 4 additions & 2 deletions tests/integration_tests/test_chat_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,10 @@ def test_case(
expected=lambda s: (
s.attachments is not None
and len(s.attachments) > 0
and isinstance(s.attachments[0].url, str)
and s.attachments[0].url.startswith("https://vertexaisearch")
and isinstance(s.attachments[0].reference_url, str)
and s.attachments[0].reference_url.startswith(
"https://vertexaisearch"
)
and "carlos alcaraz" in s.content.lower()
),
)
Expand Down
Loading