![GPT-3 教程:行程安排器](https://www.aihubpro.cn/wp-content/uploads/2023/05/20230504124842-6453a9aa953e8.png)
在本教程中,我们将为行程安排创建简单的 GPT-3 驱动的 Streamlit 服务。我们将使用 GPT-3 根据用户输入生成行程安排。我们将使用 Streamlit 为我们的应用程序创建简单的 UI。
🚀 先决条件
首先,我们需要安装必要的库:
!pip install openai!pip install streamlit!pip install python-dotenv
现在我们可以创建.env
文件并添加我们的 OpenAI API 密钥:
OPENAI_API_KEY=<your api key>
我们需要做的最后一件事是创建main.py
文件,导入所有必要的库并从.env
文件加载我们的 API 密钥:
import osimport randomfrom datetime import datetime, timedeltaimport openaiimport streamlit as stfrom dotenv import load_dotenvload_dotenv()openai.api_key = os.getenv('OPENAI_API_KEY')
📝 创建 Streamlit 应用
现在我们可以创建我们的 Streamlit 应用程序。首先,我们需要创建一个函数,根据用户输入生成提示和行程安排:
example_destinations = ['Paris', 'London', 'New York', 'Tokyo', 'Sydney', 'Hong Kong', 'Singapore', 'Warsaw', 'Mexico City', 'Palermo']random_destination = random.choice(example_destinations)now_date = datetime.now()# round to nearest 15 minutesnow_date = now_date.replace(minute=now_date.minute // 15 * 15, second=0, microsecond=0)# split into date and time objectsnow_time = now_date.time()now_date = now_date.date() + timedelta(days=1)def generate_prompt(destination, arrival_to, arrival_date, arrival_time, departure_from, departure_date, departure_time, additional_information, **kwargs): return f'''Prepare trip schedule for {destination}, based on the following information:* Arrival To: {arrival_to}* Arrival Date: {arrival_date}* Arrival Time: {arrival_time}* Departure From: {departure_from}* Departure Date: {departure_date}* Departure Time: {departure_time}* Additional Notes: {additional_information}'''.strip()def submit(): prompt = generate_prompt(**st.session_state) # generate output output = openai.Completion.create( engine='text-davinci-003', prompt=prompt, temperature=0.45, top_p=1, frequency_penalty=2, presence_penalty=0, max_tokens=1024 ) st.session_state['output'] = output['choices'][0]['text']
现在我们可以使用 Streamlit 为我们的应用程序创建 UI:
# Initializationif 'output' not in st.session_state: st.session_state['output'] = '--'st.title('GPT-3 Trip Scheduler')st.subheader('Let us plan your trip!')
让我们为用户输入创建表单:
with st.form(key='trip_form'): c1, c2, c3 = st.columns(3) with c1: st.subheader('Destination') origin = st.text_input('Destination', value=random_destination, key='destination') st.form_submit_button('Submit', on_click=submit) with c2: st.subheader('Arrival') st.selectbox('Arrival To', ('Airport', 'Train Station', 'Bus Station', 'Ferry Terminal', 'Port', 'Other'), key='arrival_to') st.date_input('Arrival Date', value=now_date, key='arrival_date') st.time_input('Arrival Time', value=now_time, key='arrival_time') with c3: st.subheader('Departure') st.selectbox('Departure From', ('Airport', 'Train Station', 'Bus Station', 'Ferry Terminal', 'Port', 'Other'), key='departure_from') st.date_input('Departure Date', value=now_date + timedelta(days=1), key='departure_date') st.time_input('Departure Time', value=now_time, key='departure_time') st.text_area('Additional Information', height=200, value='I want to visit as many places as possible! (respect time)', key='additional_information')
最后,我们可以显示生成的行程安排:
st.subheader('Trip Schedule')st.write(st.session_state.output)
🚀 运行 Streamlit 应用程序
现在我们可以运行我们的应用程序:
!streamlit run main.py
🏆 结果
让我们检查结果!
![GPT-3 教程:行程安排器](https://www.aihubpro.cn/wp-content/uploads/2023/05/20230504124842-6453a9aaa37ab.png)