hunkim commited on
Commit
b8f6efa
1 Parent(s): 9f844b4

Create es_gpt.py

Browse files
Files changed (1) hide show
  1. es_gpt.py +75 -0
es_gpt.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from elasticsearch import Elasticsearch
2
+ import os
3
+ import json
4
+ import requests
5
+
6
+ ES_URL = os.environ["ES_URL"]
7
+ ES_USER = os.environ["ES_USER"]
8
+ ES_PASS = os.environ["ES_PASS"]
9
+ ES_CA_CERT = os.environ["ES_CA_CERT"]
10
+
11
+
12
+ class ESGPT:
13
+ def __init__(self, index_name):
14
+ self.es = Elasticsearch(ES_URL, http_auth=(ES_USER, ES_PASS),
15
+ ca_certs=ES_CA_CERT, verify_certs=True)
16
+ self.index_name = index_name
17
+ self.model_engine = os.environ["OPENAI_GPT_ENGINE"]
18
+ self.api_key = os.environ["OPENAI_API_KEY"]
19
+
20
+ def index(self, doc_id, doc):
21
+ self.es.index(index=self.index_name,
22
+ id=doc_id,
23
+ document=doc)
24
+
25
+ def search(self, query):
26
+ body = {
27
+ "query": {
28
+ "query_string": {"query": query}
29
+ }
30
+ }
31
+
32
+ results = self.es.search(index=self.index_name, body=body)
33
+ return results['hits']['hits']
34
+
35
+ def _paper_results_to_text(self, results):
36
+ text_result = ""
37
+ for paper in results:
38
+ title = ""
39
+ if "title" in paper["_source"]:
40
+ title = paper["_source"]["title"]
41
+
42
+ abstract = ""
43
+ if "abctract" in paper["_source"]:
44
+ abstract = paper["_source"]["abstract"]
45
+
46
+ paper_str = f"{title}:\n{abstract[:100]}\n\n"
47
+ text_result += paper_str
48
+ return text_result
49
+
50
+ def summarize(self, query, results):
51
+ # Generate summaries for each search result
52
+ result_json_str = self._paper_results_to_text(results)
53
+ if result_json_str == "":
54
+ result_json_str = "No results found"
55
+
56
+ print(result_json_str[:500])
57
+
58
+ body = {
59
+ "model": self.model_engine,
60
+ "prompt": f"Please summarize the following search results for query: {query}:\n{result_json_str[:1000]}",
61
+ "max_tokens": 1000,
62
+ "n": 1,
63
+ "stop": None,
64
+ "temperature": 0.5,
65
+ "stream": True,
66
+ }
67
+
68
+ headers = {"Content-Type": "application/json",
69
+ "Authorization": f"Bearer {self.api_key}"}
70
+
71
+ resp = requests.post("https://api.openai.com/v1/completions",
72
+ headers=headers,
73
+ data=json.dumps(body),
74
+ stream=True)
75
+ return resp