-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ✨ Feat(chat): poc of chat using langstream + sse * ♻️ Refactoring(llm): move around class/defs/files * ✨ Feat(llm): select llm backend via envs * ✨ Feat(chat): integrate with rag capabilities * ✨ Feat(chat): select rag document from chat * 🐛 Bug(parrot): make parrot work with rag stream * ♻️ Refactoring(sse): cleanup and fix sse format * ♻️ Refactoring(openai): use template * ♻️ Refactoring: misc cleanup
- Loading branch information
1 parent
0b2ea96
commit e2c1929
Showing
29 changed files
with
665 additions
and
87 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
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
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
Empty file.
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,32 @@ | ||
from typing import Iterable, Any | ||
|
||
from langstream import Stream | ||
from langstream.contrib import OpenAIChatStream, OpenAIChatDelta, OpenAIChatMessage | ||
|
||
from fai_backend.chat.stream import create_chat_prompt | ||
from fai_backend.chat.template import PromptTemplate | ||
from fai_backend.llm.protocol import ILLMStreamProtocol | ||
from fai_backend.llm.models import LLMDataPacket | ||
|
||
|
||
class OpenAILLM(ILLMStreamProtocol): | ||
|
||
def __init__(self, template: PromptTemplate): | ||
self.template = template | ||
|
||
async def create(self) -> Stream[str, LLMDataPacket]: | ||
def messages(in_data: Any) -> Iterable[OpenAIChatMessage]: | ||
prompt = create_chat_prompt({ | ||
"name": self.template.name, | ||
"messages": self.template.messages, | ||
"settings": self.template.settings, | ||
}) | ||
prompt.format_prompt(self.template.input_map_fn(in_data)) | ||
return prompt.to_messages() | ||
|
||
return OpenAIChatStream[str, OpenAIChatDelta]( | ||
"RecipeStream", | ||
messages, | ||
model="gpt-4", | ||
temperature=0, | ||
).map(lambda delta: LLMDataPacket(content=delta.content, user_friendly=True)) |
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,40 @@ | ||
import asyncio | ||
from random import uniform | ||
from typing import Any | ||
|
||
from langstream import Stream | ||
|
||
from fai_backend.llm.protocol import ILLMStreamProtocol | ||
from fai_backend.llm.models import LLMDataPacket | ||
|
||
|
||
class ParrotLLM(ILLMStreamProtocol): | ||
""" | ||
Parrot (mock) LLM protocol reference implementation. | ||
Parrot will respond with the same message as its input, with a random delay between tokens (words). | ||
""" | ||
|
||
def __init__(self, min_delay: float = 0.1, max_delay: float = 1.0): | ||
self.min_delay = min_delay | ||
self.max_delay = max_delay | ||
|
||
async def to_generator(self, input_message: str | Any): | ||
if not isinstance(input_message, str): | ||
if isinstance(input_message, list) and "query" in input_message[0]: | ||
input_message = input_message[0]["query"] | ||
else: | ||
yield "squawk?" | ||
return | ||
|
||
import re | ||
parts = re.findall(r'\S+\s*', input_message) | ||
for part in parts: | ||
yield part | ||
await asyncio.sleep(uniform(self.min_delay, self.max_delay)) | ||
|
||
async def create(self) -> Stream[str, LLMDataPacket]: | ||
return Stream[str, str]( | ||
"ParrotStream", | ||
self.to_generator | ||
).map(lambda delta: LLMDataPacket(content=delta, user_friendly=True)) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import dataclasses | ||
from datetime import datetime | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class LLMMessage(BaseModel): | ||
date: datetime | ||
source: str | None = None | ||
content: str | None = None | ||
|
||
|
||
@dataclasses.dataclass | ||
class LLMDataPacket: | ||
content: str | ||
user_friendly: bool = False |
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,14 @@ | ||
from typing import Protocol | ||
|
||
from langstream import Stream | ||
|
||
from fai_backend.llm.models import LLMDataPacket | ||
|
||
|
||
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. | ||
""" | ||
... |
Empty file.
Empty file.
11 changes: 11 additions & 0 deletions
11
fai-rag-app/fai-backend/fai_backend/llm/serializer/impl/base64.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,11 @@ | ||
import base64 | ||
|
||
from pydantic import BaseModel | ||
|
||
from fai_backend.llm.serializer.protocol import ISerializer | ||
|
||
|
||
class Base64Serializer(ISerializer): | ||
def serialize(self, input_data: BaseModel) -> str: | ||
output_data: str = input_data.model_dump_json(exclude_none=True) | ||
return base64.b64encode(output_data.encode("utf-8")).decode("utf-8") |
8 changes: 8 additions & 0 deletions
8
fai-rag-app/fai-backend/fai_backend/llm/serializer/impl/json.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,8 @@ | ||
from pydantic import BaseModel | ||
|
||
from fai_backend.llm.serializer.protocol import ISerializer | ||
|
||
|
||
class JSONSerializer(ISerializer): | ||
def serialize(self, input_data: BaseModel) -> str: | ||
return input_data.model_dump_json(exclude_none=True) |
11 changes: 11 additions & 0 deletions
11
fai-rag-app/fai-backend/fai_backend/llm/serializer/protocol.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,11 @@ | ||
from typing import Protocol | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class ISerializer(Protocol): | ||
def serialize(self, input_data: BaseModel) -> str: | ||
""" | ||
""" | ||
... |
Oops, something went wrong.