How to extract key points from text with AI
Quick answer
Use the
OpenAI Python SDK to send your text to a chat model like gpt-4o with a prompt instructing it to extract key points. Parse the response from client.chat.completions.create to get concise bullet points or summaries.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install openai>=1.0
Setup
Install the official openai Python package and set your API key as an environment variable.
- Run
pip install openaito install the SDK. - Set your API key in your shell:
export OPENAI_API_KEY='your_api_key'(Linux/macOS) orsetx OPENAI_API_KEY "your_api_key"(Windows).
pip install openai output
Collecting openai Downloading openai-1.x.x-py3-none-any.whl (xx kB) Installing collected packages: openai Successfully installed openai-1.x.x
Step by step
This example sends a text passage to the gpt-4o chat model with a prompt to extract key points. It prints the bullet points returned by the model.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
text_to_summarize = (
"Artificial intelligence (AI) is transforming industries by automating tasks, "
"enhancing decision-making, and enabling new capabilities. Key benefits include increased efficiency, "
"cost reduction, and improved accuracy. Challenges remain in ethics, bias, and data privacy."
)
prompt = (
f"Extract the key points from the following text as concise bullet points:\n\n{text_to_summarize}"
)
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": prompt}]
)
key_points = response.choices[0].message.content
print("Key points extracted:\n", key_points) output
Key points extracted: - AI automates tasks and transforms industries. - Benefits include increased efficiency, cost reduction, and improved accuracy. - Challenges include ethics, bias, and data privacy.
Common variations
- Use
gpt-4o-minifor faster, cheaper extraction with slightly less detail. - Use async calls with
asyncioandawait client.chat.completions.create(...)for concurrency. - Stream partial results by setting
stream=Trueand iterating over chunks. - Use Anthropic's
claude-3-5-sonnet-20241022model with theanthropicSDK for alternative summarization.
import asyncio
import os
from openai import OpenAI
async def extract_key_points_async(text: str):
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
prompt = f"Extract the key points from the following text as concise bullet points:\n\n{text}"
response = await client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}],
stream=True
)
async for chunk in response:
print(chunk.choices[0].delta.content or "", end="", flush=True)
if __name__ == "__main__":
text = "AI improves efficiency, reduces costs, and raises ethical concerns."
asyncio.run(extract_key_points_async(text)) output
- AI improves efficiency - reduces costs - raises ethical concerns
Troubleshooting
- If you get authentication errors, verify your
OPENAI_API_KEYenvironment variable is set correctly. - If the model returns irrelevant or verbose output, refine your prompt to be more explicit about concise bullet points.
- For rate limits, implement exponential backoff retries or upgrade your API plan.
- Ensure you use the latest
openaiSDK version to avoid deprecated method errors.
Key Takeaways
- Use the OpenAI Python SDK with
gpt-4oto extract key points via chat completions. - Craft clear prompts requesting concise bullet points for best summarization results.
- Async and streaming calls improve performance for large or multiple texts.
- Always set your API key securely via environment variables to avoid authentication issues.