-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_1.py
59 lines (41 loc) · 1.35 KB
/
main_1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import streamlit as st
import openai
import os
from dotenv import load_dotenv
load_dotenv()
openai.api_key = os.getenv("my_api_key")
# Set the model and chatbot's name
model_name = 'gpt-3.5-turbo'
chatbot_name = 'ChatGPT Bot'
# Initialize conversation history
conversation = []
# Main function to interact with the chatbot
def run_chatbot(message):
conversation.append({'role': 'user', 'content': message})
# Create the list of messages for OpenAI API
messages = [{'role': 'system', 'content': 'You are ChatGPT Bot.'}]
messages.extend(conversation)
# Call OpenAI API to generate a response
response = openai.ChatCompletion.create(
model=model_name,
messages=messages
)
# Get the generated reply from the response
reply = response['choices'][0]['message']['content']
conversation.append({'role': 'bot', 'content': reply})
return reply
# Streamlit app layout
def app_layout():
st.title(chatbot_name)
# Initial system message
st.markdown('Chat with the ChatGPT Bot!')
# User input
user_input = st.text_input("User:", "")
# Send user message and get bot's reply
if st.button("Send"):
bot_reply = run_chatbot(user_input)
# Display bot's reply
st.text_area("Bot:", value=bot_reply, height=100)
# Run the Streamlit app
if __name__ == '__main__':
app_layout()