File size: 2,106 Bytes
586f134
869c019
7a78078
 
586f134
6879f93
855cbdb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e36e61e
 
 
a806bea
e36e61e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from astroquery.nasa_ads import ADS
import re
import logging
import os

def extract_keywords_with_gpt(context, client, max_tokens=100, temperature=0.3):
    
    keyword_prompt = f"Extract 3 most important scientific keywords from the following user query:\n\n{context}"
    
    response = client.chat.completions.create(
        model="gpt-4",
        messages=[
            {"role": "system", "content": "You are an expert in identifying key scientific terms and concepts."},
            {"role": "user", "content": keyword_prompt}
        ],
        max_tokens=max_tokens,
        temperature=temperature
    )
    
    extracted_keywords = response.choices[0].message.content.strip()
    
    cleaned_keywords = re.sub(r'\d+\.\s*', '', extracted_keywords)

    keywords_list = [kw.strip() for kw in cleaned_keywords.split("\n") if kw.strip()]
    
    return keywords_list

def fetch_nasa_ads_references(ads_query):
    """Fetch relevant NASA ADS papers and format them for readability."""
    ADS.TOKEN = os.getenv('ADS_API_KEY')
    try:
        # Query NASA ADS for relevant papers
        papers = ADS.query_simple(ads_query)

        if not papers or len(papers) == 0:
            return [("No results found", "N/A", "N/A", "N/A", "N/A", "N/A")]

        # Include authors in the references
        references = []
        for paper in papers[:5]:  # Limit to 5 references
            title = paper.get('title', ['Title not available'])[0]
            abstract = paper.get('abstract', 'Abstract not available')
            authors = ", ".join(paper.get('author', [])[:3]) + (" et al." if len(paper.get('author', [])) > 3 else "")
            bibcode = paper.get('bibcode', 'N/A')
            pub = paper.get('pub', 'Unknown Journal')
            pubdate = paper.get('pubdate', 'Unknown Date')

            references.append((title, abstract, authors, bibcode, pub, pubdate))

        return references

    except Exception as e:
        logging.error(f"Error fetching ADS references: {str(e)}")
        return [("Error fetching references", "See logs for details", "N/A", "N/A", "N/A", "N/A")]