gpt4 book ai didi

python - 使用 Python 和 Kivy 库创建具有 Openai 人工智能的虚拟助手

转载 作者:行者123 更新时间:2023-12-02 05:47:15 24 4
gpt4 key购买 nike

如标题所示,我用这段代码创建了一个虚拟助手

import json
import openai

with open("secrets.json") as f:
secrets = json.load(f)
api_key = secrets["api_key"]

openai.api_key = api_key


def get_response(messages: list):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
temperature=1.0 # 0.0 - 2.0
)
return response.choices[0].message


if __name__ == "__main__":
messages = [
{"role": "system", "content": "Sei un assistente virtuale chiamata JOI e parli italiano."}
]
try:
while True:
user_input = input("\nYou: ")
messages.append({"role": "user", "content": user_input})
new_message = get_response(messages=messages)
print(f"\nJOI: {new_message['content']}")

messages.append(new_message)
except KeyboardInterrupt:
print("see you soon bye!")

虚拟助手在终端中工作正常,但我希望使用 kivy 将消息写入标签,所以我安装了 kivy 和 kivymd 并编写了这段代码

from kivymd.app import MDApp
from kivy.lang import Builder

KV = '''
Screen:
GridLayout:
rows: 2
ScrollView:
MDLabel:
id: mdlab
text: "Virtual Assistant!"
size_hint_y: None
padding_x: 30
height: self.texture_size[1]
text_size: self.width, None

MDRaisedButton:
id: mdbu
text: "GO!"
size_hint_x: 1
on_press: app.message()
'''

class virtualAssistantApp(MDApp):

def build(self):
self.title = "Virtual Assistant"
self.theme_cls.theme_style = "Light" # "Dark"
self.theme_cls.primary_palette = "Teal"
self.theme_cls.primary_hue = "400"
return Builder.load_string(KV)

def message(self):
self.root.ids["mdlab"].text = "Button pressed"

virtualAssistantApp().run()

如何在 ID 为 mdlab 的标签中插入消息?

最佳答案

您可以在 message() 方法中使用 += 而不是 =:

def message(self):
self.root.ids["mdlab"].text += "\nButton pressed"

关于python - 使用 Python 和 Kivy 库创建具有 Openai 人工智能的虚拟助手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75676544/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com