-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcall_api.py
More file actions
98 lines (82 loc) · 2.5 KB
/
call_api.py
File metadata and controls
98 lines (82 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import requests
import json
BASE_URL = "http://localhost:8888"
def test_ollama():
"""GET /ollama_test — 서버‑Ollama 연결 확인"""
print("test 1: /ollama_test")
print("===========================")
try:
r = requests.get(
f"{BASE_URL}/ollama_test",
params={"prompt": "just say hi"},
timeout=30,
)
r.raise_for_status()
print("response:")
print(r.json().get("response"))
except requests.RequestException as e:
print("에러 발생:", e)
def test_command():
"""GET /command — 전장 상태 입력으로 LLM 명령 생성"""
print("\ntest 2: /command (LLM‑based decision)")
print("======================================")
# 전장 정보
battle_state = {
"robot_availability": {
"robot1": True,
"robot2": True,
"robot3": True,
},
"robot_equipment": {
"robot1": {
"대전차 무장": 2,
"소총 무장": 2,
"통신중계기": 1,
},
"robot2": {
"소총 무장": 2,
"통신중계기": 1,
},
"robot3": {
"소총 무장": 1,
"지뢰탐지기": 2,
"통신중계기": 1,
},
},
"enemy_size": {
"적 전차": 0,
"적 병사": 0,
},
"battlefield_info": {
"우회로 여부": True,
"피격 여부": False,
"적 지뢰 여부": True,
},
"possible_responses": [
"소총",
"대전차",
"통신중계기",
"회피",
"대기",
"기존 과업",
],
}
payload = {"state": battle_state}
try:
print("request:")
print(battle_state)
r = requests.post(f"{BASE_URL}/command", json=payload, timeout=30)
r.raise_for_status()
# print("응답:", r.json())
# pasre response
raw_str = r.json().get("response").strip()
if raw_str.startswith("```json"):
raw_str = raw_str[len("```json") : -len("```")].strip()
parsed_json = json.loads(raw_str) # validate json
print("parsed response:")
print(parsed_json)
except requests.RequestException as e:
print("에러 발생:", e)
if __name__ == "__main__":
test_ollama()
test_command()