提问人:EW1 提问时间:10/20/2023 最后编辑:EW1 更新时间:10/24/2023 访问量:52
面板 - 使用 Enter 键而不是 Button 来输入信息
Panel - Using the Enter key rather than a Button, for entering information
问:
请注意:该代码取自 OpenAI 和 DeepLearning.AI 的“ChatGPT Prompt Engineering for Developers”课程。
我的问题是关于库面板的使用,特别是如何使用 Enter 键而不是使用按钮来输入信息,代码如下所示。
请注意,要使以下代码正常工作,您需要注册并获取 openai api_key。
要安装 OpenAI Python 库,请执行以下操作:
!pip 安装 openai 该库需要使用您帐户的密钥进行配置,该密钥可在以下网站上找到:https://platform.openai.com/account/api-keys
在使用库之前,您可以将其设置为OPENAI_API_KEY环境变量:
!export OPENAI_API_KEY='sk-...' 或者,将 openai.api_key 设置为其值:
导入 OpenAI openai.api_key = “sk-...”
然而,问题在于 Panel 的使用,而不是 chatGPT。
问题是如何使用 Enter 键而不是使用按钮来输入信息。
代码如下:
import os
import openai
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv()) # read local .env file
openai.api_key = os.getenv('OPENAI_API_KEY')
def get_completion_from_messages(messages, model="gpt-3.5-turbo", temperature=0):
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=temperature, # this is the degree of randomness of the model's output
)
return response.choices[0].message["content"]
def collect_messages(_):
prompt = inp.value_input
inp.value = ''
context.append({'role':'user', 'content':f"{prompt}"})
response = get_completion_from_messages(context)
context.append({'role':'assistant', 'content':f"{response}"})
panels.append(
pn.Row('User:', pn.pane.Markdown(prompt, width=600)))
panels.append(
pn.Row('Assistant:', pn.pane.Markdown(response, width=600, style={'background-color': \
'#F6F6F6'})))
return pn.Column(*panels)
import panel as pn # GUI
pn.extension()
panels = [] # collect display
context = [ {'role':'system', 'content':"""
You are OrderBot, an automated service to collect orders for a pizza
restaurant. \
You first greet the customer, then collects the order, \
and then asks if it's a pickup or delivery. \
You wait to collect the entire order, then summarize it and check for a
final \
time if the customer wants to add anything else. \
If it's a delivery, you ask for an address. \
Finally you collect the payment.\
Make sure to clarify all options, extras and sizes to uniquely \
identify the item from the menu.\
You respond in a short, very conversational friendly style. \
"""}]
# This is the relevant piece of code below.
inp = pn.widgets.TextInput(value="Hi", placeholder='Enter text here…')
button_conversation = pn.widgets.Button(name="Chat!")
interactive_conversation = pn.bind(collect_messages, button_conversation)
dashboard = pn.Column(
inp,
pn.Row(button_conversation),
pn.panel(interactive_conversation, loading_indicator=True, height=300),
)
dashboard
`
我想知道如何使用 Enter 键通过面板库输入信息,而不是使用按钮,如上面的代码所示。
答: 暂无答案
评论