Search code examples
pythonollama

Connect to remote server with LLAMA with python and ollama library


I am trying to connect to a remote computer that is running ollama from another computer on local network. Both computers are running Windows. The remote computer is running Windows 11 Pro and local is running Windows 10 Pro. I am getting the following error:

Traceback (most recent call last):
  File "C:\Users\ps\OneDrive\Code\Python\app.py", line 13, in <module>
    response = ollama.generate(model='llama3.2b', prompt=my_prompt)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ps\OneDrive\Code\Python\llama_test\Lib\site-packages\ollama\_client.py", line 242, in generate
    return self._request(
           ^^^^^^^^^^^^^^
  File "C:\Users\ps\OneDrive\Code\Python\llama_test\Lib\site-packages\ollama\_client.py", line 178, in _request
    return cls(**self._request_raw(*args, **kwargs).json())
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ps\OneDrive\Code\Python\llama_test\Lib\site-packages\ollama\_client.py", line 124, in _request_raw
    raise ConnectionError(CONNECTION_ERROR_MESSAGE) from None
ConnectionError: Failed to connect to Ollama. Please check that Ollama is downloaded, running and accessible. https://ollama.com/download

Below is the script I am trying to run.

import ollama 

# Connect to the remote Ollama server
client = ollama.Client(host='http://192.168.1.154:11434')

note = "notes.md"

with open(note,'r') as file:
    content = file.read()

my_prompt = f'This is a person note, what is it about? {content}'

response = ollama.generate(model='llama3.2b', prompt=my_prompt)
actual_response = response['response']
print(actual_response)

# Stop ollama server
#  Get-Process -Name ollama | Stop-Process -Force

According to error, it seems like the remote computer is refusing the connection. I have tried to following:

  1. Checked the IP Address by using the ping command in Command Prompt. I get messages back from the remote computer.
  2. On the remote server, I allowed incoming messages to port 11434 via Windows Defender FireWall
  3. Checked if the Ollama server is running
  4. Updated the Ollama server

I am not sure if my code is not right, or the remote computer is not setup correctly. Any suggestions would be appreciated.

UPDATE:

  • Added outbound rule to local computer
  • Modified the OLLAMA_HOST in the windows environmental variables to 0.0.0.0

Solution

  • This works for me:

    import ollama 
    
    # Connect to the remote Ollama server
    client = ollama.Client(host='http://192.168.1.154:11434')
    
    note = "notes.md"
    
    with open(note,'r') as file:
        content = file.read()
    
    my_prompt = f'This is a person note, what is it about? {content}'
    
    # Use the client instance you created
    response = client.generate(model='llama3.2b', prompt=my_prompt)
    actual_response = response['response']
    print(actual_response)
    

    Looking at your code, I can see that you're creating a client with a custom host:

    pythonCopyclient = ollama.Client(host='http://192.168.1.154:11434')
    

    But then you're not using this client when making the request:

    pythonCopyresponse = ollama.generate(model='llama3.2b', prompt=my_prompt)
    

    Instead, you should use the client you created:

    pythonCopyresponse = client.generate(model='llama3.2b', prompt=my_prompt)
    

    When you call ollama.generate() directly, it's trying to connect to the default host (localhost), not your remote server.