intent classification- openai/ langchain

2k views Asked by At

What are the ways that we can do intent classification in a conversation. If we have used conversational chain is there anyway we can do that.

I need to identify the users intention to update or insert operation. But I dont know which line exactly say the intention. Any suggestions

1

There are 1 answers

0
林抿均 On

This is my code using AzureOpenAI and LangChain to do the intent classification. Btw, this is zero-shot prompting. It is better for you to have examples to feed in the prompt to make the classification more promissing.

import os
import pandas as pd
from dotenv import load_dotenv
import openai
from langchain.llms.huggingface_hub import HuggingFaceHub
from langchain.llms.openai import OpenAI, AzureOpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain

# Load the OpenAI API key from the .env file
def load_openai_credentials():
    load_dotenv()
    openai.api_type = os.environ["OPENAI_API_TYPE"]
    openai.api_key = os.environ["OPENAI_API_KEY"]
    openai.api_base = os.environ["OPENAI_API_BASE"]
    openai.api_version = os.environ["OPENAI_API_VERSION"]

# Instartiate AzureOpenAI object 
def create_llm():
    llm = AzureOpenAI(
        deployment_name="text-davinci-003",
        model_name="text-davinci-003",
        temperature=0.3,
        max_tokens=40,
        # frequency_penalty=0, # Default is 0
        presence_penalty=0.88, # Default is 0
    )
    return llm

# Create prompt template
def create_prompt(llm):
    prompt = """
    Please act as a robust and well-trained intent classifier that can identify the most likely PRECISE, SHORT and GENERIC intent behind a user's query WITHOUT USING the proper and common noun subject from the user's query.

    The identified intent must be less than six words.

    User's query: {question}

    Intent:
    """
    return PromptTemplate(template=prompt, input_variables=['question'])

def main():

    # ============================================= Azure OpenAI =============================================

    # Load the OpenAI API key from the .env file
    load_openai_credentials()

    # Create LLM
    llm = create_llm()

    # Create prompt template
    prompt_template = create_prompt(llm)

    # Create LLMChain
    llm_chain = LLMChain(llm=llm, prompt=prompt_template)

    # ============================================= Load Query Data from CSV =============================================

    # Load the Excel workbook
    df = pd.read_excel('J.AI Search & Chat Latest PBI_OpenAI.xlsx')

    # Convert the column 'Latest Search Merge New[SearchString]' as a list
    lst_of_str = df['Latest Search Merge New[SearchString]'].tolist()
    # lst_of_str = ["What is DD&I?", "What is Transformers?", "PD&T", "Who is the head of Enterprise Data Hub in Petronas Digital Pte. Ltd."]

    # Convert list_of_str variable to a list of dictionaries
    # Format: questions = [{'question': "1st str in the list"}, {'question': "2nd str in the list"}, ...]
    questions = [{'question': str(s)} for s in lst_of_str]

    # Get intents from LLMChain
    intents = llm_chain.generate(questions)
    # print(f'{intents.dict().get("generations")}\n')

    # Retrieve the intents and save it as a list
    intents_lst = [intent[0].get('text').replace('\n', ' ').strip() for intent in intents.dict().get("generations")]

    for intent in intents.dict().get("generations"):
        print(intent[0].get('text').replace('\n', ' ').strip())
    
    # Convert the list to a series
    intents_series = pd.Series(intents_lst)

    # Add the Series to the DataFrame
    df = df.assign(**{'Intent': intents_series})

    # Save the DataFrame to a CSV file
    df.to_excel('OpenAI Intent.csv', index=False)


if __name__ == "__main__":
    main()