This service provides a REST API for retrieving and analyzing news articles using RAG (Retrieval Augmented Generation) with OpenAI's GPT models.
- Clone the repository
- Copy
.env.default
to.env
and fill in your API keys:
cp .env.default .env
- Install dependencies:
pip install -r requirements.txt
Create a .env
file with the following variables:
OPENAI_API_KEY
: Your OpenAI API keyAPP_API_KEY
: API key for authenticating requests to this service
python app.py
The service will start on http://localhost:8000
Endpoint: POST /api/retrieval
Retrieves relevant news articles based on a query.
# Using curl
curl -X POST "http://localhost:8000/api/retrieval" \
-H "x-api-key: YOUR_APP_API_KEY" \
-H "Content-Type: application/json" \
-d '{"query": "latest AI developments"}'
# Using Python
import requests
import json
response = requests.post(
"http://localhost:8000/api/retrieval",
headers={
"x-api-key": "YOUR_APP_API_KEY",
"Content-Type": "application/json"
},
json={"query": "latest AI developments"}
)
print(json.dumps(response.json(), indent=2))
Endpoint: POST /api/completion
Generates an AI response based on conversation history and relevant news articles.
# Using curl
curl -X POST "http://localhost:8000/api/completion" \
-H "x-api-key: YOUR_APP_API_KEY" \
-H "Content-Type: application/json" \
-d '{"messages": [{"role": "user", "content": "What are the latest developments in AI?"}]}'
# Using Python
import requests
import json
response = requests.post(
"http://localhost:8000/api/completion",
headers={
"x-api-key": "YOUR_APP_API_KEY",
"Content-Type": "application/json"
},
json={
"messages": [
{"role": "user", "content": "What are the latest developments in AI?"}
]
}
)
print(json.dumps(response.json(), indent=2))
{
"articles": [
{
"title": "Article Title",
"url": "https://example.com/article",
"date": "2024-03-21"
}
]
}
{
"response": "Generated response with citations [Source 1] and analysis..."
}
You can include date-related queries in your questions:
curl -X POST "http://localhost:8000/api/retrieval" \
-H "x-api-key: YOUR_APP_API_KEY" \
-H "Content-Type: application/json" \
-d '{"query": "AI news from last week"}'
The completion endpoint maintains conversation context:
messages = [
{"role": "user", "content": "What are the latest AI developments?"},
{"role": "assistant", "content": "According to recent news..."},
{"role": "user", "content": "Tell me more about those developments"}
]
response = requests.post(
"http://localhost:8000/api/completion",
headers={
"x-api-key": "YOUR_APP_API_KEY",
"Content-Type": "application/json"
},
json={"messages": messages}
)
The API returns standard HTTP status codes:
- 200: Successful request
- 401: Invalid API key
- 500: Internal server error with stack trace
Error responses include a detail message explaining the error.