Skip to content

Commit 130ffed

Browse files
committed
๐Ÿ› [fix] ์ง€์‹๊ทธ๋ž˜ํ”„ ์ƒ์„ฑ ์ˆœํ™˜์ฐธ์กฐ ์˜ค๋ฅ˜ ํ•ด๊ฒฐ
์ด์ œ ๊ธด ํ…์ŠคํŠธ๋„ ์‚ฝ์ž… ๊ฐ€๋Šฅ
1 parent fa0e117 commit 130ffed

9 files changed

Lines changed: 70 additions & 70 deletions

File tree

โ€Žsrc/main/java/com/going/server/domain/chatbot/service/ChatbotServiceImpl.javaโ€Ž

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ public class ChatbotServiceImpl implements ChatbotService {
4242
// ์›๋ฌธ ๋ฐ˜ํ™˜
4343
@Override
4444
public CreateChatbotResponseDto getOriginalText(String graphId) {
45-
Graph graph = graphRepository.getByGraph(Long.valueOf(graphId));
45+
Long dbId = graphRepository.findDbIdByGraphId(Long.valueOf(graphId));
46+
Graph graph = graphRepository.getByGraph(dbId);
4647

4748
return CreateChatbotResponseDto.builder()
4849
.chatContent(graph.getContent()) // ์›๋ฌธ ํ…์ŠคํŠธ
@@ -54,7 +55,8 @@ public CreateChatbotResponseDto getOriginalText(String graphId) {
5455
// ์š”์•ฝ๋ณธ ์ƒ์„ฑ
5556
@Override
5657
public CreateChatbotResponseDto getSummaryText(String graphId) {
57-
Graph graph = graphRepository.getByGraph(Long.valueOf(graphId));
58+
Long dbId = graphRepository.findDbIdByGraphId(Long.valueOf(graphId));
59+
Graph graph = graphRepository.getByGraph(dbId);
5860

5961
String context = Optional.ofNullable(graph.getContent())
6062
.filter(s -> !s.trim().isEmpty())
@@ -72,12 +74,11 @@ public CreateChatbotResponseDto getSummaryText(String graphId) {
7274
// GraphRAG ์ฑ—๋ด‡ ์‘๋‹ต ์ƒ์„ฑ
7375
@Override
7476
public CreateChatbotResponseDto createAnswerWithRAG(String graphStrId, CreateChatbotRequestDto requestDto) {
75-
Long graphId = Long.valueOf(graphStrId);
76-
// 404 : ์ง€์‹๊ทธ๋ž˜ํ”„ ์ฐพ์„ ์ˆ˜ ์—†์Œ
77-
Graph graph = graphRepository.getByGraph(graphId);
77+
Long dbId = graphRepository.findDbIdByGraphId(Long.valueOf(graphStrId));
78+
Graph graph = graphRepository.getByGraph(dbId);
7879

7980
if (requestDto.isNewChat()) {
80-
deletePreviousChat(graphId);
81+
deletePreviousChat(dbId);
8182
}
8283

8384
Chatting userChat = Chatting.builder()
@@ -88,11 +89,11 @@ public CreateChatbotResponseDto createAnswerWithRAG(String graphStrId, CreateCha
8889
.build();
8990
chattingRepository.save(userChat);
9091

91-
List<Chatting> chatHistory = chattingRepository.findAllByGraphId(graphId);
92+
List<Chatting> chatHistory = chattingRepository.findAllByGraphId(dbId);
9293

9394
// RAG ์‘๋‹ต ์ƒ์„ฑ (์‘๋‹ต + ๋ฉ”ํƒ€ ํฌํ•จ)
9495
CreateChatbotResponseDto responseDto = graphRAGService.createAnswerWithGraphRAG(
95-
graphId,
96+
dbId,
9697
requestDto.getChatContent(),
9798
chatHistory
9899
);
@@ -107,18 +108,16 @@ public CreateChatbotResponseDto createAnswerWithRAG(String graphStrId, CreateCha
107108
// ๊ธฐ๋ณธ ์‘๋‹ต ์ƒ์„ฑ
108109
@Override
109110
public CreateChatbotResponseDto createSimpleAnswer(String graphStrId, CreateChatbotRequestDto createChatbotRequestDto) {
110-
Long graphId = Long.valueOf(graphStrId);
111-
112-
// 404 : ์ง€์‹๊ทธ๋ž˜ํ”„ ์ฐพ์„ ์ˆ˜ ์—†์Œ
113-
Graph graph = graphRepository.getByGraph(graphId);
111+
Long dbId = graphRepository.findDbIdByGraphId(Long.valueOf(graphStrId));
112+
Graph graph = graphRepository.getByGraph(dbId);
114113

115114
// ์ƒˆ๋กœ์šด ๋Œ€ํ™”์ธ ๊ฒฝ์šฐ ๊ธฐ์กด ์ฑ„ํŒ… ์‚ญ์ œ
116115
if (createChatbotRequestDto.isNewChat()) {
117-
deletePreviousChat(graphId);
116+
deletePreviousChat(dbId);
118117
}
119118

120119
// ๊ธฐ์กด ์ฑ„ํŒ… ๋‚ด์—ญ ์กฐํšŒ
121-
List<Chatting> chatHistory = chattingRepository.findAllByGraphId(graphId);
120+
List<Chatting> chatHistory = chattingRepository.findAllByGraphId(dbId);
122121

123122
// ์‚ฌ์šฉ์ž ์ž…๋ ฅ ์ฑ„ํŒ…
124123
String newChat = createChatbotRequestDto.getChatContent();

โ€Žsrc/main/java/com/going/server/domain/graph/entity/Graph.javaโ€Ž

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import lombok.Setter;
77
import org.springframework.data.neo4j.core.schema.*;
88

9+
import java.util.ArrayList;
910
import java.util.List;
1011

1112
@Node("Graph")
@@ -29,11 +30,8 @@ public class Graph extends BaseEntity {
2930
private boolean connectPerfect; //connect ํ€ด์ฆˆ ๋งŒ์ ‘ ์—ฌ๋ถ€
3031
private boolean picturePerfect; //picture ํ€ด์ฆˆ ๋งŒ์ ‘ ์—ฌ๋ถ€
3132

33+
@Builder.Default
3234
@Relationship(type = "HAS_NODE", direction = Relationship.Direction.OUTGOING)
33-
private List<GraphNode> nodes;
35+
private List<GraphNode> nodes = new ArrayList<>();
3436

35-
// Long โ†’ String ๋ณ€ํ™˜ (ํ”„๋ก ํŠธ ์ „์†ก ์‹œ)
36-
public String getIdAsString() {
37-
return id != null ? String.valueOf(id) : null;
38-
}
3937
}
Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package com.going.server.domain.graph.entity;
22

3-
import com.fasterxml.jackson.annotation.JsonIgnore;
43
import lombok.Builder;
54
import lombok.EqualsAndHashCode;
65
import lombok.Getter;
76
import lombok.Setter;
8-
import org.springframework.data.neo4j.core.schema.*;
7+
import org.springframework.data.neo4j.core.schema.GeneratedValue;
8+
import org.springframework.data.neo4j.core.schema.Id;
9+
import org.springframework.data.neo4j.core.schema.RelationshipProperties;
10+
import org.springframework.data.neo4j.core.schema.TargetNode;
911

10-
import java.util.ArrayList;
1112
import java.util.Objects;
1213

1314
@RelationshipProperties
@@ -18,34 +19,30 @@ public class GraphEdge {
1819

1920
@Id
2021
@GeneratedValue
21-
private Long id; // Neo4j ๋‚ด๋ถ€ ID
22+
private Long id;
2223

2324
@EqualsAndHashCode.Include
2425
private String source;
2526

2627
@EqualsAndHashCode.Include
27-
private String label; // ๊ด€๊ณ„ ๋ผ๋ฒจ
28+
private String label;
2829

2930
@EqualsAndHashCode.Include
3031
@TargetNode
31-
@Relationship(type = "RELATED", direction = Relationship.Direction.INCOMING)
32-
@JsonIgnore
33-
private GraphNode target; // ์—ฐ๊ฒฐ ๋Œ€์ƒ ๋…ธ๋“œ
32+
private GraphNode target; // ์—ฌ๊ธฐ์— ๋ฐฉํ–ฅ ๋ถ™์ด์ง€ ๋งˆ์„ธ์š”
3433

3534
@Override
3635
public boolean equals(Object o) {
3736
if (this == o) return true;
38-
if (o == null || getClass() != o.getClass()) return false;
39-
GraphEdge edge = (GraphEdge) o;
40-
return Objects.equals(source, edge.source)
41-
&& Objects.equals(label, edge.label)
42-
&& edge.target != null && target != null
43-
&& Objects.equals(target.getNodeId(), edge.target.getNodeId());
37+
if (!(o instanceof GraphEdge edge)) return false;
38+
return Objects.equals(source, edge.source) &&
39+
Objects.equals(label, edge.label) &&
40+
target != null && edge.target != null &&
41+
Objects.equals(target.getNodeId(), edge.target.getNodeId());
4442
}
4543

4644
@Override
4745
public int hashCode() {
4846
return Objects.hash(source, label, target != null ? target.getNodeId() : null);
4947
}
50-
5148
}

โ€Žsrc/main/java/com/going/server/domain/graph/entity/GraphNode.javaโ€Ž

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.springframework.data.neo4j.core.schema.*;
77

88
import java.util.ArrayList;
9+
import java.util.HashSet;
910
import java.util.List;
1011
import java.util.Set;
1112

@@ -29,9 +30,9 @@ public class GraphNode {
2930
private String includeSentence; //ํ•ด๋‹น ๋…ธ๋“œ(๋‹จ์–ด)๊ฐ€ ํฌํ•จ๋œ ๋ฌธ์žฅ
3031
private String image;
3132

32-
// @Transient // Neo4j๊ฐ€ ๋งคํ•‘ํ•˜์ง€ ์•Š์Œ
33+
@Builder.Default
3334
@ToString.Exclude
3435
@JsonIgnore
3536
@Relationship(type = "RELATED", direction = Relationship.Direction.OUTGOING)
36-
private Set<GraphEdge> edges;
37+
private Set<GraphEdge> edges = new HashSet<>();
3738
}

โ€Žsrc/main/java/com/going/server/domain/graph/repository/GraphRepository.javaโ€Ž

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,23 @@
1313

1414
@Registered
1515
public interface GraphRepository extends Neo4jRepository<Graph, Long> {
16+
default Graph getByGraph(Long graphId) {
17+
return findById(graphId).orElseThrow(GraphNotFoundException::new);
18+
}
1619
// default Graph getByGraph(Long graphId) {
17-
// return findById(graphId).orElseThrow(GraphNotFoundException::new);
20+
// return findGraphWithEdgesByGraphId(graphId).orElseThrow(GraphNotFoundException::new);
1821
// }
1922

20-
default Graph getByGraph(Long graphId) {
21-
return findGraphWithEdgesByGraphId(graphId).orElseThrow(GraphNotFoundException::new);
22-
}
23+
// @Query("""
24+
//MATCH (g:Graph {id: $graphId})-[:HAS_NODE]->(n:GraphNode)
25+
//OPTIONAL MATCH (n)-[r:RELATED]->(m:GraphNode)
26+
//RETURN g, collect(DISTINCT n) as nodes, collect(DISTINCT r) as rels
27+
//""")
28+
// Optional<Graph> findGraphWithEdgesByGraphId(@Param("graphId") Long graphId);
2329

24-
@Query("MATCH (g:Graph) WHERE g.id = $graphId RETURN g")
25-
Optional<Graph> findByGraphId(@Param("graphId") Long graphId);
2630

27-
// ๊ทธ๋ž˜ํ”„ + ๋…ธ๋“œ + ์—ฃ์ง€๊นŒ์ง€ ์ „๋ถ€ fetch
28-
@Query("""
29-
MATCH (g:Graph {id: $graphId})-[:HAS_NODE]->(n:GraphNode)
30-
OPTIONAL MATCH (n)-[r]->(m:GraphNode)
31-
RETURN g, collect(DISTINCT n) as nodes, collect(DISTINCT r) as rels, collect(DISTINCT m) as targets
32-
""")
33-
Optional<Graph> findGraphWithEdgesByGraphId(@Param("graphId") Long graphId);
31+
@Query("MATCH (g:Graph {id: $graphId}) RETURN id(g)")
32+
Long findDbIdByGraphId(@Param("graphId") Long graphId);
3433

3534
@Query("MATCH (g:Graph) RETURN max(g.id)")
3635
Long findMaxGraphId();

โ€Žsrc/main/java/com/going/server/domain/graph/service/GraphServiceImpl.javaโ€Ž

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,21 @@ public GraphListDto getGraphList() {
6363

6464
@Override
6565
public void deleteGraph(Long graphId) {
66-
Graph graph = graphRepository.getByGraph(graphId);
66+
Long dbId = graphRepository.findDbIdByGraphId(Long.valueOf(graphId));
67+
Graph graph = graphRepository.getByGraph(dbId);
68+
6769
//๊ทธ๋ž˜ํ”„์— ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ ์‚ญ์ œ
6870
if (graph.getNodes() != null) {
6971
graph.getNodes().forEach(node -> graphNodeRepository.deleteById(node.getId()));
7072
}
7173
graphRepository.deleteById(graph.getId());
7274
}
7375

76+
@Transactional(readOnly = true)
7477
@Override
7578
public KnowledgeGraphDto getGraph(Long graphId) {
76-
Graph graph = graphRepository.getByGraph(graphId);
79+
Long dbId = graphRepository.findDbIdByGraphId(graphId);
80+
Graph graph = graphRepository.getByGraph(dbId);
7781
log.info("[getGraph] ์กฐํšŒ๋œ Graph ID: {}, Title: {}", graph.getId(), graph.getTitle());
7882

7983
List<NodeDto> nodeDtoList = new ArrayList<>();
@@ -112,7 +116,9 @@ public KnowledgeGraphDto getGraph(Long graphId) {
112116

113117
@Override
114118
public NodeDto getNode(Long graphId, Long nodeId) {
115-
Graph graph = graphRepository.getByGraph(graphId);
119+
Long dbId = graphRepository.findDbIdByGraphId(graphId);
120+
Graph graph = graphRepository.getByGraph(dbId);
121+
116122
//๋…ธ๋“œ ์ฐพ๊ธฐ
117123
GraphNode node = null;
118124
for (GraphNode n : graph.getNodes()) {
@@ -132,7 +138,9 @@ public NodeDto getNode(Long graphId, Long nodeId) {
132138
@Override
133139
@Transactional
134140
public void addNode(Long graphId, NodeAddDto nodeAddDto) {
135-
Graph graph = graphRepository.getByGraph(graphId);
141+
Long dbId = graphRepository.findDbIdByGraphId(graphId);
142+
Graph graph = graphRepository.getByGraph(dbId);
143+
136144
GraphNode parentNode = null;
137145
for (GraphNode node : graph.getNodes()) {
138146
if (node.getNodeId().equals(Long.parseLong(nodeAddDto.getParentId()))) {
@@ -168,7 +176,7 @@ public void addNode(Long graphId, NodeAddDto nodeAddDto) {
168176
parentNode.getEdges().add(newEdge);
169177
graphNodeRepository.save(parentNode);
170178

171-
graph = graphRepository.getByGraph(graphId);
179+
graph = graphRepository.getByGraph(dbId);
172180

173181
if (graph.getNodes() == null) {
174182
graph.setNodes(new ArrayList<>());
@@ -181,7 +189,8 @@ public void addNode(Long graphId, NodeAddDto nodeAddDto) {
181189
@Override
182190
public void deleteNode(Long graphId, Long nodeId) {
183191
//๊ทธ๋ž˜ํ”„ ๊ฒ€์ฆ
184-
Graph graph = graphRepository.getByGraph(graphId);
192+
Long dbId = graphRepository.findDbIdByGraphId(graphId);
193+
Graph graph = graphRepository.getByGraph(dbId);
185194
GraphNode node = null;
186195
for (GraphNode n : graph.getNodes()) {
187196
if (n.getNodeId().equals(nodeId)) {
@@ -199,7 +208,8 @@ public void deleteNode(Long graphId, Long nodeId) {
199208
@Override
200209
public void modifyNode(Long graphId, Long nodeId, NodeModifyDto nodeModifyDto) {
201210
//๊ทธ๋ž˜ํ”„ ๊ฒ€์ฆ
202-
Graph graph = graphRepository.getByGraph(graphId);
211+
Long dbId = graphRepository.findDbIdByGraphId(graphId);
212+
Graph graph = graphRepository.getByGraph(dbId);
203213
//๋…ธ๋“œ ์ฐพ๊ธฐ
204214
GraphNode node = null;
205215
for (GraphNode n : graph.getNodes()) {

โ€Žsrc/main/java/com/going/server/domain/quiz/service/QuizServiceImpl.javaโ€Ž

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ public class QuizServiceImpl implements QuizService{
2323
// ๋ชจ๋“œ ๋ณ„ ํ€ด์ฆˆ ์ƒ์„ฑ
2424
@Override
2525
public QuizCreateResponseDto quizCreate(String graphIdStr, String mode) {
26-
Long graphId = Long.valueOf(graphIdStr);
26+
Long dbId = graphRepository.findDbIdByGraphId(Long.valueOf(graphIdStr));
27+
Graph graph = graphRepository.getByGraph(dbId);
2728

28-
// 404 : ์ง€์‹๊ทธ๋ž˜ํ”„ ์ฐพ์„ ์ˆ˜ ์—†์Œ
29-
Graph graph = graphRepository.getByGraph(graphId);
3029

3130
Object quizDto = switch (mode) {
3231
case "listenUp" -> listenUpQuizGenerator.generate(graph);
@@ -41,10 +40,8 @@ public QuizCreateResponseDto quizCreate(String graphIdStr, String mode) {
4140
// ๋งŒ์ ์ผ ๊ฒฝ์šฐ Graph Quiz ์ •๋ณด ์—…๋ฐ์ดํŠธ
4241
@Override
4342
public void updateIfPerfect(String graphIdStr, String mode) {
44-
Long graphId = Long.valueOf(graphIdStr);
45-
46-
// 404 : ์ง€์‹ ๊ทธ๋ž˜ํ”„ ์ฐพ์„ ์ˆ˜ ์—†์Œ
47-
Graph graph = graphRepository.getByGraph(graphId);
43+
Long dbId = graphRepository.findDbIdByGraphId(Long.valueOf(graphIdStr));
44+
Graph graph = graphRepository.getByGraph(dbId);
4845

4946
switch (mode){
5047
case "listenUp":

โ€Žsrc/main/java/com/going/server/domain/rag/service/GraphRAGService.javaโ€Ž

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ public class GraphRAGService {
3333
* ๋ณธ ๋ฉ”์„œ๋“œ๋Š” LangChain ์—†์ด ๊ตฌํ˜„ํ•œ Spring ๊ธฐ๋ฐ˜ GraphRAG์˜ ํ•ต์‹ฌ ํ๋ฆ„์ž…๋‹ˆ๋‹ค.
3434
*/
3535
public CreateChatbotResponseDto createAnswerWithGraphRAG(
36-
Long graphId,
36+
Long dbId,
3737
String userQuestion,
3838
List<Chatting> chatHistory
3939
) {
40-
Graph graph = graphRepository.getByGraph(graphId);
41-
log.info("[GraphRAG] graphId: {}, question: {}", graphId, userQuestion);
40+
Graph graph = graphRepository.getByGraph(dbId);
41+
log.info("[GraphRAG] dbId: {}, question: {}", dbId, userQuestion);
4242

4343
// 1. ์งˆ๋ฌธ โ†’ Cypher ์ฟผ๋ฆฌ ์ƒ์„ฑ
4444
String cypherQuery = cypherQueryGenerator.generate(userQuestion).trim()
@@ -48,7 +48,7 @@ public CreateChatbotResponseDto createAnswerWithGraphRAG(
4848
log.info("[GraphRAG] Generated Cypher Query:\n{}", cypherQuery);
4949

5050
// 2. ์ฟผ๋ฆฌ ์‹คํ–‰ โ†’ ๋ฌธ๋งฅ(context) ๋ฐ ๋…ธ๋“œ ๋ผ๋ฒจ ์ถ”์ถœ
51-
List<GraphQueryResult> queryResults = graphQueryExecutor.runQuery(graphId, cypherQuery);
51+
List<GraphQueryResult> queryResults = graphQueryExecutor.runQuery(dbId, cypherQuery);
5252
List<String> contextChunks = queryResults.stream()
5353
.map(GraphQueryResult::getSentence)
5454
.toList();
@@ -76,7 +76,7 @@ public CreateChatbotResponseDto createAnswerWithGraphRAG(
7676

7777
return CreateChatbotResponseDto.of(
7878
response,
79-
graphId.toString(),
79+
dbId.toString(),
8080
answer.getCreatedAt(),
8181
contextChunks,
8282
sourceNodes

โ€Žsrc/main/java/com/going/server/domain/upload/service/UploadServiceImpl.javaโ€Ž

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ public UploadResponseDto uploadFile(UploadRequestDto dto) {
5858
Map<String, String> paresData = pdfOcrService.parse(jsonResponse);
5959
String text = paresData.get("์ฝ๊ธฐ์ž๋ฃŒ");
6060
log.info("text log={}",text);
61-
62-
//๋ชจ๋ธ์— ๋Œ๋ฆฐ ๊ฐ’์„ ๋ฐ›์•„์˜ด
61+
//๋ชจ๋ธ์— ๋Œ๋ฆฐ ๊ฐ’์„ ๋ฐ›์•„์˜ด
6362
String response = setModelData(text);
6463

6564
ObjectMapper mapper = new ObjectMapper();

0 commit comments

Comments
ย (0)