File size: 1,974 Bytes
8e9fd43
 
 
2c42748
8e9fd43
 
 
 
2c42748
 
8e9fd43
 
 
 
2c42748
8e9fd43
c1ba890
8e9fd43
606b2ff
8e9fd43
 
 
 
c1ba890
8e9fd43
 
 
 
2812e86
bf0cf05
8e9fd43
bf0cf05
02f3cf9
8e9fd43
 
 
 
 
 
 
 
 
 
 
 
2c42748
8e9fd43
5da06ea
 
8e9fd43
7edbb26
2c42748
 
8e9fd43
 
 
 
 
 
 
 
2c42748
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
# app/routes/question.py
import os
import requests
from fastapi import APIRouter
from pydantic import BaseModel
from typing import List
from redis_client import redis_client as r
from dotenv import load_dotenv
from huggingface_hub import InferenceClient
import re

load_dotenv()

GNEWS_API_KEY = os.getenv("GNEWS_API_KEY")
HF_TOKEN = os.getenv("HF_TOKEN")  # Hugging Face token for private models if needed

askMe = APIRouter()

client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.3", token=os.getenv("HF_TOKEN"))

class QuestionInput(BaseModel):
    question: str

@askMe.post("/ask")
async def ask_question(input: QuestionInput):
    question = input.question

    # Extract keywords (simple version)
    keywords = re.findall(r"\b\w{4,}\b", question)
    query_string = " AND ".join(f'"{kw}"' for kw in keywords[:7])

    print("Keywords are", query_string)

    gnews_url = f"https://gnews.io/api/v4/search?q={query_string}&lang=en&max=3&expand=content&token={GNEWS_API_KEY}"
    try:
        response = requests.get(gnews_url, timeout=10)
        response.raise_for_status()
        articles = response.json().get("articles", [])
    except Exception as e:
        return {"error": f"GNews API error: {str(e)}"}

    # Combine article content for context
    context = "\n\n".join([
        article.get("content") or article.get("description") or ""
        for article in articles
    ])[:1500]  # truncate to keep model input size safe

    print("And context is", context)

    # Build prompt
    prompt = f"<s>[INST] Use the context below to answer the question. If the context is insufficient, say 'I am unable to answer'.\n\nContext:\n{context}\n\nQuestion: {question} [/INST]"

    result = client.text_generation(prompt, max_new_tokens=256, temperature=0.7)

    return {
        "question": question,
        "answer": result.strip(),
        "sources": [
            {"title": a["title"], "url": a["url"]}
            for a in articles
        ]
    }