-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode
66 lines (53 loc) · 2.34 KB
/
Code
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
import openai
from scholarly import scholarly
from langchain.schema import HumanMessage
from langchain.chat_models import ChatOpenAI
#Function to search and retrieve papers from Google Scholar that match all keywords
def search_papers(keywords):
search_query = scholarly.search_pubs(" ".join(keywords))
papers = []
for _ in range(100): #Fetch up to 100 papers and then filter
try:
paper = next(search_query)
# Check if all keywords are in the title or abstract
title = paper['bib']['title'].lower()
abstract = paper['bib'].get('abstract', '').lower()
if all(keyword.lower() in title or keyword.lower() in abstract for keyword in keywords):
papers.append(paper)
if len(papers) >= 10: #Limit to 10 papers
break
except StopIteration:
break
return papers
#Function to summarize a list of papers using OpenAI's gpt-3.5-turbo model via LangChain
def summarize_papers(papers, temperature=0):
summaries = []
chat = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=temperature)
for paper in papers:
title = paper['bib']['title']
year = paper['bib'].get('pub_year', 'Unknown year')
abstract = paper['bib'].get('abstract', 'No abstract available.')
input_text = f"""Summarize the following research paper:
Title: {title}
Year: {year}
Abstract: {abstract}
"""
prompt = HumanMessage(content=input_text)
summary_response = chat([prompt])
summary = summary_response.content.strip()
summaries.append({
'title': title,
'year': year,
'summary': summary
})
return summaries
#Prompt the user for keywords to search for papers
keywords_input = input("Enter the keywords to search for papers (separated by commas): ")
keywords = [keyword.strip() for keyword in keywords_input.split(",")]
#Search for papers using the provided keywords
papers = search_papers(keywords)
#Summarize the retrieved papers
summaries = summarize_papers(papers, temperature=0) #Temperature set to 0 for deterministic results
#Print the summaries of the papers
for i, summary_info in enumerate(summaries, 1):
print(f"Summary of paper {i}:\nTitle: {summary_info['title']}\nYear: {summary_info['year']}\nSummary: {summary_info['summary']}\n")