Monday, July 10, 2023

Langchain OpenAI Functions Agent

The OpenAI Functions Agent in Langchain is designed to work with models like gpt-3.5-turbo-0613 and gpt-4–0613 to automatically complete the tasks from tool description to JSON generation to function calls

a) Tool Creation

The tool creation is following the normal Langchain process of custom tool creation. You need to define a class GetflightInPeriodTool inherit BaseTool class by register name, description, _run() and _arun(), and do a format and existence check for input arguments via GetflightInPeriodCheckInput()


from langchain.tools import BaseTool

from pydantic import BaseModel, Field

from typing import Optional, Type


class GetflightInPeriodCheckInput(BaseModel):


    fly_from: str = Field(..., description="the 3-digit code for departure airport")

    fly_to: str = Field(..., description="the 3-digit code for arrival airport")

    date_from: str = Field(..., description="the dd/mm/yyyy format of start date for the range of search")

    date_to: str = Field(..., description="the dd/mm/yyyy format of end date for the range of search")

    sort: str = Field(..., description="the catagory for low-to-high sorting, only support 'price', 'duration', 'date'")


class GetflightInPeriodTool(BaseTool):

    name = "get_flight_in_period"

    description = """Useful when you need to search the flights info. You can sort the result by "sort" argument.

                    if there is no year, you need to use 2023 for search.

                    Try to understand the parameters of every flight


                  """

    def _run(self, fly_from: str, fly_to: str, date_from: str, date_to: str, sort: str):

        get_flight_in_period_response = get_flight_in_period(fly_from, fly_to, date_from, date_to, sort)


        return get_flight_in_period_response


    def _arun(self, fly_from: str, fly_to: str, date_from: str, date_to: str, sort: str):

        raise NotImplementedError("This tool does not support async")



    args_schema: Optional[Type[BaseModel]] = GetflightInPeriodCheckInput


b) Agent Creation


After the tool is created which can successfully request flight info from Kiwi API, the next step is to simply create a Langchain agent with it.


Import the Langchain methods and provide your OpenAI API Key.


from langchain.agents import initialize_agent, Tool

from langchain.agents import AgentType

from langchain.chat_models import ChatOpenAI

import os

os.environ["OPENAI_API_KEY"] = "{Your_API_Key}"



Push our GetflightInPeriodTool() into tools list and use ChatOpenAI() to create llm with gpt-3.5-turbo-0613 model. Please note that this time we should select OPENAI_FUNCTIONS as the agent type to activate the function callings features.


tools = [GetflightInPeriodTool()]

llm = ChatOpenAI(temperature=0.7, model="gpt-3.5-turbo-0613")    

open_ai_agent = initialize_agent(tools,

                            llm,

                            agent=AgentType.OPENAI_FUNCTIONS,

                            verbose=True)


@cl.langchain_factory(use_async=False)

def agent():

    tools = [GetflightInPeriodTool()]

    functions = [format_tool_to_openai_function(t) for t in tools]

    print(functions[0])


    llm = ChatOpenAI(temperature=0.7, model="gpt-3.5-turbo-0613")

    

    open_ai_agent = initialize_agent(tools,

                            llm,

                            agent=AgentType.OPENAI_FUNCTIONS,

                            verbose=True)

    return open_ai_agent


Now running it as Chainlist is pretty easy as below 

chainlit run flight_chatbot.py


references:

https://levelup.gitconnected.com/monetize-your-chatbot-by-using-openais-function-calling-and-langchain-157d0d97cd79

No comments:

Post a Comment