Please note: The code is taken from the course "ChatGPT Prompt Engineering for Developers" by OpenAI and DeepLearning.AI.
My question is about the use of the library Panel, specifically how I can use the Enter key to enter information rather than using a button, the code appears below.
Please note that for the following code to work you will need to register and obtain an openai api_key.
To install the OpenAI Python library:
!pip install openai The library needs to be configured with your account's secret key, which is available on the website: https://platform.openai.com/account/api-keys
You can either set it as the OPENAI_API_KEY environment variable before using the library:
!export OPENAI_API_KEY='sk-...' Or, set openai.api_key to its value:
import openai openai.api_key = "sk-..."
However, the question is about the use of Panel, rather than about chatGPT.
The question is how I can use the Enter key to enter information rather than using a button.
The code is as follows:
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
`
I would like to know how it is possible to use the Enter key to enter information using the Panel library, instead of using a button, as appears in the code above.