Introduction
On this article, we are going to see how one can construct an online utility utilizing OpenAI with the assistance of Langchain. This net app permits customers to transform unstructured emails into correctly formatted English. Customers can enter their e mail textual content, specify the specified tone and dialect (formal/casual and American/British English), and the app will present a superbly formatted e mail within the chosen type. We cant construct scale functions each time, simply copy-pasting the prompts with our queries; as a substitute, let’s get began and construct this wonderful “Skilled E-mail Author” device.
Studying Targets
- To discover ways to construct a phenomenal net utility utilizing Streamlit.
- To know what immediate engineering is and how one can create efficient prompts for producing emails.
- To discover ways to question OpenAI LLM utilizing Langchain’s PromptTemplate.
- To discover ways to deploy Python functions utilizing Streamlit.
This text was revealed as part of the Information Science Blogathon.
Streamlit Setup
First, we have to perceive what streamlit is, the way it works, and how one can set it up for our use case. Streamlit permits us to create net functions in Python and host them domestically and on the internet. First, go to your terminal and set up streamlit utilizing the beneath command
pip set up streamlit
Create an empty Python file for our script and run the file utilizing the beneath command
python -m streamlit run [your_file_name.py]
It will begin an empty streamlit app within the tackle localhost:8501. Open your browser and seek for this tackle to visualise the app. One of many cool issues about this app is you can also make edits to your code base, and it’ll mechanically replace your app within the browser.
Construct Frontend
Let’s begin our code by including a header and web page title to our utility. I’m naming it as “Skilled E-mail Author”
#Importing Streamlit library.
import streamlit as st
# Arrange Streamlit app with Header and Title
st.set_page_config(page_title="Skilled E-mail Author", page_icon=":robotic:")
st.header("Skilled E-mail Author")
Output:
Subsequent, we want enter from the person to know which e mail the person desires. For this, we use the text_area perform offered by streamlit.
# Get the person enter e mail textual content
def getEmail():
input_text = st.text_area(label="E-mail Enter", label_visibility='collapsed',
placeholder="Your E-mail...", key="input_text")
return input_text
input_text = getEmail()
Output:
Subsequent, we have to have two dropdowns to ask the person which tone person is anticipating in his e mail, i.e., Formal and Casual and in addition which English dialect the person is anticipating, i.e., American English, British English.
# Show dropdowns for choosing tone and dialect
column1, column2 = st.columns(2)
with column1:
tone_drop_down = st.selectbox(
'Which tone would you want your e mail to have?',
('Formal', 'Casual'))
with column2:
dialect_drop_down = st.selectbox(
'Which English Dialect would you want?',
('American', 'British'))
The above code will create two columns, every containing a dropdown utilizing the selectbox() perform.
Output:
We have to emphasize that anytime customers replace these alternatives, it ought to rerun all the app for us. Consider it as an enormous refresh each time you toggle one thing on within the dropdowns.
Immediate Engineering
Now we have to take the e-mail enter given by the person and cross it with a immediate template by Langchain with the configuration that the person has chosen from the dropdowns. Then we have to get the correctly formatted output from OpenAI.
To take action, we have to arrange a immediate template. We have to do some immediate Engineering for optimized output on this immediate template. Immediate engineering is a means of developing prompts utilizing which we will ask our queries to language fashions and fetch correct outcomes. You’ll be able to modify this template primarily based in your wants.
1. The immediate ought to clearly describe what the person is giving enter. For instance,
Beneath is an e mail which may be unstructured and poorly worded.
2. The immediate ought to clearly clarify what language mannequin ought to give output. For instance,
Your aim is to:
- Format the e-mail correctly
- Convert the enter e mail into the tone laid out in curly braces.
- Convert the enter e mail into the dialect laid out in curly braces.
- Please begin the e-mail with a heat introduction. Add the introduction if it’s essential to.
3. The immediate ought to include examples to make sure that mannequin will concentrate on output expectations.
4. Lastly, the immediate ought to clearly point out what person inputs are and what every enter refers to.
Beneath is the immediate that we created by following the above guidelines
# Outline the template for the e-mail conversion job
template = """
Beneath is an e mail which may be unstructured and poorly worded.
Your aim is to:
- Format the e-mail correctly
- Convert the enter e mail into the tone laid out in curly braces.
- Convert the enter e mail into the dialect laid out in curly braces.
Take these examples of various tones as reference:
- Formal: We went to Hyderabad for the weekend. We've a whole lot of issues to let you know.
- Casual: Went to Hyderabad for the weekend. Tons to let you know.
Beneath are some examples of phrases in several dialects:
- American: Rubbish, cookie, inexperienced thumb, car parking zone, pants, windshield,
French Fries, cotton sweet, house
- British: Inexperienced fingers, automotive park, trousers, windscreen, chips, candyfloss,
flag, garbage, biscuit
Instance Sentences from every dialect:
- American: As they strolled by the colourful neighborhood, Sarah requested her
good friend if he wished to seize a espresso on the close by café. The autumn
foliage was breathtaking, and so they loved the nice climate,
chatting about their weekend plans.
- British: As they wandered by the picturesque neighbourhood, Sarah requested her
good friend if he fancied getting a espresso on the close by café. The autumn
leaves have been gorgeous, and so they savoured the nice climate, chatting
about their weekend plans.
Please begin the e-mail with a heat introduction. Add the introduction if it's essential to.
Beneath is the e-mail, tone, and dialect:
TONE: {tone}
DIALECT: {dialect}
EMAIL: {e mail}
YOUR {dialect} RESPONSE:
"""
Now create the immediate utilizing PromptTemplate class by Langchain, utilizing which we will inject our person inputs into the immediate.
#Importing PromptTemplate class
from langchain import PromptTemplate
# Create a PromptTemplate occasion to handle the enter variables and the template
immediate = PromptTemplate(
input_variables=["tone", "dialect", "email"],
template=query_template,
)
Load Language Mannequin
Be sure to have your OpenAI API Keys obtainable with you. If not, observe the beneath steps.
- Go to ‘https://openai.com/ and create your account.
- Login into your account and choose ‘API’ in your dashboard.
- Now click on in your profile icon, then choose ‘View API Keys’.
- Choose ‘Create new secret key’, copy it, and put it aside.
Code for OpenAI API Key
Beneath is a code for the perform that takes OpenAI API Key as enter from the person utilizing the text_input() perform and shows the pattern API Key as a placeholder.
# Show textual content enter for OpenAI API Key
def fetchAPIKey():
input_text = st.text_input(
label="OpenAI API Key ", placeholder="Ex: vk-Cb8un42twmA8tf...", key="openai_api_key_input")
return input_text
# Get the OpenAI API Key from the person
openai_api_key = fetchAPIKey()
Output:
We have to be sure that our API secret is both in our script, which isn’t really useful as a result of we don’t wish to code it laborious anyplace, or it must be in the environment variables that our code can pull from. One strategy to create environmental variables is through the use of a separate .env file.
Steps for Environmental Variables
Comply with the beneath steps for creating environmental variables:
1: Open your terminal and Set up python-dotenv package deal utilizing the command “pip set up python dotenv”.
2: Create a file named “.env”.
3: Retailer your API Key within the beneath format
API_KEY=your_api_key_here
4: Load dotenv package deal and fetch your environmental variables utilizing that package deal
from dotenv import load_dotenv
import os
# Load the setting variables from the .env file
load_dotenv()
# Entry the API key utilizing os.environ
openai_api_key = os.environ.get("API_KEY")
This methodology protects your API keys from by chance exposing API Keys straight in your code. Hold this file safe as a substitute of sharing publicly.
Nevertheless, as OpenAI API permits a restricted variety of API requests, we are going to ask customers to enter their API keys as a substitute of giving ours. On this case, we are going to load OpenAI with a temperature equal to 0.7, which implies will probably be artistic. The beneath code throws us an error if we cross an invalid OpenAI API Key. Additional, we should present acceptable warnings if the person enters invalid keys.
#Importing OpenAI Library
from langchain.llms import OpenAI
# Operate to load the Language Mannequin
def loadLanguageModel(api_key_openai):
llm = OpenAI(temperature=.7, openai_api_key=api_key_openai)
return llm
Instance
Allow us to give a pattern instance to the person in order that the person can perceive what he ought to give as enter and what he can anticipate in return. Let’s create a “Present an Instance” button within the entrance. The beneath perform updates the textual content field with a pattern unstructured and poorly worded e mail question.
# Operate to replace the textual content field with an instance e mail
def textBoxUpdateWithExample():
print("in up to date")
st.session_state.input_text = "Vinay I'm begins work at yours workplace from monday"
# Button to indicate an instance e mail
st.button("*Present an Instance*", sort="secondary",
assist="Click on to see an instance of the e-mail you'll be changing.", on_click=textBoxUpdateWithExample)
st.markdown("### Your E-mail:")
Output:
Subsequent, we have to be sure that the person has inputted his API key, and in addition, he ought to have given some question within the textual content field earlier than invoking the language mannequin. If he invokes the mannequin with out API Key or invalid API, we have to present correct directions to the person to fetch the proper secret key.
# If the person has offered input_text, proceed with e mail conversion
if input_text:
if not openai_api_key:
# If API Key is just not offered, present a warning
st.warning(
'Please insert OpenAI API Key. Directions [here](https://assist.openai.com/en/articles/4936850-where-do-i-find-my-secret-api-key)', icon="⚠️")
st.cease()
# Load the Language Mannequin with the offered API Key
llm = loadLanguageModel(api_key_openai=openai_api_key)
# Format the e-mail utilizing the PromptTemplate and the Language Mannequin
prompt_with_email = immediate.format(
tone=tone_drop_down, dialect=dialect_drop_down, e mail=input_text)
formatted_email = llm(prompt_with_email)
# Show the formatted e mail
st.write(formatted_email)
Output:
If the person inputs the proper API key and correct e mail textual content within the textual content field, we format the immediate utilizing the e-mail textual content and configurations entered by the person, i.e., tone, and dialect. Then we are going to cross this immediate into our language mannequin, and the LLM will give us the response as a correctly formatted e mail which we are going to present the person below the “Your E-mail” tab utilizing the streamlit write() perform.
Deploy the Utility
Comply with the beneath steps to deploy the applying:
1: First, we should push our code into the GitHub repository. Earlier than pushing, create a necessities.txt file containing a listing of all of the dependencies of our code.
- langchain
- openai
- streamlit
2: Head to streamlit.io and create an account by authorizing GitHub.
3: Login into your streamlit account.
4: Click on on create a brand new app and cross all the small print of the GitHub repository. Underneath the Fundamental file path, give the identify of the file which comprises the Python script. Lastly, click on on deploy.
5: Save the App URL. After a couple of minutes, you possibly can see your utility reside on the internet utilizing that URL.
Full Implementation
# Import required libraries
from langchain import PromptTemplate
import streamlit as st
from langchain.llms import OpenAI
# Outline the template for the e-mail conversion job
query_template = query_template = """
Beneath is an e mail which may be unstructured and poorly worded.
Your aim is to:
- Format the e-mail correctly
- Convert the enter e mail into the tone laid out in curly braces.
- Convert the enter e mail into the dialect laid out in curly braces.
Take these examples of various tones as reference:
- Formal: We went to Hyderabad for the weekend. We've a whole lot of issues to let you know.
- Casual: Went to Hyderabad for the weekend. Tons to let you know.
Beneath are some examples of phrases in several dialects:
- American: Rubbish, cookie, inexperienced thumb, car parking zone, pants, windshield,
French Fries, cotton sweet, house
- British: Inexperienced fingers, automotive park, trousers, windscreen, chips, candyfloss,
flag, garbage, biscuit
Instance Sentences from every dialect:
- American: As they strolled by the colourful neighborhood, Sarah requested her
good friend if he wished to seize a espresso on the close by café. The autumn
foliage was breathtaking, and so they loved the nice climate,
chatting about their weekend plans.
- British: As they wandered by the picturesque neighbourhood, Sarah requested her
good friend if he fancied getting a espresso on the close by café. The autumn
leaves have been gorgeous, and so they savoured the nice climate, chatting
about their weekend plans.
Please begin the e-mail with a heat introduction. Add the introduction if it's essential to.
Beneath is the e-mail, tone, and dialect:
TONE: {tone}
DIALECT: {dialect}
EMAIL: {e mail}
YOUR {dialect} RESPONSE:
"""
# Create a PromptTemplate occasion to handle the enter variables and the template
immediate = PromptTemplate(
input_variables=["tone", "dialect", "email"],
template=query_template,
)
# Operate to load the Language Mannequin
def loadLanguageModel(api_key_openai):
llm = OpenAI(temperature=.7, openai_api_key=api_key_openai)
return llm
# Arrange Streamlit app with Header and Title
st.set_page_config(page_title="Skilled E-mail Author", page_icon=":robotic:")
st.header("Skilled E-mail Author")
# Create columns for the Streamlit structure
column1, column2 = st.columns(2)
# Show textual content enter for OpenAI API Key
def fetchAPIKey():
input_text = st.text_input(
label="OpenAI API Key ", placeholder="Ex: vk-Cb8un42twmA8tf...", key="openai_api_key_input")
return input_text
# Get the OpenAI API Key from the person
openai_api_key = fetchAPIKey()
# Show dropdowns for choosing tone and dialect
column1, column2 = st.columns(2)
with column1:
tone_drop_down = st.selectbox(
'Which tone would you want your e mail to have?',
('Formal', 'Casual'))
with column2:
dialect_drop_down = st.selectbox(
'Which English Dialect would you want?',
('American', 'British'))
# Get the person enter e mail textual content
def getEmail():
input_text = st.text_area(label="E-mail Enter", label_visibility='collapsed',
placeholder="Your E-mail...", key="input_text")
return input_text
input_text = getEmail()
# Test if the e-mail exceeds the phrase restrict
if len(input_text.cut up(" ")) > 700:
st.write("Most restrict is 700 phrases. Please enter a shorter e mail")
st.cease()
# Operate to replace the textual content field with an instance e mail
def textBoxUpdateWithExample():
print("in up to date")
st.session_state.input_text = "Vinay I'm begins work at yours workplace from monday"
# Button to indicate an instance e mail
st.button("*Present an Instance*", sort="secondary",
assist="Click on to see an instance of the e-mail you'll be changing.", on_click=textBoxUpdateWithExample)
st.markdown("### Your E-mail:")
# If the person has offered input_text, proceed with e mail conversion
if input_text:
if not openai_api_key:
# If API Key is just not offered, present a warning
st.warning(
'Please insert OpenAI API Key. Directions [here](https://assist.openai.com/en/articles/4936850-where-do-i-find-my-secret-api-key)', icon="⚠️")
st.cease()
# Load the Language Mannequin with the offered API Key
llm = loadLanguageModel(api_key_openai=openai_api_key)
# Format the e-mail utilizing the PromptTemplate and the Language Mannequin
prompt_with_email = immediate.format(
tone=tone_drop_down, dialect=dialect_drop_down, e mail=input_text)
formatted_email = llm(prompt_with_email)
# Show the formatted e mail
st.write(formatted_email)
Conclusion
On this article, we’ve got seen how one can create a phenomenal net utility utilizing OpenAI LLM with the assistance of Langchain. We began with putting in and organising Streamlit. Then we created a frontend interface to take person inputs like tone, dialect, and e mail textual content from the person. After that, we created an efficient immediate to question the language mannequin utilizing these inputs. Subsequent, we initialized the OpenAI mannequin utilizing API keys by passing the immediate we created utilizing Langchain. Lastly, we deployed the applying into the net utilizing Streamlit.
Key Takeaways
- Utilizing the Streamlit library in Python, we will construct interactive net functions.
- Immediate engineering performs essential for fetching optimized outcomes from the Language mannequin.
- OpenAI LLM might be simply utilized in our Python functions utilizing the OpenAI library and its secret keys.
- Utilizing Langchain’s PromptTemplate, we will correctly format the Immediate primarily based on person enter which might additional be utilized in querying the LLM.
- Utilizing Streamlit share, we will host the Python utility in Dwell URL.
Regularly Requested Questions
A. Streamlit is an Open supply Python library that can be utilized to create interactive net functions with easy Python capabilities with out intensive data of net growth.
A. First, it’s essential to select the applying growth tech stack, after which, utilizing OpenAI secret keys, you possibly can leverage the advantages of OpenAI.
A. Sure, you should utilize ChatGPT for writing emails. You’ll be able to generate e mail content material by offering efficient prompts with clear descriptions of e mail expectations to ChatGPT.
A. The default OpenAI mannequin utilized in LangChain is OpenAI GPT-3.5-turbo.
A. OpenAI offers each free and paid providers. You may get a restricted API request, GPT-3 mannequin totally free service. You may get elevated entry to their fashions and extra API requests utilizing the paid model.
The media proven on this article is just not owned by Analytics Vidhya and is used on the Writer’s discretion.