-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Feat(chat): integrate with rag capabilities
- Loading branch information
1 parent
7e3ca39
commit d89e562
Showing
10 changed files
with
201 additions
and
98 deletions.
There are no files selected for viewing
This file contains 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 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 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
26 changes: 26 additions & 0 deletions
26
fai-rag-app/fai-backend/fai_backend/llm/impl/rag_wrapper.py
This file contains 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,26 @@ | ||
from langstream import Stream | ||
|
||
from fai_backend.llm.protocol import ILLMStreamProtocol | ||
from fai_backend.llm.models import LLMDataPacket | ||
from fai_backend.llm.service import create_rag_stream | ||
|
||
|
||
class RAGWrapper(ILLMStreamProtocol): | ||
""" | ||
Wraps an underlying Stream with RAG capabilities. | ||
The underlying stream will be supplied with document extracts in plaintext | ||
from the given collection along with the original question. | ||
""" | ||
|
||
def __init__(self, input_query: str, base_llm: ILLMStreamProtocol, rag_collection_name: str): | ||
self.input_query = input_query | ||
self.rag_collection_name = rag_collection_name | ||
self.base_llm = base_llm | ||
|
||
async def create(self) -> Stream[str, LLMDataPacket]: | ||
rag_stream = await create_rag_stream(self.input_query, self.rag_collection_name) | ||
base_stream = await self.base_llm.create() | ||
|
||
return (rag_stream | ||
.and_then(base_stream)) |
This file contains 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 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,18 +1,14 @@ | ||
from typing import Protocol, AsyncGenerator | ||
from typing import Protocol | ||
|
||
from langstream import Stream, StreamOutput | ||
from langstream import Stream | ||
|
||
from fai_backend.llm.models import LLMDataPacket | ||
|
||
class ILLMProtocol(Protocol): | ||
def run(self, input_message: str) -> AsyncGenerator[StreamOutput[str], str]: | ||
""" | ||
Takes an input message and returns an async stream resolving to the response from the LLM. | ||
:param input_message: The input message, as a string. | ||
:type input_message: str | ||
:return: The resulting `Stream[str, str]` object. | ||
:rtype: Stream[str, str] | ||
|
||
class ILLMStreamProtocol(Protocol): | ||
async def create(self) -> Stream[str, LLMDataPacket]: | ||
""" | ||
Create a Stream that takes a str (generally a question) and returns | ||
a stream of tokens (strings) of the response given by the LLM. | ||
""" | ||
... |
This file contains 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.