Home Machine Learning Internet Utility Utilizing OpenAI and Langchain

Internet Utility Utilizing OpenAI and Langchain

0
Internet Utility Utilizing OpenAI and Langchain

[ad_1]

Introduction

On this article, we are going to see construct an online software utilizing OpenAI with the assistance of Langchain. This net app permits customers to transform unstructured emails into correctly formatted English. Customers can enter their electronic mail textual content, specify the specified tone and dialect (formal/casual and American/British English), and the app will present a fantastically formatted electronic mail within the chosen type. We cant construct scale purposes each time, simply copy-pasting the prompts with our queries; as a substitute, let’s get began and construct this superb “Skilled E-mail Author” device.

Web Application Using OpenAI | Langchain

Studying Goals

  • To learn to construct a stupendous net software utilizing Streamlit.
  • To know what immediate engineering is and create efficient prompts for producing emails.
  • To learn to question OpenAI LLM utilizing Langchain’s PromptTemplate.
  • To learn to deploy Python purposes utilizing Streamlit.

This text was printed as part of the Knowledge Science Blogathon.

Streamlit Setup

First, we have to perceive what streamlit is, the way it works, and set it up for our use case. Streamlit permits us to create net purposes in Python and host them domestically and on the internet. First, go to your terminal and set up streamlit utilizing the under command

pip set up streamlit

Create an empty Python file for our script and run the file utilizing the under command

python -m streamlit run [your_file_name.py]

This can 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 robotically replace your app within the browser.

Construct Frontend

Let’s begin our code by including a header and web page title to our software. 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:

Web Application Using OpenAI | Langchain

Subsequent, we want enter from the person to know which electronic mail the person needs. For this, we use the text_area perform supplied by streamlit.

# Get the person enter electronic 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:

 Source: Author

Subsequent, we have to have two dropdowns to ask the person which tone person is anticipating in his electronic mail, i.e., Formal and Casual and likewise 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 electronic 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:

 Source: Author

We have to emphasize that anytime customers replace these picks, 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 go it with a immediate template by means of 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 strategy of developing prompts utilizing which we will ask our queries to language fashions and fetch correct outcomes. You may modify this template based mostly in your wants.

1. The immediate ought to clearly describe what the person is giving enter. For instance,

    Under is an electronic mail that could be unstructured and poorly worded.

2. The immediate ought to clearly clarify what language mannequin ought to give output. For instance,

Your purpose is to:

  • Format the e-mail correctly
  • Convert the enter electronic mail into the tone laid out in curly braces.
  • Convert the enter electronic mail into the dialect laid out in curly braces.
  • Please begin the e-mail with a heat introduction. Add the introduction if you’ll want to.

3. The immediate ought to include examples to make sure that mannequin will pay attention to output expectations.

4. Lastly, the immediate ought to clearly point out what person inputs are and what every enter refers to.

Under is the immediate that we created by following the above guidelines

