Skip to content

Commit

Permalink
Refactor GPTDirectApproach class and update Chat component
Browse files Browse the repository at this point in the history
  • Loading branch information
dayland-ms committed Mar 20, 2024
1 parent 516fb2c commit cd97700
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 73 deletions.
85 changes: 15 additions & 70 deletions app/backend/approaches/gpt_direct_approach.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,15 @@ class GPTDirectApproach(Approach):
USER = "user"
ASSISTANT = "assistant"

system_message_chat_conversation = """You are an Azure OpenAI Completion system. Your persona is {systemPersona} who helps answer questions about an agency's data. {response_length_prompt}
User persona is {userPersona}
Your goal is to provide accurate and relevant answers based on the facts. Make sure to avoid making assumptions or adding personal opinions.
Emphasize the use of facts.
Here is how you should answer every question:
-Please respond with relevant information from the data in the response.
system_message_chat_conversation = """You are an Azure OpenAI Completion system. Your persona is {systemPersona} who helps users interact with a Large Language Model. {response_length_prompt}
User persona is {userPersona}. You are having a conversation with a user and you need to provide a response.
{follow_up_questions_prompt}
{injected_prompt}
"""
follow_up_questions_prompt_content = """
Generate three very brief follow-up questions that the user would likely ask next about their agencies data. Use triple angle brackets to reference the questions, e.g. <<<Are there exclusions for prescriptions?>>>. Try not to repeat questions that have already been asked.
Generate three very brief follow-up questions that the user would likely ask next about their previous chat context. Use triple angle brackets to reference the questions, e.g. <<<Are there exclusions for prescriptions?>>>. Try not to repeat questions that have already been asked.
Only generate questions and do not generate any text before or after the questions, such as 'Next Questions'
"""

Expand Down Expand Up @@ -105,33 +99,7 @@ async def run(self, history: Sequence[dict[str, str]], overrides: dict[str, Any]
system_persona = overrides.get("system_persona", "")
response_length = int(overrides.get("response_length") or 1024)

user_q = 'Generate search query for: ' + history[-1]["user"]

query_prompt=self.query_prompt_template.format(query_term_language=self.query_term_language)

# STEP 1: Generate an optimized keyword search query based on the chat history and the last question
messages = self.get_messages_from_history(
query_prompt,
self.model_name,
history,
user_q,
self.query_prompt_few_shots,
self.chatgpt_token_limit - len(user_q)
)

chat_completion = openai.ChatCompletion.create(
deployment_id=self.chatgpt_deployment,
model=self.model_name,
messages=messages,
temperature=0.0,
max_tokens=32,
n=1)

generated_query = chat_completion.choices[0].message.content
#if we fail to generate a query, return the last user question
if generated_query.strip() == "0":
generated_query = history[-1]["user"]

user_q = 'Generate response for: ' + history[-1]["user"]

#Generate the follow up prompt to be sent to the GPT model
follow_up_questions_prompt = (
Expand All @@ -140,39 +108,16 @@ async def run(self, history: Sequence[dict[str, str]], overrides: dict[str, Any]
else ""
)

# Allow client to replace the entire prompt, or to inject into the existing prompt using >>>
prompt_override = overrides.get("prompt_template")

if prompt_override is None:
system_message = self.system_message_chat_conversation.format(
injected_prompt="",
follow_up_questions_prompt=follow_up_questions_prompt,
response_length_prompt=self.get_response_length_prompt_text(
response_length
),
userPersona=user_persona,
systemPersona=system_persona,
)
elif prompt_override.startswith(">>>"):
system_message = self.system_message_chat_conversation.format(
injected_prompt=prompt_override[3:] + "\n ",
follow_up_questions_prompt=follow_up_questions_prompt,
response_length_prompt=self.get_response_length_prompt_text(
response_length
),
userPersona=user_persona,
systemPersona=system_persona,
)
else:
system_message = self.system_message_chat_conversation.format(
follow_up_questions_prompt=follow_up_questions_prompt,
response_length_prompt=self.get_response_length_prompt_text(
response_length
),
userPersona=user_persona,
systemPersona=system_persona,
)

system_message = self.system_message_chat_conversation.format(
injected_prompt="",
follow_up_questions_prompt=follow_up_questions_prompt,
response_length_prompt=self.get_response_length_prompt_text(
response_length
),
userPersona=user_persona,
systemPersona=system_persona,
)

#Generate a contextual and content-specific answer using the search results and chat history.
#Added conditional block to use different system messages for different models.
messages = self.get_messages_from_history(
Expand All @@ -198,7 +143,7 @@ async def run(self, history: Sequence[dict[str, str]], overrides: dict[str, Any]
return {
"data_points": [],
"answer": f"{urllib.parse.unquote(chat_completion.choices[0].message.content)}",
"thoughts": f"Searched for:<br>{generated_query}<br><br>Conversations:<br>" + msg_to_display.replace('\n', '<br>'),
"thoughts": f"Searched for:<br>{user_q}<br><br>Conversations:<br>" + msg_to_display.replace('\n', '<br>'),
"citation_lookup": {}
}

4 changes: 1 addition & 3 deletions app/frontend/src/pages/chat/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -439,9 +439,7 @@ const Chat = () => {
<TextField className={styles.chatSettingsSeparator} defaultValue={userPersona} label="User Persona" onChange={onUserPersonaChange} />
<TextField className={styles.chatSettingsSeparator} defaultValue={systemPersona} label="System Persona" onChange={onSystemPersonaChange} />
<ResponseLengthButtonGroup className={styles.chatSettingsSeparator} onClick={onResponseLengthChange} defaultValue={responseLength} />
{activeChatMode != ChatMode.Ungrounded &&
<ResponseTempButtonGroup className={styles.chatSettingsSeparator} onClick={onResponseTempChange} defaultValue={responseTemp} />
}
<ResponseTempButtonGroup className={styles.chatSettingsSeparator} onClick={onResponseTempChange} defaultValue={responseTemp} />
{activeChatMode != ChatMode.Ungrounded &&
<div>
<Separator className={styles.chatSettingsSeparator}>Filter Search Results by</Separator>
Expand Down

0 comments on commit cd97700

Please sign in to comment.