Skip to content

Commit 7643062

Browse files
committed
add ollama http endpoint example
1 parent 869ad4f commit 7643062

4 files changed

Lines changed: 78 additions & 0 deletions

File tree

CHANGELOG.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
[0.1.9]-2026-04-30
5+
-------------------
6+
7+
Add first Ollama example:
8+
Use Ollama's HTTP API to generate a response from a local LLM model and print the response.
9+
410
[0.1.8]-2025-10-25
511
-------------------
612

ai/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
This directory contains examples of using [Ollama](https://ollama.com) with Python. Some of you might be wondering: What is Ollama? It's a tool that allows you to use LLMs locally. Once Ollama is installed and a large language model (LLM) has been downloaded, you can use that model in the terminal. Ollama also has a simple graphical interface (similar to ChatGPT).
44

55
[This blog post](https://niftycode.eu/using-large-language-models-with-ollama-locally-on-mac/) describes how to install Ollama on a Mac. The procedure shown should be very similar under Linux and Windows.
6+
7+
The first example (ollama-api.py) demonstrates how to access Ollama's HTTP endpoint. A request is sent to a local model via this endpoint. The response is displayed in the terminal and saved to the file `generated_output.md` (Markdown format).

ai/ollama-api.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env python3
2+
3+
"""
4+
This script interacts with the Ollama API to generate text based on a given prompt.
5+
The output will be printed to the console and saved to a Markdown file named `generated_output.md`.
6+
Version: 1.1
7+
Python 3.13+
8+
Date created: April 30th, 2026
9+
Date modified: -
10+
"""
11+
12+
import json
13+
import sys
14+
15+
import requests # type: ignore
16+
from requests.exceptions import RequestException # type: ignore
17+
18+
# Endpoint of Ollama API
19+
URL = "http://localhost:11434/api/generate"
20+
21+
data = {
22+
"model": "qwen2.5-coder",
23+
"prompt": "Write a Python class called `Cameras` with the field `manufacturer`.",
24+
"max_tokens": 1000,
25+
"temperature": 0.6,
26+
"top_p": 0.9,
27+
"n": 1,
28+
"stream": False,
29+
}
30+
31+
32+
def fetch_generated_text() -> str:
33+
"""
34+
Fetch generated text from the Ollama API.
35+
"""
36+
37+
generated_text = ""
38+
39+
try:
40+
# Make a POST request to the Ollama API
41+
response: requests.Response = requests.post(URL, json=data, stream=True)
42+
except RequestException as e:
43+
print(f"Error message:\n{e}")
44+
sys.exit("No connection to Ollama API. Exit program!")
45+
46+
for line in response.iter_lines():
47+
if line:
48+
decoded_line = line.decode("utf-8")
49+
result = json.loads(decoded_line)
50+
# Get the generated text from the response
51+
generated_text = result.get("response", "")
52+
53+
return generated_text
54+
55+
56+
def main() -> None:
57+
"""Main function to execute the script."""
58+
59+
answer: str = fetch_generated_text()
60+
61+
print(answer, end="", flush=True)
62+
63+
# Save the generated text to a Markdown file
64+
with open("generated_output.md", "a", encoding="utf-8") as md_file:
65+
md_file.write(answer)
66+
67+
68+
if __name__ == "__main__":
69+
main()

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ black
44
mypy
55
pytest
66
rich
7+
requests
78
pycryptodome

0 commit comments

Comments
 (0)