From a95029ab637fd8c83fec485f3b5201009a988727 Mon Sep 17 00:00:00 2001 From: tobiadefami Date: Mon, 15 Jan 2024 00:19:36 +0100 Subject: [PATCH 1/3] install with docker --- Dockerfile | 21 +++++++++++++++++++++ docker-compose.yml | 11 +++++++++++ 2 files changed, 32 insertions(+) create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..bf26e2e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,21 @@ +# # Use the official Python 3.10 image as the base image +FROM python:3.11 + +# Set the working directory +WORKDIR /ollama-python + +# Install Poetry +RUN pip install --no-cache-dir poetry + +# Copy the project files into the working directory +COPY . . + +# Install project dependencies using Poetry +RUN poetry config virtualenvs.create false \ + && poetry install --only main + +# Install pre-commit and set up the hooks +RUN poetry run pre-commit install +# Specify the command to run your application +CMD ["sh", "-c", "while :; do sleep 10; done"] + diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..34c254d --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,11 @@ +version: '3.8' + +services: + ollama-python: + build: + context: . + dockerfile: Dockerfile + volumes: + - .:/ollama-python + stdin_open: true # Keep container running + tty: true From a8213c1f2c05745fc2bc130346632e1cbea096b4 Mon Sep 17 00:00:00 2001 From: tobiadefami Date: Tue, 16 Jan 2024 20:58:44 +0100 Subject: [PATCH 2/3] dunno if async_base_api works yett 4 --- ollama_python/endpoints/async_base_api.py | 34 +++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 ollama_python/endpoints/async_base_api.py diff --git a/ollama_python/endpoints/async_base_api.py b/ollama_python/endpoints/async_base_api.py new file mode 100644 index 0000000..21b102a --- /dev/null +++ b/ollama_python/endpoints/async_base_api.py @@ -0,0 +1,34 @@ +from ollama_python.endpoints.base import BaseAPI +import aiohttp +import json +from typing import Optional, Callable, AsyncGenerator + + +class AsyncBaseApi(BaseAPI): + def __init__(self, base_url: str = "http://localhost:11434/api"): + super().__init__(base_url) + self.session = aiohttp.ClientSession() + + async def _stream( + self, endpoint: str, parameters: dict, return_type: Optional[Callable] = None + ) -> AsyncGenerator: + """ + Stream the response from the given endpoint + :param endpoint: The endpoint to stream from + :param parameters: The parameters to send + :return: A generator that yields the response + """ + with self.session.post( + f"{self.base_url}/{endpoint}", json=parameters, stream=True + ) as session: + session.raise_for_status() + for line in session.iter_lines(): + if line: + resp = json.loads(line) + yield return_type(**resp) if return_type else resp + + async def _post(): + pass + + async def _get(): + pass From 48393c5a06ea09a66db88777f886b281f5ae7f95 Mon Sep 17 00:00:00 2001 From: tobiadefami Date: Tue, 16 Jan 2024 21:01:13 +0100 Subject: [PATCH 3/3] tests required for async_base_api --- ollama_python/endpoints/async_base_api.py | 45 +++++++++++++++++++---- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/ollama_python/endpoints/async_base_api.py b/ollama_python/endpoints/async_base_api.py index 21b102a..e4e113b 100644 --- a/ollama_python/endpoints/async_base_api.py +++ b/ollama_python/endpoints/async_base_api.py @@ -18,17 +18,46 @@ async def _stream( :param parameters: The parameters to send :return: A generator that yields the response """ - with self.session.post( - f"{self.base_url}/{endpoint}", json=parameters, stream=True + async with self.session.post( + f"{self.base_url}/{endpoint}", + json=parameters, + stream=True, + raise_for_status=True, ) as session: - session.raise_for_status() - for line in session.iter_lines(): + async for line in session.iter_lines(): if line: resp = json.loads(line) yield return_type(**resp) if return_type else resp - async def _post(): - pass + async def _post( + self, + endpoint: str, + parameters: Optional[dict] = None, + return_type: Optional[Callable] = None, + ): + """ + Send a POST request to the given endpoint + :param endpoint: + :param parameters: + :param return_type: + :return: + """ + async with self.session.post( + f"{self.base_url}/{endpoint}", json=parameters, raise_for_status=True + ) as session: + data = await session.json() + + return return_type(**data) if return_type else session.status - async def _get(): - pass + async def _get(self, endpoint: str, return_type: Optional[Callable] = None): + """ + Send a GET request to the given endpoint + :param endpoint: + :param return_type: + :return: + """ + async with self.session.get( + f"{self.base_url}/{endpoint}", raise_for_status=True + ) as session: + data = await session.json() + return return_type(**data) if return_type else session.status