mirror of
https://github.com/dair-ai/Prompt-Engineering-Guide
synced 2024-11-10 01:13:36 +00:00
105 lines
4.4 KiB
Plaintext
105 lines
4.4 KiB
Plaintext
# PAL (Ohjelma-Avusteiset Kielimallit)
|
|
|
|
import { Callout, FileTree } from 'nextra-theme-docs'
|
|
import {Screenshot} from 'components/screenshot'
|
|
import PAL from '../../img/pal.png'
|
|
|
|
[Gao ym., (2022)](https://arxiv.org/abs/2211.10435) esittelee menetelmän, joka käyttää LLM:ää luonnollisen kielen ongelmien lukemiseen ja hyödyntää ohjelmistoja välivaiheen päättelyn generointiin. Ratkaisua kutsututaan ohjelma-avusteiseksi kielimalliksi (Program-Aided Language Model, PAL), ja se eroaa ajatusketjukehotteesta siten, että sen sijaan että se käyttää vapaamuotoista tekstiä ratkaisun saamiseksi, se siirtävää ratkaisuvaiheen ohjelmalliselle suoritusympäristölle, kuten Python-tulkille.
|
|
|
|
<Screenshot src={PAL} alt="PAL" />
|
|
Kuvan lähde: [Gao ym., (2022)](https://arxiv.org/abs/2211.10435)
|
|
|
|
Katsotaan esimerkkiä käyttämällä LangChainia ja OpenAI GPT-3:a. Haluamme kehittää yksinkertaisen sovelluksen, joka pystyy tulkitsemaan kysymyksen ja antamaan vastauksen hyödyntämällä Python-tulkkia.
|
|
|
|
Erityisesti olemme kiinnostuneita luomaan toiminnallisuuden, joka mahdollistaa LLM:n käytön kysymyksiin, jotka edellyttävät päivämäärätiedon ymmärtämistä. Tarjoamme LLM:lle kehotteen, joka sisältää muutamia esimerkkejä, jotka on otettu [täältä](https://github.com/reasoning-machines/pal/blob/main/pal/prompt/date_understanding_prompt.py).
|
|
|
|
Tarvitsemme alla listatut työkalut:
|
|
|
|
```python
|
|
import openai
|
|
from datetime import datetime
|
|
from dateutil.relativedelta import relativedelta
|
|
import os
|
|
from langchain.llms import OpenAI
|
|
from dotenv import load_dotenv
|
|
```
|
|
|
|
Määritellään ensin muutama asia:
|
|
|
|
```python
|
|
load_dotenv()
|
|
|
|
# API configuration
|
|
openai.api_key = os.getenv("OPENAI_API_KEY")
|
|
|
|
# for LangChain
|
|
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
|
|
```
|
|
|
|
Mallin ympäristön asetukset:
|
|
|
|
```python
|
|
llm = OpenAI(model_name='text-davinci-003', temperature=0)
|
|
```
|
|
|
|
Kehotteen + kysymyksen asetukset:
|
|
|
|
```python
|
|
question = "Today is 27 February 2023. I was born exactly 25 years ago. What is the date I was born in MM/DD/YYYY?"
|
|
|
|
DATE_UNDERSTANDING_PROMPT = """
|
|
# Q: 2015 is coming in 36 hours. What is the date one week from today in MM/DD/YYYY?
|
|
# If 2015 is coming in 36 hours, then today is 36 hours before.
|
|
today = datetime(2015, 1, 1) - relativedelta(hours=36)
|
|
# One week from today,
|
|
one_week_from_today = today + relativedelta(weeks=1)
|
|
# The answer formatted with %m/%d/%Y is
|
|
one_week_from_today.strftime('%m/%d/%Y')
|
|
# Q: The first day of 2019 is a Tuesday, and today is the first Monday of 2019. What is the date today in MM/DD/YYYY?
|
|
# If the first day of 2019 is a Tuesday, and today is the first Monday of 2019, then today is 6 days later.
|
|
today = datetime(2019, 1, 1) + relativedelta(days=6)
|
|
# The answer formatted with %m/%d/%Y is
|
|
today.strftime('%m/%d/%Y')
|
|
# Q: The concert was scheduled to be on 06/01/1943, but was delayed by one day to today. What is the date 10 days ago in MM/DD/YYYY?
|
|
# If the concert was scheduled to be on 06/01/1943, but was delayed by one day to today, then today is one day later.
|
|
today = datetime(1943, 6, 1) + relativedelta(days=1)
|
|
# 10 days ago,
|
|
ten_days_ago = today - relativedelta(days=10)
|
|
# The answer formatted with %m/%d/%Y is
|
|
ten_days_ago.strftime('%m/%d/%Y')
|
|
# Q: It is 4/19/1969 today. What is the date 24 hours later in MM/DD/YYYY?
|
|
# It is 4/19/1969 today.
|
|
today = datetime(1969, 4, 19)
|
|
# 24 hours later,
|
|
later = today + relativedelta(hours=24)
|
|
# The answer formatted with %m/%d/%Y is
|
|
today.strftime('%m/%d/%Y')
|
|
# Q: Jane thought today is 3/11/2002, but today is in fact Mar 12, which is 1 day later. What is the date 24 hours later in MM/DD/YYYY?
|
|
# If Jane thought today is 3/11/2002, but today is in fact Mar 12, then today is 3/1/2002.
|
|
today = datetime(2002, 3, 12)
|
|
# 24 hours later,
|
|
later = today + relativedelta(hours=24)
|
|
# The answer formatted with %m/%d/%Y is
|
|
later.strftime('%m/%d/%Y')
|
|
# Q: Jane was born on the last day of Feburary in 2001. Today is her 16-year-old birthday. What is the date yesterday in MM/DD/YYYY?
|
|
# If Jane was born on the last day of Feburary in 2001 and today is her 16-year-old birthday, then today is 16 years later.
|
|
today = datetime(2001, 2, 28) + relativedelta(years=16)
|
|
# Yesterday,
|
|
yesterday = today - relativedelta(days=1)
|
|
# The answer formatted with %m/%d/%Y is
|
|
yesterday.strftime('%m/%d/%Y')
|
|
# Q: {question}
|
|
""".strip() + '\n'
|
|
```
|
|
|
|
```python
|
|
llm_out = llm(DATE_UNDERSTANDING_PROMPT.format(question=question))
|
|
print(llm_out)
|
|
```
|
|
|
|
```python
|
|
exec(llm_out)
|
|
print(born)
|
|
```
|
|
|
|
Lopputuloksena: `02/27/1998` |