함수호출 프롬프트 엔지니어링
실행 준비
!pip install openai
!pip install gradio
Gradio ChatGPT 템플릿
import gradio as gr
from openai import OpenAI
client = OpenAI()
def generate_response(prompt_text, history):
completion = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "user", "content": prompt_text}
]
)
response = completion.choices[0].message;
return response.content
gr.ChatInterface(
generate_response,
chatbot=gr.Chatbot(height=300, label='AI 상품상담 에이전트 입니다.'),
textbox=gr.Textbox(placeholder="질문을 입력해 주세요", container=False, scale=7),
title="AI 상품상담 에이전트",
).launch()
함수 인수 가져오기
import gradio as gr
from openai import OpenAI
import json
client = OpenAI()
tools = [
{
"type": "function",
"function": {
"name": "product_inquiry",
"description": "상품에 대한 문의사항을 처리하는 함수",
"parameters": {
"type": "object",
"properties": {
"prod_type": {
"type": "string",
"description": "상품 유형으로서 상품을 표현하는 일반적인 용어가 사용되며, '세탁기', '냉장고', 'TV' 중에서 하나를 선택해줘",
},
},
"required": ["prod_type"]
},
},
},
]
def generate_response(prompt_text, history):
completion = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "상품에 대한 문의사항을 받았을 경우, product_inquiry 함수를 호출해줘."},
{"role": "user", "content": prompt_text}
],
tools=tools,
)
response = completion.choices[0].message;
if response.tool_calls:
tool_call = response.tool_calls[0]
arguments = tool_call.function.arguments
function_name = tool_call.function.name
arguments_json = json.loads(arguments)
if function_name == "product_inquiry":
prod_type = arguments_json.get('prod_type')
return prod_type
else:
return "정의되지 않은 함수입니다."
elif response.function_call:
return response.function_call
else:
return response.content
gr.ChatInterface(
generate_response,
chatbot=gr.Chatbot(height=300, label='AI 상품상담 에이전트 입니다.'),
textbox=gr.Textbox(placeholder="질문을 입력해 주세요", container=False, scale=7),
title="AI 상품상담 에이전트",
).launch()
함수 추가하기
import gradio as gr
from openai import OpenAI
import json
client = OpenAI()
# 쿼리를 실행하는 함수 정의
def product_inquiry(prod_type, prompt_text):
res = prod_type + " 데이터를 함수 호출로 요청받았습니다."
return res
tools = [
{
"type": "function",
"function": {
"name": "product_inquiry",
"description": "상품에 대한 문의사항을 처리하는 함수",
"parameters": {
"type": "object",
"properties": {
"prod_type": {
"type": "string",
"description": "상품 유형으로서 상품을 표현하는 일반적인 용어가 사용되며, '세탁기', '냉장고', 'TV' 중에서 하나를 선택해줘",
},
},
"required": ["prod_type"]
},
},
},
]
def generate_response(prompt_text, history):
completion = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "상품에 대한 문의사항을 받았을 경우, product_inquiry 함수를 호출해줘."},
{"role": "user", "content": prompt_text}
],
tools=tools,
)
response = completion.choices[0].message;
if response.tool_calls:
tool_call = response.tool_calls[0]
arguments = tool_call.function.arguments
function_name = tool_call.function.name
arguments_json = json.loads(arguments)
if function_name == "product_inquiry":
prod_type = arguments_json.get('prod_type')
# 데이터 조회 함수 실행
query_res = product_inquiry(prod_type, prompt_text)
return query_res
else:
return "정의되지 않은 함수입니다."
elif response.function_call:
return response.function_call
else:
return response.content
gr.ChatInterface(
generate_response,
chatbot=gr.Chatbot(height=300, label='AI 상품상담 에이전트 입니다.'),
textbox=gr.Textbox(placeholder="질문을 입력해 주세요", container=False, scale=7),
title="AI 상품상담 에이전트",
).launch()