[AI] OpenAI API를 이용한 위치기반 검색 자동화
OpenAI의 Chat Completions API를 활용하면 사용자의 자연어 질문을 기반으로 위치 검색 쿼리를 자동으로 생성할 수 있습니다. OpenStreetMap의 Overpass API를 사용법에 대해 GPT는 학습되어 있기 때문에 프롬프트와 위치 조합으로 원하는 장소와 시설 정보를 조회할 수 있다.
다음은 OpenAI의 Node.js SDK 사용법과 Overpass API 쿼리 자동화를 구현하는 과정이다.
설치
먼저 node.js에서 OpenAI API를 사용하기 위해 openai 라이브러리를 설치한다.
npm install openai
API 호출 기본 설정
기본 설정 및 API 호출 코드는 다음과 같다. Completions 모델은 gpt-3.5-turbo를 사용하였다.(작성 기준일 2023년 6월 13일)
import { Configuration, OpenAIApi, CreateCompletionRequest } from 'openai';
const configuration = new Configuration({
organization: `${ORGANIZATION_ID}`,
apiKey: `${OPENAI_API_KEY}`
});
const openai = new OpenAIApi(configuration);
const params: CreateCompletionRequest = {
prompt: `${input}`,
model: 'gpt-3.5-turbo',
max_tokens: 2048,
temperature: 0
};
const { data } = await openai.createCompletion(params);
return data.choices[0].text;
공식문서 : https://github.com/openai/openai-node
GitHub - openai/openai-node: Official JavaScript / TypeScript library for the OpenAI API
Official JavaScript / TypeScript library for the OpenAI API - openai/openai-node
github.com
자연어를 Overpass API 쿼리로 변경
OpenAI Chat API를 이용하면 사용자의 질문에 따라 Overpass API 쿼리를 자동 생성할 수 있다.
Request
{
"model": "gpt-3.5-turbo",
"messages": [
{"role": "system", "content": "Write an OpenStreetMap Overpass API data query statement..."},
{"role": "user", "content": "동대문역 750m 근방에 있는 아파트를 찾아줘..."}
]
}
Response
{
"choices": [
{
"message": {
"content": "[out:json][timeout:25];(node[\"building\"=\"apartments\"](around:750,37.571279,127.009285);way[\"building\"=\"apartments\"](around:750,37.571279,127.009285);relation[\"building\"=\"apartments\"](around:750,37.571279,127.009285););out;>;out skel qt;"
}
}
]
}
위의 응답을 통해 다음과 같이 URL을 생성할 수 있다.
https://overpass-api.de/api/interpreter?data=[out:json][timeout:25];(node["building"="apartments"](around:750,37.571279,127.009285);way["building"="apartments"](around:750,37.571279,127.009285);relation["building"="apartments"](around:750,37.571279,127.009285););out;>;out skel qt;
프롬프트
다음의 프롬프트를 사용하면 위치 기반 질문은 Overpass API로, 경로 탐색 질문은 OpenRouteService API로 처리할 수 있다.
1. 위치를 찾는 질문이면 아래와 같은 조건으로 응답해줘. 이해했으면 'Yes 위치검색' 라고만 말해줘.
1.1 질문에 대해서 장소를 검색할 수 있는 OpenStreetMap의 Overpass API 쿼리를 작성해줘.
1.2 작성된 쿼리를 Get 방식의 요청문. https://overpass-api.de/api/interpreter?data={QUERY}
1.3 설명은 제외하고 URL만 한줄로 응답
2. 경로를 찾는 질문이면 아래와 같은 조건으로 응답해줘. 이해했으면 'Yes 경로검색' 라고만 말해줘.
2.1 api_key : {open_ai_api_key}
2.2 좌표는 경도, 위도 순서로 표시
2.3 Get 방식의 요청문. https://api.openrouteservice.org/v2/directions/{profile}?{PARAMETER}
2.4 설명은 제외하고 URL만 한줄로 응답
ChatGPT API 새로운 기능 (2023년 6월 기준)
- Chat Completions API의 함수 호출 기능
- new function calling capability in the Chat Completions API
- updated and more steerable versions of gpt-4 and gpt-3.5-turbo
- new 16k context version of gpt-3.5-turbo (vs the standard 4k version)
- 75% cost reduction on our state-of-the-art embeddings model
- 25% cost reduction on input tokens for gpt-3.5-turbo
- announcing the deprecation timeline for the gpt-3.5-turbo-0301 and gpt-4-0314 models