yyin0 commited on
Commit
940e926
·
1 Parent(s): 374a607

bot added

Browse files
Files changed (2) hide show
  1. app.py +13 -1
  2. helper.py +53 -0
app.py CHANGED
@@ -3,7 +3,7 @@ from datetime import datetime
3
 
4
  import streamlit as st
5
 
6
- from helper import current_year, invoke_search_api
7
 
8
  # API KEY
9
  API_KEY = os.environ["API_KEY"]
@@ -169,6 +169,18 @@ if prompt := st.chat_input(
169
  response = f"""
170
  Please see search results below: \n{md_data}
171
  """
 
 
 
 
 
 
 
 
 
 
 
 
172
  except Exception as e:
173
  st.warning(f"Failed to fetch data: {e}")
174
  response = "We are fixing the API right now. Please check back later."
 
3
 
4
  import streamlit as st
5
 
6
+ from helper import ChatBot, current_year, invoke_search_api
7
 
8
  # API KEY
9
  API_KEY = os.environ["API_KEY"]
 
169
  response = f"""
170
  Please see search results below: \n{md_data}
171
  """
172
+
173
+ # API Call
174
+ bot = ChatBot()
175
+ bot.history = st.session_state.messages.copy() # Update history from messages
176
+ response = bot.generate_response(
177
+ f"""
178
+ Here's user prompt: {prompt}
179
+ Here's relevant content: {response}
180
+
181
+ Answer user prompt based on the relevant content."""
182
+ )
183
+
184
  except Exception as e:
185
  st.warning(f"Failed to fetch data: {e}")
186
  response = "We are fixing the API right now. Please check back later."
helper.py CHANGED
@@ -5,6 +5,7 @@ from typing import Any, Dict, List
5
 
6
  import requests
7
  import streamlit as st
 
8
 
9
  api_key = os.environ["API_KEY"]
10
 
@@ -67,3 +68,55 @@ def current_year() -> int:
67
 
68
  # Extract and return the current year
69
  return now.year
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  import requests
7
  import streamlit as st
8
+ from openai import OpenAI
9
 
10
  api_key = os.environ["API_KEY"]
11
 
 
68
 
69
  # Extract and return the current year
70
  return now.year
71
+
72
+
73
+ class ChatBot:
74
+ """
75
+ A simple chatbot class that interacts with the OpenAI API to generate responses
76
+ based on user prompts and maintains a conversation history.
77
+ """
78
+
79
+ def __init__(self):
80
+ """
81
+ Initialize the ChatBot instance by creating an OpenAI client and setting up
82
+ an initial conversation history with the role of 'system' as a helpful assistant.
83
+ """
84
+ self.client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
85
+ self.history = [{"role": "system", "content": "You are a helpful assistant."}]
86
+
87
+ def generate_response(self, prompt: str) -> str:
88
+ """
89
+ Generate a response from the chatbot based on the user's prompt.
90
+
91
+ Args:
92
+ prompt (str): The input message from the user.
93
+
94
+ Returns:
95
+ str: The chatbot's response to the provided prompt.
96
+ """
97
+ # Add the user's prompt to the history with the role of 'user'.
98
+ self.history.append({"role": "user", "content": prompt})
99
+
100
+ # Request a completion from the OpenAI API using the conversation history.
101
+ completion = self.client.chat.completions.create(
102
+ model="gpt-3.5-turbo", # NOTE: feel free to change it to gpt-4, or gpt-4o
103
+ messages=self.history,
104
+ )
105
+
106
+ # Extract the assistant's response from the completion result.
107
+ response = completion.choices[0].message.content
108
+
109
+ # Add the assistant's response to the history with the role of 'assistant'.
110
+ self.history.append({"role": "assistant", "content": response})
111
+
112
+ # Return the generated response.
113
+ return response
114
+
115
+ def get_history(self) -> list:
116
+ """
117
+ Retrieve the conversation history between the user and the chatbot.
118
+
119
+ Returns:
120
+ list: A list of messages representing the conversation history.
121
+ """
122
+ return self.history