Created
March 20, 2024 23:31
-
-
Save HebeHH/50f4b3dacb53b2b17d5247e9783cca22 to your computer and use it in GitHub Desktop.
Basic RAG news teller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests, sys, os | |
from openai import OpenAI | |
newsapi_apikey = os.environ.get('NEWS_APIKEY') | |
# This is a terrible excuse for a keywords function. Please replace. Most issues with this RAG can be blamed on this. | |
def get_keywords(input_text): | |
stripped = ''.join([i for i in input_text if i.isalpha() or i == ' ']) | |
keywords = [x for x in stripped.split(' ') if len(x) > 5] | |
return keywords | |
def retrieve_relevant_news(input_text): | |
keywords = get_keywords(input_text) | |
print(keywords) | |
newsapi_url = ('https://newsapi.org/v2/everything?q=' | |
+ '&q='.join(keywords) | |
+ '&from=2024-03-10&sortBy=relevancy&searchIn=title,description' | |
+ '&apiKey=' + newsapi_apikey) | |
response = requests.get(newsapi_url).json()['articles'] | |
useful_results = [item['title'] + "\n" + item['description'] for item in response if item['source']['id']][:10] | |
return '\n'.join(useful_results) | |
def augment_context(retrieved_data, input_text): | |
augmented_context = [ | |
{ | |
"role": "system", | |
"content": "You are a news broadcaster. Always let the user know about the details in the following news:\n" + retrieved_data | |
}, | |
{ | |
"role": "user", "content": input_text | |
}, | |
] | |
return augmented_context | |
def generate_answer(augmented_context): | |
client = OpenAI() | |
response = client.chat.completions.create( | |
model="gpt-3.5-turbo", | |
messages=augmented_context | |
) | |
return response.choices[0].message.content | |
def run_rag(input_text): | |
print("Question asked:", input_text) | |
retrieved_data = retrieve_relevant_news(input_text) | |
print("\nRETRIEVED information:\n", retrieved_data) | |
augmented_context = augment_context(retrieved_data, input_text) | |
print("\nAUGMENTED context with the retrieved information") | |
answer = generate_answer(augmented_context) | |
print("\nGENERATED answer:\n", answer) | |
return answer | |
run_rag(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
Use from command line with query as argument.
python do-rag.py "How did the Nvidia announcement go?"
Requires:
NEWS_APIKEY