-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathyoon.cpp
More file actions
1052 lines (893 loc) · 37.1 KB
/
yoon.cpp
File metadata and controls
1052 lines (893 loc) · 37.1 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <vector>
#include "sqlite3.h"
#include <curl/curl.h>
#include <ctime>
#include <nlohmann/json.hpp>
#include <string>
#include "moonsoo.h" //센서데이터
#include <deque>
#include <numeric>
#include <locale>
#ifdef _WIN32
#define DLL_EXPORT extern "C" __declspec(dllexport)
#include <windows.h>
#else
#define DLL_EXPORT extern "C"
#endif
using json = nlohmann::json;
using namespace std;
//수정금지 필수함수
size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* userp) {
userp->append((char*)contents, size * nmemb);
return size * nmemb;
}
// 전방 선언
struct Answer;
class ChatCache;
//응답 기록 데이터용 구조체&함수 ==============================================
struct Answer {
//응답 분석 구조체 (이 형식대로 구조체에 들어가고, 이 외 형식은 강제로 제거)
//시스템정보
int id = -1; //몇번째인가
string chat_id; //어느 세션인가
string role; //System, User
//응답정보
string text; //응답 텍스트
string key1; //키워드 세개
string key2;
string key3;
float approval = 0.0;
time_t timestamp = time(nullptr);
};
//JS 직렬화 함수 (구조체 -> JSON), 밑에는 그 반대
inline void to_json(json& j, const Answer& msg) { //구조체 그대로 다시 주기
j = json{
{"id", msg.id},
{"chat_id", msg.chat_id},
{"role", msg.role},
{"content", msg.text},
{"timestamp", msg.timestamp},
{"key1", msg.key1},
{"key2", msg.key2},
{"key3", msg.key3}
};
}
inline void from_json(const json& j, Answer& msg) {
j.at("id").get_to(msg.id);
j.at("chat_id").get_to(msg.chat_id);
j.at("role").get_to(msg.role);
j.at("content").get_to(msg.text);
j.at("timestamp").get_to(msg.timestamp);
j.at("key1").get_to(msg.key1);
j.at("key2").get_to(msg.key2);
j.at("key3").get_to(msg.key3);
}
//텍스트 분석용 구조체&함수 ==================================================
struct TextAnalysisResult {
vector<string> keywords;
string emotion_label;
double impression = 0.0;
double valence = 0.0;
double arousal = 0.0;
double impressiveness = 0.0;
bool water_pump = false;
};
inline void to_json(json& j, const TextAnalysisResult& result) {
j = json{
{"keywords", result.keywords},
{"emotion_label", result.emotion_label},
{"impression", result.impression},
{"valence", result.valence},
{"arousal", result.arousal},
{"impressiveness", result.impressiveness},
{"water_pump", result.water_pump}
};
}
inline void from_json(const json& j, TextAnalysisResult& result) {
j.at("keywords").get_to(result.keywords);
j.at("emotion_label").get_to(result.emotion_label);
j.at("impression").get_to(result.impression);
j.at("valence").get_to(result.valence);
j.at("arousal").get_to(result.arousal);
j.at("impressiveness").get_to(result.impressiveness);
j.at("water_pump").get_to(result.water_pump);
}
//데이터베이스 연결 및 초기화 함수 ==================================================
// 데이터베이스 초기화 및 테이블 생성
sqlite3* db = nullptr;
bool InitializeDB() {
int rc = sqlite3_open("memory.db", &db);
if (rc) {
cerr << "데이터베이스 열기 실패: " << sqlite3_errmsg(db) << endl;
return false;
}
// NonSector 테이블 생성
const char* sql_nonsector =
"CREATE TABLE IF NOT EXISTS nonsector ("
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
"chat_id TEXT,"
"role TEXT,"
"text TEXT,"
"key1 TEXT,"
"key2 TEXT,"
"key3 TEXT,"
"approval REAL,"
"timestamp INTEGER"
");";
// Sector 테이블 생성
const char* sql_sector =
"CREATE TABLE IF NOT EXISTS sector ("
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
"chat_id TEXT,"
"role TEXT,"
"text TEXT,"
"key1 TEXT,"
"key2 TEXT,"
"key3 TEXT,"
"approval REAL,"
"timestamp INTEGER"
");";
// Space 테이블 생성
const char* sql_space =
"CREATE TABLE IF NOT EXISTS space ("
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
"summary TEXT,"
"timestamp INTEGER"
");";
// Endless Memory 테이블 생성
const char* sql_endless =
"CREATE TABLE IF NOT EXISTS endless ("
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
"chat_id TEXT,"
"role TEXT,"
"text TEXT,"
"key1 TEXT,"
"key2 TEXT,"
"key3 TEXT,"
"approval REAL,"
"timestamp INTEGER"
");";
char* errMsg = nullptr;
if (sqlite3_exec(db, sql_nonsector, nullptr, nullptr, &errMsg) != SQLITE_OK ||
sqlite3_exec(db, sql_sector, nullptr, nullptr, &errMsg) != SQLITE_OK ||
sqlite3_exec(db, sql_space, nullptr, nullptr, &errMsg) != SQLITE_OK ||
sqlite3_exec(db, sql_endless, nullptr, nullptr, &errMsg) != SQLITE_OK) {
cerr << "테이블 생성 오류: " << errMsg << endl;
sqlite3_free(errMsg);
return false;
}
return true;
}
DLL_EXPORT
bool initialize_db() {
return InitializeDB();
}
// Answer 구조체를 DB에 저장하는 함수
bool SaveAnswerToDB(const Answer& answer, const std::string& table_name) {
string sql = "INSERT INTO " + table_name +
" (chat_id, role, text, key1, key2, key3, approval, timestamp) "
"VALUES (?, ?, ?, ?, ?, ?, ?, ?);";
sqlite3_stmt* stmt;
if (sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, nullptr) != SQLITE_OK) {
cerr << "SQL 준비 오류: " << sqlite3_errmsg(db) << endl;
return false;
}
sqlite3_bind_text(stmt, 1, answer.chat_id.c_str(), -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 2, answer.role.c_str(), -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 3, answer.text.c_str(), -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 4, answer.key1.c_str(), -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 5, answer.key2.c_str(), -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 6, answer.key3.c_str(), -1, SQLITE_STATIC);
sqlite3_bind_double(stmt, 7, answer.approval);
sqlite3_bind_int64(stmt, 8, answer.timestamp);
bool success = (sqlite3_step(stmt) == SQLITE_DONE);
sqlite3_finalize(stmt);
return success;
}
// Space 요약을 DB에 저장하는 함수
bool SaveSpaceToDB(const string& summary) {
string sql = "INSERT INTO space (summary, timestamp) VALUES (?, ?);";
sqlite3_stmt* stmt;
if (sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, nullptr) != SQLITE_OK) {
cerr << "SQL 준비 오류: " << sqlite3_errmsg(db) << endl;
return false;
}
sqlite3_bind_text(stmt, 1, summary.c_str(), -1, SQLITE_STATIC);
sqlite3_bind_int64(stmt, 2, time(nullptr));
bool success = (sqlite3_step(stmt) == SQLITE_DONE);
sqlite3_finalize(stmt);
return success;
}
DLL_EXPORT
const char* test() {
return "Hello asshole!";
}
//ChatGPT API 호출 함수 =========================================================
// GPT 호출 메인함수
json gpt(const string& prompt, const string& api_key) {
CURL* curl = curl_easy_init();
string response;
json result;
if (curl) {
struct curl_slist* headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json; charset=UTF-8");
headers = curl_slist_append(headers, ("Authorization: Bearer " + api_key).c_str());
//모델 설정
string model = "gpt-4.1";
// API 요청 본문 구성
json request_body = {
{"model", model},
{"messages", json::array({
{
{"role", "user"},
{"content", prompt}
}
})},
{"temperature", 0.7}
};
string request_body_str = request_body.dump(-1, ' ', false, json::error_handler_t::replace);
curl_easy_setopt(curl, CURLOPT_CAINFO, "cacert.pem"); //인증서 추가가
curl_easy_setopt(curl, CURLOPT_URL, "https://api.openai.com/v1/chat/completions");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request_body_str.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
curl_easy_setopt(curl, CURLOPT_ENCODING, "UTF-8");
CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK) {
cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << endl;
result["error"] = curl_easy_strerror(res);
}
else {
try {
result = json::parse(response); // 기본적으로 std::string을 처리합니다.
} catch (const json::parse_error& e) {
cerr << "JSON 파싱 오류: " << e.what() << endl;
// 오류 처리 로직 추가
}
}
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
else {
result["error"] = "CURL 초기화 실패";
}
return result;
}
// node.js에서 사용할 GPT 호출 메인함수 ################################
DLL_EXPORT
const char* gpt_json_string(const char* prompt, const char* api_key) {
static std::string result_str;
json result = gpt(std::string(prompt), std::string(api_key));
result_str = result.dump(); // JSON을 문자열로 변환
return result_str.c_str(); // 문자열 포인터 반환 (static buffer)
}
// ##################################################################
//텍스트 감정 분석함수부분=======================================================
// 분석용 ChatGPT API 호출 함수
json CallChatGPTForAnalysis(const string& prompt, const string& api_key) {
json response = gpt(prompt, api_key);
if (response.contains("error")) {
throw runtime_error(response["error"].get<string>());
}
try {
string content = response["choices"][0]["message"]["content"];
return json::parse(content);
}
catch (const json::exception& e) {
throw runtime_error("API 응답 파싱 오류: " + string(e.what()));
}
}
// 텍스트 분석 통합 함수 (원클릭)
TextAnalysisResult TextAnalysis(const string& text, string api_key) {
TextAnalysisResult result;
Answer answer;
try {
if (api_key.empty()) {
throw runtime_error("API 키 오류");
}
// UTF-8 검증
if (!text.empty()) {
// 입력 문자열을 UTF-8로 변환
string utf8_text = text;
// 통합된 분석 프롬프트
string unified_prompt =
"다음 텍스트를 분석하여 정확히 아래 JSON 형식으로 반환해주세요.\n"
"{\n"
" \"keywords\": [\"키워드1\", \"키워드2\", \"키워드3\"],\n"
" \"emotion_label\": \"감정라벨\",\n"
" \"valence\": 0.0,\n"
" \"arousal\": 0.0,\n"
" \"impressiveness\": 0.0,\n"
" \"water_pump\": false\n"
"}\n"
"조건:\n"
"- keywords: 정확히 3개의 핵심 키워드 추출\n"
"- emotion_label: 감정을 나타내는 한 단어\n"
"- valence: 0 ~ 2 사이의 실수\n"
"- arousal: 0 ~ 1 사이의 실수\n"
"- impressiveness: 0 ~ 1 사이의 실수\n"
"- water_pump: 물을 마시교 있는 경우, 혹은 펌프를 킬 경우 true, 펌프를 끄거나 물을 그만 마실 경우 false\n"
"분석할 텍스트: " + utf8_text;
// 나머지 코드는 동일
json analysis_response = CallChatGPTForAnalysis(unified_prompt, api_key);
result.keywords = analysis_response["keywords"].get<vector<string>>();
result.emotion_label = analysis_response["emotion_label"].get<string>();
result.valence = analysis_response["valence"].get<double>();
result.arousal = analysis_response["arousal"].get<double>();
result.impressiveness = analysis_response["impressiveness"].get<double>();
result.water_pump = analysis_response["water_pump"].get<bool>();
// 기존 인상 점수 계산 로직 유지
double norm_valence = (result.valence + 1.0) / 2.0;
result.impression = (norm_valence * 0.1 + result.arousal * 0.4 +
result.impressiveness * 0.5) * 10.0;
// Answer 구조체 통합
if (!result.keywords.empty()) {
answer.key1 = result.keywords[0];
if (result.keywords.size() > 1) answer.key2 = result.keywords[1];
if (result.keywords.size() > 2) answer.key3 = result.keywords[2];
}
answer.approval = static_cast<float>(result.impression) / 10.0f;
}
}
catch (const exception& e) {
cerr << "텍스트 분석 오류: " << e.what() << endl;
// 오류 시 기본값 설정
result.keywords = vector<string>();
result.emotion_label = "";
result.impression = 0.0;
result.valence = 0.0;
result.arousal = 0.0;
result.impressiveness = 0.0;
result.water_pump = 0;
}
return result;
}
// node.js에서 사용할 감정분석 함수 ################################
DLL_EXPORT
const char* analyze_text(const char* input, const char* api_key) {
static std::string result_json;
try {
// 분석 실행
TextAnalysisResult result = TextAnalysis(std::string(input), std::string(api_key));
// 구조체를 JSON으로 변환
json j = result;
result_json = j.dump();
return result_json.c_str(); // JSON string 반환
} catch (const std::exception& e) {
result_json = R"({"error":")" + std::string(e.what()) + R"("})";
return result_json.c_str();
}
}
// ################################################################
//기억 생성부========================================================================
float avg_aproval = 0.0; //4
float avg_aproval_20 = 0.0; //20
struct Memory {
deque<Answer> nonSector; // 최근 40개 저장
deque<Answer> sector; // 중요 응답 5개 저장
deque<Answer> endless; // 영구 기억 저장 추가
deque<string> space; // 요약된 내용 3개 저장
static const size_t NON_SECTOR_MAX = 40;
static const size_t SECTOR_MAX = 7;
static const size_t SPACE_MAX = 3;
static const size_t ENDLESS_MAX = 10; // 영구 기억 최대 개수
};
// 캐시 시스템 구현
class ChatCache {
private:
// 싱글톤 인스턴스
static ChatCache* instance;
// 캐시 초기화 상태
bool isInitialized = false;
ChatCache() {} // private 생성자
public:
Memory memory; // 기존 메모리 구조체
static ChatCache* getInstance() {
if (instance == nullptr) {
instance = new ChatCache();
}
return instance;
}
// DB에서 데이터 로드하여 캐시 초기화
bool InitializeCache() {
if (isInitialized) return true;
try {
// NonSector 로드 (최근 40개)
string sql_nonsector = "SELECT * FROM nonsector ORDER BY timestamp DESC LIMIT 40";
LoadAnswersFromDB(sql_nonsector, memory.nonSector);
// Sector 로드 (최근 7개)
string sql_sector = "SELECT * FROM sector ORDER BY timestamp DESC LIMIT 7";
LoadAnswersFromDB(sql_sector, memory.sector);
// Space 로드 (최근 3개)
string sql_space = "SELECT summary FROM space ORDER BY timestamp DESC LIMIT 3";
LoadSpaceFromDB(sql_space, memory.space);
// Endless 메모리 로드 추가
string sql_endless = "SELECT * FROM endless ORDER BY timestamp DESC LIMIT 10";
LoadAnswersFromDB(sql_endless, memory.endless);
// 평균 계산 초기화
if (!memory.nonSector.empty()) {
for (const auto& answer : memory.nonSector) {
CalculateApprovals(answer);
}
}
isInitialized = true;
return true;
}
catch (const exception& e) {
cerr << "캐시 초기화 오류: " << e.what() << endl;
return false;
}
}
// DB에서 Answer 데이터 로드 - 선언과 정의를 합침
void LoadAnswersFromDB(const string& sql, deque<Answer>& target) {
sqlite3_stmt* stmt = nullptr;
if (sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, nullptr) != SQLITE_OK) {
throw runtime_error(string("SQL 준비 오류: ") + sqlite3_errmsg(db));
}
while (sqlite3_step(stmt) == SQLITE_ROW) {
Answer answer;
answer.id = sqlite3_column_int(stmt, 0);
answer.chat_id = string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1)));
answer.role = string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 2)));
answer.text = string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 3)));
answer.key1 = string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 4)));
answer.key2 = string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 5)));
answer.key3 = string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 6)));
answer.approval = static_cast<float>(sqlite3_column_double(stmt, 7));
answer.timestamp = sqlite3_column_int64(stmt, 8);
target.push_back(answer);
}
sqlite3_finalize(stmt);
}
// DB에서 Space 데이터 로드
void LoadSpaceFromDB(const string& sql, deque<string>& target) {
sqlite3_stmt* stmt;
if (sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, nullptr) != SQLITE_OK) {
throw runtime_error("SQL 준비 오류");
}
while (sqlite3_step(stmt) == SQLITE_ROW) {
string summary = string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)));
target.push_back(summary);
}
sqlite3_finalize(stmt);
}
// 캐시 정리
void ClearCache() {
memory.nonSector.clear();
memory.sector.clear();
memory.space.clear();
memory.endless.clear(); // endless 메모리도 클리어
isInitialized = false;
}
// 평균 계산 함수를 클래스 내부로 이동
void CalculateApprovals(const Answer& newAnswer) {
static deque<float> recent4; // 최근 4개 저장
static deque<float> recent20; // 최근 20개 저장
// 최근 4개 관리
recent4.push_back(newAnswer.approval);
if (recent4.size() > 4) {
recent4.pop_front();
}
// 최근 20개 관리
recent20.push_back(newAnswer.approval);
if (recent20.size() > 20) {
recent20.pop_front();
}
// 평균 계산
avg_aproval = accumulate(recent4.begin(), recent4.end(), 0.0f) / recent4.size();
avg_aproval_20 = accumulate(recent20.begin(), recent20.end(), 0.0f) / recent20.size();
}
};
// 싱글톤 인스턴스 초기화
ChatCache* ChatCache::instance = nullptr;
Memory memory; //전역변수로 가져오기
void CalculateApprovals(const Answer& newAnswer) {
ChatCache* cache = ChatCache::getInstance();
cache->CalculateApprovals(newAnswer);
}
// 전방 선언
void Space(const string& combinedText, string api_key);
// 최근40개
void NonSector(const Answer& newAnswer) {
ChatCache* cache = ChatCache::getInstance();
// 메모리에 추가
cache->memory.nonSector.push_back(newAnswer);
if (cache->memory.nonSector.size() > Memory::NON_SECTOR_MAX) {
cache->memory.nonSector.pop_front();
}
// 평균 계산 업데이트
CalculateApprovals(newAnswer);
// 실시간 DB 저장 유지
SaveAnswerToDB(newAnswer, "nonsector");
}
DLL_EXPORT
bool add_nonsector_from_json(const char* json_str) {
static std::string err_msg;
try {
json j = json::parse(json_str);
Answer ans = j.get<Answer>(); // 이미 from_json 정의돼 있어야 함
NonSector(ans);
return true;
} catch (const std::exception& e) {
std::cerr << "[ERROR] JSON 파싱 또는 NonSector 실패: " << e.what() << std::endl;
return false;
}
}
void Sector(const Answer& answer, string api_key) {
ChatCache* cache = ChatCache::getInstance();
// approval이 평균보다 3 이상 높은 경우에만 저장
if (answer.approval >= (avg_aproval + 3.0f)) {
cache->memory.sector.push_back(answer);
SaveAnswerToDB(answer, "sector"); // DB 저장 추가
// 섹터가 가득 찼을 때 (7개)
if (cache->memory.sector.size() >= Memory::SECTOR_MAX) {
// 가장 오래된 응답들을 하나의 문자열로 결합
string combinedText;
for (int i = 0; i < 3; ++i) { // 처음 3개 응답 결합
combinedText += cache->memory.sector.front().text + "\n";
cache->memory.sector.pop_front();
}
// Space로 요약하여 저장
Space(combinedText, api_key);
}
}
}
void Space(const string& combinedText, string api_key) {
ChatCache* cache = ChatCache::getInstance();
try {
// GPT를 사용하여 텍스트 요약
string summary_prompt =
"다음 대화들을 한 문장으로 간단히 요약해주세요:\n" + combinedText;
json response = gpt(summary_prompt, api_key);
string summary = response["choices"][0]["message"]["content"];
// 요약된 내용 저장
cache->memory.space.push_back(summary);
SaveSpaceToDB(summary); // DB 저장 추가
// 최대 3개까지만 유지
if (cache->memory.space.size() > Memory::SPACE_MAX) {
cache->memory.space.pop_front();
}
}
catch (const exception& e) {
cerr << "요약 생성 오류: " << e.what() << endl;
}
}
void EndlessMemory(const Answer& answer) {
// approval이 9 이상이거나 최근 20개 평균보다 3 이상 클 때
if (answer.approval >= 9.0f || (answer.approval >= (avg_aproval_20 + 3.0f))) {
SaveAnswerToDB(answer, "endless"); // DB 저장 추가
}
}
// 대화 시작 시 호출할 함수
bool StartChat() {
ChatCache* cache = ChatCache::getInstance();
return cache->InitializeCache();
}
DLL_EXPORT
bool start_chat() {
return StartChat();
}
// 대화 종료 시 호출할 함수
void EndChat() {
ChatCache* cache = ChatCache::getInstance();
cache->ClearCache();
}
DLL_EXPORT
void end_chat() {
EndChat(); // 메모리 캐시 정리
}
//프롬포트 빌딩계열 함수 =================================================================
string PromptBuilder(string api, string plant_name) {
ChatCache* cache = ChatCache::getInstance();
string base_prompt = "";
// // 1. 기본 페르소나 프롬프트
// base_prompt = "너의 이름은 " + plant_name + ". 당신은 식물이며 친구이자 당신을 기르는 사람인 " + user_name + "과 대화하고 있다. 50자 이하로 답장하라";
// // 2. 기억 통합 - 디버깅 정보 추가
// cout << "[DEBUG] NonSector 개수: " << cache->memory.nonSector.size() << endl;
// cout << "[DEBUG] Sector 개수: " << cache->memory.sector.size() << endl;
// cout << "[DEBUG] Space 개수: " << cache->memory.space.size() << endl;
// // NonSector (최근 기억) - 개선된 순회
// if (!cache->memory.nonSector.empty()) {
// base_prompt += "\n\n[직전대화]";
// int count = 0;
// // 최신 것부터 보여주기 위해 역순으로 순회
// for (auto it = cache->memory.nonSector.rbegin();
// it != cache->memory.nonSector.rend() && count < 5;
// ++it, ++count) {
// base_prompt += "\n- [" + it->role + "] " + it->text;
// // 키워드 정보도 추가
// if (!it->key1.empty()) {
// base_prompt += " (키워드: " + it->key1;
// if (!it->key2.empty()) base_prompt += ", " + it->key2;
// if (!it->key3.empty()) base_prompt += ", " + it->key3;
// base_prompt += ")";
// }
// }
// }
// // Sector (중요 기억) - 개선
// if (!cache->memory.sector.empty()) {
// base_prompt += "\n\n[단기 중점기억]";
// for (const auto& ans : cache->memory.sector) {
// base_prompt += "\n- [중요] " + ans.text;
// base_prompt += " (인상점수: " + to_string(ans.approval) + ")";
// }
// }
// // Space (요약된 기억) - 개선
// if (!cache->memory.space.empty()) {
// base_prompt += "\n\n[장기 열화기억]";
// for (const auto& summary : cache->memory.space) {
// base_prompt += "\n- [요약] " + summary;
// }
// }
// // Endless Memory 섹션 추가
// if (!cache->memory.endless.empty()) {
// base_prompt += "\n\n[장기기억]";
// for (const auto& ans : cache->memory.endless) {
// base_prompt += "\n- [영구] " + ans.text;
// // 키워드 정보 추가
// if (!ans.key1.empty()) {
// base_prompt += " (키워드: " + ans.key1;
// if (!ans.key2.empty()) base_prompt += ", " + ans.key2;
// if (!ans.key3.empty()) base_prompt += ", " + ans.key3;
// base_prompt += ")";
// }
// // 감정 강도 표시
// base_prompt += " [감정 강도: " + to_string(ans.approval) + "]";
// }
// }
// // 디버깅 정보 추가
// cout << "[DEBUG] Endless Memory 개수: " << cache->memory.endless.size() << endl;
// 3. 현재 상태 프롬프트 추가 - 에러 처리 개선
base_prompt += "\n\n현재 상태:";
try {
// 센서값 해석 및 상태 추가
auto sensorData = get_binary(api);
// 센서 데이터 유효성 검사
cout << "[DEBUG] 센서1 값: " << (int)sensorData.sensor1 << endl;
cout << "[DEBUG] 센서2 값: " << (int)sensorData.sensor2 << endl;
cout << "[DEBUG] 센서3 값: " << (int)sensorData.sensor3 << endl;
cout << "[DEBUG] 센서4 값: " << (int)sensorData.sensor4 << endl;
cout << "[DEBUG] 급수 펌프 onoff 값: " << (bool)sensorData.onoff << endl;
// 온도 (sensor1: 0-50℃를 0-255로 매핑)
float temp = (sensorData.sensor1);
base_prompt += "\n- 현재 온도: " + to_string(temp) + "℃";
if (temp <= 15.0f) {
base_prompt += " → 차가운 공기에 움츠러들고 있어요";
}
else if (temp > 28.0f) {
base_prompt += " → 더위 때문에 힘이 빠져요";
}
else {
base_prompt += " → 적당한 온도로 기분이 좋아요";
}
// 습도 (sensor2: 0-100%를 0-255로 매핑)
float humidity = (sensorData.sensor2);
base_prompt += "\n- 현재 습도: " + to_string(humidity) + "%";
if (humidity < 40.0f) {
base_prompt += " → 공기가 너무 건조해서 목이 마르고 잎이 바스락거려요";
}
else if (humidity > 70.0f) {
base_prompt += " → 습도가 너무 높아서 숨쉬기가 답답해요";
}
else {
base_prompt += " → 습도가 적당해서 상쾌해요";
}
// 토양 습도 (sensor3: 0-100%를 0-255로 매핑)
float soilMoisture = (sensorData.sensor3 / 255.0f) * 100.0f;
base_prompt += "\n- 토양 습도: " + to_string(soilMoisture) + "%";
if (soilMoisture < 89.0f) {
base_prompt += " → 뿌리가 마르고 있어요. 물을 주세요!";
}
else if (soilMoisture > 128.0f) {
base_prompt += " → 물이 너무 많아서 뿌리가 숨을 못 쉬어요!";
}
else {
base_prompt += " → 토양 상태가 좋아요. 뿌리가 편해요.";
}
// 조도 (sensor4: 0-1000 lux를 0-255로 매핑)
float lightLux = (sensorData.sensor4);
base_prompt += "\n- 조도: " + to_string(lightLux) + " lux";
if (lightLux < 80.0f) {
base_prompt += " → 너무 어두워요. 햇빛을 보고싶어요";
}
else if (lightLux < 190.0f) {
base_prompt += " → 서늘한 곳에 있어요. 너무 밝지도 어둡지도 않아요";
}
else {
base_prompt += " → 빛이 딱 좋아요! 광합성하기 좋아요";
}
// 급수 펌프 (onoff)
base_prompt += "\n- 급수 펌프: " + to_string(sensorData.onoff);
if(sensorData.onoff) {
if(soilMoisture > 128.0f) {
base_prompt += "\n- 급수 펌프: 꺼짐 → 지금은 물이 많아요. 지금은 괜찮으니 나중에 주세요!";
} else {
base_prompt += "\n- 급수 펌프: 켜짐 → 시원하게 물이 들어오고 있어요. 목이 축여져서 기분이 좋아요.";
}
} else {
base_prompt += "\n- 급수 펌프: 꺼짐 → 물이 멈췄어요. 지금은 괜찮지만 나중에 또 줘요!";
}
// 감정 상태 추가 (평균 승인도 기반)
// base_prompt += "\n- 최근 기분: ";
// if (avg_aproval >= 7.0f) {
// base_prompt += "매우 행복해요!";
// }
// else if (avg_aproval >= 5.0f) {
// base_prompt += "기분이 좋아요";
// }
// else if (avg_aproval >= 3.0f) {
// base_prompt += "평범해요";
// }
// else {
// base_prompt += "조금 우울해요";
// }
}
catch (const exception& e) {
cout << "[ERROR] 센서 데이터 읽기 실패: " << e.what() << endl;
base_prompt += "\n- 센서 상태를 확인할 수 없어서 조금 불안해요";
}
// 4. 디버깅용 프롬프트 출력
cout << "\n[DEBUG] 생성된 프롬프트 길이: " << base_prompt.length() << endl;
cout << "[DEBUG] 평균 승인도(4개): " << avg_aproval << endl;
cout << "[DEBUG] 평균 승인도(20개): " << avg_aproval_20 << endl;
return base_prompt;
}
DLL_EXPORT
const char* prompt_builder(const char* api, const char* plant_name) {
static std::string result_str;
try {
result_str = PromptBuilder(std::string(api), std::string(plant_name));
return result_str.c_str();
} catch (const std::exception& e) {
result_str = "[ERROR] " + std::string(e.what());
return result_str.c_str();
}
}
//get_binary_json##########################################
DLL_EXPORT
const char* get_binary_json(const char* api_key) {
static std::string result_str;
try {
ParsedPacket pkt = get_binary(std::string(api_key));
// 구조체를 JSON으로 변환 (명시적으로 정수 캐스팅)
json j = {
{"sensor1", static_cast<int>(pkt.sensor1)},
{"sensor2", static_cast<int>(pkt.sensor2)},
{"sensor3", static_cast<int>(pkt.sensor3)},
{"sensor4", static_cast<int>(pkt.sensor4)},
{"onoff", static_cast<int>(pkt.onoff)}, // 핵심 수정
{"led", static_cast<int>(pkt.led)} // 같이 수정 추천
};
result_str = j.dump();
return result_str.c_str();
} catch (const std::exception& e) {
result_str = R"({"error":")" + std::string(e.what()) + R"("})";
return result_str.c_str();
}
}
DLL_EXPORT
void post_binary_c(const char* api_key, uint8_t s1, uint8_t s2, uint8_t s3, uint8_t s4) {
if (!api_key || strlen(api_key) == 0) {
cerr << "[ERROR] API 키가 비어 있습니다." << endl;
return;
}
try {
post_binary(string(api_key), s1, s2, s3, s4);
} catch (const exception& e) {
cerr << "[ERROR] post_binary_c 예외: " << e.what() << endl;
}
}
extern "C" DLL_EXPORT
void onoff_bin_c(const char* api_key, uint8_t onoff, int led) {
if (!api_key || strlen(api_key) == 0) {
cerr << "[ERROR] API 키가 비어 있습니다." << endl;
return;
}
try {
// onoff가 0이 아니면 true, 0이면 false
onoff_bin(std::string(api_key), onoff != 0, led);
} catch (const std::exception& e) {
cerr << "[ERROR] onoff_bin_c 예외: " << e.what() << endl;
}
}
int main(void) {
// 1. 콘솔 입출력 UTF-8 설정
#ifdef _WIN32
// Windows에서 UTF-8 입출력을 위한 설정
SetConsoleOutputCP(CP_UTF8);
SetConsoleCP(CP_UTF8);
#endif
// 로케일 설정
setlocale(LC_ALL, ".UTF-8");
// 이전 코드와 동일
string api = "";
string api_key = "";
string plant_name = "";
if (!InitializeDB()) {
cerr << "데이터베이스 초기화 실패" << endl;
return 1;
}
// 2. 대화 세션 시작 및 캐시 초기화
if (!StartChat()) {
cerr << "채팅 세션 초기화 실패" << endl;
return 1;
}
cout << "세션 통과" << endl;
try {
// 3. 채팅 세션 ID 생성
string chat_id = to_string(time(nullptr));
while (true) {
// 4. 프롬프트 생성 및 사용자 입력 처리
string userInput;
cout << "\n사용자 입력: ";
getline(cin, userInput);
if (userInput == "quit" || userInput == "exit") {
break;
}
// 5. 사용자 입력 분석 및 저장
Answer userAnswer;
userAnswer.chat_id = chat_id;
userAnswer.role = "user";
userAnswer.text = userInput;
userAnswer.timestamp = time(nullptr);
// 텍스트 분석 수행
TextAnalysisResult analysis = TextAnalysis(userInput, api_key);
// 분석 결과를 Answer에 통합
if (!analysis.keywords.empty()) {
userAnswer.key1 = analysis.keywords[0];
if (analysis.keywords.size() > 1) userAnswer.key2 = analysis.keywords[1];
if (analysis.keywords.size() > 2) userAnswer.key3 = analysis.keywords[2];
}
userAnswer.approval = static_cast<float>(analysis.impression) / 10.0f;
// 6. 메모리 시스템에 저장
NonSector(userAnswer);
if (userAnswer.approval >= avg_aproval + 3.0f) {