Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

integrate chatbot using openai api and enhanced user experience #75

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions chatbot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import os
import openai
import taipy

# Initialize OpenAI client with API key from environment variable
openai.api_key = os.environ.get("OPENAI_API_KEY")

# Create a Taipy GUI
gui = taipy.get_gui()

# Define the chatbot interface
page = """
<|{conversation}|table|show_all|width=100%|>
<|{current_user_message}|input|label=Write your message here...|on_action=send_message|class_name=fullwidth|>
"""

# Function to send messages and handle responses
def send_message(state: taipy.State) -> None:
# Prepare the conversation context
user_message = state.current_user_message.strip()
if user_message:
# Append user message to the context
state.context += f"Human: {user_message}\nAI:"

# Get response from OpenAI
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": state.context}]
)

# Extract the AI's response
ai_message = response['choices'][0]['message']['content'].strip()

# Update the conversation state
state.context += f"{ai_message}\n"
state.conversation.append({"Human": user_message, "AI": ai_message})

# Clear the input field for the next message
state.current_user_message = ""

# Run the Taipy application
if __name__ == "__main__":
gui.run(dark_mode=True, title="Fest Registration Chatbot", page=page)