-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathollama.py
67 lines (58 loc) · 2.48 KB
/
ollama.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import requests
import json
def test_ollama_connection():
"""Test connection to local Ollama server with basic prompt"""
# Set headers with User-Agent and update URL to use v1 endpoint
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:121.0)',
'Content-Type': 'application/json'
}
# First check if Ollama service is running
try:
health_check = requests.get("http://127.0.0.1:11434",
headers=headers,
timeout=5)
health_check.raise_for_status()
print("Ollama service is running")
except requests.exceptions.ConnectionError:
print("\nError: Cannot connect to Ollama service")
print("Please ensure Ollama is installed and running on port 11434")
print("Try running 'OLLAMA_HOST=0.0.0.0:11434 ollama serve' in a terminal")
return
except requests.exceptions.HTTPError as e:
print(f"\nError: Ollama service returned an error - {str(e)}")
print("Please check if Ollama is properly configured")
return
except Exception as e:
print(f"\nUnexpected error connecting to Ollama: {str(e)}")
print("Please ensure Ollama is installed and running correctly")
return
# Test generate endpoint
try:
url = "http://127.0.0.1:11434/api/generate"
payload = {
"model": "llama3.2:latest",
"prompt": "hello are you there",
"stream": False # Set to False to get a single response
}
print("\nSending test prompt to Ollama...")
response = requests.post(url, headers=headers, json=payload)
response.raise_for_status()
# Process the response line by line
full_response = ""
for line in response.text.splitlines():
if line.strip(): # Skip empty lines
try:
data = json.loads(line)
if 'response' in data:
full_response += data['response']
except json.JSONDecodeError:
continue
print("\nOllama response:")
print(full_response if full_response else "No response received")
except requests.exceptions.RequestException as e:
print(f"Error making request to Ollama: {str(e)}")
except Exception as e:
print(f"Unexpected error: {str(e)}")
if __name__ == "__main__":
test_ollama_connection()