Azure OpenAI: Prompts to ChatGPT

Image from https://www.pexels.com/@mizunokozuki/
Image from https://www.pexels.com/@mizunokozuki/

Yet another blog on OpenAI (previous blogs, Similarity Search and  Summarize an article). This is about prompt creation. Most of the time, I want to generate chat responses that are in a machine-readable format like JSON. We shall work on it in the blog.

Dependencies

python = "^3.10"
openai = "^0.27.7"
langchain = "^0.0.189"
python-dotenv = "^1.0.0"

Environment Parameters

OPENAI_API_TYPE="azure"
OPENAI_API_BASE="<azure openai endpoint>"
OPENAI_API_KEY="<azure openai key>"
OPENAI_API_VERSION="<azure openai API version>"
I have the OPENAI_API_VERSION as 2023-03-15-preview

Source Code

After some effort in writing the prompts, I landed on this.
from dotenv import load_dotenv
from langchain import LLMChain
from langchain.chat_models import AzureChatOpenAI
from langchain.schema import AIMessage, HumanMessage, SystemMessage
from langchain.prompts.chat import (
    ChatPromptTemplate,
    HumanMessagePromptTemplate,
)


if __name__ == "__main__":
    load_dotenv()

    llm = AzureChatOpenAI(deployment_name="gpt-35-turbo")

    chat_prompts = ChatPromptTemplate.from_messages(
        [
            SystemMessage(
                content="You are a historian, help to provide information on presidents."
            ),
            HumanMessage(
                content="Example: Jimmy Carter is inaugurated in year 1977. And he is a from the Democratic party"
            ),
            AIMessage(content="interpretation"),
            HumanMessage(
                content='Example: { "name": "Jimmy Carter", "year": "1977-1981", "party": "Democratic"}'
            ),
            AIMessage(content="answer in JSON format"),
            HumanMessagePromptTemplate.from_template(
                template='Question: "{question}". Provide answer in a list in JSON format.'
            ),
        ]
    )

    chain = LLMChain(llm=llm, prompt=chat_prompts)
    response = chain.generate([{"question": "List all the presidents in USA."}])

    print(response.generations[0][0].text)


Result

