Using ChatGPT to Interact with Third-Party Applications in Python

usmanmalik57 3 Tallied Votes 1K Views Share

Integrating language models like ChatGPT into third-party applications has become increasingly popular due to their ability to comprehend and generate human-like text. However, it's crucial to acknowledge the limitations of ChatGPT, such as its knowledge cut-off date in September 2021 and its inability to access external sources like Wikipedia or Python directly.

Recognizing this challenge, Harrison Chase, the co-founder, and CEO of LangChain, came up with an innovative solution. He developed the Python LangChain module, which empowers developers to integrate third-party applications with large language models seamlessly. This breakthrough opens up a world of possibilities, allowing developers to harness the power of language models while effectively processing information from external sources.

In this article, we will explore the fascinating concept of using ChatGPT to interact with third-party applications using the Python LangChain module. By the end, you will have a deeper understanding of how to leverage this integration and create even more sophisticated and efficient applications.

Importing ChatGPT from LangChain

The first step is to install the Python LangChain module which you can do with the following pip command.

pip install langchain

Next, you need to import the ChatOpenAI class from the langchain.chat_models module. The ChatOpenAI class allows you to create an instance of ChatGPT. To do so, pass gpt-3.5-turbo model to the model_name attribute of the ChatOpenAI class. The OpenAI’s gpt-3.5turbo model powers ChatGPT. You also need to pass your OpenAI API key to the open_api_key attribute.

from langchain.chat_models import ChatOpenAI
import os

api_key = os.getenv('OPENAI_KEY2')
chatgpt = ChatOpenAI(model_name="gpt-3.5-turbo",
                    openai_api_key = api_key)

We are now ready to integrate third-party applications with ChatGPT in Python.

Using ChatGPT to Extract Information from Wikipedia

As earlier said, ChatGPT’s knowledge cut-off date is September 2021 and it cannot answer queries beyond that period. For example, if you ask ChatGPT to return the summary of the Wikipedia article on the 2022 Wimbledon Championships, you will get the following answer:

image_1.PNG

LangChain agents allow you to interact with third-party applications. Check out the list of all LangChain agent integrations for more information.

Let’s see how to integrate ChatGPT with third-party applications such as Wikipedia with the help of an example code.

You have to import the load_tools, initialize_agent, and AgentType entities from the langchain.agents module.

Next, you should provide the agent type as input to the load_tools class. In the example script below, the agent type specified is wikipedia. The subsequent step involves creating an agent object using the initialize_agent() method. When calling the initialize_agent() method, you will need to pass the tool type, the ChatGPT instance, and the agent type as arguments. If you set the verbose parameter to True, it will display the thought process involved in the agent's task execution.

In the following script, we ask the Wikipedia agent to return the summary of the Wikipedia article on the 2022 Wimbledon Championships.

In the output, you can see the agent’s though process and the final results that contain the summary of the article.

from langchain.agents import load_tools, initialize_agent, AgentType

tools = load_tools(
    ['wikipedia'], 
)

agent_chain = initialize_agent(
    tools,
    chatgpt,
    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
    verbose=True,
)

agent_chain.run(
    "Return the summary of Wikipedia article on the 2022 Wimbledon Championships.",
)

image_2.PNG

Extracting Information from Arxiv

Let’s see another example. We will retrieve the title and author names of an article from ArXiv which is a popular open-access repository for scientific research papers, preprints, and other scholarly articles.

The script remains the same except that you have to pass arxiv as the parameter value to the load_tools() method.

tools = load_tools(
    ["arxiv"], 
)

agent_chain = initialize_agent(
    tools,
    chatgpt,
    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
    verbose=True,
)

agent_chain.run(
    "Give me the title and the author names of the paper 1301.3781",
)

image_3.PNG

Extracting Information from CSV

LangChain provides methods to directly create agent instances for particular tasks. For instance, the create_csv_agent() method from the langchain.agents module allows you to create a CSV agent that interacts with CSV files.

Let’s see an example. The following script imports a dataset containing a company’s employee attrition information.

import pandas as pd
dataset = pd.read_csv(r'D:\Datasets\employee_attrition_dataset.csv')
dataset.head()

image_4.PNG

Let’s use a CSV agent to get information from this file. We will ask ChatGPT to return the total number of employees from the sales department.

In the output, you can see the process that ChatGPT adopted to return the output.

from langchain.agents import create_csv_agent

agent = create_csv_agent(
    chatgpt,
    r'D:\Datasets\employee_attrition_dataset.csv',
    verbose=True
)

agent.run("Return the total number of employees from the sales department.")

image_5.PNG

Extracting Information from Pandas DataFrame

Similarly, you can extract information from a Pandas dataframe using create_pandas_dataframe_agent() method. In the following script we ask ChatGPT to return the total number of employees from the sales department whose education field is medical.

import pandas as pd
dataset = pd.read_csv(r'D:\Datasets\employee_attrition_dataset.csv')

from langchain.agents import create_pandas_dataframe_agent

agent = create_pandas_dataframe_agent(
    chatgpt,
    dataset,
    verbose=True
)

agent.run("Return the total number of employees from the sales department whose education field is medical.")

image_6.PNG

And with that, I will end this tutorial. I hope you would have liked it. Feel free to share your opinion on this article. I will appreciate if you provide suggestions on how to use LangChain to integrate ChatGPT with other third party applications.

AndreRet 526 Senior Poster

Great tutorial on ho to get started!

catherine_11 0 Newbie Poster

Integrating ChatGPT with third-party applications in Python involves utilizing OpenAI's API. Begin by obtaining API credentials, then craft Python scripts to send requests and process responses. Adhere to OpenAI's documentation for optimal integration, ensuring secure and efficient interaction with the ChatGPT model.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.