Update app.py
Browse files
app.py
CHANGED
@@ -1,80 +1,80 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
from groq import Groq
|
3 |
-
from typing import List, Optional
|
4 |
-
from dotenv import load_dotenv
|
5 |
-
import json, os
|
6 |
-
from pydantic import BaseModel
|
7 |
-
from dspy_inference import get_expanded_query_and_topic
|
8 |
-
|
9 |
-
load_dotenv()
|
10 |
-
|
11 |
-
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
12 |
-
USER_AVATAR = "π€"
|
13 |
-
BOT_AVATAR = "π€"
|
14 |
-
|
15 |
-
if "messages" not in st.session_state:
|
16 |
-
st.session_state.messages = [{"role": "assistant", "content": "Hi, How can I help you today?"}]
|
17 |
-
if "conversation_state" not in st.session_state:
|
18 |
-
st.session_state["conversation_state"] = [{"role": "assistant", "content": "Hi, How can I help you today?"}]
|
19 |
-
|
20 |
-
def main():
|
21 |
-
st.title("
|
22 |
-
|
23 |
-
for message in st.session_state.messages:
|
24 |
-
image = USER_AVATAR if message["role"] == "user" else BOT_AVATAR
|
25 |
-
with st.chat_message(message["role"], avatar=image):
|
26 |
-
st.markdown(message["content"])
|
27 |
-
|
28 |
-
system_prompt = f'''You are a helpful assistant who can answer any question that the user asks.
|
29 |
-
'''
|
30 |
-
if prompt := st.chat_input("User input"):
|
31 |
-
st.chat_message("user", avatar=USER_AVATAR).markdown(prompt)
|
32 |
-
st.session_state.messages.append({"role": "user", "content": prompt})
|
33 |
-
conversation_context = st.session_state["conversation_state"]
|
34 |
-
conversation_context.append({"role": "user", "content": prompt})
|
35 |
-
|
36 |
-
# Use dspy to expand the query and get the topic
|
37 |
-
expanded_query = get_expanded_query_and_topic(prompt, conversation_context)
|
38 |
-
|
39 |
-
context = []
|
40 |
-
context.append({"role": "system", "content": system_prompt})
|
41 |
-
context.extend(st.session_state["conversation_state"])
|
42 |
-
|
43 |
-
# Add the expanded query to the context
|
44 |
-
if expanded_query.expand != "None":
|
45 |
-
context.append({"role": "system", "content": f"Expanded query: {expanded_query.expand}"})
|
46 |
-
context.append({"role": "system", "content": f"Topic: {expanded_query.topic}"})
|
47 |
-
|
48 |
-
response = client.chat.completions.create(
|
49 |
-
messages=context,
|
50 |
-
model="llama3-70b-8192",
|
51 |
-
temperature=0,
|
52 |
-
top_p=1,
|
53 |
-
stop=None,
|
54 |
-
stream=True,
|
55 |
-
)
|
56 |
-
|
57 |
-
with st.chat_message("assistant", avatar=BOT_AVATAR):
|
58 |
-
result = ""
|
59 |
-
res_box = st.empty()
|
60 |
-
for chunk in response:
|
61 |
-
if chunk.choices[0].delta.content:
|
62 |
-
new_content = chunk.choices[0].delta.content
|
63 |
-
result += new_content
|
64 |
-
res_box.markdown(f'{result}')
|
65 |
-
|
66 |
-
# Display expanded question and tags separately
|
67 |
-
st.markdown("---")
|
68 |
-
# st.markdown("**Query Analysis:**")
|
69 |
-
if expanded_query.expand != "None":
|
70 |
-
st.markdown(f"**Expanded Question:** {expanded_query.expand}")
|
71 |
-
else:
|
72 |
-
st.markdown("**Expanded Question:** No expansion needed")
|
73 |
-
st.markdown(f"**Topic:** {expanded_query.topic}")
|
74 |
-
|
75 |
-
assistant_response = result
|
76 |
-
st.session_state.messages.append({"role": "assistant", "content": assistant_response})
|
77 |
-
conversation_context.append({"role": "assistant", "content": assistant_response})
|
78 |
-
|
79 |
-
if __name__ == '__main__':
|
80 |
main()
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from groq import Groq
|
3 |
+
from typing import List, Optional
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
import json, os
|
6 |
+
from pydantic import BaseModel
|
7 |
+
from dspy_inference import get_expanded_query_and_topic
|
8 |
+
|
9 |
+
load_dotenv()
|
10 |
+
|
11 |
+
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
12 |
+
USER_AVATAR = "π€"
|
13 |
+
BOT_AVATAR = "π€"
|
14 |
+
|
15 |
+
if "messages" not in st.session_state:
|
16 |
+
st.session_state.messages = [{"role": "assistant", "content": "Hi, How can I help you today?"}]
|
17 |
+
if "conversation_state" not in st.session_state:
|
18 |
+
st.session_state["conversation_state"] = [{"role": "assistant", "content": "Hi, How can I help you today?"}]
|
19 |
+
|
20 |
+
def main():
|
21 |
+
st.title("Query expansion and tagging")
|
22 |
+
|
23 |
+
for message in st.session_state.messages:
|
24 |
+
image = USER_AVATAR if message["role"] == "user" else BOT_AVATAR
|
25 |
+
with st.chat_message(message["role"], avatar=image):
|
26 |
+
st.markdown(message["content"])
|
27 |
+
|
28 |
+
system_prompt = f'''You are a helpful assistant who can answer any question that the user asks.
|
29 |
+
'''
|
30 |
+
if prompt := st.chat_input("User input"):
|
31 |
+
st.chat_message("user", avatar=USER_AVATAR).markdown(prompt)
|
32 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
33 |
+
conversation_context = st.session_state["conversation_state"]
|
34 |
+
conversation_context.append({"role": "user", "content": prompt})
|
35 |
+
|
36 |
+
# Use dspy to expand the query and get the topic
|
37 |
+
expanded_query = get_expanded_query_and_topic(prompt, conversation_context)
|
38 |
+
|
39 |
+
context = []
|
40 |
+
context.append({"role": "system", "content": system_prompt})
|
41 |
+
context.extend(st.session_state["conversation_state"])
|
42 |
+
|
43 |
+
# Add the expanded query to the context
|
44 |
+
if expanded_query.expand != "None":
|
45 |
+
context.append({"role": "system", "content": f"Expanded query: {expanded_query.expand}"})
|
46 |
+
context.append({"role": "system", "content": f"Topic: {expanded_query.topic}"})
|
47 |
+
|
48 |
+
response = client.chat.completions.create(
|
49 |
+
messages=context,
|
50 |
+
model="llama3-70b-8192",
|
51 |
+
temperature=0,
|
52 |
+
top_p=1,
|
53 |
+
stop=None,
|
54 |
+
stream=True,
|
55 |
+
)
|
56 |
+
|
57 |
+
with st.chat_message("assistant", avatar=BOT_AVATAR):
|
58 |
+
result = ""
|
59 |
+
res_box = st.empty()
|
60 |
+
for chunk in response:
|
61 |
+
if chunk.choices[0].delta.content:
|
62 |
+
new_content = chunk.choices[0].delta.content
|
63 |
+
result += new_content
|
64 |
+
res_box.markdown(f'{result}')
|
65 |
+
|
66 |
+
# Display expanded question and tags separately
|
67 |
+
st.markdown("---")
|
68 |
+
# st.markdown("**Query Analysis:**")
|
69 |
+
if expanded_query.expand != "None":
|
70 |
+
st.markdown(f"**Expanded Question:** {expanded_query.expand}")
|
71 |
+
else:
|
72 |
+
st.markdown("**Expanded Question:** No expansion needed")
|
73 |
+
st.markdown(f"**Topic:** {expanded_query.topic}")
|
74 |
+
|
75 |
+
assistant_response = result
|
76 |
+
st.session_state.messages.append({"role": "assistant", "content": assistant_response})
|
77 |
+
conversation_context.append({"role": "assistant", "content": assistant_response})
|
78 |
+
|
79 |
+
if __name__ == '__main__':
|
80 |
main()
|