rn we’re just repeating what we did previously (making the functions for openai) but just with pydantic → just creating the schema
class WeatherSearch(BaseModel): """Call this with an airport code to get the weather at that airport""" airport_code: str = Field(description="airport code to get weather for")from langchain.utils.openai_functions import convert_pydantic_to_openai_functionweather_function = convert_pydantic_to_openai_function(WeatherSearch)
{'name': 'WeatherSearch',
'description': 'Call this with an airport code to get the weather at that airport',
'parameters': {'title': 'WeatherSearch',
'description': 'Call this with an airport code to get the weather at that airport',
'type': 'object',
'properties': {'airport_code': {'title': 'Airport Code',
'description': 'airport code to get weather for',
'type': 'string'}},
'required': ['airport_code']}}
you need a function description string """""" or else u cannot convert
from langchain.prompts import ChatPromptTemplateprompt = ChatPromptTemplate.from_messages([ ("system", "You are a helpful assistant"), ("user", "{input}")])chain = prompt | model_with_functionchain.invoke({"input": "what is the weather in sf?"})
multiple functions
Even better, we can pass a set of function and let the LLM decide which to use based on the question context
class ArtistSearch(BaseModel): """Call this to get the names of songs by a particular artist""" artist_name: str = Field(description="name of artist to look up") n: int = Field(description="number of results")class WeatherSearch(BaseModel): """Call this with an airport code to get the weather at that airport""" airport_code: str = Field(description="airport code to get weather for")functions = [ convert_pydantic_to_openai_function(WeatherSearch), convert_pydantic_to_openai_function(ArtistSearch),]model_with_functions = model.bind(functions=functions)model_with_functions.invoke("what is the weather in sf?")# AIMessage(content='', additional_kwargs={'function_call': {'name': 'WeatherSearch', 'arguments': '{"airport_code":"SFO"}'}})model_with_functions.invoke("what are three songs by taylor swift?")# AIMessage(content='', additional_kwargs={'function_call': {'name': 'ArtistSearch', 'arguments': '{"artist_name":"Taylor Swift","n":3}'}})model_with_functions.invoke("hi!")# AIMessage(content='Hello! How can I assist you today?')