ollama / ollama-python

Ollama Python library
https://ollama.com
MIT License
2.67k stars 220 forks source link

Model 'llama3' Not Found but from another Docker container #196

Closed bauerem closed 6 days ago

bauerem commented 6 days ago

If I make a request to the ollama-service with http://localhost:11434 then I get a correct response. If I make a request from another Docker container:

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import requests
import json
from ollama import Client
client = Client(host='http://ollama-service:11434')

app = FastAPI()

class UserInput(BaseModel):
    input: str

@app.post("/process")
def process_input(user_input: UserInput):
    model_name = "llama3"
    messages = [
        {
            'role': 'user',
            'content': user_input.input,
        },
    ]

    try:
        response = client.chat(model=model_name, messages=messages)

        result = response['message']['content']

        return {"answer": result}
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error processing the request with Ollama: {str(e)}")

I get the error: model 'llama3' not found, try pulling it first

If I run this FastAPI app outside the Docker container and replace the 'ollama-service' with 'localhost', everything works fine.

Docker compose file

 version: '3.8'

services:
  fastapi-app:
    build: ./api
    volumes:
      - .:/usr/src/app
    ports:
      - "8000:8000"
    depends_on:
      - ollama-service

  ollama-service:
    image: ollama/ollama:latest 
    environment:
      - OLLAMA_MODEL=llama3 
    ports:
      - "11434:11434"

Dockerfile

FROM python:3.10

WORKDIR /app

COPY . /app

RUN pip install --no-cache-dir -r requirements.txt

EXPOSE 8000

CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]