jackllll1312 / midtern

0 stars 0 forks source link

對話框 #2

Open jackllll1312 opened 6 months ago

jackllll1312 commented 6 months ago

import toga from toga.style import Pack from toga.style.pack import COLUMN, ROW, RIGHT, LEFT from datetime import datetime from openai import AzureOpenAI

class ChatApp(toga.App): def init(self, *args, *kwargs): super().init(args, **kwargs) self.client = AzureOpenAI( api_key=("77a0ca4ac2524d5f97a937bf4b69e305"), api_version="2023-12-01-preview", azure_endpoint=("https://azure-openai-ais-poc.openai.azure.com/") ) self.conversation = [] self.system_message = {"role": "system", "content": "你是個非常有幫助的PWC AI助理,講中文"}

def startup(self):
    self.main_window = toga.MainWindow(title=self.formal_name, size=(600, 400))

    # Create conversation history boxes with a scrollbar
    self.assistant_history = toga.Box(style=Pack(direction=COLUMN, padding=5))
    self.user_history = toga.Box(style=Pack(direction=COLUMN, padding=5))
    self.user_messages = []  # 用戶對話歷史列表

    # Create user input box with a send button
    self.user_input = toga.TextInput(style=Pack(flex=1, padding=5))
    self.send_button = toga.Button('Send', on_press=self.send_message, style=Pack(padding=(0, 5)))

    # Create conversation and input boxes
    conversation_box = toga.Box(
        children=[self.assistant_history, self.user_history],
        style=Pack(direction=COLUMN, padding=5)
    )
    input_box = toga.Box(
        children=[self.user_input, self.send_button],
        style=Pack(direction=ROW, padding=5)
    )

    # Create the main layout box
    main_box = toga.Box(
        children=[conversation_box, input_box],
        style=Pack(direction=COLUMN)
    )

    self.main_window.content = main_box
    self.main_window.show()

def send_message(self, widget):
    user_input = self.user_input.value
    if user_input:
        current_time = datetime.now().strftime("%H:%M")

        # Append user message to the conversation
        user_message = {"role": "user", "content": user_input}
        self.conversation.append(user_message)

        # Generate reply from OpenAI Chat API
        response = self.client.chat.completions.create(
            model="TestPOC35Turbo",
            messages=self.conversation
        )

        # Extract assistant's reply from the response
        assistant_reply = response.choices[0].message.content

        # Append assistant's reply to the conversation
        assistant_message = {"role": "assistant", "content": assistant_reply}
        self.conversation.append(assistant_message)

        # Update conversation history with speech bubble-like display
        user_chat_bubble = self.create_chat_bubble('User', user_input)
        user_chat_bubble.style.justify = RIGHT  # 將對話框靠右對齊
        assistant_chat_bubble = self.create_chat_bubble('Assistant', assistant_reply)
        assistant_chat_bubble.style.justify = LEFT  # 將對話框靠左對齊
        self.user_history.add(user_chat_bubble)
        self.assistant_history.add(assistant_chat_bubble)

        # Save user message for history
        self.user_messages.append(user_chat_bubble)

        # Clear user input
        self.user_input.value = ''

def create_chat_bubble(self, sender, content):
# Create a chat bubble widget
    chat_bubble = toga.Box(style=Pack(padding=5, background_color='#0084ff' if sender == 'User' else '#e0e0e0'))

# Add a label with sender and content
    label = toga.Label(f'{sender}: {content}', style=Pack(color='white' if sender == 'User' else 'black'))
    label.style.justify = RIGHT if sender == 'User' else LEFT  # 設定 justify 屬性
    chat_bubble.add(label)

    return chat_bubble

def main(): return ChatApp('Chat App', 'org.beeware.chatapp')

if name == 'main': app = main() app.main_loop()

jackllll1312 commented 6 months ago

import toga from toga.style import Pack from toga.style.pack import COLUMN, ROW

class ChatApp(toga.App):

def startup(self):
    # Set up main window
    self.main_window = toga.MainWindow(title=self.formal_name, size=(600, 400))

    # Create a box to hold the conversation and the user input
    main_box = toga.Box(style=Pack(direction=COLUMN))

    # Create the conversation box with a scrollbar
    self.conversation_box = toga.ScrollContainer(style=Pack(direction=COLUMN, flex=1))

    # Create the user input box with a send button
    self.user_input = toga.TextInput(style=Pack(flex=1))
    self.send_button = toga.Button('Send', on_press=self.send_message)
    input_box = toga.Box(children=[self.user_input, self.send_button], style=Pack(direction=ROW))

    # Add the conversation and the input box to the main box
    main_box.add(self.conversation_box)
    main_box.add(input_box)

    # Set the main window content
    self.main_window.content = main_box
    self.main_window.show()

def send_message(self, widget):
    # Get the user input
    user_input = self.user_input.value

    # Create chat bubbles for the user and the assistant
    if user_input:
        # User chat bubble on the right
        user_label = toga.Label(user_input, style=Pack(padding_left=10, padding_right=10, padding_top=5, padding_bottom=5, background_color='royalblue', color='white'))
        user_chat_bubble = toga.Box(style=Pack(direction=ROW, padding=5, alignment='right'))
        user_chat_bubble.add(user_label)

        # Assistant chat bubble on the left
        assistant_label = toga.Label('...', style=Pack(padding_left=10, padding_right=10, padding_top=5, padding_bottom=5, background_color='lightgray', color='black'))
        assistant_chat_bubble = toga.Box(style=Pack(direction=ROW, padding=5, alignment='left'))
        assistant_chat_bubble.add(assistant_label)

        # Add chat bubbles to the conversation
        self.conversation_box.content.add(user_chat_bubble)
        self.conversation_box.content.add(assistant_chat_bubble)

        # Scroll to the bottom of the conversation box
        self.conversation_box.vertical_scroll = self.conversation_box.vertical_scroll.maximum

        # Clear the user input
        self.user_input.value = ''

        # Placeholder for where you would get the actual response
        assistant_label.text = 'Assistant response to: ' + user_input

def main(): return ChatApp('Chat App', 'org.beeware.chatapp')

if name == 'main': app = main() app.main_loop()

jackllll1312 commented 6 months ago

def create_chat_bubble(self, sender, content):

Create a chat bubble widget with rounded corners

chat_bubble = toga.Box(style=Pack(padding=10, background_color='#007AFF' if sender == 'User' else '#F0F0F0', border_radius=12))

# Add a label with sender and content
label = toga.Label(f'{content}', style=Pack(color='white' if sender == 'User' else 'black', padding_left=10))
chat_bubble.add(label)

return chat_bubble

Modify the main layout to include a dark background

def startup(self): ... main_box = toga.Box( children=[conversation_box, input_box], style=Pack(direction=COLUMN, background_color='#131313') # Dark background ) ...