AshokReddy commited on
Commit
cb27de2
1 Parent(s): 84a9542

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +60 -0
  2. requirements.txt +4 -0
  3. utils.py +26 -0
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ## Importing Libraries
2
+ import streamlit as st
3
+ from streamlit_chat import message
4
+ from utils import get_initial_message, get_chatgpt_response, update_chat
5
+ import openai
6
+
7
+ ## Retrieving the openAI Key from secrets.toml
8
+ openai.api_key = st.secrets["api_secret"]
9
+
10
+ ##Page Config
11
+ st.set_page_config(page_title= 'Travel Advisor',
12
+ page_icon= 'page_icon.jpg')
13
+ st.title('AI ChatBot - Travel')
14
+ st.subheader('powered by CloudHub (https://cloudhubs.nl/)')
15
+
16
+ ##Choosing the GPT Model
17
+ ##model = "gpt-3.5-turbo"
18
+ model = "gpt-4"
19
+
20
+ ##Initializing Session
21
+ if 'generated' not in st.session_state:
22
+ st.session_state['generated'] = []
23
+ if 'past' not in st.session_state:
24
+ st.session_state['past'] = []
25
+
26
+ if 'messages' not in st.session_state:
27
+ st.session_state['messages'] = get_initial_message()
28
+
29
+ ##Chat Display and expand functionality
30
+ if st.session_state['generated']:
31
+ for i in range(len(st.session_state['generated'])):
32
+ message(st.session_state['past'][i], is_user=True, key=str(i) + '_user')
33
+ message(st.session_state["generated"][i], key=str(i))
34
+
35
+ # Initialize reset_input flag
36
+ if 'reset_input' not in st.session_state:
37
+ st.session_state['reset_input'] = False
38
+
39
+ # Place the user input box and the button at the bottom
40
+ # Use reset_input flag to determine the default value of the input box
41
+ default_value = "" if st.session_state['reset_input'] else st.session_state.get('input', '')
42
+ query = st.text_input('Query: ', value=default_value, key='input')
43
+ clicked = st.button('Chat!')
44
+
45
+ ##Process the user input with a spinner graph
46
+ if clicked:
47
+ with st.spinner("generating..."):
48
+ messages = st.session_state['messages']
49
+ messages = update_chat(messages, "user", query)
50
+ response = get_chatgpt_response(messages, model)
51
+ messages = update_chat(messages, "assistant", response)
52
+ st.session_state.past.append(query)
53
+ st.session_state.generated.append(response)
54
+
55
+ # Indicate the need to reset the input box on the next render
56
+ st.session_state['reset_input'] = True
57
+ st.experimental_rerun()
58
+ else:
59
+ # If not clicked, reset the flag
60
+ st.session_state['reset_input'] = False
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ streamlit
2
+ streamlit-chat
3
+ openai
4
+ python-dotenv
utils.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai
2
+
3
+ ##Setup initial conversation
4
+ def get_initial_message():
5
+ messages=[
6
+ {"role": "system", "content": '''
7
+ You are the Virtual Advisor, a knowledgeable AI developed to act as a Travel Companion and Planne.
8
+ When a user expresses interest in traveling, immediately start by asking for details such as destination, origin, departure date.
9
+ Your expertise is limited to travel domain, and any questions related to other topics should be politely redirected
10
+ '''}
11
+ ]
12
+ return messages
13
+
14
+ ##take the messages and the model as input, makes an API call to ChatGPT, and returns the generated response
15
+ def get_chatgpt_response(messages, model="gpt-3.5-turbo"):
16
+ print("model: ", model)
17
+ response = openai.ChatCompletion.create(
18
+ model=model,
19
+ messages=messages
20
+ )
21
+ return response['choices'][0]['message']['content']
22
+
23
+ ##appends new messages to the conversation
24
+ def update_chat(messages, role, content):
25
+ messages.append({"role": role, "content": content})
26
+ return messages