Skip to content

Commit

Permalink
integrate chatbot using openai api and enhanced user experience
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushdubey570 committed Oct 23, 2024
1 parent bc0ce9c commit ae312c1
Showing 1 changed file with 43 additions and 0 deletions.
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)

0 comments on commit ae312c1

Please sign in to comment.