Open jackllll1312 opened 4 months ago
from kivy.app import App from kivy.uix.boxlayout import BoxLayout from kivy.uix.scrollview import ScrollView from kivy.uix.textinput import TextInput from kivy.uix.button import Button from kivy.uix.label import Label from datetime import datetime from openai import AzureOpenAI
class ChatApp(App): def build(self): self.client = AzureOpenAI( api_key="77a0ca4ac2524d5f97a937bf4b69e305", api_version="2023-12-01-preview", azure_endpoint="https://azure-openai-ais-poc.openai.azure.com/" ) self.conversation = []
# Main layout
main_layout = BoxLayout(orientation='vertical', padding=10)
# Conversation history (scrollable)
self.conversation_history = BoxLayout(orientation='vertical', size_hint_y=None)
scroll_view = ScrollView(size_hint=(1, 0.9))
scroll_view.add_widget(self.conversation_history)
# User input and send button
input_layout = BoxLayout(orientation='horizontal', size_hint_y=None, height=50)
self.user_input = TextInput(size_hint_x=0.8, multiline=False)
send_button = Button(text='Send', size_hint_x=0.2)
send_button.bind(on_press=self.send_message)
input_layout.add_widget(self.user_input)
input_layout.add_widget(send_button)
main_layout.add_widget(scroll_view)
main_layout.add_widget(input_layout)
return main_layout
def send_message(self, instance):
user_input = self.user_input.text.strip()
if user_input:
current_time = datetime.now().strftime("%H:%M")
self.add_chat_bubble('User', user_input, current_time)
# Simulate API call and response
response = self.client.chat.completions.create(
model="TestPOC35Turbo",
messages=[{"role": "user", "content": user_input}]
)
assistant_reply = response.choices[0].message.content
self.add_chat_bubble('Assistant', assistant_reply, current_time)
self.user_input.text = '' # Clear input field
def add_chat_bubble(self, sender, content, timestamp):
# Create a chat bubble (label) and add it to the conversation history
chat_bubble = Label(text=f'{sender} ({timestamp}): {content}', size_hint_y=None, markup=True)
if sender == 'User':
chat_bubble.color = (0, 0.5, 1, 1)
else:
chat_bubble.color = (0, 0, 0, 1)
self.conversation_history.add_widget(chat_bubble)
if name == 'main': ChatApp().run()
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助理,講中文"}
Create conversation and input boxes
)
children=[conversation_box, input_box], style=Pack(direction=COLUMN, background_color='#131313') # Dark background )
Add both user and assistant messages to the conversation history
def main(): return ChatApp('Chat App', 'org.beeware.chatapp')
if name == 'main': app = main() app.main_loop()