AI

How to Use OpenAI Functions to Get Structured Outputs?

OpenAI is a powerful tool that provides API which can be used to build AI applications using Natural language Processing or NLP models. LangChain allows the use of OpenAI modules and its API key to integrate with AI models like chatbots and many more. OpenAI offers multiple applications that are used widely across the globe to create models to solve NLP challenges.

This post demonstrates the process of using OpenAI functions to get structured outputs using LangChain.

How to Use OpenAI Functions to Get Structured Outputs?

To use OpenAI functions to get structured outputs using LangChain, simply follow this easy guide containing multiple OpenAI functions:

Install Prerequisites

Install the LangChain framework using the following code on any Python IDE like Google Collaboratory:

pip install langchain

 

Install OpenAI modules to use its functions in LangChain to build powerful applications:

pip install openai

 

After installing the OpenAI module, simply provide the API key for the OpenAI after executing the following code:

import os
import getpass

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

 

Import Libraries

After integrating the OpenAI API key with the model, simply import libraries using the LangChain tools and OpenAI functions:

from typing import Optional

from langchain.chains.openai_functions import (
    create_openai_fn_chain,
    create_structured_output_chain,
)
from langchain.chat_models import ChatOpenAI
from langchain.prompts import ChatPromptTemplate, HumanMessagePromptTemplate
from langchain.schema import HumanMessage, SystemMessage

 

Method 1: Using Pydantic Classes to Get Structured Output

OpenAI functions can be built using pydantic classes which is the library of the OpenAI that can be used to perform multiple tasks like type checking, validation, error checking, and many more. Create the Person class and configure its details in the class as used by the following code:

from pydantic import BaseModel, Field


class Person(BaseModel):
    """Identifying information about a person."""

    name: str = Field(..., description="The person's name")
#describing the details of the person to get from the prompt
    age: int = Field(..., description="The person's age")
    fav_food: Optional[str]=Field(None, description="The person's favorite food")

 

Design the prompt template for the chatbot using LLM and then write prompt/command to extract useful information from it by running the chain from LangChain:

llm = ChatOpenAI(temperature=0)

prompt_msgs = [
    SystemMessage(
        content="Brilliant algorithm for creating bots"
    ),
    HumanMessage(
        content="Use the given format to get data from the following input:"
    ),
    HumanMessagePromptTemplate.from_template("{input}"),
    HumanMessage(content="Tips: Answer in the Proper Format"),
]
prompt = ChatPromptTemplate(messages=prompt_msgs)

chain = create_structured_output_chain(Person, llm, prompt, verbose=True)
chain.run("Sally is 13")

 

Output

The followings screenshot displays the information from the prompt and displays the variables defined in the Person class:

Method 2: Using JSON Schema to Get Structured Output

JSON schema can be used to describe the configurations of the OpenAI function. The JSON Schema mentioned in the following code is used to describe the person’s details like name, age, and favorite food:

json_schema = {
    "title": "Person",
    "description": "Identifying information about a person.",
    "type": "object",
#properties containing the details of the person to get from the Human input
    "properties": {
        "name": {"title": "Name", "description": "The person's name", "type": "string"},
        "age": {"title": "Age", "description": "The person's age", "type": "integer"},
        "fav_food": {
            "title": "Fav Food",
            "description": "The person's favorite food",
            "type": "string",
        },
    },
    "required": ["name", "age"],
}

 

Get the structured output from the JSON Schema function by providing the required data from the prompt:

chain = create_structured_output_chain(json_schema, llm, prompt, verbose=True)
chain.run("Sally is 13")

 

The following screenshot displays the name and age of the person being discussed in the above prompt:

That is all about using OpenAI Functions to get structured outputs using LangChain.

Conclusion

To use OpenAI functions for getting structured outputs using LangChain, simply install the LangChain and OpenAI modules to use its functions. After that, import libraries to create pydantic functions which can be used to build and implement OpenAI generic functions as well as the structured output function. JSON Schema is another method to use OpenAI functions in LangChain which can build question-answering chatbots. This post demonstrated the process of using OpenAI functions to get structured outputs using 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.