Skip to content

Commit

Permalink
cleanup and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
nwaughachukwuma committed Oct 26, 2024
1 parent 26c94d3 commit 73a1f5a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 50 deletions.
8 changes: 4 additions & 4 deletions backend/chat_with_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

def get_system_message(content_type: ContentType):
return f"""
1. You're a super-intelligent AI. Your task is to understand what audiocast the user wants to listen to.
1. You're a super-intelligent AI. Your task is to understand what audiocast a user wants to listen to.
2. You will steer the conversation until you have enough context after which you should terminate.
3. Keep the conversation short, say 2-3 back and forth - questions and answers.
4. As soon as the user's request is clear terminate the conversation by saying, "Ok, thanks for clarifying! Please click the button below to start generating the audiocast."
3. Keep the conversation short, say 3-5 back and forth i.e., questions and answers.
4. As soon as the user's request is clear terminate the conversation by saying, "Ok, thanks for clarifying! Based on your specifications, you want to listen to [Best case summary of user request so far]. Please click the button below to start generating the audiocast."
5. You can also terminate the conversation using a varied response strictly similar to (4) above.
6. If the user's request remains unclear after 3 responses for clarity, terminate the conversation by saying, "Your request is not very specific but from what I understand, you want to listen to [Best case summary of user request so far]. Please click the button below to start generating the audiocast."
6. If the user's request remains unclear after 5 responses for clarity, terminate the conversation by saying, "Your request is not very specific but from what I understand, you want to listen to [Best case summary of user request so far]. Please click the button below to start generating the audiocast."
GENERAL IDEA AND WORKFLOW:
Expand Down
22 changes: 5 additions & 17 deletions frontend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import httpx
import streamlit as st
from chat_utils import chat_request, content_types
from env_var import APP_URL, BACKEND_URL
from example_utils import content_types, display_example_cards
from example_utils import display_example_cards

# Initialize session state
if "chat_session_id" not in st.session_state:
Expand Down Expand Up @@ -46,22 +47,9 @@
with st.chat_message("user"):
st.write(prompt)

# Send message to backend
response = httpx.post(
f"{BACKEND_URL}/api/chat/{st.session_state.chat_session_id}",
json={
"message": {"role": "user", "content": prompt},
"content_type": content_type,
},
timeout=None,
)

response.raise_for_status()

if response.status_code == 200:
ai_message = ""
for line in response.iter_lines():
ai_message += line
ai_message = chat_request(prompt, content_type)

if ai_message:
st.session_state.messages.append({"role": "assistant", "content": ai_message})

with st.chat_message("assistant"):
Expand Down
38 changes: 38 additions & 0 deletions frontend/chat_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from typing import Dict, List, Literal

import httpx
import streamlit as st
from env_var import BACKEND_URL

ContentType = Literal["story", "podcast", "sermon", "science"]

content_types: List[ContentType] = ["story", "podcast", "sermon", "science"]

content_examples: Dict[ContentType, str] = {
"story": "Tell me a story about a magical kingdom with dragons and wizards.",
"podcast": "Create a podcast about the history of space exploration.",
"sermon": "Write a sermon about finding peace in times of trouble.",
"science": "Explain the concept of black holes in simple terms.",
}


def chat_request(prompt: str, content_type: ContentType):
"""
Send a chat request to the backend server and return the AI response.
"""
response = httpx.post(
f"{BACKEND_URL}/api/chat/{st.session_state.chat_session_id}",
json={
"message": {"role": "user", "content": prompt},
"content_type": content_type,
},
timeout=None,
)

response.raise_for_status()

ai_message = ""
for line in response.iter_lines():
ai_message += line

return ai_message
34 changes: 5 additions & 29 deletions frontend/example_utils.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
from typing import Dict, List, Literal

import httpx
import streamlit as st
from env_var import BACKEND_URL

ContentType = Literal["story", "podcast", "sermon", "science"]

content_types: List[ContentType] = ["story", "podcast", "sermon", "science"]

content_examples: Dict[ContentType, str] = {
"story": "Tell me a story about a magical kingdom with dragons and wizards.",
"podcast": "Create a podcast about the history of space exploration.",
"sermon": "Write a sermon about finding peace in times of trouble.",
"science": "Explain the concept of black holes in simple terms.",
}
from chat_utils import chat_request, content_examples


def display_example_cards():
Expand Down Expand Up @@ -48,23 +34,13 @@ def display_example_cards():
if st.button(example, use_container_width=True):
# Add selected example to messages and trigger rerun to enter chat mode
st.session_state.messages.append({"role": "user", "content": example})
response = httpx.post(
f"{BACKEND_URL}/api/chat/{st.session_state.chat_session_id}",
json={
"message": {"role": "user", "content": example},
"content_type": content_type,
},
timeout=None,
)

response.raise_for_status()

if response.status_code == 200:
ai_message = ""
for line in response.iter_lines():
ai_message += line
ai_message = chat_request(example, content_type)
if ai_message:
st.session_state.messages.append(
{"role": "assistant", "content": ai_message}
)

st.rerun()
else:
st.error("Failed to generate AI response. Please try again.")

0 comments on commit 73a1f5a

Please sign in to comment.