File size: 11,989 Bytes
5e5d326
2099574
ef26f24
5e5d326
 
 
 
 
 
 
 
 
 
 
5db794d
5e5d326
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef26f24
5e5d326
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c777f0
5e5d326
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c777f0
 
 
 
5e5d326
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa7ef71
 
 
 
 
5e5d326
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c777f0
5e5d326
6c777f0
5e5d326
 
 
 
 
6c777f0
5e5d326
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
import os
TOGETHER_API_KEY = os.environ.get("TOGETHER_API_KEY")
SEMANTIC_SCHOLAR_API_KEY = os.environ.get("SEMANTIC_SCHOLAR_API_KEY")
import re
import time
import json
import shutil
import requests


import spacy
#!python -m spacy download en_core_web_lg
from openai import OpenAI, APIError




from llama_index import (
    VectorStoreIndex,
    SimpleDirectoryReader,
    ServiceContext,
    load_index_from_storage
)
from llama_index.embeddings import HuggingFaceEmbedding, TogetherEmbedding
from llama_index.storage.storage_context import StorageContext













# 处理原始.text数据抹去citation,或者直接从用户出获得没有citation的introduct
def remove_citation(text):
    # Regular expression to match \cite{...}
    pattern = r'\\cite\{[^}]*\}'

    # Replace \cite{...} with an empty string
    text = re.sub(pattern, '', text)

    # Replace multiple spaces with a single space
    text = re.sub(r' +', ' ', text)

    # Replace spaces before punctuation marks with just the punctuation marks
    text = re.sub(r"\s+([,.!?;:()\[\]{}])", r"\1", text)
    
    return text



def get_chat_completion(client, prompt, llm_model, max_tokens):
    messages = [
        {
            "role": "system",
            "content": "You are an AI assistant",
        },
        {
            "role": "user",
            "content": prompt,
        }
    ]
    try:
        chat_completion = client.chat.completions.create(
            messages=messages,
            model=llm_model,
            max_tokens=max_tokens
        )
        return chat_completion.choices[0].message.content
    except APIError as e:
        # Handle specific API errors
        print(f"API Error: {e}")
    except Exception as e:
        # Handle other exceptions
        print(f"Error: {e}")








def get_relevant_papers(search_query, sort=True, count=10):
    """
    search_query (str): the required query parameter and its value (in this case, the keyword we want to search for)
    count (int): the number of relevant papers to return for each query

    Semantic Scholar Rate limit:
    1 request per second for the following endpoints:
    /paper/batch
    /paper/search
    /recommendations
    10 requests / second for all other calls
    """
    # Define the paper search endpoint URL; All keywords in the search query are matched against the paper’s title and abstract. 
    url = 'https://api.semanticscholar.org/graph/v1/paper/search'
    # Define headers with API key
    headers = {'x-api-key': SEMANTIC_SCHOLAR_API_KEY}

    query_params = {
        'query': search_query,
        'fields': 'url,title,year,abstract,authors.name,journal,citationStyles,tldr,referenceCount,citationCount',
        'limit': 20,
    }
    # Send the API request
    response = requests.get(url, params=query_params, headers=headers)

    # Check response status
    if response.status_code == 200:
        json_response = response.json() 
        if json_response['total'] != 0:
            papers = json_response['data']
        else:
            papers = []
    
        # Sort the papers based on citationCount in descending order
        if sort: 
            papers = sorted(papers, key=lambda x: x['citationCount'], reverse=True)
      
        return papers[:count]
    else:
        print(f"Request failed with status code {response.status_code}: {response.text}")


def save_papers(unique_dir, papers):
    os.makedirs(unique_dir, exist_ok=True)
    # Save each dictionary to a separate JSON file
    for i, dictionary in enumerate(papers):
        filename = os.path.join(unique_dir, f"{dictionary['paperId']}.json")
        with open(filename, 'w') as json_file:
            json.dump(dictionary, json_file, indent=4)
    print(f"{len(papers)} papers saved as JSON files successfully at {unique_dir}.")






def get_index(service_context, docs_dir, persist_dir):
    documents = SimpleDirectoryReader(docs_dir, filename_as_id=True).load_data()

    # check if storage already exists
    PERSIST_DIR = persist_dir
    if not os.path.exists(PERSIST_DIR):
        print('create new index')
        index = VectorStoreIndex.from_documents(
            documents, service_context=service_context, show_progress=False
        )
        # store it for later
        index.storage_context.persist(persist_dir=PERSIST_DIR)
    else:
        print('load the existing index')
        # load the existing index
        storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
        index = load_index_from_storage(storage_context, service_context=service_context)
        # refresh the index
        refreshed_docs = index.refresh_ref_docs(documents, update_kwargs={"delete_kwargs": {"delete_from_docstore": True}})
        print(f'refreshed_docs:\n{refreshed_docs}')

    return index


def get_paper_data(text):
    """text = node.text """
    dictionary_from_json = json.loads(text)

    bibtex = dictionary_from_json['citationStyles']['bibtex']
    bibtex = bibtex.replace('&', 'and')
    citation_label = re.findall(r"@(\w+){([\w'-]+)", bibtex)[0][1]
    
    citationCount = dictionary_from_json['citationCount']

    if dictionary_from_json['tldr'] is not None:
        tldr = dictionary_from_json['tldr']['text']
    else:
        tldr = 'No tldr available'
    
    url = dictionary_from_json['url']
    
    return citation_label, (bibtex, citationCount, tldr, url)


def move_cite_inside_sentence(sent, ez_citation):
    if sent[-1]!='\n':
        character = sent[-1]
        sent_new = sent[:-1] + ' <ez_citation>' + character
    else:  
        count = sent.count('\n')
        sent = sent.strip()
        character = sent[-1]
        sent_new = sent[:-1] + ' <ez_citation>' + character + '\n'*count
        
    return sent_new.replace('<ez_citation>', ez_citation)


