Building a Local LLM · 13/30 · API and Document Search
Full series and learning sequence · Practice code ZIP
A Python program can send the same kind of question used in the chat window. Build a small function that sends JSON to Ollama and extracts the response text. It becomes the request layer for later file summaries and diagnostic-note processing.
Use the Python virtual environment from Part 8 and the Windows Ollama installation from Part 2. The shared HTTP helper saved here will also be imported by later examples.
HTTP address, method, body
The default local address is 127.0.0.1:11434, and the conversation creation path is /api/chat. The version lookup uses GET, but the conversation puts JSON in the body of the POST request. The code below is for the Ollama native API, so it does not mix with the format in /v1/chat/completions.
Messages contains conversation records composed of role and content. If you specify stream=false, you will receive one completed JSON instead of multiple response fragments. Streaming is covered in measurement examples in Part 26 after learning the request and response structures.
Save public request function
Save the code below as llm_http.py in the local-llm-lab folder. You can also use the files in the practice ZIP. Before running, turn on the Ollama app and finish downloading the baseline model.
import json
import urllib.request
BASE = "http://127.0.0.1:11434"
MODEL = "qwen3:4b-instruct"
def post(path, payload, timeout=180):
data = json.dumps(payload, ensure_ascii=False).encode("utf-8")
req = urllib.request.Request(BASE + path, data=data,
headers={"Content-Type": "application/json; charset=utf-8"})
with urllib.request.urlopen(req, timeout=timeout) as response:
return json.load(response)
if __name__ == "__main__":
result = post("/api/chat", {
"model": MODEL,
"messages": [{"role": "user", "content": "오프사이트 백업의 목적을 두 문장으로 설명해 줘."}],
"stream": False,
"options": {"num_ctx": 4096, "num_predict": 256, "temperature": 0.2}
})
print(result["message"]["content"])
.\.venv\Scripts\python.exe .\llm_http.py
When the response sentence is output, the HTTP request sent from Python has reached Ollama and the model. The following examples use the post function from the same folder. When distributing externally, the request address is not given any value and the allowed servers are managed through settings.
Handling request failures
| Error/Symptom | check location |
|---|---|
| Connection refused | Ollama app ·11434 version API |
| 404 / no model | Request path·ollama list·model name |
| read timeout | Load time, CPU inference, and context size |
| JSON parsing failed | Whether stream=false · HTML returned by proxy |
In the practice code, you can check where the error occurred using traceback. The production app handles connection failures, HTTP errors, and JSON parsing failures separately. Retries should be limited in number and overall time.
Conversation records and log management
The API does not automatically store all previous conversations. To reference the previous content, please put the necessary utterance in messages in the next request. It is up to the application to store the conversation history and reduce it to fit the token budget.
The log first records the time, model, request identifier, status, and processing time. If you leave the entire document and response, your personal information may be saved. If you continue to use automation, you will also determine the retention period and deletion method for logs.
reference material
Documentation and model tags checked: October 18, 2026. Primary lab environment: Windows 11 x64.



