์คํด๊ฐ ๋ง์..
openai์์ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ๋๊ฒ์ด ์๋
๊ธฐ์กด ์ง๋ฌธ : "{country}์ ์๋๊ฐ ์ด๋์ผ?" ๋ผ๊ณ ๋ฌผ์ผ๋ฉด ์์ธ์ด๋ผ๋ ๋ต๋ณ์ด ์ค๋๋ฐ
function calling์ ํ๋ฉด "๋๋ ํ๊ตญ์ ์๋๊ฐ ์ด๋์ธ์ง ์๊ณ ์ถ์ด?" ๋ผ๊ณ ๋ฌผ์ผ๋ฉด
get_city(country) ๋ผ๋ ํจ์๊ฐ ์๋ค๊ณ ํ๋ฉด "ํจ์ get_city๋ฅผ ์คํํ๊ณ argument ๋ก ํ๊ตญ์ ๋ฃ์ด๋ผ." ๋ผ๊ณ ์๋ ค์ฃผ๋๊ฒ. ์ค์ ๋ก ํจ์๋ฅผ ์คํํ๋๊ฒ๋ ์๋.
๊ทธ๋ฌ๋ ์ค์ ๋ก openai์์ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ๋๊ฒ์ด ์๋
๊ฒฐ๊ณผ๋ get_city์์ ์ฒ๋ฆฌํด์ผํจ.
from dotenv import load_dotenv
load_dotenv()
from langchain.prompts import PromptTemplate
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
temperature=0.1,
)
template = "{country}์ ์๋๋ ๋ญ์ผ?"
# ํ
ํ๋ฆฟ ์์ฑ
prompt = PromptTemplate.from_template(template=template)
prompt
chain.invoke({"country":"ํ๊ตญ"})
add function
def get_city(country):
print(country)
add schema
schema = {
"name": "get_city",
"description": "๋๋ผ์ ์๋๋ฅผ ๊ฐ์ ธ์ต๋๋ค.",
"parameters": {
"type": "object",
"properties": {
"country": { "type": "string","description": "๋๋ผ ์ด๋ฆ" }
}
},
"required": ["country"],
}
llm = ChatOpenAI(
temperature=0.1,
).bind(
function_call={
"name": "get_city",
},
functions=[
schema,
],
)
template = "{country}์ ์๋๋ ๋ญ์ผ?"
# ํ
ํ๋ฆฟ ์์ฑ
prompt = PromptTemplate.from_template(template=template)
prompt
chain = prompt | llm
chain.invoke({"country": "ํ๊ตญ"})
AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{"country":"ํ๊ตญ"}', 'name': 'get_city'}}, )
response = chain.invoke({"country": "ํ๊ตญ"})
arg_from_ai = response.additional_kwargs["function_call"]
arg_from_ai
arg_from_ai["arguments"]
arg_from_ai["name"]
import json
arguments = json.loads(arg_from_ai["arguments"])
country = arguments["country"]
country
if arg_from_ai["name"] == "get_city":
get_city(country)
Last updated