-
Notifications
You must be signed in to change notification settings - Fork 2.1k
refactor: stream_llm_answer #4772
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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
68 changes: 68 additions & 0 deletions
68
backend/onyx/agents/agent_search/shared_graph_utils/llm.py
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from datetime import datetime | ||
from typing import Literal | ||
|
||
from langchain.schema.language_model import LanguageModelInput | ||
from langgraph.types import StreamWriter | ||
|
||
from onyx.agents.agent_search.shared_graph_utils.utils import write_custom_event | ||
from onyx.chat.models import AgentAnswerPiece | ||
from onyx.llm.interfaces import LLM | ||
|
||
|
||
def stream_llm_answer( | ||
llm: LLM, | ||
prompt: LanguageModelInput, | ||
event_name: str, | ||
writer: StreamWriter, | ||
agent_answer_level: int, | ||
agent_answer_question_num: int, | ||
agent_answer_type: Literal["agent_level_answer", "agent_sub_answer"], | ||
timeout_override: int | None = None, | ||
max_tokens: int | None = None, | ||
) -> tuple[list[str], list[float]]: | ||
"""Stream the initial answer from the LLM. | ||
|
||
Args: | ||
llm: The LLM to use. | ||
prompt: The prompt to use. | ||
event_name: The name of the event to write. | ||
writer: The writer to write to. | ||
agent_answer_level: The level of the agent answer. | ||
agent_answer_question_num: The question number within the level. | ||
agent_answer_type: The type of answer ("agent_level_answer" or "agent_sub_answer"). | ||
timeout_override: The LLM timeout to use. | ||
max_tokens: The LLM max tokens to use. | ||
|
||
Returns: | ||
A tuple of the response and the dispatch timings. | ||
""" | ||
response: list[str] = [] | ||
dispatch_timings: list[float] = [] | ||
|
||
for message in llm.stream( | ||
prompt, timeout_override=timeout_override, max_tokens=max_tokens | ||
): | ||
# TODO: in principle, the answer here COULD contain images, but we don't support that yet | ||
content = message.content | ||
if not isinstance(content, str): | ||
raise ValueError( | ||
f"Expected content to be a string, but got {type(content)}" | ||
) | ||
|
||
start_stream_token = datetime.now() | ||
write_custom_event( | ||
event_name, | ||
AgentAnswerPiece( | ||
answer_piece=content, | ||
level=agent_answer_level, | ||
level_question_num=agent_answer_question_num, | ||
answer_type=agent_answer_type, | ||
), | ||
writer, | ||
) | ||
end_stream_token = datetime.now() | ||
|
||
dispatch_timings.append((end_stream_token - start_stream_token).microseconds) | ||
response.append(content) | ||
|
||
return response, dispatch_timings |
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.
Uh oh!
There was an error while loading. Please reload this page.