OPENAI_INTEGRATION.md•3.43 kB
# 🚀 HRFCO OpenAI Function Calling Integration
## ✅ 완료된 변환 작업
### MCP → REST API 변환
- **이전**: MCP 서버 (JSON-RPC 프로토콜)
- **현재**: REST API (HTTP GET/POST)
- **호환성**: OpenAI Function Calling 완전 지원
### 핵심 구성요소
#### 1. REST API 서버 (`openai_api_server.py`)
```python
@app.get("/observatories")
async def get_observatories(hydro_type: str = "waterlevel", limit: int = 5):
"""OpenAI Function이 호출할 엔드포인트"""
result = await client.get_observatories(hydro_type, limit)
return result
```
#### 2. Function 정의 (`openai_function_definition.json`)
```json
{
"name": "get_korean_water_observatories",
"description": "Get Korean water level or rainfall observatory information",
"parameters": {
"type": "object",
"properties": {
"hydro_type": {"type": "string", "enum": ["waterlevel", "rainfall", "dam"]},
"limit": {"type": "integer", "minimum": 1, "maximum": 10}
}
}
}
```
## 🔧 OpenAI API 사용법
### 1. 서버 시작
```bash
cd /home/ubuntu/hrfco-service
source venv/bin/activate
python3 openai_api_server.py
```
### 2. Function 정의 가져오기
```bash
curl http://localhost:8000/openai/functions
```
### 3. OpenAI API 호출 예제
```python
import openai
# Function 정의
functions = [
{
"name": "get_korean_water_observatories",
"description": "Get Korean water observatory data",
"parameters": {
"type": "object",
"properties": {
"hydro_type": {"type": "string", "enum": ["waterlevel", "rainfall"]},
"limit": {"type": "integer", "minimum": 1, "maximum": 10}
}
}
}
]
# ChatGPT 호출
response = openai.ChatCompletion.create(
model="gpt-4",
messages=[{"role": "user", "content": "한국의 수위 관측소 3개를 조회해줘"}],
functions=functions,
function_call="auto"
)
# Function call 처리
if response.choices[0].message.get("function_call"):
function_call = response.choices[0].message["function_call"]
# API 호출 로직 실행
```
## 📊 성능 최적화
### ✅ 응답 크기 제한
- **총 관측소**: 1,366개
- **반환 제한**: 3-10개 (설정 가능)
- **응답 크기**: 859 bytes (1KB 미만)
### ✅ API 엔드포인트
- **Health Check**: `GET /health`
- **Observatory Data**: `GET /observatories?hydro_type=waterlevel&limit=5`
- **Function Definitions**: `GET /openai/functions`
## 🌐 배포 옵션
### Option 1: 로컬 서버
```bash
# 현재 실행 중
http://localhost:8000
```
### Option 2: 클라우드 배포
- **Heroku**: `Procfile` 생성 필요
- **AWS Lambda**: Serverless 변환 필요
- **Google Cloud Run**: Docker 컨테이너화 필요
## 🔗 실제 사용 예제
### 테스트 결과
```json
{
"observatories": [
{
"wlobscd": "1001602",
"obsnm": "평창군(송정교)",
"addr": "강원특별자치도 평창군 진부면",
"almwl": "5"
}
],
"total_count": 1366,
"returned_count": 3
}
```
### Function Response Size: 859 bytes ✅
## 🎯 다음 단계
1. **OpenAI API 키 설정**
2. **Function 정의를 OpenAI 프로젝트에 추가**
3. **실제 ChatGPT에서 테스트**
4. **프로덕션 배포 (선택사항)**
---
**🎉 핵심 성과**: MCP → REST API 변환 완료, OpenAI Function Calling 호환성 확보, 응답 크기 최적화 유지!