File size: 2,803 Bytes
7ad28cd
 
 
 
 
 
f320346
7ad28cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83a7b80
7ad28cd
 
 
 
 
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
import os
import requests
import json
import google.generativeai as genai

# Load API keys from environment variables
SERPER_API_KEY = os.getenv('X_API_KEY')
GEMINI_API_KEY = os.getenv('GEMINI_API_KEY')
genai.configure(api_key=GEMINI_API_KEY)
model = genai.GenerativeModel('gemini-1.5-flash')

def search_articles(query:str):
    """
    Searches for articles related to the query using Serper API.
    Returns a list of dictionaries containing article URLs, headings, and text.
    """
    articles = None
    # implement the search logic - retrieves articles
    url = "https://google.serper.dev/search"

    payload = json.dumps({
    "q":query
    })
    headers = {
    'X-API-KEY': SERPER_API_KEY,
    'Content-Type': 'application/json'
    }

    response = requests.request("POST", url, headers=headers, data=payload)
    articles = response.text
    return articles


def fetch_article_content(articles):
    """
    Fetches the article content, extracting headings and text.
    """
    content = ""
    # implementation of fetching headings and content from the articles
    data = json.loads(articles)

    # Extract from answerBox if it exists
    if 'answerBox' in data:
        if 'title' in data['answerBox']:
            content += data['answerBox']['title']+"\n"
        if 'snippet' in data['answerBox']:
            content += data['answerBox']['snippet']+"\n"

    # Extract from organic search results
    if 'organic' in data:
        for result in data['organic']:
            if 'title' in result:
                content += result['title']+"\n"
            if 'snippet' in result:
                content += result['snippet']+"\n"

    # Extract from peopleAlsoAsk
    if 'peopleAlsoAsk' in data:
        for question in data['peopleAlsoAsk']:
            if 'title' in question:
                content += question['title']+"\n"
            if 'snippet' in question:
                content += question['snippet']+"\n"
    return content.strip()


def generate_answer(content,query):
    """
    Generates an answer from the concatenated content using GPT-4.
    The content and the user's query are used to generate a contextual answer.
    """
    # Create the prompt based on the content and the query
    response = None
    system_prompt = f"""You are a helpful assistant. Use the following context to answer the user's query. If the context doesn't contain relevant information, say so.\n
    Below is the context : \n
    {content}\n
    Below is the user query:
    {query}\n
    Based on the user query above and the context given provide with highly accurate response for the user query . You should cover all points , each and every small concrete detail's in the context.
    """

    response = model.generate_content(system_prompt)
    return response.text