LangChain

How to Implement OpenAI Functions Using OpenAPI Call in LangChain?

OpenAI in LangChain is used to build chatbots using Natural language Processing domains in Artificial Intelligence. OpenAI provides the API Keys which can be integrated with LLM applications to manage question-answering models which makes it more efficient. The user can extract data from the internet using the OpenAPI call by implementing OpenAI functions.

This guide will explain the process of implementing OpenAI functions using OpenAPI calls in LangChain.

How to Implement OpenAI Functions Using OpenAPI Call in LangChain?

To implement OpenAI functions using OpenAPI calls, simply follow this guide will different OpenAPI calls:

Setup Prerequisites

Install LangChain modules using the following code to start using OpenAI functions:

pip install langchain

 

Install the OpenAI module to use its functions in LangChain:

pip install openai

 

Use the API key of the OpenAI after executing the following code:

import os
import getpass

os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:")

 

Method 1: Using Klarna OpenAPI Call

Once the OpenAI API key is integrated with the model, simply import the “get_openapi_chian” library:

from langchain.chains.openai_functions.openapi import get_openapi_chain

 
Use the library with the Klarna OpenAPI call and get data by executing the chain:

chain = get_openapi_chain(
    "https://www.klarna.com/us/shopping/public/openai/v0/api-docs/"
)

 

After that, simply execute chain.run() function with the command written inside its braces to get data accordingly:

chain.run("Shirt options for men in blue color")

 
The data extracted from the OpenAPI call based on the command are the details of the shirts available for men in the blue color:

Method 2: Using OpenAI Function in Translation Service

Execute the “get_openapi_chain()” function using the link of the translation model to get translation in different languages:

chain = get_openapi_chain("https://api.speak.com/openapi.yaml", verbose=True)

 
Execute the chain with a prompt with the language to translate the text inside its arguments:

chain.run("Say How are you in arabic")

 

Output

The output screenshot displays the JSON format of the command converting “How are you” in Arabic:

Method 3: Using XKCD OpenAPI Call

Another OpenAPI call is XKCD which can be used to get details of the books using its link as displayed in the following code:

chain = get_openapi_chain(
"https://gist.githubusercontent.com/roaldnefs/053e505b2b7a807290908fe9aa3e1f00/raw/0a
212622ebfef501163f91e23803552411ed00e4/openapi.yaml"

)

 

Run the prompt used inside the chain.run() function to extract information using the OpenAPI call:

chain.run("What's today's fiction?")

 
The following screenshot displays the books available in the fiction genre with their details like number, year, title, etc.:


That is all about implementing OpenAI functions using OpenAPI calls in LangChain.

Conclusion

To implement OpenAI functions using the OpenAPI call in LangChain, simply install LangChain and OpenAI modules to implement its different functions. After that, setup an OpenAI API key from its account and then use different OpenAPI calls like Klarna, Translation service, and XKCD. This guide has explained the process of implementing OpenAI functions using OpenAPI calls in LangChain.

About the author

Talha Mahmood

As a technical author, I am eager to learn about writing and technology. I have a degree in computer science which gives me a deep understanding of technical concepts and the ability to communicate them to a variety of audiences effectively.