Skip to content

Commit

Permalink
Ollama Integration (#132)
Browse files Browse the repository at this point in the history
* add ollama generator

* add test skeleton

* add __inits__

* add __inits__

* add __inits__

* add pyproject

* add version

* Add instructions for building local environment

* Add integration tests using docker ollama service

* Add integration test market in pyproject.toml

* lint with black

* Delete integrations/__init__.py

* drop unused dunders

* update pyproject based on elasticsearch integration

* rename metadata to meta

* Use full /generate URL from ollama in init

* add docstrings and additional post arguments

* add github action

* change single to double quotes in tests

* fix init paths

* make timeout argument explicit in POST

* lint with black

* fix typo from jina to ollama in hatch lint

* ignore pytest and haystack typing issues

* correct type hint in output type

* add assertion for replies and meta in tests

* update labeler with ollama

* try to install and run ollama wo docker

* try to see if the issue with ports is related to concurrency

* another try

* better try to run ollama

* Update ollama.yml

* Update ollama.yml

* simplify docker-compose

* try to pull the model when the container is already running

* add -d

* Update ollama.yml

* Delete integrations/ollama/docker-compose.yml

* Update integrations/ollama/pyproject.toml

Co-authored-by: Stefano Fiorucci <[email protected]>

* Add timeout to POST Request

* Remove unpacking of dictionary arguments to pass mypy

* Add docstring to run function

* add test for init defaults, delete telemetry tests

* drop use of dataclass for housing response data

* modify type hints in protected methods

* update readme with new docker testing regime

* refactor post_args to json payload only

* try using a github service for Ollama

* modify post_args test to new json_payload

* add ollama integration to general README

* refinements

* fix tests

* rm unused fixture

---------

Co-authored-by: Stefano Fiorucci <[email protected]>
  • Loading branch information
AlistairLR112 and anakin87 authored Jan 3, 2024
1 parent 0d0c1af commit b80138e
Show file tree
Hide file tree
Showing 9 changed files with 497 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ integration:unstructured-fileconverter:
- any-glob-to-any-file: "integrations/unstructured/fileconverter/**/*"
- any-glob-to-any-file: ".github/workflows/unstructured_fileconverter.yml"

integration:ollama:
- changed-files:
- any-glob-to-any-file: "integrations/ollama/**/*"
- any-glob-to-any-file: ".github/workflows/ollama.yml"

# Topics
topic:CI:
- changed-files:
Expand Down
59 changes: 59 additions & 0 deletions .github/workflows/ollama.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# This workflow comes from https://github.com/ofek/hatch-mypyc
# https://github.com/ofek/hatch-mypyc/blob/5a198c0ba8660494d02716cfc9d79ce4adfb1442/.github/workflows/test.yml
name: Test / ollama

on:
schedule:
- cron: "0 0 * * *"
pull_request:
paths:
- "integrations/ollama/**"
- ".github/workflows/ollama.yml"

concurrency:
group: ollama-${{ github.head_ref }}
cancel-in-progress: true

env:
PYTHONUNBUFFERED: "1"
FORCE_COLOR: "1"
LLM_FOR_TESTS: "orca-mini"

jobs:
run:
name: Python ${{ matrix.python-version }} on ${{ startsWith(matrix.os, 'macos-') && 'macOS' || startsWith(matrix.os, 'windows-') && 'Windows' || 'Linux' }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
python-version: ["3.9","3.10","3.11"]
services:
ollama:
image: ollama/ollama:latest
ports:
- 11434:11434
options: --name ollama

steps:
- name: Pull the LLM in the Ollama service
run: docker exec ollama ollama pull ${{ env.LLM_FOR_TESTS }}

- uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Install Hatch
run: pip install --upgrade hatch

- name: Lint
working-directory: integrations/ollama
if: matrix.python-version == '3.9'
run: hatch run lint:all

- name: Run tests
working-directory: integrations/ollama
run: hatch run cov
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,6 @@ deepset-haystack
| [opensearch-haystack](integrations/opensearch/) | Document Store | [![PyPI - Version](https://img.shields.io/pypi/v/opensearch-haystack.svg)](https://pypi.org/project/opensearch-haystack) | [![Test / opensearch](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/opensearch.yml/badge.svg)](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/opensearch.yml) |
| [qdrant-haystack](integrations/qdrant/) | Document Store | [![PyPI - Version](https://img.shields.io/pypi/v/qdrant-haystack.svg?color=orange)](https://pypi.org/project/qdrant-haystack) | [![Test / qdrant](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/qdrant.yml/badge.svg)](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/qdrant.yml) |
| [unstructured-fileconverter-haystack](integrations/unstructured/fileconverter/) | File converter | [![PyPI - Version](https://img.shields.io/pypi/v/unstructured-fileconverter-haystack.svg)](https://pypi.org/project/unstructured-fileconverter-haystack) | [![Test / unstructured / fileconverter](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/unstructured_fileconverter.yml/badge.svg)](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/unstructured_fileconverter.yml) |
| [jina-haystack](integrations/jina/) | Embedder | [![PyPI - Version](https://img.shields.io/pypi/v/jina-haystack.svg)](https://pypi.org/project/jina-haystack) | [![Test / cohere](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/jina.yml/badge.svg)](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/jina.yml) |
| [pinecone-haystack](integrations/pinecone/) | Document Store | [![PyPI - Version](https://img.shields.io/pypi/v/pinecone-haystack.svg?color=orange)](https://pypi.org/project/pinecone-haystack) | [![Test / pinecone](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/pinecone.yml/badge.svg)](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/pinecone.yml) |
| [jina-haystack](integrations/jina/) | Embedder | [![PyPI - Version](https://img.shields.io/pypi/v/jina-haystack.svg)](https://pypi.org/project/jina-haystack) | [![Test / cohere](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/jina.yml/badge.svg)](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/jina.yml)
| [pinecone-haystack](integrations/pinecone/) | Document Store | [![PyPI - Version](https://img.shields.io/pypi/v/pinecone-haystack.svg?color=orange)](https://pypi.org/project/pinecone-haystack) | [![Test / pinecone](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/pinecone.yml/badge.svg)](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/pinecone.yml) |
| [ollama-haystack](integrations/ollama/) | Generator | [![PyPI - Version](https://img.shields.io/pypi/v/ollama-haystack.svg?color=orange)](https://pypi.org/project/ollama-haystack) | [![Test / ollama](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/ollama.yml/badge.svg)](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/ollama.yml) |
39 changes: 39 additions & 0 deletions integrations/ollama/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# ollama-haystack

[![PyPI - Version](https://img.shields.io/pypi/v/ollama-haystack.svg)](https://pypi.org/project/ollama-haystack)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/ollama-haystack.svg)](https://pypi.org/project/ollama-haystack)

-----

**Table of Contents**

- [Installation](#installation)
- [License](#license)

## Installation

```console
pip install ollama-haystack
```

## License

`ollama-haystack` is distributed under the terms of the [Apache-2.0](https://spdx.org/licenses/Apache-2.0.html) license.

## Testing

To run tests first start a Docker container running Ollama and pull a model for integration testing
It's recommended to use the smallest model possible for testing purposes - see https://ollama.ai/library for a list that Ollama supportd

```console
docker run -d -p 11434:11434 --name ollama ollama/ollama:latest
docker exec ollama ollama pull <your model here>
```

Then run tests:

```console
hatch run test
```

The default model used here is ``orca-mini``
180 changes: 180 additions & 0 deletions integrations/ollama/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
[build-system]
requires = ["hatchling", "hatch-vcs"]
build-backend = "hatchling.build"

[project]
name = "ollama-haystack"
dynamic = ["version"]
description = 'An integration between the Ollama LLM framework and Haystack'
readme = "README.md"
requires-python = ">=3.8"
license = "Apache-2.0"
keywords = []
authors = [
{ name = "Alistair Rogers", email = "[email protected]" },
{ name = "Sachin Sachdeva", email = "[email protected]" },
{ name = "deepset GmbH", email = "[email protected]" },
]
classifiers = [
"Development Status :: 4 - Beta",
"Programming Language :: Python",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
]
dependencies = ["haystack-ai", "requests"]

[project.urls]
Documentation = "https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/ollama#readme"
Issues = "https://github.com/deepset-ai/haystack-core-integrations/issues"
Source = "https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/ollama"

[tool.hatch.version]
source = "vcs"
tag-pattern = 'integrations\/ollama-v(?P<version>.*)'

[tool.hatch.version.raw-options]
root = "../.."
git_describe_command = 'git describe --tags --match="integrations/ollama-v[0-9]*"'

[tool.hatch.envs.default]
dependencies = [
"coverage[toml]>=6.5",
"pytest",
]
[tool.hatch.envs.default.scripts]
test = "pytest {args:tests}"
test-cov = "coverage run -m pytest {args:tests}"
cov-report = [
"- coverage combine",
"coverage report",
]
cov = [
"test-cov",
"cov-report",
]

[[tool.hatch.envs.all.matrix]]
python = ["3.8", "3.9", "3.10", "3.11", "3.12"]


[tool.hatch.envs.lint]
detached = true
dependencies = [
"black>=23.1.0",
"mypy>=1.0.0",
"ruff>=0.0.243",
]

[tool.hatch.envs.lint.scripts]
typing = "mypy --install-types --non-interactive {args:src/ollama_haystack tests}"
style = [
"ruff {args:.}",
"black --check --diff {args:.}",
]
fmt = [
"black {args:.}",
"ruff --fix {args:.}",
"style",
]
all = [
"style",
"typing",
]

[tool.hatch.metadata]
allow-direct-references = true

[tool.ruff.isort]
known-first-party = ["ollama_haystack"]

[tool.black]
target-version = ["py37"]
line-length = 120
skip-string-normalization = true

[tool.ruff]
target-version = "py37"
line-length = 120
select = [
"A",
"ARG",
"B",
"C",
"DTZ",
"E",
"EM",
"F",
"I",
"ICN",
"ISC",
"N",
"PLC",
"PLE",
"PLR",
"PLW",
"Q",
"RUF",
"S",
"T",
"TID",
"UP",
"W",
"YTT",
]
ignore = [
# Allow non-abstract empty methods in abstract base classes
"B027",
# Ignore checks for possible passwords
"S105", "S106", "S107",
# Ignore complexity
"C901", "PLR0911", "PLR0912", "PLR0913", "PLR0915",
]
unfixable = [
# Don't touch unused imports
"F401",
]

[tool.ruff.flake8-tidy-imports]
ban-relative-imports = "all"

[tool.ruff.per-file-ignores]
# Tests can use magic values, assertions, and relative imports
"tests/**/*" = ["PLR2004", "S101", "TID252"]


[tool.coverage.run]
source_pkgs = ["ollama_haystack", "tests"]
branch = true
parallel = true


[tool.coverage.paths]
ollama_haystack = ["src/ollama_haystack", "*/ollama-haystack/src/ollama_haystack"]
tests = ["tests", "*/ollama-haystack/tests"]

[tool.coverage.report]
exclude_lines = [
"no cov",
"if __name__ == .__main__.:",
"if TYPE_CHECKING:",
]

[tool.pytest.ini_options]
markers = [
"integration: marks tests as slow (deselect with '-m \"not integration\"')",
]
addopts = [
"--import-mode=importlib",
]

[[tool.mypy.overrides]]
module = [
"haystack.*",
"pytest.*"
]
ignore_missing_imports = true
7 changes: 7 additions & 0 deletions integrations/ollama/src/ollama_haystack/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# SPDX-FileCopyrightText: 2023-present deepset GmbH <[email protected]>
#
# SPDX-License-Identifier: Apache-2.0

from ollama_haystack.generator import OllamaGenerator

__all__ = ["OllamaGenerator"]
Loading

0 comments on commit b80138e

Please sign in to comment.