Skip to content

tests required for async_base_api #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]

11 changes: 11 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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
63 changes: 63 additions & 0 deletions ollama_python/endpoints/async_base_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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
"""
async with self.session.post(
f"{self.base_url}/{endpoint}",
json=parameters,
stream=True,
raise_for_status=True,
) as session:
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(
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(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