What it does

You type one or more topics, separated by semicolons, for example Nifty Bank; IT sector; FII buying. For each topic the script searches the web for that day's Indian market news, downloads the top five articles, and has Llama 3.2, running locally through Ollama, write a short plain-English summary with the sources listed underneath. Each run can be saved to a timestamped text file such as indian_market_analysis_20241104_140505.txt.

The pipeline

  1. Check the model is running. On start-up the script sends a one-word prompt to Ollama's local API (http://localhost:11434/api/generate). If that fails it stops immediately with instructions, instead of failing halfway through a run.
  2. Search. Each topic becomes the query "<topic> Indian stock market news today", sent through the googlesearch package, which returns the first five result URLs. There is a two-second pause between results so the script doesn't hammer the search engine.
  3. Extract. newspaper3k downloads each page and separates the article body, headline and publish date from the menus, adverts and comments around it. Pages it can't parse are skipped.
  4. Summarise. The first 1,500 characters of each article go into a single prompt that asks for a neutral, Finshots-style summary with specific numbers.
  5. Report. The summary is printed with every source's title, date and URL, so each claim can be checked against the original article.

A real run

Here is the start of an actual saved report from 4 November 2024, for the topic HDB Finance IPO:

Topic: HDB Finance IPO
------------------------------
Summary:
HDB Financial Services, the non-banking lending unit of HDFC Bank, has filed
draft papers with the Securities and Exchange Board of India (SEBI) for its
initial public offering (IPO), valuing it at Rs 12,500 crore. The IPO will be
a combination of a fresh issue of equity shares worth Rs 2,500 crore and an
Offer-for-Sale (OFS) of up to Rs 10,000 crore by HDFC Bank, which holds a
94.6% stake in the lender. ...

Jefferies, Goldman Sachs

The summary is accurate and readable, and it pulled the key numbers (₹12,500 crore total, ₹2,500 crore fresh issue, ₹10,000 crore OFS, 94.6% stake) together from several articles. Two problems are visible too, and both are worth understanding if you build something like this.

Lesson 1: tokens are not words

The summary ends with a dangling "Jefferies, Goldman Sachs". That's not the model's choice; it was cut off. The prompt asks for "EXACTLY 200 words", but the request also sets num_predict: 200, which limits the output to 200 tokens. A token is a piece of a word, and English averages about three-quarters of a word per token, so 200 tokens runs out at roughly 150 words, in the middle of a sentence. The fix is to give the model headroom (around 350 tokens for 200 words) and trim to the last full sentence afterwards, not to an arbitrary word count.

"Exactly 200 words" is also not an instruction a language model can follow reliably. Models don't count words as they write. "About 150–200 words" gets the same result without implying a precision that isn't there.

Lesson 2: a search result is not always an article

The sources for that run included "hdb financial ipo: Latest News & Videos, Photos about hdb financial ipo", an Economic Times topic page that lists other articles rather than being one. newspaper3k still extracts some text from pages like that, so the model is partly summarising a list of headlines. Two of the five sources also came back with "Date not available", which matters for a tool whose whole point is today's news. Simple filters help a lot: skip URLs containing /topic/ or /tag/, drop articles with fewer than a couple of hundred words, and drop articles whose publish date is missing or older than a day or two.

Lesson 3: use the model's own prompt format

The prompt is wrapped in [INST] ... [/INST], which is the instruction format of Llama 2 and Mistral. Llama 3 models use a different chat template, and Ollama's /api/generate endpoint already applies the right template for the model you name. So those tags are just extra text to Llama 3.2. They don't break anything, but the cleaner approach is Ollama's /api/chat endpoint with a system message for the analyst role and a user message for the articles.

Why run the model locally?

A summary like this costs nothing to generate with a local model, so you can run it for ten topics every morning without watching an API bill. The articles never leave your machine apart from being downloaded, and you can swap Llama 3.2 for any other model Ollama supports by changing one argument. The trade-off is speed: on a laptop without a GPU, each topic takes noticeably longer than a cloud API call would.

Running it

ollama pull llama3.2
pip install newspaper3k googlesearch-python requests lxml_html_clean
python news.py

Enter topics separated by semicolons, wait for the summaries, and answer y to save the report. As with anything that summarises financial news, treat the output as a starting point for reading the sources, not as investment advice.