def write_bib_file(bib_file_content, data):
    bibtex, citationCount, tldr, url = data
    bib_file_content = bib_file_content + f'\n%citationCount: {citationCount}\n%tldr: {tldr}\n%url: {url}\n' + bibtex
    return bib_file_content
    

def write_citation(sent, bib_file_content, retrieved_nodes, sim_threshold=0.75):
    labels = []
    for node in retrieved_nodes:
        citation_label, data = get_paper_data(node.text)
        print('relevant paper id (node.id_):', node.id_, 'match score (node.score):', node.score)
        print('relevant paper data:', *data)
        print('-'*30)

        if node.score > sim_threshold and citation_label != "None":
            labels.append(citation_label)
            if not (citation_label in bib_file_content):
                bib_file_content = write_bib_file(bib_file_content, data)
        else:
            continue

    labels = ', '.join(labels)
    if labels:
        ez_citation = f'\cite{{{labels}}}'
        sent_new = move_cite_inside_sentence(sent, ez_citation)
    else:
        sent_new = sent

    return sent_new, bib_file_content


get_prompt = lambda sentence: f"""
I want to use semantic scholar paper search api to find the relevant papers, can you read the following text then suggest me an suitable search query for this task? 

Here is an example for using the api:
<example>
```python
import requests
# Define the paper search endpoint URL
url = 'https://api.semanticscholar.org/graph/v1/paper/search'
# Define the required query parameter and its value (in this case, the keyword we want to search for)
query_params = {{
    'query': 'semantic scholar platform',
    'limit': 3
}}
# Make the GET request with the URL and query parameters
searchResponse = requests.get(url, params=query_params)
```
</example>

Here is the text:
<text>
{sentence}
</text>
"""


# main block
def main(sentences, count, client, llm_model, max_tokens, service_context):
    """count (int): the number of relevant papers to return for each query"""
    sentences_new = []
    bib_file_content = ''
    for sentence in sentences:
        prompt = get_prompt(sentence)
        
        response = get_chat_completion(client, prompt, llm_model, max_tokens)
    
        # Define a regular expression pattern to find the value of 'query'
        pattern = r"'query': '(.*?)'"
        matches = re.findall(pattern, response)
        if matches:
            search_query = matches[0]
        else:
            search_query = sentence[:2] # use the first two words as the search query
        
        relevant_papers = get_relevant_papers(search_query, sort=True, count=count)
        
        if relevant_papers:
            # save papers to json files and build index
            unique_dir = os.path.join("papers", f"{int(time.time())}")
            persist_dir = os.path.join("index", f"{int(time.time())}")
            save_papers(unique_dir, relevant_papers)
            index = get_index(service_context, unique_dir, persist_dir)
            
            # get sentence's most similar papers
            retriever = index.as_retriever(service_context=service_context, similarity_top_k=5)
            retrieved_nodes = retriever.retrieve(sentence)

            sent_new, bib_file_content = write_citation(sentence, bib_file_content, retrieved_nodes, sim_threshold=0.7)
            sentences_new.append(sent_new)
        else:
            sentences_new.append(sentence)
        
        print('sentence:', sentence.strip())
        print('search_query:', search_query)
        print('='*30)

    return sentences_new, bib_file_content




def ez_cite(introduction, debug=False):
    nlp = spacy.load("en_core_web_lg")
    doc = nlp(introduction)
    sentences = [sentence.text for sentence in doc.sents]
    sentences = [ remove_citation(sentence) for sentence in sentences]

    client = OpenAI(api_key=TOGETHER_API_KEY,
    base_url='https://api.together.xyz',
    )

    llm_model = "Qwen/Qwen1.5-72B-Chat"
    max_tokens = 1000

    embed_model = TogetherEmbedding(model_name="togethercomputer/m2-bert-80M-8k-retrieval", api_key=TOGETHER_API_KEY) 
    service_context = ServiceContext.from_defaults(
        llm=None, embed_model=embed_model, chunk_size=8192, # chunk_size must be bigger than the whole .json so that all info is preserved, in this case, one doc is one node
    )

    if debug:
        sentences = sentences[:2]

    sentences_new, bib_file_content = main(sentences, count=10, 
                                           client=client,
                                           llm_model=llm_model, 
                                           max_tokens=max_tokens, 
                                           service_context=service_context)

    with open('intro.bib', 'w') as bib_file:
        bib_file.write(bib_file_content)

    final_intro = ' '.join(sentences_new)
    print(final_intro)
    print('='*30)

    dir_path = "index"
    try:
        # Delete the directory and its contents
        shutil.rmtree(dir_path)
        print(f"Directory '{dir_path}' deleted successfully.")
    except Exception as e:
        print(f"Error deleting directory '{dir_path}': {e}")


    dir_path = "papers"
    try:
        # Delete the directory and its contents
        shutil.rmtree(dir_path)
        print(f"Directory '{dir_path}' deleted successfully.")
    except Exception as e:
        print(f"Error deleting directory '{dir_path}': {e}")

    return final_intro, bib_file_content



example1 = r"""In the current Noisy Intermediate-Scale Quantum (NISQ) era, a few methods have been proposed to construct useful quantum algorithms that are compatible with mild hardware restrictions. Most of these methods involve the specification of a quantum circuit Ansatz, optimized in a classical fashion to solve specific computational tasks. 

Next to variational quantum eigensolvers in chemistry and variants of the quantum approximate optimization algorithm, machine learning approaches based on such parametrized quantum circuits stand as some of the most promising practical applications to yield quantum advantages."""



if __name__ == "__main__":

    final_intro, bib_file_content = ez_cite(example1, debug=True)