Reputation: 1
On a windows 11 machine, I am trying to get a json reponse from the llama3 model on my local ollama installation on jupyter notebook but it does not work
Steps I tried:
This below snippet works
import ollama
prompt = "What is the capital of France?"
response = ollama.chat(
model="llama3",
messages=[{"role":"user","content":prompt}]
)
print(response['message']['content'])
But this one does not work:
import requests
def query_ollama(prompt: str, model: str = "llama3") -> dict:
url = "http://localhost:11434/completion" # Try this endpoint
payload = {"model": model, "prompt": prompt}
response = requests.post(url, json=payload)
# Debug output
print("Status Code:", response.status_code)
print("Raw Response:", response.text)
if response.status_code == 200:
try:
return response.json()
except ValueError as e:
print("JSON Decode Error:", e)
return {"error": "Invalid JSON response"}
else:
return {"error": f"Request failed with status code {response.status_code}"}
# Test the function
prompt = "What is the capital of France?"
response_json = query_ollama(prompt)
print(response_json)
Output is Status Code: 404 Raw Response: 404 page not found {'error': 'Request failed with status code 404'}
I tried
!taskkill /F /IM ollama.exe
!ollama serve #(which kind of hangs,maybe coz its busy serving!)
!curl http://localhost:11434/models #(gives 404 page not found)
I'm so confused, what is wrong here? TIA
Upvotes: 0
Views: 15