# Outline the template for the e-mail conversion activity
template = """
    Under is an electronic mail that could be unstructured and poorly worded.
    Your purpose is to:
    - Format the e-mail correctly
    - Convert the enter electronic mail into the tone laid out in curly braces. 
    - Convert the enter electronic 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. Now we have quite a lot of issues to let you know.
    - Casual: Went to Hyderabad for the weekend. Tons to let you know.  

    Under are some examples of phrases in numerous dialects:
    - American: Rubbish, cookie, inexperienced thumb, parking zone, pants, windshield, 
      French Fries, cotton sweet, residence
    - British: Inexperienced fingers, automotive park, trousers, windscreen, chips, candyfloss, 
      flag, garbage, biscuit

    Instance Sentences from every dialect:
    - American: As they strolled by means of 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, they usually loved the nice climate, 
                chatting about their weekend plans.
    - British: As they wandered by means of the picturesque neighbourhood, Sarah requested her 
               good friend if he fancied getting a espresso on the close by café. The autumn 
               leaves had been beautiful, they usually savoured the nice climate, chatting 
               about their weekend plans.

    Please begin the e-mail with a heat introduction. Add the introduction if you'll want to.
    
    Under is the e-mail, tone, and dialect:
    TONE: {tone}
    DIALECT: {dialect}
    EMAIL: {electronic 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

Ensure you have your OpenAI API Keys accessible with you. If not, observe the under 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.
Web Application Using OpenAI | Langchain | Large Language Model

Code for OpenAI API Key

Under 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:

OpenAI API Key | Web Application Using OpenAI | Langchain

We have to be sure that our API secret is both in our script, which isn’t really helpful as a result of we don’t wish to code it laborious anyplace, or it ought to be in the environment variables that our code can pull from. One approach to create environmental variables is by utilizing a separate .env file.

Steps for Environmental Variables

Comply with the under steps for creating environmental variables:

1: Open your terminal and Set up python-dotenv bundle utilizing the command “pip set up python dotenv”.

2: Create a file named “.env”.

3: Retailer your API Key within the under format

API_KEY=your_api_key_here

4: Load dotenv bundle and fetch your environmental variables utilizing that bundle

from dotenv import load_dotenv
import os

# Load the surroundings variables from the .env file
load_dotenv()

# Entry the API key utilizing os.environ
openai_api_key = os.environ.get("API_KEY")

This technique protects your API keys from by accident exposing API Keys instantly 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 suggests will probably be inventive. The under code throws us an error if we go 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 count on in return. Let’s create a “Present an Instance” button within the entrance. The under perform updates the textual content field with a pattern unstructured and poorly worded electronic mail question.

# Operate to replace the textual content field with an instance electronic 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 electronic 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:

 Source: Author

Subsequent, we have to be sure that the person has inputted his API key, and likewise, 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 supplied input_text, proceed with electronic mail conversion
if input_text:
    if not openai_api_key:
        # If API Key is just not supplied, 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 supplied 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, electronic mail=input_text)
    formatted_email = llm(prompt_with_email)
    # Show the formatted electronic mail
    st.write(formatted_email)

Output:

 Source: Author

If the person inputs the proper API key and correct electronic 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 go this immediate into our language mannequin, and the LLM will give us the response as a correctly formatted electronic mail which we are going to present the person underneath the “Your E-mail” tab utilizing the streamlit write() perform.

Deploy the Utility

Comply with the under steps to deploy the appliance:

1: First, we should push our code into the GitHub repository. Earlier than pushing, create a necessities.txt file containing an inventory 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 go all the small print of the GitHub repository. Below the Most important file path, give the title of the file which incorporates the Python script. Lastly, click on on deploy.

 Source: Author

5: Save the App URL. After a couple of minutes, you possibly can see your software dwell on the internet utilizing that URL.

 Source: Author

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 activity
query_template = query_template = """
    Under is an electronic mail that could be unstructured and poorly worded.
    Your purpose is to:
    - Format the e-mail correctly
    - Convert the enter electronic mail into the tone laid out in curly braces. 
    - Convert the enter electronic 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. Now we have quite a lot of issues to let you know.
    - Casual: Went to Hyderabad for the weekend. Tons to let you know.  

    Under are some examples of phrases in numerous dialects:
    - American: Rubbish, cookie, inexperienced thumb, parking zone, pants, windshield, 
                French Fries, cotton sweet, residence
    - British: Inexperienced fingers, automotive park, trousers, windscreen, chips, candyfloss, 
               flag, garbage, biscuit

    Instance Sentences from every dialect:
    - American: As they strolled by means of 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, they usually loved the nice climate, 
                chatting about their weekend plans.
    - British: As they wandered by means of the picturesque neighbourhood, Sarah requested her 
               good friend if he fancied getting a espresso on the close by café. The autumn 
               leaves had been beautiful, they usually savoured the nice climate, chatting 
               about their weekend plans.

    Please begin the e-mail with a heat introduction. Add the introduction if you'll want to.
    
    Under is the e-mail, tone, and dialect:
    TONE: {tone}
    DIALECT: {dialect}
    EMAIL: {electronic 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 electronic mail to have?',
        ('Formal', 'Casual'))

with column2:
    dialect_drop_down = st.selectbox(
        'Which English Dialect would you want?',
        ('American', 'British'))

# Get the person enter electronic 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()

# Verify 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 electronic mail")
    st.cease()

# Operate to replace the textual content field with an instance electronic 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 electronic 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 supplied input_text, proceed with electronic mail conversion
if input_text:
    if not openai_api_key:
        # If API Key is just not supplied, 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 supplied 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, electronic mail=input_text)
    formatted_email = llm(prompt_with_email)
    # Show the formatted electronic mail
    st.write(formatted_email)

Conclusion

On this article, we’ve seen create a stupendous net software utilizing OpenAI LLM with the assistance of Langchain. We began with putting in and establishing Streamlit. Then we created a frontend interface to take person inputs like tone, dialect, and electronic 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 appliance into the net utilizing Streamlit.

Key Takeaways

  • Utilizing the Streamlit library in Python, we will construct interactive net purposes.
  • Immediate engineering performs essential for fetching optimized outcomes from the Language mannequin.
  • OpenAI LLM might be simply utilized in our Python purposes utilizing the OpenAI library and its secret keys.
  • Utilizing Langchain’s PromptTemplate, we will correctly format the Immediate based mostly on person enter which might additional be utilized in querying the LLM.
  • Utilizing Streamlit share, we will host the Python software in Stay URL.

Continuously Requested Questions

Q1. What’s using the Streamlit library in Python?

A. Streamlit is an Open supply Python library that can be utilized to create interactive net purposes with easy Python capabilities with out in depth data of net improvement.

Q2. Learn how to create an app with OpenAI?

A. First, you’ll want to select the appliance improvement tech stack, after which, utilizing OpenAI secret keys, you possibly can leverage the advantages of OpenAI.

Q3. Can I take advantage of ChatGPT to jot down emails?

A. Sure, you should use ChatGPT for writing emails. You may generate electronic mail content material by offering efficient prompts with clear descriptions of electronic mail expectations to ChatGPT.

This autumn. What’s the default OpenAI mannequin in LangChain?

A. The default OpenAI mannequin utilized in LangChain is OpenAI GPT-3.5-turbo.

Q5. Can I take advantage of OpenAI free of charge?

A. OpenAI offers each free and paid providers. You may get a restricted API request, GPT-3 mannequin free of charge 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. 

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here