-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
upd: docker deployment + chain links + DocumentedRunnable
- Loading branch information
1 parent
0eeef24
commit 9490ae2
Showing
33 changed files
with
947 additions
and
919 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
FROM python:3.11-slim | ||
|
||
WORKDIR /app | ||
|
||
COPY requirements.txt requirements.txt | ||
RUN pip install --no-cache-dir -r requirements.txt | ||
|
||
# Run as non-root user for security | ||
RUN useradd -m user | ||
RUN chown -R user:user /app | ||
USER user | ||
|
||
|
||
ENV PORT=8000 | ||
ENV ADMIN_MODE=0 | ||
ENV PYTHONPATH=. | ||
ENV DATABASE_URL=sqlite:///db/rag.sqlite3 | ||
|
||
|
||
EXPOSE $PORT | ||
|
||
COPY . ./backend | ||
|
||
CMD python -m uvicorn backend.main:app --host 0.0.0.0 --port $PORT |
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,3 @@ | ||
from backend.api_plugins.insecure_authentication.insecure_authentication import insecure_authentication_routes | ||
from backend.api_plugins.secure_authentication.secure_authentication import authentication_routes | ||
from backend.api_plugins.sessions.sessions import session_routes |
File renamed without changes.
File renamed without changes.
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 was deleted.
Oops, something went wrong.
27 changes: 27 additions & 0 deletions
27
backend/rag_components/chain_links/answer_question_from_docs_and_history.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,27 @@ | ||
"""This chain answers the provided question based on documents it retreives and the conversation history""" | ||
from langchain_core.retrievers import BaseRetriever | ||
from pydantic import BaseModel | ||
from backend.rag_components.chain_links.rag_basic import rag_basic | ||
from backend.rag_components.chain_links.condense_question import condense_question | ||
|
||
from backend.rag_components.chain_links.documented_runnable import DocumentedRunnable | ||
from backend.rag_components.chain_links.retrieve_and_format_docs import fetch_docs_chain | ||
|
||
|
||
class QuestionWithHistory(BaseModel): | ||
question: str | ||
chat_history: str | ||
|
||
|
||
class Response(BaseModel): | ||
response: str | ||
|
||
|
||
def answer_question_from_docs_and_history_chain(llm, retriever: BaseRetriever) -> DocumentedRunnable: | ||
reformulate_question = condense_question(llm) | ||
answer_question = rag_basic(llm, retriever) | ||
|
||
chain = reformulate_question | answer_question | ||
typed_chain = chain.with_types(input_type=QuestionWithHistory, output_type=Response) | ||
|
||
return DocumentedRunnable(typed_chain, chain_name="Answer question from docs and history", user_doc=__doc__) |
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,38 @@ | ||
"""This chain condenses the chat history and the question into one standalone question.""" | ||
from langchain_core.prompts import PromptTemplate | ||
from langchain_core.output_parsers import StrOutputParser | ||
from pydantic import BaseModel | ||
|
||
from backend.rag_components.chain_links.documented_runnable import DocumentedRunnable | ||
|
||
|
||
class QuestionWithChatHistory(BaseModel): | ||
question: str | ||
chat_history: str | ||
|
||
|
||
class StandaloneQuestion(BaseModel): | ||
standalone_question: str | ||
|
||
|
||
prompt = """ | ||
<s>[INST] <<SYS>> | ||
Given the conversation history and the following question, can you rephrase the user's question in its original language so that it is self-sufficient. You are presented with a conversation that may contain some spelling mistakes and grammatical errors, but your goal is to understand the underlying question. Make sure to avoid the use of unclear pronouns. | ||
If the question is already self-sufficient, return the original question. If it seem the user is authorizing the chatbot to answer without specific context, make sure to reflect that in the rephrased question. | ||
<</SYS>> | ||
Chat history: {chat_history} | ||
Question: {question} | ||
[/INST] | ||
""" # noqa: E501 | ||
|
||
|
||
def condense_question(llm) -> DocumentedRunnable: | ||
condense_question_prompt = PromptTemplate.from_template(prompt) # chat_history, question | ||
|
||
standalone_question = condense_question_prompt | llm | StrOutputParser() | ||
|
||
typed_chain = standalone_question.with_types(input_type=QuestionWithChatHistory, output_type=StandaloneQuestion) | ||
return DocumentedRunnable(typed_chain, chain_name="Condense question and history", prompt=prompt, user_doc=__doc__) |
Oops, something went wrong.