Skip to content

Instantly share code, notes, and snippets.

Last active February 26, 2023 23:50
What would you like to do?
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
logger = logging.getLogger()
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(logging.Formatter('%(levelname)s: %(message)s'))
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}.
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"],
timezone = "America/Los_Angeles"
phrase = ' '.join(sys.argv[1:])
"now": now,
"timezone": timezone,
"phrase": phrase,
prompt = one_input_prompt.format(now=now, timezone=timezone, phrase=phrase)
result = llm(prompt)
logger.debug({"result": result})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment