SiddarthaRachakonda
commited on
Commit
•
6f793f1
1
Parent(s):
8a96d3e
modified history prompts
Browse files- app/chains.py +3 -2
- app/crud.py +11 -2
- app/main.py +10 -3
- app/prompts.py +12 -5
- app/schemas.py +3 -1
app/chains.py
CHANGED
@@ -5,6 +5,7 @@ import app.schemas as schemas
|
|
5 |
from app.prompts import (
|
6 |
raw_prompt,
|
7 |
raw_prompt_formatted,
|
|
|
8 |
format_context,
|
9 |
tokenizer
|
10 |
)
|
@@ -23,10 +24,10 @@ llm = HuggingFaceEndpoint(
|
|
23 |
simple_chain = (raw_prompt | llm).with_types(input_type=schemas.UserQuestion)
|
24 |
|
25 |
# TODO: create formatted_chain by piping raw_prompt_formatted and the LLM endpoint.
|
26 |
-
formatted_chain = raw_prompt_formatted | llm
|
27 |
|
28 |
# TODO: use history_prompt_formatted and HistoryInput to create the history_chain
|
29 |
-
history_chain =
|
30 |
|
31 |
# TODO: Let's construct the standalone_chain by piping standalone_prompt_formatted with the LLM
|
32 |
standalone_chain = None
|
|
|
5 |
from app.prompts import (
|
6 |
raw_prompt,
|
7 |
raw_prompt_formatted,
|
8 |
+
history_prompt_formatted,
|
9 |
format_context,
|
10 |
tokenizer
|
11 |
)
|
|
|
24 |
simple_chain = (raw_prompt | llm).with_types(input_type=schemas.UserQuestion)
|
25 |
|
26 |
# TODO: create formatted_chain by piping raw_prompt_formatted and the LLM endpoint.
|
27 |
+
formatted_chain = (raw_prompt_formatted | llm).with_types(input_type=schemas.UserQuestion)
|
28 |
|
29 |
# TODO: use history_prompt_formatted and HistoryInput to create the history_chain
|
30 |
+
history_chain = (history_prompt_formatted | llm).with_types(input_type=schemas.HistoryInput)
|
31 |
|
32 |
# TODO: Let's construct the standalone_chain by piping standalone_prompt_formatted with the LLM
|
33 |
standalone_chain = None
|
app/crud.py
CHANGED
@@ -1,6 +1,8 @@
|
|
1 |
from sqlalchemy.orm import Session
|
2 |
import app.models as models
|
3 |
import app.schemas as schemas
|
|
|
|
|
4 |
|
5 |
|
6 |
def get_or_create_user(db: Session, username: str):
|
@@ -18,7 +20,14 @@ def add_message(db: Session, message: schemas.MessageBase, username: str):
|
|
18 |
# - create a models.Message instance
|
19 |
# - pass the retrieved user to the message instance
|
20 |
# - save the message instance to the database
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
def get_user_chat_history(db: Session, username: str):
|
24 |
-
|
|
|
|
|
|
1 |
from sqlalchemy.orm import Session
|
2 |
import app.models as models
|
3 |
import app.schemas as schemas
|
4 |
+
from datetime import datetime
|
5 |
+
|
6 |
|
7 |
|
8 |
def get_or_create_user(db: Session, username: str):
|
|
|
20 |
# - create a models.Message instance
|
21 |
# - pass the retrieved user to the message instance
|
22 |
# - save the message instance to the database
|
23 |
+
user = get_or_create_user(db, username)
|
24 |
+
message = models.Message(message=message.message, type=message.type, user=user, timestamp=datetime.now(), user_id=user.id)
|
25 |
+
db.add(message)
|
26 |
+
db.commit()
|
27 |
+
db.refresh(message)
|
28 |
+
return message
|
29 |
|
30 |
def get_user_chat_history(db: Session, username: str):
|
31 |
+
|
32 |
+
user = get_or_create_user(db, username)
|
33 |
+
return user.messages
|
app/main.py
CHANGED
@@ -6,13 +6,13 @@ from langserve.serialization import WellKnownLCSerializer
|
|
6 |
from typing import List
|
7 |
from sqlalchemy.orm import Session
|
8 |
|
9 |
-
from app.chains import simple_chain, formatted_chain
|
10 |
import app.crud as crud
|
11 |
import app.models as models
|
12 |
import app.schemas as schemas
|
13 |
from app.database import SessionLocal, engine
|
14 |
from app.callbacks import LogResponseCallback
|
15 |
-
|
16 |
|
17 |
models.Base.metadata.create_all(bind=engine)
|
18 |
|
@@ -57,7 +57,14 @@ async def history_stream(request: Request, db: Session = Depends(get_db)):
|
|
57 |
# - We add as part of the user history the current question by using add_message.
|
58 |
# - We create an instance of HistoryInput by using format_chat_history.
|
59 |
# - We use the history input within the history chain.
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
|
63 |
@app.post("/rag/stream")
|
|
|
6 |
from typing import List
|
7 |
from sqlalchemy.orm import Session
|
8 |
|
9 |
+
from app.chains import simple_chain, formatted_chain, history_chain
|
10 |
import app.crud as crud
|
11 |
import app.models as models
|
12 |
import app.schemas as schemas
|
13 |
from app.database import SessionLocal, engine
|
14 |
from app.callbacks import LogResponseCallback
|
15 |
+
from app.prompts import format_chat_history
|
16 |
|
17 |
models.Base.metadata.create_all(bind=engine)
|
18 |
|
|
|
57 |
# - We add as part of the user history the current question by using add_message.
|
58 |
# - We create an instance of HistoryInput by using format_chat_history.
|
59 |
# - We use the history input within the history chain.
|
60 |
+
data = await request.json()
|
61 |
+
history_input = schemas.HistoryInput(**data['input'])
|
62 |
+
user_name = history_input.username
|
63 |
+
crud.add_message(db, schemas.MessageBase(message=history_input.question, type="user"), user_name)
|
64 |
+
chat_history = crud.get_user_chat_history(db, user_name)
|
65 |
+
chat_history_str = format_chat_history(chat_history)
|
66 |
+
history_input.chat_history = chat_history_str
|
67 |
+
return EventSourceResponse(generate_stream(history_input, history_chain))
|
68 |
|
69 |
|
70 |
@app.post("/rag/stream")
|
app/prompts.py
CHANGED
@@ -20,7 +20,10 @@ def format_prompt(prompt) -> PromptTemplate:
|
|
20 |
def format_chat_history(messages: List[models.Message]):
|
21 |
# TODO: implement format_chat_history to format
|
22 |
# the list of Message into a text of chat history.
|
23 |
-
|
|
|
|
|
|
|
24 |
|
25 |
def format_context(docs: List[str]):
|
26 |
# TODO: the output of the DataIndexer.search is a list of text,
|
@@ -33,7 +36,11 @@ raw_prompt = "{question}"
|
|
33 |
|
34 |
# TODO: Create the history_prompt prompt that will capture the question and the conversation history.
|
35 |
# The history_prompt needs a {chat_history} placeholder and a {question} placeholder.
|
36 |
-
history_prompt: str =
|
|
|
|
|
|
|
|
|
37 |
|
38 |
# TODO: Create the standalone_prompt prompt that will capture the question and the chat history
|
39 |
# to generate a standalone question. It needs a {chat_history} placeholder and a {question} placeholder,
|
@@ -48,11 +55,11 @@ raw_prompt_formatted = format_prompt(raw_prompt)
|
|
48 |
raw_prompt = PromptTemplate.from_template(raw_prompt)
|
49 |
|
50 |
# TODO: use format_prompt to create history_prompt_formatted
|
51 |
-
history_prompt_formatted: PromptTemplate =
|
52 |
# TODO: use format_prompt to create standalone_prompt_formatted
|
53 |
-
standalone_prompt_formatted: PromptTemplate =
|
54 |
# TODO: use format_prompt to create rag_prompt_formatted
|
55 |
-
rag_prompt_formatted: PromptTemplate =
|
56 |
|
57 |
|
58 |
|
|
|
20 |
def format_chat_history(messages: List[models.Message]):
|
21 |
# TODO: implement format_chat_history to format
|
22 |
# the list of Message into a text of chat history.
|
23 |
+
chat_history = ""
|
24 |
+
for message in messages:
|
25 |
+
chat_history += f"{message.type}: {message.message}\n"
|
26 |
+
return chat_history
|
27 |
|
28 |
def format_context(docs: List[str]):
|
29 |
# TODO: the output of the DataIndexer.search is a list of text,
|
|
|
36 |
|
37 |
# TODO: Create the history_prompt prompt that will capture the question and the conversation history.
|
38 |
# The history_prompt needs a {chat_history} placeholder and a {question} placeholder.
|
39 |
+
history_prompt: str = """Given the following conversation provide a helpful answer to the follow up question.
|
40 |
+
Chat History:
|
41 |
+
{chat_history}
|
42 |
+
Follow Up question: {question}
|
43 |
+
helpful answer:"""
|
44 |
|
45 |
# TODO: Create the standalone_prompt prompt that will capture the question and the chat history
|
46 |
# to generate a standalone question. It needs a {chat_history} placeholder and a {question} placeholder,
|
|
|
55 |
raw_prompt = PromptTemplate.from_template(raw_prompt)
|
56 |
|
57 |
# TODO: use format_prompt to create history_prompt_formatted
|
58 |
+
history_prompt_formatted: PromptTemplate = format_prompt(history_prompt)
|
59 |
# TODO: use format_prompt to create standalone_prompt_formatted
|
60 |
+
standalone_prompt_formatted: PromptTemplate = format_prompt(standalone_prompt)
|
61 |
# TODO: use format_prompt to create rag_prompt_formatted
|
62 |
+
rag_prompt_formatted: PromptTemplate = format_prompt(rag_prompt)
|
63 |
|
64 |
|
65 |
|
app/schemas.py
CHANGED
@@ -6,12 +6,14 @@ class UserQuestion(BaseModel):
|
|
6 |
|
7 |
# TODO: create a HistoryInput data model with a chat_history and question attributes.
|
8 |
class HistoryInput(BaseModel):
|
9 |
-
|
|
|
10 |
|
11 |
# TODO: let's create a UserRequest data model with a question and username attribute.
|
12 |
# This will be used to parse the input request.
|
13 |
class UserRequest(BaseModel):
|
14 |
username: str
|
|
|
15 |
|
16 |
# TODO: implement MessageBase as a schema mapping from the database model to the
|
17 |
# FastAPI data model. Basically MessageBase should have the same attributes as models.Message
|
|
|
6 |
|
7 |
# TODO: create a HistoryInput data model with a chat_history and question attributes.
|
8 |
class HistoryInput(BaseModel):
|
9 |
+
chat_history: str
|
10 |
+
question: str
|
11 |
|
12 |
# TODO: let's create a UserRequest data model with a question and username attribute.
|
13 |
# This will be used to parse the input request.
|
14 |
class UserRequest(BaseModel):
|
15 |
username: str
|
16 |
+
question: str
|
17 |
|
18 |
# TODO: implement MessageBase as a schema mapping from the database model to the
|
19 |
# FastAPI data model. Basically MessageBase should have the same attributes as models.Message
|