-
Notifications
You must be signed in to change notification settings - Fork 375
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
Generating Citations with a Custom Model Deployment #830
Comments
@bcicc I'll check this out the upcoming week, I don't recall the citations format by heart. I'm probably going to step through the code with a debugger and check the actual expected format, if you're in a rush my suggestion is to step through it on your side as well. Just from a glance, your citation looks fine however 🤔 |
@bcicc The general format is correct, your error is most likely the hardcoded
The document id needs to exist in the |
Thank you for the response. Here is the code i'm using, this is meant to implement Gemini grounding. Below the code is the docker logs output showing the citations object. Let me know what you think. stream = self.gemini.generate_content(
messages, stream=True, safety_settings=safety_settings
)
full_response = ""
try:
for chunk in stream:
grounding_chunks = chunk.candidates[
0
].grounding_metadata.grounding_chunks
grounding_supports = chunk.candidates[
0
].grounding_metadata.grounding_supports
chunk_text = chunk.text
if grounding_chunks:
full_response = "".join(
[support.segment.text for support in grounding_supports]
)
ctx.get_logger().info(
event="Grounding Info", grounding=grounding_chunks
)
documents = [
{
"id": str(hash(chunk.web.uri)),
"text": chunk_text,
"title": chunk.web.title,
"url": chunk.web.uri,
"tool_name": "google_web_search",
}
for chunk in grounding_chunks
]
search_results = [
{
str(hash(chunk.web.uri)): chunk_text,
}
for chunk in grounding_chunks
]
citations = [
{
"start": support.segment.start_index or 0,
"end": support.segment.end_index or 0,
"text": support.segment.text or "",
"document_ids": [
str(hash(chunk.web.uri))
for idx, chunk in enumerate(grounding_chunks)
if idx in support.grounding_chunk_indices
],
}
for support in grounding_supports
]
ctx.get_logger().info(
event="Grounding citations", citations=citations
)
yield {
"event_type": StreamEvent.SEARCH_RESULTS,
"documents": documents,
"search_results": search_results,
}
yield {
"event_type": StreamEvent.CITATION_GENERATION,
"citations": citations,
}
yield {
"event_type": StreamEvent.TEXT_GENERATION,
"text": chunk_text,
"response": {
"text": chunk_text,
"generation_id": "",
},
}
usage_metadata = stream.usage_metadata
ctx.get_logger().info(
event="Gemini Usage Metadata", usage_metadata=usage_metadata
)
yield {
"event_type": StreamEvent.STREAM_END,
"text": full_response,
"finish_reason": "COMPLETE",
"chat_history": chat_history
+ [ChatMessage(role=ChatRole.CHATBOT, message=full_response).to_dict()],
"response": {
"text": full_response,
"generation_id": "",
},
}
except Exception as e:
error = str(e)
ctx.get_logger().error(event="Error in invoke_chat_stream", error=error)
yield {
"event_type": StreamEvent.TEXT_GENERATION,
"text": str(e),
"response": {
"text": str(e),
"generation_id": "",
},
}
yield {
"event_type": "stream-end",
"text": str(e),
"finish_reason": "COMPLETE",
"chat_history": chat_request.chat_history
or [] + [ChatMessage(role=ChatRole.CHATBOT, message=error).to_dict()],
"response": {
"text": error,
"generation_id": "",
},
} Docker logs:
|
@bcicc Great is that version working for you now? |
I may not have been clear, I am still having the issue. I'm not sure what I'm doing wrong. I haven't been able to generate a citation properly yet, so I am not even sure what it looks like although I know from the frontend code there is supposed to be highlighting. |
@bcicc What's the error that pops up, if any? I would step through each part of the chat stream handling code to see what's happening exactly with your citations. The document ids in the citations need to exist in the DB as well I believe, in the user facing |
What is the issue?
I am unable to generate citations with a custom model deployment by simply yielding a
CITATION_GENERATION
event, nor by including citations in theSTREAM_END
event in the model'sinvoke_chat_stream
method.Here is my code in
invoke_chat_stream
:I can see in the frontend that the citation-generation event comes through the chat-stream endpoint with the appropriate data, but no UI component is rendered. I think I may be misunderstanding something about how citations are implemented. Help would be appreciated.
Additional information
No response
The text was updated successfully, but these errors were encountered: