-
Notifications
You must be signed in to change notification settings - Fork 206
Add support for LLM token usage data streaming #116
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e83ea4c
Add support for LLM token usage data streaming
nv-nikkulkarni 8b9e3b4
Log the LLM token usage
nv-nikkulkarni d66d0de
Update to LLM API tokens in ChatNVIDIA
nv-nikkulkarni 626d011
(llm): switch to ChatOpenAI and fix token usage reporting
nv-nikkulkarni 9442c17
Fix failing init/integration tests
nv-nikkulkarni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,7 +48,7 @@ | |
| from langchain_core.output_parsers.string import StrOutputParser | ||
| from langchain_core.prompts import MessagesPlaceholder | ||
| from langchain_core.prompts.chat import ChatPromptTemplate | ||
| from langchain_core.runnables import RunnableAssign | ||
| from langchain_core.runnables import RunnableAssign, RunnableGenerator | ||
| from opentelemetry import context as otel_context | ||
| from requests import ConnectTimeout | ||
|
|
||
|
|
@@ -1408,14 +1408,15 @@ async def _llm_chain( | |
| prompt_template = ChatPromptTemplate.from_messages(message) | ||
| llm = get_llm(config=self.config, **llm_settings) | ||
|
|
||
| chain = ( | ||
| # Chain for streaming: we remove StrOutputParser so we yield AIMessageChunks, | ||
| # allowing us to access .usage_metadata in the response generator. | ||
| stream_chain = ( | ||
| prompt_template | ||
| | llm | ||
| | self.StreamingFilterThinkParser | ||
| | StrOutputParser() | ||
| ) | ||
| # Create async stream generator | ||
| stream_gen = chain.astream( | ||
| stream_gen = stream_chain.astream( | ||
| {"question": query_text}, config={"run_name": "llm-stream"} | ||
| ) | ||
| # Eagerly fetch first chunk to trigger any errors before returning response | ||
|
|
@@ -2437,7 +2438,16 @@ def generate_filter_for_collection(collection_name): | |
| self._print_conversation_history(message) | ||
| prompt = ChatPromptTemplate.from_messages(message) | ||
|
|
||
| chain = prompt | llm | self.StreamingFilterThinkParser | StrOutputParser() | ||
| # Base chain (no usage sentinel) used for non-streaming reflection path. | ||
| # We keep StrOutputParser here because we want the full string response for logic. | ||
| base_chain = prompt | llm | self.StreamingFilterThinkParser | StrOutputParser() | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check again if still valid |
||
|
|
||
| # Streaming chain: yields AIMessageChunks to preserve usage metadata. | ||
| stream_chain = ( | ||
| prompt | ||
| | llm | ||
| | self.StreamingFilterThinkParser | ||
| ) | ||
|
|
||
| # Check response groundedness if we still have reflection | ||
| # iterations available | ||
|
|
@@ -2496,8 +2506,8 @@ def generate_filter_for_collection(collection_name): | |
| status_code=ErrorCodeMapping.SUCCESS, | ||
| ) | ||
| else: | ||
| # Create async stream generator | ||
| stream_gen = chain.astream( | ||
| # Create async stream generator using the streaming chain | ||
| stream_gen = stream_chain.astream( | ||
| {"question": query, "context": docs}, | ||
| config={"run_name": "llm-stream"}, | ||
| ) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check again if still valid