04-03 sample_request.py
(line by line response)

01 - Create a directory somewhere

To get the respone line by line like in chatbots, use this script

import requests
import json
url = "http://localhost:11434/api/chat"

payload = {
    "model": "llama3", # replace with name of you model
    "messages": [{"role":"user","content":"What is python?"}]
}

# Send the HTTP POST request with streaming True
response = requests.post(url,json=payload,stream=True)

# Check the response, iterate through it
if response.status_code == 200:
    print("Streaming Response from ollama:")
    for line in response.iter_lines(decode_unicode=True):
        if line:
            try:
                # Parse each line
                json_data = json.loads(line)
                # Extract and print the message 
                if "message" in json_data and "content" in json_data["message"]:
                    print(json_data["message"]["content"], end="")
            except json.JSONDecodeError:
                printf(f"\nFailed to parse line:{line}")
    print()
else:
    print(f"Error: {response.status_code}")
    print(response.text)
                    

make sure you use pip3 and install request

pip3 install requests

run this command

python3 sample_response.py

or whatever you named it