File size: 4,766 Bytes
d695a29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html>

<head>
    <title>Summary and Search Example</title>
    <script>

        function search() {
            const resultsElement = document.getElementById("results");
            const query = document.getElementById("query").value;
            const url = `/search?q=${encodeURIComponent(query)}`;
            const eventSource = new EventSource(url);
            console.log(eventSource);

            eventSource.onerror = function (e) {
                if (e.readyState == EventSource.CLOSED) {
                    // Connection was closed.
                }
                eventSource.close();
            };

            eventSource.onmessage = function (event) {
                console.log(event);
                if (!event.data || event.data == "[DONE]") {
                    eventSource.close();
                    return;
                }
                const result = JSON.parse(event.data);
                const item = document.createElement("div");
                item.classList.add("search-result");

                const title = document.createElement("b");
                title.textContent = result._source.title;
                item.appendChild(title);

                const authors = document.createElement("p");
                authors.textContent = `Authors: ${result._source.author}`;
                item.appendChild(authors);

                const abs = document.createElement("p");
                abs.classList.add("abstract");
                abs.textContent = `${result._source.abstract.substring(0, 500)}...`;
                item.appendChild(abs);

                resultsElement.appendChild(item);
            };
        }

        function summarize() {
            const summaryElement = document.getElementById("summary");
            const query = document.getElementById("query").value;
            const url = `summary?q=${encodeURIComponent(query)}`;
            const eventSource = new EventSource(url);
            eventSource.onmessage = function (event) {
                if (!event.data || event.data == "[DONE]") {
                    eventSource.close();
                    return;
                }
                const result = JSON.parse(event.data);
                summaryElement.innerHTML += result['choices'][0]['text'];
            };
        }

        function clearResults() {
            const resultsElement = document.getElementById("results");
            const summaryElement = document.getElementById("summary");
            if (summaryElement) {
                summaryElement.innerHTML = "";
            }
            if (resultsElement) {
                resultsElement.innerHTML = "";
            }
        }

        function searchAndSummarize() {
            clearResults();
            search();
            summarize();
        }
    </script>
    <style>
        body {
            display: flex;
            flex-wrap: wrap;
        }

        #search-container {
            flex: 0 0 100%;
            padding: 20px;
            box-sizing: border-box;
        }

        #results-container {
            flex: 0 0 70%;
            padding: 20px;
            box-sizing: border-box;
        }

        #summary-container {
            flex: 0 0 30%;
            padding: 20px;
            box-sizing: border-box;
            background-color: #f2f2f2;
        }

        .search-input-container {
            display: flex;
            margin-bottom: 20px;
        }

        #query {
            flex: 1;
            margin-right: 10px;
        }

        #results {
            max-height: 80vh;
            overflow-y: scroll;
        }

        .search-result {
            margin-bottom: 10px;
            padding-bottom: 10px;
            border-bottom: 1px solid #ddd;
        }

        .search-result a {
            font-size: 18px;
            font-weight: bold;
            color: #1a0dab;
            text-decoration: none;
        }

        .search-result a:hover {
            text-decoration: underline;
        }

        .search-result p {
            margin: 10px 0;
            font-size: 14px;
        }
    </style>
</head>

<body>
    <div id="search-container">
        <h2>Elasticsearch with GPT Summary</h2>
        <div class="search-input-container">
            <input type="text" id="query" placeholder="Enter a query (e.g., bug, gan)" onkeydown="if (event.key === 'Enter') searchAndSummarize()">
            <button onclick="searchAndSummarize()">Search</button>
        </div>
    </div>
    <div id="results-container">
        <h3>Search Results</h3>
        <div id="results"></div>
    </div>
    <div id="summary-container">
        <h3>GPT Summary</h3>
        <div id="summary"></div>
    </div>
</body>


</html>