Skip to content

Commit

Permalink
Extract more config
Browse files Browse the repository at this point in the history
Signed-off-by: Hemslo Wang <[email protected]>
  • Loading branch information
hemslo committed Feb 20, 2024
1 parent 020d08c commit a4b2da6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 20 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
AUTH_TOKEN=
CHAT_PROVIDER=openai
DEBUG=0
EMBEDDING_DIM=1536
EMBEDDING_PROVIDER=openai
OLLAMA_CHAT_MODEL=llama2
Expand All @@ -9,3 +10,6 @@ OPENAI_API_KEY=
OPENAI_CHAT_MODEL=gpt-3.5-turbo-0125
OPENAI_EMBEDDING_MODEL=text-embedding-3-small
REDIS_URL=redis://localhost:6379/
REPHRASE_PROMPT=
RETRIEVAL_QA_CHAT_SYSTEM_PROMPT=
VERBOSE=0
23 changes: 3 additions & 20 deletions app/chains/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,10 @@
from langchain.prompts.prompt import PromptTemplate
from pydantic import Field, BaseModel

from app import config
from app.dependencies.llm import get_llm
from app.dependencies.redis import get_redis

RETRIEVAL_QA_CHAT_SYSTEM_PROMPT = """\
Answer any questions based solely on the context below:
<context>
{context}
</context>
"""

REPHRASE_PROMPT = """\
Given the following conversation and a follow up question, \
rephrase the follow up question to be a standalone question.
Chat History:
{chat_history}
Follow Up Input: {input}
Standalone Question:
"""


def build_chat_chain():
llm = get_llm()
Expand All @@ -52,7 +35,7 @@ def build_chat_chain():
},
)

rephrase_prompt = PromptTemplate.from_template(REPHRASE_PROMPT)
rephrase_prompt = PromptTemplate.from_template(config.REPHRASE_PROMPT)

retriever_chain = create_history_aware_retriever(
llm,
Expand All @@ -64,7 +47,7 @@ def build_chat_chain():
[
(
"system",
RETRIEVAL_QA_CHAT_SYSTEM_PROMPT,
config.RETRIEVAL_QA_CHAT_SYSTEM_PROMPT,
),
MessagesPlaceholder(variable_name="chat_history"),
(
Expand Down
33 changes: 33 additions & 0 deletions app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from pathlib import Path

from dotenv import load_dotenv
from langchain.globals import set_debug, set_verbose

load_dotenv()

Expand All @@ -10,6 +11,8 @@
raise ValueError("AUTH_TOKEN is not set in the environment variables")

CHAT_PROVIDER = os.environ.get("CHAT_PROVIDER", "openai")
DEBUG = os.getenv("DEBUG", "0") == "1"
VERBOSE = os.getenv("VERBOSE", "0") == "1"
EMBEDDING_DIM = int(os.environ.get("EMBEDDING_DIM", 1536))
EMBEDDING_PROVIDER = os.environ.get("EMBEDDING_PROVIDER", "openai")
INDEX_NAME = "document"
Expand All @@ -22,3 +25,33 @@
"OPENAI_EMBEDDING_MODEL", "text-embedding-3-small"
)
REDIS_URL = os.getenv("REDIS_URL", "redis://localhost:6379/")

REPHRASE_PROMPT = (
os.getenv("REPHRASE_PROMPT")
or """\
Given the following conversation and a follow up question, \
rephrase the follow up question to be a standalone question.
Chat History:
{chat_history}
Follow Up Input: {input}
Standalone Question:
"""
)

RETRIEVAL_QA_CHAT_SYSTEM_PROMPT = (
os.getenv("RETRIEVAL_QA_CHAT_SYSTEM_PROMPT")
or """\
You are an assistant for question-answering tasks. \
Use the following pieces of retrieved context to answer the question. \
If you don't know the answer, just say that you don't know. \
Use three sentences maximum and keep the answer concise.
<context>
{context}
</context>
"""
)

set_debug(DEBUG)
set_verbose(VERBOSE)

0 comments on commit a4b2da6

Please sign in to comment.