Skip to content

Instantly share code, notes, and snippets.

@danielcorin
Last active February 26, 2023 23:50
Show Gist options
  • Save danielcorin/c19483b9c52c1e2970db9f6f2493e7d7 to your computer and use it in GitHub Desktop.
Save danielcorin/c19483b9c52c1e2970db9f6f2493e7d7 to your computer and use it in GitHub Desktop.
Datetime parsing with GPT-3
import datetime
import json
import logging
import sys
from dotenv import load_dotenv
from langchain import PromptTemplate
from langchain.llms import OpenAI
load_dotenv()
logger = logging.getLogger()
logger.setLevel(logging.INFO)
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(logging.Formatter('%(levelname)s: %(message)s'))
logger.addHandler(handler)
template = """
You will be given a phrase where the timezone is {timezone} and you will parse all the time ranges.
You will return the time ranges as a list of JSON list with timezone {timezone} and as ISO 8601 timestamps.
All outputs should be in the future.
Assume today is 2023-02-01 for the following examples:
Example input:
```
Friday 4-5:30pm or Saturday 2-3:30pm
```
Example output:
[
["2023-02-04 16:00:00-08:00", "2023-02-04 17:30:00-08:00"],
["2023-02-05 14:00:00-08:00", "2023-02-05 13:30:00-08:00"]
]
Example 2 input:
```
today at 1pm
```
Example 2 output:
[
["2023-02-01 14:00:00-08:00", "2023-02-01 13:30:00-08:00"]
]
The current time in UTC is: {now}.
Phrase: {phrase}.
Result:
"""
llm = OpenAI(model_name="text-davinci-003")
now = datetime.datetime.utcnow().strftime("%A, %B %d, %Y")
one_input_prompt = PromptTemplate(
input_variables=["now", "timezone", "phrase"],
template=template,
)
timezone = "America/Los_Angeles"
phrase = ' '.join(sys.argv[1:])
logger.debug({
"now": now,
"timezone": timezone,
"phrase": phrase,
})
prompt = one_input_prompt.format(now=now, timezone=timezone, phrase=phrase)
result = llm(prompt)
logger.debug({"result": result})
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment