-
Notifications
You must be signed in to change notification settings - Fork 469
Add streaming #61
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
Add streaming #61
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
7a36af6
refactor and add streaming functions
john0isaac 172104c
refactor code
john0isaac 24ac36b
implement chat/stream/ endpoint and fix empty choices error
john0isaac 04cf3b2
add tests and fix db_session issue
john0isaac 6beb091
fix typo and add missing check
john0isaac c48dac3
initial frontend
john0isaac 3e50e61
fix type for streaming to conform with Microsoft Chat Protocol
john0isaac 5ca4ac3
add working streaming frontend
john0isaac bcfabd0
Update src/backend/fastapi_app/rag_simple.py
john0isaac f5a2733
apply feedback from pr review
john0isaac 2663256
add pytest-snapshot
john0isaac 063bb9d
add type for chat overrides
john0isaac 5a568e3
Merge branch 'main' into add-streaming
pamelafox 3d726dd
Use jsonlines for gitattributes to work
pamelafox ebdb48b
Update test filenames and folder name
pamelafox 0227789
Refactor to avoid error when streaming
pamelafox 8b32800
Change back to log info
pamelafox 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
-r src/backend/requirements.txt | ||
ruff | ||
mypy | ||
pre-commit | ||
pip-tools | ||
pip-compile-cross-platform | ||
pytest | ||
pytest-cov | ||
pytest-asyncio | ||
pytest-snapshot | ||
mypy | ||
locust |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import pathlib | ||
from abc import ABC, abstractmethod | ||
from collections.abc import AsyncGenerator | ||
|
||
from openai.types.chat import ChatCompletionMessageParam | ||
|
||
from fastapi_app.api_models import ( | ||
ChatParams, | ||
ChatRequestOverrides, | ||
RetrievalResponse, | ||
RetrievalResponseDelta, | ||
ThoughtStep, | ||
) | ||
from fastapi_app.postgres_models import Item | ||
|
||
|
||
class RAGChatBase(ABC): | ||
current_dir = pathlib.Path(__file__).parent | ||
query_prompt_template = open(current_dir / "prompts/query.txt").read() | ||
answer_prompt_template = open(current_dir / "prompts/answer.txt").read() | ||
|
||
def get_params(self, messages: list[ChatCompletionMessageParam], overrides: ChatRequestOverrides) -> ChatParams: | ||
response_token_limit = 1024 | ||
prompt_template = overrides.prompt_template or self.answer_prompt_template | ||
|
||
enable_text_search = overrides.retrieval_mode in ["text", "hybrid", None] | ||
enable_vector_search = overrides.retrieval_mode in ["vectors", "hybrid", None] | ||
|
||
original_user_query = messages[-1]["content"] | ||
if not isinstance(original_user_query, str): | ||
raise ValueError("The most recent message content must be a string.") | ||
past_messages = messages[:-1] | ||
|
||
return ChatParams( | ||
top=overrides.top, | ||
temperature=overrides.temperature, | ||
retrieval_mode=overrides.retrieval_mode, | ||
use_advanced_flow=overrides.use_advanced_flow, | ||
response_token_limit=response_token_limit, | ||
prompt_template=prompt_template, | ||
enable_text_search=enable_text_search, | ||
enable_vector_search=enable_vector_search, | ||
original_user_query=original_user_query, | ||
past_messages=past_messages, | ||
) | ||
|
||
@abstractmethod | ||
async def prepare_context( | ||
self, chat_params: ChatParams | ||
) -> tuple[list[ChatCompletionMessageParam], list[Item], list[ThoughtStep]]: | ||
raise NotImplementedError | ||
|
||
@abstractmethod | ||
async def answer( | ||
self, | ||
chat_params: ChatParams, | ||
contextual_messages: list[ChatCompletionMessageParam], | ||
results: list[Item], | ||
earlier_thoughts: list[ThoughtStep], | ||
) -> RetrievalResponse: | ||
raise NotImplementedError | ||
|
||
@abstractmethod | ||
async def answer_stream( | ||
self, | ||
chat_params: ChatParams, | ||
contextual_messages: list[ChatCompletionMessageParam], | ||
results: list[Item], | ||
earlier_thoughts: list[ThoughtStep], | ||
) -> AsyncGenerator[RetrievalResponseDelta, None]: | ||
raise NotImplementedError | ||
if False: | ||
yield 0 |
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.
Uh oh!
There was an error while loading. Please reload this page.