[
  {
    "name": "George Washington",
    "year": "1789-1797",
    "party": "Independent"
  },
  {
    "name": "John Adams",
    "year": "1797-1801",
    "party": "Federalist"
  },
  {
    "name": "Thomas Jefferson",
    "year": "1801-1809",
    "party": "Democratic-Republican"
  },
  {
    "name": "James Madison",
    "year": "1809-1817",
    "party": "Democratic-Republican"
  },
  {
    "name": "James Monroe",
    "year": "1817-1825",
    "party": "Democratic-Republican"
  },
  {
    "name": "John Quincy Adams",
    "year": "1825-1829",
    "party": "Democratic-Republican"
  },
  {
    "name": "Andrew Jackson",
    "year": "1829-1837",
    "party": "Democratic"
  },
  {
    "name": "Martin Van Buren",
    "year": "1837-1841",
    "party": "Democratic"
  },
  {
    "name": "William Henry Harrison",
    "year": "1841",
    "party": "Whig"
  },
  {
    "name": "John Tyler",
    "year": "1841-1845",
    "party": "Whig"
  },
  {
    "name": "James K. Polk",
    "year": "1845-1849",
    "party": "Democratic"
  },
  {
    "name": "Zachary Taylor",
    "year": "1849-1850",
    "party": "Whig"
  },
  {
    "name": "Millard Fillmore",
    "year": "1850-1853",
    "party": "Whig"
  },
  {
    "name": "Franklin Pierce",
    "year": "1853-1857",
    "party": "Democratic"
  },
  {
    "name": "James Buchanan",
    "year": "1857-1861",
    "party": "Democratic"
  },
  {
    "name": "Abraham Lincoln",
    "year": "1861-1865",
    "party": "Republican"
  },
  {
    "name": "Andrew Johnson",
    "year": "1865-1869",
    "party": "Democratic/National Union"
  },
  {
    "name": "Ulysses S. Grant",
    "year": "1869-1877",
    "party": "Republican"
  },
  {
    "name": "Rutherford B. Hayes",
    "year": "1877-1881",
    "party": "Republican"
  },
  {
    "name": "James A. Garfield",
    "year": "1881",
    "party": "Republican"
  },
  {
    "name": "Chester A. Arthur",
    "year": "1881-1885",
    "party": "Republican"
  },
  {
    "name": "Grover Cleveland",
    "year": "1885-1889",
    "party": "Democratic"
  },
  {
    "name": "Benjamin Harrison",
    "year": "1889-1893",
    "party": "Republican"
  },
  {
    "name": "Grover Cleveland",
    "year": "1893-1897",
    "party": "Democratic"
  },
  {
    "name": "William McKinley",
    "year": "1897-1901",
    "party": "Republican"
  },
  {
    "name": "Theodore Roosevelt",
    "year": "1901-1909",
    "party": "Republican"
  },
  {
    "name": "William Howard Taft",
    "year": "1909-1913",
    "party": "Republican"
  },
  {
    "name": "Woodrow Wilson",
    "year": "1913-1921",
    "party": "Democratic"
  },
  {
    "name": "Warren G. Harding",
    "year": "1921-1923",
    "party": "Republican"
  },
  {
    "name": "Calvin Coolidge",
    "year": "1923-1929",
    "party": "Republican"
  },
  {
    "name": "Herbert Hoover",
    "year": "1929-1933",
    "party": "Republican"
  },
  {
    "name": "Franklin D. Roosevelt",
    "year": "1933-1945",
    "party": "Democratic"
  },
  {
    "name": "Harry S. Truman",
    "year": "1945-1953",
    "party": "Democratic"
  },
  {
    "name": "Dwight D. Eisenhower",
    "year": "1953-1961",
    "party": "Republican"
  },
  {
    "name": "John F. Kennedy",
    "year": "1961-1963",
    "party": "Democratic"
  },
  {
    "name": "Lyndon B. Johnson",
    "year": "1963-1969",
    "party": "Democratic"
  },
  {
    "name": "Richard Nixon",
    "year": "1969-1974",
    "party": "Republican"
  },
  {
    "name": "Gerald Ford",
    "year": "1974-1977",
    "party": "Republican"
  },
  {
    "name": "Jimmy Carter",
    "year": "1977-1981",
    "party": "Democratic"
  },
  {
    "name": "Ronald Reagan",
    "year": "1981-1989",
    "party": "Republican"
  },
  {
    "name": "George H. W. Bush",
    "year": "1989-1993",
    "party": "Republican"
  },
  {
    "name": "Bill Clinton",
    "year": "1993-2001",
    "party": "Democratic"
  },
  {
    "name": "George W. Bush",
    "year": "2001-2009",
    "party": "Republican"
  },
  {
    "name": "Barack Obama",
    "year": "2009-2017",
    "party": "Democratic"
  },
  {
    "name": "Donald Trump",
    "year": "2017-2021",
    "party": "Republican"
  },
  {
    "name": "Joe Biden",
    "year": "2021-Present",
    "party": "Democratic"
  }
]

When I changed the question to "List all the presidents in Singapore.". I got

Singapore is a republic and does not have a president in the traditional
sense. The head of state is the President of Singapore, who holds a ceremonial
role and is elected by Parliament. The following are the Presidents of Singapore
in chronological order:

[
  {
    "name": "Yusof Ishak",
    "term": "1965-1970"
  },
  {
    "name": "Benjamin Sheares",
    "term": "1971-1981"
  },
  {
    "name": "Devan Nair",
    "term": "1981-1985"
  },
  {
    "name": "Wee Kim Wee",
    "term": "1985-1993"
  },
  {
    "name": "Ong Teng Cheong",
    "term": "1993-1999"
  },
  {
    "name": "S R Nathan",
    "term": "1999-2011"
  },
  {
    "name": "Tony Tan",
    "term": "2011-2017"
  },
  {
    "name": "Halimah Yacob",
    "term": "2017-present"
  }
]

This looks right. party property values are not provided because Singapore's Presidents are not associated with any parties.

And, sometimes, I got this answer. (because the temperature value for ChatGPT defaults to 0.7)

I'm sorry, but there are no presidents in Singapore. Singapore is a republic
with a parliamentary system of government. The head of state is the President,
but the role is largely ceremonial and executive power is vested in the Prime
Minister and the Cabinet. The current President of Singapore is Halimah Yacob,
who was elected in 2017.

Observations

It is not easy to write good prompts. There are several good online courses on Prompt Engineering. One from Coursera and the other good one from deep learning.

And, to get a consistent JSON format (so that your code can parse it), we can set the temperature of ChatGPT to zero.




Comments

Popular posts from this blog

OpenAI: Functions Feature in 2023-07-01-preview API version

Storing embedding in Azure Database for PostgreSQL

Happy New Year, 2024 from DALL-E