DeepLearning.AI의 강의 정리내용 입니다.
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_function
weather_function = convert_pydantic_to_openai_function(WeatherSearch)
weather_function
# 결과
{'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']}}
# Docstring이 없으면 생성 안됨
class WeatherSearch1(BaseModel):
airport_code: str = Field(description="airport code to get weather for")
# Field의 description은 없어도 됨
class WeatherSearch2(BaseModel):
"""Call this with an airport code to get the weather at that airport"""
airport_code: str
# 이로 생성된 함수 사용
from langchain.chat_models import ChatOpenAI
model = ChatOpenAI()
model.invoke("what is the weather in SF today?", functions=[weather_function])
# 결과
AIMessage(content='', additional_kwargs={'function_call': {'name': 'WeatherSearch', 'arguments': '{\n "airport_code": "SFO"\n}'}})
# 또는 bind
model_with_function = model.bind(functions=[weather_function])
# 또는 강제
model_with_forced_function = model.bind(functions=[weather_function], function_call={"name": "WeatherSearch")
model_with_forced_function.invoke("hi!")
# 결과
AIMessage(content='', additional_kwargs={'function_call': {'name': 'WeatherSearch', 'arguments': '{\n "airport_code": "JFK"\n}'}})
from langchain.prompts import ChatPromptTemplate
prompt = ChatPromptTemplate.from_messages([
("system", "You are a helpful assistant"),
("user", {input}")
])
chain = prompt | model_with_function
chain.invoke({"input": "what is the weather in sf"})
# 결과
AIMessage(content='', additional_kwargs={'function_call': {'name': 'WeatherSearch', 'arguments': '{\n "airport_code": "SFO"\n}'}})
# 예시용, 단순히 갯수만 반환해줌.
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")
# pydantic
functions = [
convert_pydantic_to_openai_function(WeatherSearch),
convert_pydantic_to_openai_function(ArtistSearch),
]
# bind로 update
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': '{\n "airport_code": "SFO"\n}'}})
model_with_functions.invoke("what are three songs by taylor swift?")
# 결과
AIMessage(content='', additional_kwargs={'function_call': {'name': 'ArtistSearch', 'arguments': '{\n "artist_name": "taylor swift",\n "n": 3\n}'}})
model_with_functions.invoke("hi!")
# 결과
AIMessage(content='Hello! How can I assist you today?')