
Save time reading Hacker News comments using Converse API
I share how Amazon Q Developer helped me to create summaries using the Amazon Bedrock Converse API of Hacker News items
{"exhaustive":{"nbHits":true,"typo":true},"exhaustiveNbHits":true,"exhaustiveTypo":true,"hits":[{"_highlightResult":{"author":{"matchLevel":"none","matchedWords":[],"value":"sbeaks"},"comment_text":{"matchLevel":"none","matchedWords":[],"value":"Are we going to end up with rererecapture where to post you need something on your phone/laptop measuring typing speed and scanning your face with an IR cam? Or a checkmark showing typed out by hand? Wouldn't get rid of ai content but may slow down bots posting it."},"story_title":{"matchLevel":"none","matchedWords":[],"value":"Open source AI is the path forward"},"story_url":{"matchLevel":"none","matchedWords":[],"value":"https://about.fb.com/news/2024/07/open-source-ai-is-the-path-forward/"}},"_tags":["comment","author_sbeaks","story_41046773"],"author":"sbeaks","comment_text":"Are we going to end up with rererecapture where to post you need something on your phone/laptop measuring typing speed and scanning your face with an IR cam? Or a checkmark showing typed out by hand? Wouldn't get rid of ai content but may slow down bots posting it.","created_at":"2024-07-24T10:48:04Z","created_at_i":1721818084,"objectID":"41055592","parent_id":41054918,"story_id":41046773,"story_title":"Open source AI is the path forward","story_url":"https://about.fb.com/news/2024/07/open-source-ai-is-the-path-forward/","updated_at":"2024-07-24T10:52:28Z"}],"hitsPerPage":1,"nbHits":703,"nbPages":703,"page":0,"params":"tags=comment%2Cstory_41046773&hitsPerPage=1&advancedSyntax=true&analyticsTags=backend","processingTimeMS":1,"processingTimingsMS":{"_request":{"roundTrip":13}},"query":"","serverTimeMS":1}
Amazon Q Developer : "I have the following json data payload that is returned when running against an API. Provide sample code in Python that will allow me to export all iterations of the the comment_text to the console, separated by a comma. Here is the sample json"
1
2
3
4
5
6
7
8
9
10
11
import json
# Sample JSON data
json_data = <copy of the above data>
# Load the JSON data
data = json.loads(json.dumps(json_data))
# Extract the comment_text values and print them separated by a comma
comment_texts = [hit["comment_text"] for hit in data["hits"]]
print(", ".join(comment_texts))
Amazon Q Developer : "can you update the code so that it gets the json data from the following URL - https://hn.algolia.com/api/v1/searchbydate?tags=comment,story_41046773&hitsPerPage=1"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import json
import requests
# URL to fetch JSON data
url = "https://hn.algolia.com/api/v1/search_by_date?tags=comment,story_41046773&hitsPerPage=2"
# Send a GET request to the URL
response = requests.get(url)
# Check if the request was successful
if response.status_code == 200:
# Load the JSON data
data = response.json()
# Extract the comment_text values and print them separated by a comma
comment_texts = [hit["comment_text"] for hit in data["hits"]]
print(", ".join(comment_texts))
else:
print(f"Error: {response.status_code}")
Amazon Q Developer : "can you updated the code so that each comment is separated by a new line break"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import json
import requests
# URL to fetch JSON data
url = "https://hn.algolia.com/api/v1/search_by_date?tags=comment,story_41046773&hitsPerPage=2"
# Send a GET request to the URL
response = requests.get(url)
# Check if the request was successful
if response.status_code == 200:
# Load the JSON data
data = response.json()
# Extract the comment_text values and print them separated by a new line
comment_texts = [hit["comment_text"] for hit in data["hits"]]
print("\n".join(comment_texts))
else:
print(f"Error: {response.status_code}")
Amazon Q Developer : "if i wanted to output to a file, how would i update the code"
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
import json
import requests
# URL to fetch JSON data
url = "https://hn.algolia.com/api/v1/search_by_date?tags=comment,story_41046773&hitsPerPage=2"
# Send a GET request to the URL
response = requests.get(url)
# Check if the request was successful
if response.status_code == 200:
# Load the JSON data
data = response.json()
# Extract the comment_text values
comment_texts = [hit["comment_text"] for hit in data["hits"]]
# Open a file for writing
with open("comments.txt", "w") as file:
# Write each comment to the file, separated by a new line
file.write("\n".join(comment_texts))
print("Comments written to comments.txt")
else:
print(f"Error: {response.status_code}")
wc comments.txt
757 42864 283516 comments.txt
Amazon Q Developer : "Can you update the code so that it takes in a command line option for the story_ in the URL"
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
import json
import requests
import sys
# Check if a command-line argument is provided
if len(sys.argv) < 2:
print("Usage: python script.py story_id")
sys.exit(1)
# Get the story_id from the command-line argument
story_id = sys.argv[1]
# URL to fetch JSON data
url = f"https://hn.algolia.com/api/v1/search_by_date?tags=comment,story_{story_id}&hitsPerPage=1000"
# Send a GET request to the URL
response = requests.get(url)
# Check if the request was successful
if response.status_code == 200:
# Load the JSON data
data = response.json()
# Extract the comment_text values
comment_texts = [hit["comment_text"] for hit in data["hits"]]
# Open a file for writing
with open("comments.txt", "w") as file:
# Write each comment to the file, separated by a new line
file.write("\n".join(comment_texts))
print("Comments written to comments.txt")
else:
print(f"Error: {response.status_code}")
Amazon Q Developer : "I have the following code. Can you show me how to update it so that it reads from a file called comments.txt and then summarises the text. Here is the code "
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
import os
import boto3
import json
# Load the contents of the comments.txt file
with open('comments.txt', 'r') as file:
file_contents = file.read()
# Create a Bedrock client
bedrock = boto3.client('bedrock-runtime', region_name='us-east-1')
# Define the model ID
model_id = 'anthropic.claude-3-sonnet-20240229-v1:0'
message_list = []
initial_message = {
"role": "user",
"content": [
{ "text": "Summarise the the text and provide the top three to five main talking points. Ignore any comments that use bad language, or profanities" },
{ "document": {
"format":"txt",
"name":"comments",
"source":{"bytes":file_contents}
}}
],
}
message_list.append(initial_message)
response = bedrock.converse(
modelId="anthropic.claude-3-sonnet-20240229-v1:0",
messages=message_list,
inferenceConfig={
"maxTokens": 4000,
"temperature": 0
},
)
response_message = response['output']['message']
print(json.dumps(response_message['content'][0]['text'], indent=4))
"Here are the main talking points from the text, summarized:\n\n1. Meta (Facebook) has released its Llama 3.1 language model, including versions with 405 billion, 70 billion, and 8 billion parameters, as open source AI models. This makes frontier-level AI models freely available to most people and organizations, except Meta's major competitors.\n\n2. Meta argues that open sourcing AI models promotes safety, as it allows wider scrutiny and prevents a small number of companies from monopolizing the technology. However, there is debate around whether the Llama models truly qualify as "open source" given the restrictions in the license and lack of access to training data.\n\n3. Meta's motivations for open sourcing the models are seen as both promoting the public good and undercutting competitors like OpenAI, Microsoft, and Google, which have been monetizing their AI models through APIs and cloud services.\n\n4. The release of the open Llama models is expected to significantly impact the AI industry, potentially disrupting the business models of closed AI providers and accelerating the development and adoption of open AI systems.\n\n5. There are concerns around the potential misuse of open AI models, as well as questions about the practicality of truly open sourcing large language models given the challenges of sharing training data and the computational resources required for training."
AttributeError: 'BedrockRuntime' object has no attribute 'converse'
botocore.errorfactory.ValidationException: An error occurred (ValidationException) when calling the Converse operation: Your account is not authorized to invoke this API operation.
from tqdm import tqdm
import logging
import os
# Configure logging
logger = logging.getLogger()
logger.setLevel(logging.DEBUG) # Set the desired logging level (DEBUG for more verbose logging)
logging.basicConfig(level=logging.INFO)
session = boto3.Session(region_name='us-east-1')
boto3.set_stream_logger('', logging.DEBUG
2024-07-24 13:30:59,591 botocore.hooks [DEBUG] Event before-parameter-build.bedrock-runtime.Converse: calling handler <function generate_idempotent_uuid at 0x103a543a0>
DEBUG:botocore.regions:Calling endpoint provider with parameters: {'Region': 'eu-west-1', 'UseDualStack': False, 'UseFIPS': False}
bedrock = boto3.client('bedrock-runtime', region_name='us-east-1')
Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.