From c572e2714580ecfb046313c68d5c539a48b39e4c Mon Sep 17 00:00:00 2001 From: vasilije Date: Wed, 19 Feb 2025 15:15:59 -0800 Subject: [PATCH 01/48] Draft ollama test --- .github/workflows/test_ollama.yml | 59 +++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 .github/workflows/test_ollama.yml diff --git a/.github/workflows/test_ollama.yml b/.github/workflows/test_ollama.yml new file mode 100644 index 0000000000..8af4bbb963 --- /dev/null +++ b/.github/workflows/test_ollama.yml @@ -0,0 +1,59 @@ +name: test | ollama + +on: + schedule: + - cron: "0 0 * * *" # daily schedule + workflow_dispatch: + +jobs: + update-insights: + runs-on: ubuntu-latest + services: + ollama: + image: ollama/ollama + ports: + - 11434:11434 + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + token: ${{ secrets.GH_PAT }} # Use PAT instead of default token + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: "3.x" + + - name: Cache pip packages + uses: actions/cache@v4 + with: + path: ~/.cache/pip + key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }} + restore-keys: | + ${{ runner.os }}-pip- + + - name: Install dependencies + run: pip install -r requirements.txt + + - name: Wait for Ollama to be ready + run: | + for i in {1..30}; do + curl -s http://localhost:11434/api/tags && break + sleep 2 + done + + - name: Pull Model + run: | + curl -X POST http://localhost:11434/api/pull -d '{"name": "mistral"}' + + + run_simple_example_test: + uses: ./.github/workflows/reusable_python_example.yml + with: + example-location: ./examples/python/simple_example.py + secrets: + LLM_API_KEY: ${{ secrets.OPENAI_API_KEY }} + OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} + GRAPHISTRY_USERNAME: ${{ secrets.GRAPHISTRY_USERNAME }} + GRAPHISTRY_PASSWORD: ${{ secrets.GRAPHISTRY_PASSWORD }} \ No newline at end of file From 2de364b93731f9074af118c6afa114270f678c46 Mon Sep 17 00:00:00 2001 From: vasilije Date: Wed, 19 Feb 2025 18:02:33 -0800 Subject: [PATCH 02/48] Ollama test end to end --- .github/workflows/test_ollama.yml | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/.github/workflows/test_ollama.yml b/.github/workflows/test_ollama.yml index 8af4bbb963..2473de3c2d 100644 --- a/.github/workflows/test_ollama.yml +++ b/.github/workflows/test_ollama.yml @@ -1,9 +1,9 @@ name: test | ollama on: - schedule: - - cron: "0 0 * * *" # daily schedule workflow_dispatch: + pull_request: + types: [ labeled, synchronize ] jobs: update-insights: @@ -45,7 +45,10 @@ jobs: - name: Pull Model run: | - curl -X POST http://localhost:11434/api/pull -d '{"name": "mistral"}' + curl -X POST http://localhost:11434/api/pull -d '{"name": "ollama/llama3.2"}' + - name: Pull Embedding Model + run: | + curl -X POST http://localhost:11434/api/pull -d '{"name": "avr/sfr-embedding-mistral:latest"}' run_simple_example_test: @@ -53,7 +56,15 @@ jobs: with: example-location: ./examples/python/simple_example.py secrets: - LLM_API_KEY: ${{ secrets.OPENAI_API_KEY }} + LLM_API_KEY: "ollama" OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} GRAPHISTRY_USERNAME: ${{ secrets.GRAPHISTRY_USERNAME }} - GRAPHISTRY_PASSWORD: ${{ secrets.GRAPHISTRY_PASSWORD }} \ No newline at end of file + GRAPHISTRY_PASSWORD: ${{ secrets.GRAPHISTRY_PASSWORD }} + LLM_PROVIDER: "ollama" + LLM_ENDPOINT: "http://localhost:11434" + LLM_MODEL: "ollama/llama3.2" + EMBEDDING_PROVIDER: "ollama" + EMBEDDING_MODEL: "avr/sfr-embedding-mistral:latest" + EMBEDDING_ENDPOINT: "http://localhost:11434/api/embeddings" + EMBEDDING_DIMENSIONS: 4096 + HUGGINGFACE_TOKENIZER: "Salesforce/SFR-Embedding-Mistral" \ No newline at end of file From 8ebd9a7d8a0db78b3058c5a24c76c67cffcfa7fb Mon Sep 17 00:00:00 2001 From: vasilije Date: Wed, 19 Feb 2025 18:10:49 -0800 Subject: [PATCH 03/48] Ollama test end to end --- .github/workflows/test_ollama.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test_ollama.yml b/.github/workflows/test_ollama.yml index 2473de3c2d..0ff0517b42 100644 --- a/.github/workflows/test_ollama.yml +++ b/.github/workflows/test_ollama.yml @@ -56,10 +56,10 @@ jobs: with: example-location: ./examples/python/simple_example.py secrets: - LLM_API_KEY: "ollama" OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} GRAPHISTRY_USERNAME: ${{ secrets.GRAPHISTRY_USERNAME }} GRAPHISTRY_PASSWORD: ${{ secrets.GRAPHISTRY_PASSWORD }} + LLM_API_KEY: "ollama" LLM_PROVIDER: "ollama" LLM_ENDPOINT: "http://localhost:11434" LLM_MODEL: "ollama/llama3.2" From 9d0d96eb1cd5a9262c9605b35491b32d00acc358 Mon Sep 17 00:00:00 2001 From: vasilije Date: Fri, 21 Feb 2025 13:43:04 -0800 Subject: [PATCH 04/48] Fix ollama --- .github/workflows/test_ollama.yml | 83 ++++++++++++++++++++++--------- 1 file changed, 59 insertions(+), 24 deletions(-) diff --git a/.github/workflows/test_ollama.yml b/.github/workflows/test_ollama.yml index 0ff0517b42..d85de95ee6 100644 --- a/.github/workflows/test_ollama.yml +++ b/.github/workflows/test_ollama.yml @@ -6,7 +6,7 @@ on: types: [ labeled, synchronize ] jobs: - update-insights: + setup-ollama: runs-on: ubuntu-latest services: ollama: @@ -17,8 +17,6 @@ jobs: steps: - name: Checkout repository uses: actions/checkout@v4 - with: - token: ${{ secrets.GH_PAT }} # Use PAT instead of default token - name: Set up Python uses: actions/setup-python@v4 @@ -39,32 +37,69 @@ jobs: - name: Wait for Ollama to be ready run: | for i in {1..30}; do - curl -s http://localhost:11434/api/tags && break + if curl -s http://localhost:11434/api/tags > /dev/null; then + echo "Ollama is ready" + exit 0 + fi + echo "Waiting for Ollama... attempt $i" sleep 2 done + echo "Ollama failed to start" + exit 1 - - name: Pull Model + - name: Pull Models run: | curl -X POST http://localhost:11434/api/pull -d '{"name": "ollama/llama3.2"}' - - name: Pull Embedding Model - run: | curl -X POST http://localhost:11434/api/pull -d '{"name": "avr/sfr-embedding-mistral:latest"}' - run_simple_example_test: - uses: ./.github/workflows/reusable_python_example.yml - with: - example-location: ./examples/python/simple_example.py - secrets: - OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} - GRAPHISTRY_USERNAME: ${{ secrets.GRAPHISTRY_USERNAME }} - GRAPHISTRY_PASSWORD: ${{ secrets.GRAPHISTRY_PASSWORD }} - LLM_API_KEY: "ollama" - LLM_PROVIDER: "ollama" - LLM_ENDPOINT: "http://localhost:11434" - LLM_MODEL: "ollama/llama3.2" - EMBEDDING_PROVIDER: "ollama" - EMBEDDING_MODEL: "avr/sfr-embedding-mistral:latest" - EMBEDDING_ENDPOINT: "http://localhost:11434/api/embeddings" - EMBEDDING_DIMENSIONS: 4096 - HUGGINGFACE_TOKENIZER: "Salesforce/SFR-Embedding-Mistral" \ No newline at end of file + needs: setup-ollama + runs-on: ubuntu-latest + services: + ollama: + image: ollama/ollama + ports: + - 11434:11434 + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: "3.x" + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + + - name: Wait for Ollama to be ready + run: | + for i in {1..30}; do + if curl -s http://localhost:11434/api/tags > /dev/null; then + echo "Ollama is ready" + exit 0 + fi + echo "Waiting for Ollama... attempt $i" + sleep 2 + done + echo "Ollama failed to start" + exit 1 + + - name: Run example test + env: + OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} + GRAPHISTRY_USERNAME: ${{ secrets.GRAPHISTRY_USERNAME }} + GRAPHISTRY_PASSWORD: ${{ secrets.GRAPHISTRY_PASSWORD }} + LLM_API_KEY: "ollama" + LLM_PROVIDER: "ollama" + LLM_ENDPOINT: "http://localhost:11434" + LLM_MODEL: "ollama/llama3.2" + EMBEDDING_PROVIDER: "ollama" + EMBEDDING_MODEL: "avr/sfr-embedding-mistral:latest" + EMBEDDING_ENDPOINT: "http://localhost:11434/api/embeddings" + EMBEDDING_DIMENSIONS: "4096" + HUGGINGFACE_TOKENIZER: "Salesforce/SFR-Embedding-Mistral" + run: python ./examples/python/simple_example.py \ No newline at end of file From b4088bec29d8a1881f84f73ffcabac0c88cec056 Mon Sep 17 00:00:00 2001 From: vasilije Date: Fri, 21 Feb 2025 13:51:30 -0800 Subject: [PATCH 05/48] Fix ollama --- .github/workflows/upgrade_deps.yml | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/.github/workflows/upgrade_deps.yml b/.github/workflows/upgrade_deps.yml index 934a3ed6a7..327ee08cc7 100644 --- a/.github/workflows/upgrade_deps.yml +++ b/.github/workflows/upgrade_deps.yml @@ -2,8 +2,29 @@ name: Update Poetry Dependencies on: schedule: - - cron: '0 3 * * 0' + - cron: '0 3 * * 0' # Runs at 3 AM every Sunday + push: + paths: + - 'poetry.lock' + - 'pyproject.toml' + branches: + - main + - dev + pull_request: + paths: + - 'poetry.lock' + - 'pyproject.toml' + types: [opened, synchronize, reopened] + branches: + - main + - dev workflow_dispatch: + inputs: + debug_enabled: + type: boolean + description: 'Run the update with debug logging' + required: false + default: false jobs: update-dependencies: From b670697b8bbbef2bbebe1acbdea4dd0239c6f90a Mon Sep 17 00:00:00 2001 From: vasilije Date: Fri, 21 Feb 2025 13:55:06 -0800 Subject: [PATCH 06/48] Fix ollama --- .github/workflows/test_ollama.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/test_ollama.yml b/.github/workflows/test_ollama.yml index d85de95ee6..d6e9d41755 100644 --- a/.github/workflows/test_ollama.yml +++ b/.github/workflows/test_ollama.yml @@ -74,6 +74,7 @@ jobs: run: | python -m pip install --upgrade pip pip install -r requirements.txt + pip install ollama-python requests - name: Wait for Ollama to be ready run: | From bfe039d2aaa103645cfd81e86ff9b63c4b36221a Mon Sep 17 00:00:00 2001 From: vasilije Date: Fri, 21 Feb 2025 13:59:56 -0800 Subject: [PATCH 07/48] Fix ollama --- .github/workflows/test_ollama.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/test_ollama.yml b/.github/workflows/test_ollama.yml index d6e9d41755..1ac8377e58 100644 --- a/.github/workflows/test_ollama.yml +++ b/.github/workflows/test_ollama.yml @@ -73,7 +73,6 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - pip install -r requirements.txt pip install ollama-python requests - name: Wait for Ollama to be ready From 6bc4f6ae24a6fb65dfe44ccdd0c1735df7510d9a Mon Sep 17 00:00:00 2001 From: vasilije Date: Fri, 21 Feb 2025 14:05:27 -0800 Subject: [PATCH 08/48] Fix ollama --- .github/workflows/test_ollama.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test_ollama.yml b/.github/workflows/test_ollama.yml index 1ac8377e58..939d10ce0b 100644 --- a/.github/workflows/test_ollama.yml +++ b/.github/workflows/test_ollama.yml @@ -32,7 +32,9 @@ jobs: ${{ runner.os }}-pip- - name: Install dependencies - run: pip install -r requirements.txt + run: | + python -m pip install --upgrade pip + pip install ollama-python requests - name: Wait for Ollama to be ready run: | From 96adcfbe4e74e2c38108c9406a08e7e784bef53c Mon Sep 17 00:00:00 2001 From: vasilije Date: Fri, 21 Feb 2025 14:16:10 -0800 Subject: [PATCH 09/48] Fix ollama --- .github/workflows/test_ollama.yml | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test_ollama.yml b/.github/workflows/test_ollama.yml index 939d10ce0b..5e60a7b8fb 100644 --- a/.github/workflows/test_ollama.yml +++ b/.github/workflows/test_ollama.yml @@ -67,10 +67,21 @@ jobs: - name: Checkout repository uses: actions/checkout@v4 - - name: Set up Python - uses: actions/setup-python@v4 + - name: Setup Python + uses: actions/setup-python@v5 with: - python-version: "3.x" + python-version: '3.12.x' + + - name: Install Poetry + uses: snok/install-poetry@v1.4.1 + with: + virtualenvs-create: true + virtualenvs-in-project: true + installer-parallel: true + + - name: Install dependencies + run: | + poetry install --no-interaction --all-extras - name: Install dependencies run: | From c06c28d7a63ce8c8fbe50eb7a92bad7e000fd901 Mon Sep 17 00:00:00 2001 From: vasilije Date: Fri, 21 Feb 2025 14:27:35 -0800 Subject: [PATCH 10/48] Fix ollama --- .github/workflows/test_ollama.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test_ollama.yml b/.github/workflows/test_ollama.yml index 5e60a7b8fb..71cedf94da 100644 --- a/.github/workflows/test_ollama.yml +++ b/.github/workflows/test_ollama.yml @@ -115,4 +115,4 @@ jobs: EMBEDDING_ENDPOINT: "http://localhost:11434/api/embeddings" EMBEDDING_DIMENSIONS: "4096" HUGGINGFACE_TOKENIZER: "Salesforce/SFR-Embedding-Mistral" - run: python ./examples/python/simple_example.py \ No newline at end of file + run: poetry run python ./examples/python/simple_example.py \ No newline at end of file From edd681f7de2beee66ac64df6e5417eef0b18121b Mon Sep 17 00:00:00 2001 From: vasilije Date: Fri, 21 Feb 2025 14:46:58 -0800 Subject: [PATCH 11/48] Fix ollama --- .github/workflows/test_ollama.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/test_ollama.yml b/.github/workflows/test_ollama.yml index 71cedf94da..908a9eb894 100644 --- a/.github/workflows/test_ollama.yml +++ b/.github/workflows/test_ollama.yml @@ -101,6 +101,12 @@ jobs: echo "Ollama failed to start" exit 1 + - name: Dump Docker logs + run: | + docker ps + docker logs $(docker ps --filter "ancestor=ollama/ollama" --format "{{.ID}}") + + - name: Run example test env: OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} From 02b0109d0ed9e3832cf132546e89185d485c7ae1 Mon Sep 17 00:00:00 2001 From: vasilije Date: Fri, 21 Feb 2025 15:36:04 -0800 Subject: [PATCH 12/48] Fix ollama --- .github/workflows/test_ollama.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test_ollama.yml b/.github/workflows/test_ollama.yml index 908a9eb894..5fabbb59b9 100644 --- a/.github/workflows/test_ollama.yml +++ b/.github/workflows/test_ollama.yml @@ -86,7 +86,8 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - pip install ollama-python requests + poetry install ollama-python requests + poetry install transformers - name: Wait for Ollama to be ready run: | From a91e83eafaac717509402cd62515080718138b77 Mon Sep 17 00:00:00 2001 From: vasilije Date: Fri, 21 Feb 2025 15:48:38 -0800 Subject: [PATCH 13/48] Fix ollama --- .github/workflows/test_ollama.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test_ollama.yml b/.github/workflows/test_ollama.yml index 5fabbb59b9..6456f711eb 100644 --- a/.github/workflows/test_ollama.yml +++ b/.github/workflows/test_ollama.yml @@ -85,7 +85,6 @@ jobs: - name: Install dependencies run: | - python -m pip install --upgrade pip poetry install ollama-python requests poetry install transformers @@ -113,6 +112,7 @@ jobs: OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} GRAPHISTRY_USERNAME: ${{ secrets.GRAPHISTRY_USERNAME }} GRAPHISTRY_PASSWORD: ${{ secrets.GRAPHISTRY_PASSWORD }} + PYTHONFAULTHANDLER: 1 LLM_API_KEY: "ollama" LLM_PROVIDER: "ollama" LLM_ENDPOINT: "http://localhost:11434" From 326c41843f281d68f68f6b4277c32fb6c09b3517 Mon Sep 17 00:00:00 2001 From: vasilije Date: Fri, 21 Feb 2025 15:56:30 -0800 Subject: [PATCH 14/48] Fix ollama --- .github/workflows/test_ollama.yml | 12 +--- cognee/infrastructure/llm/ollama/adapter.py | 65 ++++++++++++++++++++- 2 files changed, 64 insertions(+), 13 deletions(-) diff --git a/.github/workflows/test_ollama.yml b/.github/workflows/test_ollama.yml index 6456f711eb..b66de43861 100644 --- a/.github/workflows/test_ollama.yml +++ b/.github/workflows/test_ollama.yml @@ -21,20 +21,12 @@ jobs: - name: Set up Python uses: actions/setup-python@v4 with: - python-version: "3.x" - - - name: Cache pip packages - uses: actions/cache@v4 - with: - path: ~/.cache/pip - key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }} - restore-keys: | - ${{ runner.os }}-pip- + python-version: "3.12.x" - name: Install dependencies run: | python -m pip install --upgrade pip - pip install ollama-python requests + pip install requests - name: Wait for Ollama to be ready run: | diff --git a/cognee/infrastructure/llm/ollama/adapter.py b/cognee/infrastructure/llm/ollama/adapter.py index 4eb3927394..ed0ef31bde 100644 --- a/cognee/infrastructure/llm/ollama/adapter.py +++ b/cognee/infrastructure/llm/ollama/adapter.py @@ -1,20 +1,28 @@ +from sys import api_version from typing import Type from pydantic import BaseModel import instructor from cognee.infrastructure.llm.llm_interface import LLMInterface from cognee.infrastructure.llm.config import get_llm_config from openai import OpenAI - +import base64 +from pathlib import Path +import os class OllamaAPIAdapter(LLMInterface): - """Adapter for a Generic API LLM provider using instructor with an OpenAI backend.""" + """Adapter for a Ollama API LLM provider using instructor with an OpenAI backend.""" + + api_version: str + + MAX_RETRIES = 5 - def __init__(self, endpoint: str, api_key: str, model: str, name: str, max_tokens: int): + def __init__(self, endpoint: str, api_key: str, model: str, name: str, max_tokens: int, api_version: str = None) -> None: self.name = name self.model = model self.api_key = api_key self.endpoint = endpoint self.max_tokens = max_tokens + self.api_version= api_version self.aclient = instructor.from_openai( OpenAI(base_url=self.endpoint, api_key=self.api_key), mode=instructor.Mode.JSON @@ -42,3 +50,54 @@ async def acreate_structured_output( ) return response + + + def create_transcript(self, input): + """Generate a audio transcript from a user query.""" + + if not os.path.isfile(input): + raise FileNotFoundError(f"The file {input} does not exist.") + + # with open(input, 'rb') as audio_file: + # audio_data = audio_file.read() + + transcription = self.aclient.transcription( + model=self.transcription_model, + file=Path(input), + api_key=self.api_key, + api_base=self.endpoint, + api_version=self.api_version, + max_retries=self.MAX_RETRIES, + ) + + return transcription + + def transcribe_image(self, input) -> BaseModel: + with open(input, "rb") as image_file: + encoded_image = base64.b64encode(image_file.read()).decode("utf-8") + + return self.aclient.completion( + model=self.model, + messages=[ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "What’s in this image?", + }, + { + "type": "image_url", + "image_url": { + "url": f"data:image/jpeg;base64,{encoded_image}", + }, + }, + ], + } + ], + api_key=self.api_key, + api_base=self.endpoint, + api_version=self.api_version, + max_tokens=300, + max_retries=self.MAX_RETRIES, + ) \ No newline at end of file From 92602aa34f8fa53bb5d089eaab4d82ede46bdff1 Mon Sep 17 00:00:00 2001 From: vasilije Date: Fri, 21 Feb 2025 16:02:08 -0800 Subject: [PATCH 15/48] Fix ollama --- .github/workflows/test_ollama.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test_ollama.yml b/.github/workflows/test_ollama.yml index b66de43861..bd7da456f7 100644 --- a/.github/workflows/test_ollama.yml +++ b/.github/workflows/test_ollama.yml @@ -77,7 +77,7 @@ jobs: - name: Install dependencies run: | - poetry install ollama-python requests + poetry install requests poetry install transformers - name: Wait for Ollama to be ready From f2d09090e1d96c71bb266fabca00da91949d3545 Mon Sep 17 00:00:00 2001 From: vasilije Date: Fri, 21 Feb 2025 16:06:05 -0800 Subject: [PATCH 16/48] Fix ollama --- .github/workflows/test_ollama.yml | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/.github/workflows/test_ollama.yml b/.github/workflows/test_ollama.yml index bd7da456f7..222f8cb70c 100644 --- a/.github/workflows/test_ollama.yml +++ b/.github/workflows/test_ollama.yml @@ -75,11 +75,6 @@ jobs: run: | poetry install --no-interaction --all-extras - - name: Install dependencies - run: | - poetry install requests - poetry install transformers - - name: Wait for Ollama to be ready run: | for i in {1..30}; do @@ -107,7 +102,7 @@ jobs: PYTHONFAULTHANDLER: 1 LLM_API_KEY: "ollama" LLM_PROVIDER: "ollama" - LLM_ENDPOINT: "http://localhost:11434" + LLM_ENDPOINT: "http://localhost:11434/api/generate" LLM_MODEL: "ollama/llama3.2" EMBEDDING_PROVIDER: "ollama" EMBEDDING_MODEL: "avr/sfr-embedding-mistral:latest" From 97465f168aec3189022c57419ccf83653d2ac7a7 Mon Sep 17 00:00:00 2001 From: vasilije Date: Fri, 21 Feb 2025 16:06:56 -0800 Subject: [PATCH 17/48] Fix ollama --- .github/workflows/test_ollama.yml | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test_ollama.yml b/.github/workflows/test_ollama.yml index 222f8cb70c..e18b4dee60 100644 --- a/.github/workflows/test_ollama.yml +++ b/.github/workflows/test_ollama.yml @@ -19,14 +19,20 @@ jobs: uses: actions/checkout@v4 - name: Set up Python - uses: actions/setup-python@v4 + uses: actions/setup-python@v5 with: python-version: "3.12.x" + - name: Install Poetry + uses: snok/install-poetry@v1.4.1 + with: + virtualenvs-create: true + virtualenvs-in-project: true + installer-parallel: true + - name: Install dependencies run: | - python -m pip install --upgrade pip - pip install requests + poetry install --no-interaction --all-extras - name: Wait for Ollama to be ready run: | From 73662b8b01d64f5d54b2f1545e51eb0ee59925da Mon Sep 17 00:00:00 2001 From: vasilije Date: Fri, 21 Feb 2025 16:12:20 -0800 Subject: [PATCH 18/48] Fix ollama --- .github/workflows/test_ollama.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test_ollama.yml b/.github/workflows/test_ollama.yml index e18b4dee60..bddf708dc4 100644 --- a/.github/workflows/test_ollama.yml +++ b/.github/workflows/test_ollama.yml @@ -108,7 +108,7 @@ jobs: PYTHONFAULTHANDLER: 1 LLM_API_KEY: "ollama" LLM_PROVIDER: "ollama" - LLM_ENDPOINT: "http://localhost:11434/api/generate" + LLM_ENDPOINT: "http://127.0.0.1:11434" LLM_MODEL: "ollama/llama3.2" EMBEDDING_PROVIDER: "ollama" EMBEDDING_MODEL: "avr/sfr-embedding-mistral:latest" From 90d96aa6ad8129cf415d27bec87d7e4bd4b94303 Mon Sep 17 00:00:00 2001 From: vasilije Date: Fri, 21 Feb 2025 16:17:50 -0800 Subject: [PATCH 19/48] Fix ollama --- .github/workflows/test_ollama.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test_ollama.yml b/.github/workflows/test_ollama.yml index bddf708dc4..edfd289104 100644 --- a/.github/workflows/test_ollama.yml +++ b/.github/workflows/test_ollama.yml @@ -49,7 +49,7 @@ jobs: - name: Pull Models run: | - curl -X POST http://localhost:11434/api/pull -d '{"name": "ollama/llama3.2"}' + curl -X POST http://localhost:11434/api/pull -d '{"name": "llama3.2"}' curl -X POST http://localhost:11434/api/pull -d '{"name": "avr/sfr-embedding-mistral:latest"}' run_simple_example_test: From 3a88b94d0185424c007fad794475c92d65e7e640 Mon Sep 17 00:00:00 2001 From: vasilije Date: Fri, 21 Feb 2025 16:25:08 -0800 Subject: [PATCH 20/48] Fix ollama --- .github/workflows/test_ollama.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test_ollama.yml b/.github/workflows/test_ollama.yml index edfd289104..35e06311b7 100644 --- a/.github/workflows/test_ollama.yml +++ b/.github/workflows/test_ollama.yml @@ -112,7 +112,7 @@ jobs: LLM_MODEL: "ollama/llama3.2" EMBEDDING_PROVIDER: "ollama" EMBEDDING_MODEL: "avr/sfr-embedding-mistral:latest" - EMBEDDING_ENDPOINT: "http://localhost:11434/api/embeddings" + EMBEDDING_ENDPOINT: "http://127.0.0.1:11434/api/embeddings" EMBEDDING_DIMENSIONS: "4096" HUGGINGFACE_TOKENIZER: "Salesforce/SFR-Embedding-Mistral" run: poetry run python ./examples/python/simple_example.py \ No newline at end of file From 11442df1aaed717e74da5bccbe3908a78501b1e8 Mon Sep 17 00:00:00 2001 From: vasilije Date: Fri, 21 Feb 2025 16:41:42 -0800 Subject: [PATCH 21/48] Fix ollama --- .github/workflows/test_ollama.yml | 50 ++++++++----------------------- 1 file changed, 12 insertions(+), 38 deletions(-) diff --git a/.github/workflows/test_ollama.yml b/.github/workflows/test_ollama.yml index 35e06311b7..5a1af17078 100644 --- a/.github/workflows/test_ollama.yml +++ b/.github/workflows/test_ollama.yml @@ -8,44 +8,7 @@ on: jobs: setup-ollama: runs-on: ubuntu-latest - services: - ollama: - image: ollama/ollama - ports: - - 11434:11434 - - steps: - - name: Checkout repository - uses: actions/checkout@v4 - - - name: Set up Python - uses: actions/setup-python@v5 - with: - python-version: "3.12.x" - - - name: Install Poetry - uses: snok/install-poetry@v1.4.1 - with: - virtualenvs-create: true - virtualenvs-in-project: true - installer-parallel: true - - name: Install dependencies - run: | - poetry install --no-interaction --all-extras - - - name: Wait for Ollama to be ready - run: | - for i in {1..30}; do - if curl -s http://localhost:11434/api/tags > /dev/null; then - echo "Ollama is ready" - exit 0 - fi - echo "Waiting for Ollama... attempt $i" - sleep 2 - done - echo "Ollama failed to start" - exit 1 - name: Pull Models run: | @@ -53,7 +16,7 @@ jobs: curl -X POST http://localhost:11434/api/pull -d '{"name": "avr/sfr-embedding-mistral:latest"}' run_simple_example_test: - needs: setup-ollama + runs-on: ubuntu-latest services: ollama: @@ -80,6 +43,17 @@ jobs: - name: Install dependencies run: | poetry install --no-interaction --all-extras + steps: + - name: Install ollama + run: curl -fsSL https://ollama.com/install.sh | sh + - name: Run ollama + run: | + ollama serve & + ollama pull llama3.2 & + ollama pull avr/sfr-embedding-mistral:latest + - name: Call ollama API + run: | + curl -d '{"model": "llama3.2", "stream": false, "prompt":"Whatever I say, asnwer with Yes"}' http://localhost:11434/api/generate - name: Wait for Ollama to be ready run: | From 1dfb0dd58b7dd5e8ec6a8dbd268e006a342b0b80 Mon Sep 17 00:00:00 2001 From: vasilije Date: Fri, 21 Feb 2025 16:42:09 -0800 Subject: [PATCH 22/48] Fix ollama --- .github/workflows/test_ollama.yml | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/.github/workflows/test_ollama.yml b/.github/workflows/test_ollama.yml index 5a1af17078..15e1b3bdf0 100644 --- a/.github/workflows/test_ollama.yml +++ b/.github/workflows/test_ollama.yml @@ -6,14 +6,6 @@ on: types: [ labeled, synchronize ] jobs: - setup-ollama: - runs-on: ubuntu-latest - - - - name: Pull Models - run: | - curl -X POST http://localhost:11434/api/pull -d '{"name": "llama3.2"}' - curl -X POST http://localhost:11434/api/pull -d '{"name": "avr/sfr-embedding-mistral:latest"}' run_simple_example_test: @@ -43,7 +35,7 @@ jobs: - name: Install dependencies run: | poetry install --no-interaction --all-extras - steps: + - name: Install ollama run: curl -fsSL https://ollama.com/install.sh | sh - name: Run ollama From 4c4723b7e0e3d4a00c4ba8c2f3ca547106e4e2a9 Mon Sep 17 00:00:00 2001 From: vasilije Date: Fri, 21 Feb 2025 16:55:41 -0800 Subject: [PATCH 23/48] Fix ollama --- .github/workflows/test_ollama.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test_ollama.yml b/.github/workflows/test_ollama.yml index 15e1b3bdf0..976220d74e 100644 --- a/.github/workflows/test_ollama.yml +++ b/.github/workflows/test_ollama.yml @@ -74,7 +74,7 @@ jobs: PYTHONFAULTHANDLER: 1 LLM_API_KEY: "ollama" LLM_PROVIDER: "ollama" - LLM_ENDPOINT: "http://127.0.0.1:11434" + LLM_ENDPOINT: "http://127.0.0.1:11434/v1/chat/completions" LLM_MODEL: "ollama/llama3.2" EMBEDDING_PROVIDER: "ollama" EMBEDDING_MODEL: "avr/sfr-embedding-mistral:latest" From 846c45e54dac5bb3daa23df083f265454d967356 Mon Sep 17 00:00:00 2001 From: vasilije Date: Fri, 21 Feb 2025 17:18:05 -0800 Subject: [PATCH 24/48] Fix ollama --- .github/workflows/test_ollama.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test_ollama.yml b/.github/workflows/test_ollama.yml index 976220d74e..f0312a81e6 100644 --- a/.github/workflows/test_ollama.yml +++ b/.github/workflows/test_ollama.yml @@ -74,7 +74,7 @@ jobs: PYTHONFAULTHANDLER: 1 LLM_API_KEY: "ollama" LLM_PROVIDER: "ollama" - LLM_ENDPOINT: "http://127.0.0.1:11434/v1/chat/completions" + LLM_ENDPOINT: "http://127.0.0.1:11434/api/generate" LLM_MODEL: "ollama/llama3.2" EMBEDDING_PROVIDER: "ollama" EMBEDDING_MODEL: "avr/sfr-embedding-mistral:latest" From 2c0bfc87a6d15d324527a9a3285d2254907ad0ba Mon Sep 17 00:00:00 2001 From: vasilije Date: Fri, 21 Feb 2025 17:45:29 -0800 Subject: [PATCH 25/48] Fix ollama --- .github/workflows/test_gemini.yml | 33 +++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 .github/workflows/test_gemini.yml diff --git a/.github/workflows/test_gemini.yml b/.github/workflows/test_gemini.yml new file mode 100644 index 0000000000..e5bf0520f3 --- /dev/null +++ b/.github/workflows/test_gemini.yml @@ -0,0 +1,33 @@ +name: test | gemini + +on: + workflow_dispatch: + pull_request: + types: [labeled, synchronize] + + +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + +jobs: + run_simple_example_test: + uses: ./.github/workflows/reusable_python_example.yml + with: + example-location: ./examples/python/simple_example.py + secrets: + OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} + GRAPHISTRY_USERNAME: ${{ secrets.GRAPHISTRY_USERNAME }} + GRAPHISTRY_PASSWORD: ${{ secrets.GRAPHISTRY_PASSWORD }} + EMBEDDING_PROVIDER: "gemini" + EMBEDDING_API_KEY: ${{ secrets.GEMINI_API_KEY }} + EMBEDDING_MODEL: "gemini/text-embedding-004" + EMBEDDING_ENDPOINT: "https://generativelanguage.googleapis.com/v1beta/models/text-embedding-004" + EMBEDDING_API_VERSION: "v1beta" + EMBEDDING_DIMENSIONS: 768 + EMBEDDING_MAX_TOKENS: 8076 + LLM_PROVIDER: "gemini" + LLM_API_KEY: ${{ secrets.GEMINI_API_KEY }} + LLM_MODEL: "gemini/gemini-1.5-flash" + LLM_ENDPOINT: "https://generativelanguage.googleapis.com/" + LLM_API_VERSION: "v1beta" From 5c7b4a5b1e1eea8f314ef4b5c5d74f504960c9c6 Mon Sep 17 00:00:00 2001 From: Daniel Molnar Date: Tue, 25 Feb 2025 12:21:44 +0100 Subject: [PATCH 26/48] Ruff it. --- cognee/infrastructure/llm/ollama/adapter.py | 84 +++++++++------------ 1 file changed, 37 insertions(+), 47 deletions(-) diff --git a/cognee/infrastructure/llm/ollama/adapter.py b/cognee/infrastructure/llm/ollama/adapter.py index ed0ef31bde..d14c8a57f6 100644 --- a/cognee/infrastructure/llm/ollama/adapter.py +++ b/cognee/infrastructure/llm/ollama/adapter.py @@ -1,28 +1,33 @@ -from sys import api_version from typing import Type from pydantic import BaseModel import instructor from cognee.infrastructure.llm.llm_interface import LLMInterface -from cognee.infrastructure.llm.config import get_llm_config from openai import OpenAI import base64 from pathlib import Path import os -class OllamaAPIAdapter(LLMInterface): - """Adapter for a Ollama API LLM provider using instructor with an OpenAI backend.""" - api_version: str +class OllamaAPIAdapter(LLMInterface): + """Adapter for an Ollama API LLM provider using instructor with an OpenAI backend.""" MAX_RETRIES = 5 - def __init__(self, endpoint: str, api_key: str, model: str, name: str, max_tokens: int, api_version: str = None) -> None: + def __init__( + self, + endpoint: str, + api_key: str, + model: str, + name: str, + max_tokens: int, + api_version: str = None, + ) -> None: self.name = name self.model = model self.api_key = api_key self.endpoint = endpoint self.max_tokens = max_tokens - self.api_version= api_version + self.api_version = api_version self.aclient = instructor.from_openai( OpenAI(base_url=self.endpoint, api_key=self.api_key), mode=instructor.Mode.JSON @@ -33,71 +38,56 @@ async def acreate_structured_output( ) -> BaseModel: """Generate a structured output from the LLM using the provided text and system prompt.""" - response = self.aclient.chat.completions.create( + response = await self.aclient.chat.completions.create( model=self.model, messages=[ - { - "role": "user", - "content": f"Use the given format to extract information from the following input: {text_input}", - }, - { - "role": "system", - "content": system_prompt, - }, + {"role": "system", "content": system_prompt}, + {"role": "user", "content": text_input}, ], - max_retries=5, - response_model=response_model, + max_tokens=self.max_tokens, ) - return response + return response_model.parse_raw(response.choices[0].message.content) - - def create_transcript(self, input): - """Generate a audio transcript from a user query.""" + def create_transcript(self, input: str): + """Generate an audio transcript from a user query.""" if not os.path.isfile(input): raise FileNotFoundError(f"The file {input} does not exist.") - # with open(input, 'rb') as audio_file: - # audio_data = audio_file.read() + with open(input, "rb") as audio_file: + transcription = self.aclient.audio.transcriptions.create( + model="whisper-1", # Ensure the correct model for transcription + file=audio_file, + language="en", + ) - transcription = self.aclient.transcription( - model=self.transcription_model, - file=Path(input), - api_key=self.api_key, - api_base=self.endpoint, - api_version=self.api_version, - max_retries=self.MAX_RETRIES, - ) + return transcription.text - return transcription + def transcribe_image(self, input: str) -> str: + """Transcribe content from an image using base64 encoding.""" + + if not os.path.isfile(input): + raise FileNotFoundError(f"The file {input} does not exist.") - def transcribe_image(self, input) -> BaseModel: with open(input, "rb") as image_file: encoded_image = base64.b64encode(image_file.read()).decode("utf-8") - return self.aclient.completion( + response = self.aclient.chat.completions.create( model=self.model, messages=[ { "role": "user", "content": [ - { - "type": "text", - "text": "What’s in this image?", - }, + {"type": "text", "text": "What’s in this image?"}, { "type": "image_url", - "image_url": { - "url": f"data:image/jpeg;base64,{encoded_image}", - }, + "image_url": {"url": f"data:image/jpeg;base64,{encoded_image}"}, }, ], } ], - api_key=self.api_key, - api_base=self.endpoint, - api_version=self.api_version, max_tokens=300, - max_retries=self.MAX_RETRIES, - ) \ No newline at end of file + ) + + return response.choices[0].message.content From 0bba1f8261a91a3a9559529887f23e1104b2c721 Mon Sep 17 00:00:00 2001 From: Daniel Molnar Date: Tue, 25 Feb 2025 13:02:46 +0100 Subject: [PATCH 27/48] Response model fun. --- cognee/infrastructure/llm/ollama/adapter.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cognee/infrastructure/llm/ollama/adapter.py b/cognee/infrastructure/llm/ollama/adapter.py index d14c8a57f6..c41f3da7fa 100644 --- a/cognee/infrastructure/llm/ollama/adapter.py +++ b/cognee/infrastructure/llm/ollama/adapter.py @@ -45,9 +45,10 @@ async def acreate_structured_output( {"role": "user", "content": text_input}, ], max_tokens=self.max_tokens, + response_model=response_model, ) - return response_model.parse_raw(response.choices[0].message.content) + return response def create_transcript(self, input: str): """Generate an audio transcript from a user query.""" From 061fbbdc5861dee2b888a57fe850a1670ce6573b Mon Sep 17 00:00:00 2001 From: Daniel Molnar Date: Tue, 25 Feb 2025 13:35:42 +0100 Subject: [PATCH 28/48] OpenAI mode. --- .github/workflows/test_ollama.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/test_ollama.yml b/.github/workflows/test_ollama.yml index f0312a81e6..f4534e559a 100644 --- a/.github/workflows/test_ollama.yml +++ b/.github/workflows/test_ollama.yml @@ -40,17 +40,17 @@ jobs: run: curl -fsSL https://ollama.com/install.sh | sh - name: Run ollama run: | - ollama serve & + ollama serve --openai & ollama pull llama3.2 & ollama pull avr/sfr-embedding-mistral:latest - name: Call ollama API run: | - curl -d '{"model": "llama3.2", "stream": false, "prompt":"Whatever I say, asnwer with Yes"}' http://localhost:11434/api/generate + curl -d '{"model": "llama3.2", "stream": false, "prompt":"Whatever I say, asnwer with Yes"}' http://localhost:11434/v1/chat/completions - name: Wait for Ollama to be ready run: | for i in {1..30}; do - if curl -s http://localhost:11434/api/tags > /dev/null; then + if curl -s http://localhost:11434/v1/models > /dev/null; then echo "Ollama is ready" exit 0 fi @@ -74,11 +74,11 @@ jobs: PYTHONFAULTHANDLER: 1 LLM_API_KEY: "ollama" LLM_PROVIDER: "ollama" - LLM_ENDPOINT: "http://127.0.0.1:11434/api/generate" + LLM_ENDPOINT: "http://127.0.0.1:11434/v1/chat/completions" LLM_MODEL: "ollama/llama3.2" EMBEDDING_PROVIDER: "ollama" EMBEDDING_MODEL: "avr/sfr-embedding-mistral:latest" - EMBEDDING_ENDPOINT: "http://127.0.0.1:11434/api/embeddings" + EMBEDDING_ENDPOINT: "http://127.0.0.1:11434//v1/embeddings" EMBEDDING_DIMENSIONS: "4096" HUGGINGFACE_TOKENIZER: "Salesforce/SFR-Embedding-Mistral" - run: poetry run python ./examples/python/simple_example.py \ No newline at end of file + run: poetry run python ./examples/python/simple_example.py From 0ed6aa6ba4913c4abe90cb07775d2779b3a02cf2 Mon Sep 17 00:00:00 2001 From: Daniel Molnar Date: Tue, 25 Feb 2025 14:11:14 +0100 Subject: [PATCH 29/48] Typo. --- .github/workflows/test_ollama.yml | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test_ollama.yml b/.github/workflows/test_ollama.yml index f4534e559a..d6d8c30747 100644 --- a/.github/workflows/test_ollama.yml +++ b/.github/workflows/test_ollama.yml @@ -45,7 +45,16 @@ jobs: ollama pull avr/sfr-embedding-mistral:latest - name: Call ollama API run: | - curl -d '{"model": "llama3.2", "stream": false, "prompt":"Whatever I say, asnwer with Yes"}' http://localhost:11434/v1/chat/completions + curl -X POST http://localhost:11434/v1/chat/completions \ + -H "Content-Type: application/json" \ + -d '{ + "model": "llama3.2", + "stream": false, + "messages": [ + { "role": "system", "content": "You are a helpful assistant." }, + { "role": "user", "content": "Whatever I say, answer with Yes." } + ] + }' - name: Wait for Ollama to be ready run: | @@ -78,7 +87,7 @@ jobs: LLM_MODEL: "ollama/llama3.2" EMBEDDING_PROVIDER: "ollama" EMBEDDING_MODEL: "avr/sfr-embedding-mistral:latest" - EMBEDDING_ENDPOINT: "http://127.0.0.1:11434//v1/embeddings" + EMBEDDING_ENDPOINT: "http://127.0.0.1:11434/v1/embeddings" EMBEDDING_DIMENSIONS: "4096" HUGGINGFACE_TOKENIZER: "Salesforce/SFR-Embedding-Mistral" run: poetry run python ./examples/python/simple_example.py From 30903336ff72cd5eec5c099699dac4345e923a5f Mon Sep 17 00:00:00 2001 From: Daniel Molnar Date: Tue, 25 Feb 2025 14:53:56 +0100 Subject: [PATCH 30/48] Add a call, homogenous localhost. --- .github/workflows/test_ollama.yml | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test_ollama.yml b/.github/workflows/test_ollama.yml index d6d8c30747..16d0a91840 100644 --- a/.github/workflows/test_ollama.yml +++ b/.github/workflows/test_ollama.yml @@ -55,6 +55,13 @@ jobs: { "role": "user", "content": "Whatever I say, answer with Yes." } ] }' + curl -X POST http://127.0.0.1:11434/v1/embeddings \ + -H "Content-Type: application/json" \ + -d '{ + "model": "avr/sfr-embedding-mistral:latest", + "input": "This is a test sentence to generate an embedding." + }' + - name: Wait for Ollama to be ready run: | @@ -83,11 +90,11 @@ jobs: PYTHONFAULTHANDLER: 1 LLM_API_KEY: "ollama" LLM_PROVIDER: "ollama" - LLM_ENDPOINT: "http://127.0.0.1:11434/v1/chat/completions" + LLM_ENDPOINT: "http://localhost:11434/v1/chat/completions" LLM_MODEL: "ollama/llama3.2" EMBEDDING_PROVIDER: "ollama" EMBEDDING_MODEL: "avr/sfr-embedding-mistral:latest" - EMBEDDING_ENDPOINT: "http://127.0.0.1:11434/v1/embeddings" + EMBEDDING_ENDPOINT: "http://localhost:11434/v1/embeddings" EMBEDDING_DIMENSIONS: "4096" HUGGINGFACE_TOKENIZER: "Salesforce/SFR-Embedding-Mistral" run: poetry run python ./examples/python/simple_example.py From 80ccf558770d8ac1dae721cf6360f20028f821ec Mon Sep 17 00:00:00 2001 From: Daniel Molnar Date: Tue, 25 Feb 2025 15:51:51 +0100 Subject: [PATCH 31/48] Should conform more. --- .github/workflows/test_ollama.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test_ollama.yml b/.github/workflows/test_ollama.yml index 16d0a91840..739e845986 100644 --- a/.github/workflows/test_ollama.yml +++ b/.github/workflows/test_ollama.yml @@ -88,10 +88,10 @@ jobs: GRAPHISTRY_USERNAME: ${{ secrets.GRAPHISTRY_USERNAME }} GRAPHISTRY_PASSWORD: ${{ secrets.GRAPHISTRY_PASSWORD }} PYTHONFAULTHANDLER: 1 - LLM_API_KEY: "ollama" + LLM_API_KEY: "" LLM_PROVIDER: "ollama" LLM_ENDPOINT: "http://localhost:11434/v1/chat/completions" - LLM_MODEL: "ollama/llama3.2" + LLM_MODEL: "llama3" EMBEDDING_PROVIDER: "ollama" EMBEDDING_MODEL: "avr/sfr-embedding-mistral:latest" EMBEDDING_ENDPOINT: "http://localhost:11434/v1/embeddings" From ce8c2da4e72d3e9d02b481823dfae2067008e5c1 Mon Sep 17 00:00:00 2001 From: Daniel Molnar Date: Tue, 25 Feb 2025 15:59:44 +0100 Subject: [PATCH 32/48] Unset, my friend, unset. --- .github/workflows/test_ollama.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/test_ollama.yml b/.github/workflows/test_ollama.yml index 739e845986..feb43a46ed 100644 --- a/.github/workflows/test_ollama.yml +++ b/.github/workflows/test_ollama.yml @@ -88,7 +88,6 @@ jobs: GRAPHISTRY_USERNAME: ${{ secrets.GRAPHISTRY_USERNAME }} GRAPHISTRY_PASSWORD: ${{ secrets.GRAPHISTRY_PASSWORD }} PYTHONFAULTHANDLER: 1 - LLM_API_KEY: "" LLM_PROVIDER: "ollama" LLM_ENDPOINT: "http://localhost:11434/v1/chat/completions" LLM_MODEL: "llama3" From 65927b39cd2390b9ff6b7f3ea84ff6cee4a76cde Mon Sep 17 00:00:00 2001 From: Vasilije <8619304+Vasilije1990@users.noreply.github.com> Date: Tue, 25 Feb 2025 19:41:16 +0100 Subject: [PATCH 33/48] Update test_ollama.yml --- .github/workflows/test_ollama.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test_ollama.yml b/.github/workflows/test_ollama.yml index feb43a46ed..b9eea184d6 100644 --- a/.github/workflows/test_ollama.yml +++ b/.github/workflows/test_ollama.yml @@ -89,11 +89,11 @@ jobs: GRAPHISTRY_PASSWORD: ${{ secrets.GRAPHISTRY_PASSWORD }} PYTHONFAULTHANDLER: 1 LLM_PROVIDER: "ollama" - LLM_ENDPOINT: "http://localhost:11434/v1/chat/completions" + LLM_ENDPOINT: "ollama:11434/v1/chat/completions" LLM_MODEL: "llama3" EMBEDDING_PROVIDER: "ollama" EMBEDDING_MODEL: "avr/sfr-embedding-mistral:latest" - EMBEDDING_ENDPOINT: "http://localhost:11434/v1/embeddings" + EMBEDDING_ENDPOINT: "ollama:11434/v1/embeddings" EMBEDDING_DIMENSIONS: "4096" HUGGINGFACE_TOKENIZER: "Salesforce/SFR-Embedding-Mistral" run: poetry run python ./examples/python/simple_example.py From 6463c2e08be8846d842656749917c544c7025596 Mon Sep 17 00:00:00 2001 From: Vasilije <8619304+Vasilije1990@users.noreply.github.com> Date: Tue, 25 Feb 2025 19:47:57 +0100 Subject: [PATCH 34/48] Update test_ollama.yml --- .github/workflows/test_ollama.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/test_ollama.yml b/.github/workflows/test_ollama.yml index b9eea184d6..fc5c498b50 100644 --- a/.github/workflows/test_ollama.yml +++ b/.github/workflows/test_ollama.yml @@ -90,6 +90,8 @@ jobs: PYTHONFAULTHANDLER: 1 LLM_PROVIDER: "ollama" LLM_ENDPOINT: "ollama:11434/v1/chat/completions" + LLM_API_KEY: "ollama" + EMBEDDING_API_KEY: "ollama" LLM_MODEL: "llama3" EMBEDDING_PROVIDER: "ollama" EMBEDDING_MODEL: "avr/sfr-embedding-mistral:latest" From 70f9b5f7c55e42c1c810d5969c0cbb897cefc80e Mon Sep 17 00:00:00 2001 From: Vasilije <8619304+Vasilije1990@users.noreply.github.com> Date: Tue, 25 Feb 2025 19:54:48 +0100 Subject: [PATCH 35/48] Update test_ollama.yml --- .github/workflows/test_ollama.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test_ollama.yml b/.github/workflows/test_ollama.yml index fc5c498b50..0acfe45b3e 100644 --- a/.github/workflows/test_ollama.yml +++ b/.github/workflows/test_ollama.yml @@ -89,13 +89,13 @@ jobs: GRAPHISTRY_PASSWORD: ${{ secrets.GRAPHISTRY_PASSWORD }} PYTHONFAULTHANDLER: 1 LLM_PROVIDER: "ollama" - LLM_ENDPOINT: "ollama:11434/v1/chat/completions" + LLM_ENDPOINT: "http://ollama:11434/v1/chat/completions" LLM_API_KEY: "ollama" EMBEDDING_API_KEY: "ollama" LLM_MODEL: "llama3" EMBEDDING_PROVIDER: "ollama" EMBEDDING_MODEL: "avr/sfr-embedding-mistral:latest" - EMBEDDING_ENDPOINT: "ollama:11434/v1/embeddings" + EMBEDDING_ENDPOINT: http://"ollama:11434/v1/embeddings" EMBEDDING_DIMENSIONS: "4096" HUGGINGFACE_TOKENIZER: "Salesforce/SFR-Embedding-Mistral" run: poetry run python ./examples/python/simple_example.py From 468268cc8f69e4999486a1ced004376548cb0d95 Mon Sep 17 00:00:00 2001 From: Daniel Molnar Date: Wed, 26 Feb 2025 10:33:02 +0100 Subject: [PATCH 36/48] Docker Composish way. --- .github/workflows/test_ollama.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/test_ollama.yml b/.github/workflows/test_ollama.yml index 0acfe45b3e..8a778b62be 100644 --- a/.github/workflows/test_ollama.yml +++ b/.github/workflows/test_ollama.yml @@ -90,12 +90,10 @@ jobs: PYTHONFAULTHANDLER: 1 LLM_PROVIDER: "ollama" LLM_ENDPOINT: "http://ollama:11434/v1/chat/completions" - LLM_API_KEY: "ollama" - EMBEDDING_API_KEY: "ollama" LLM_MODEL: "llama3" EMBEDDING_PROVIDER: "ollama" EMBEDDING_MODEL: "avr/sfr-embedding-mistral:latest" - EMBEDDING_ENDPOINT: http://"ollama:11434/v1/embeddings" + EMBEDDING_ENDPOINT: "http://ollama:11434/v1/embeddings" EMBEDDING_DIMENSIONS: "4096" HUGGINGFACE_TOKENIZER: "Salesforce/SFR-Embedding-Mistral" run: poetry run python ./examples/python/simple_example.py From c72b12d7129d8767dcd86bac237e1775ee30be24 Mon Sep 17 00:00:00 2001 From: Daniel Molnar Date: Wed, 26 Feb 2025 10:40:47 +0100 Subject: [PATCH 37/48] Let's be Pydantic. --- .github/workflows/test_ollama.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/test_ollama.yml b/.github/workflows/test_ollama.yml index 8a778b62be..84e8059baf 100644 --- a/.github/workflows/test_ollama.yml +++ b/.github/workflows/test_ollama.yml @@ -89,6 +89,7 @@ jobs: GRAPHISTRY_PASSWORD: ${{ secrets.GRAPHISTRY_PASSWORD }} PYTHONFAULTHANDLER: 1 LLM_PROVIDER: "ollama" + LLM_API_KEY: "ollama" LLM_ENDPOINT: "http://ollama:11434/v1/chat/completions" LLM_MODEL: "llama3" EMBEDDING_PROVIDER: "ollama" From c22455675c6056f6454e20de10caf88658f4bffa Mon Sep 17 00:00:00 2001 From: Daniel Molnar Date: Wed, 26 Feb 2025 11:33:23 +0100 Subject: [PATCH 38/48] Launch Docker manually. --- .github/workflows/test_ollama.yml | 64 ++++++++++++++++++------------- 1 file changed, 37 insertions(+), 27 deletions(-) diff --git a/.github/workflows/test_ollama.yml b/.github/workflows/test_ollama.yml index 84e8059baf..9b55ba62c9 100644 --- a/.github/workflows/test_ollama.yml +++ b/.github/workflows/test_ollama.yml @@ -10,11 +10,11 @@ jobs: run_simple_example_test: runs-on: ubuntu-latest - services: - ollama: - image: ollama/ollama - ports: - - 11434:11434 +# services: +# ollama: +# image: ollama/ollama +# ports: +# - 11434:11434 steps: - name: Checkout repository @@ -36,13 +36,37 @@ jobs: run: | poetry install --no-interaction --all-extras - - name: Install ollama - run: curl -fsSL https://ollama.com/install.sh | sh - - name: Run ollama +# - name: Install ollama +# run: curl -fsSL https://ollama.com/install.sh | sh +# - name: Run ollama +# run: | +# ollama serve --openai & +# ollama pull llama3.2 & +# ollama pull avr/sfr-embedding-mistral:latest + + - name: Start Ollama in a Docker container + run: | + docker run -d --name ollama -p 11434:11434 ollama/ollama serve --openai + sleep 5 # Give some time for Ollama to start + + - name: Wait for Ollama to be ready + run: | + for i in {1..30}; do + if curl -s http://localhost:11434/v1/models > /dev/null; then + echo "Ollama is ready" + exit 0 + fi + echo "Waiting for Ollama... attempt $i" + sleep 2 + done + echo "Ollama failed to start" + exit 1 + + - name: Pull required Ollama models run: | - ollama serve --openai & - ollama pull llama3.2 & - ollama pull avr/sfr-embedding-mistral:latest + curl -X POST http://localhost:11434/api/pull -d '{"name": "llama3.2"}' + curl -X POST http://localhost:11434/api/pull -d '{"name": "avr/sfr-embedding-mistral:latest"}' + - name: Call ollama API run: | curl -X POST http://localhost:11434/v1/chat/completions \ @@ -62,20 +86,6 @@ jobs: "input": "This is a test sentence to generate an embedding." }' - - - name: Wait for Ollama to be ready - run: | - for i in {1..30}; do - if curl -s http://localhost:11434/v1/models > /dev/null; then - echo "Ollama is ready" - exit 0 - fi - echo "Waiting for Ollama... attempt $i" - sleep 2 - done - echo "Ollama failed to start" - exit 1 - - name: Dump Docker logs run: | docker ps @@ -90,11 +100,11 @@ jobs: PYTHONFAULTHANDLER: 1 LLM_PROVIDER: "ollama" LLM_API_KEY: "ollama" - LLM_ENDPOINT: "http://ollama:11434/v1/chat/completions" + LLM_ENDPOINT: "http://localhost:11434/v1/chat/completions" LLM_MODEL: "llama3" EMBEDDING_PROVIDER: "ollama" EMBEDDING_MODEL: "avr/sfr-embedding-mistral:latest" - EMBEDDING_ENDPOINT: "http://ollama:11434/v1/embeddings" + EMBEDDING_ENDPOINT: "http://localhost:11434/v1/embeddings" EMBEDDING_DIMENSIONS: "4096" HUGGINGFACE_TOKENIZER: "Salesforce/SFR-Embedding-Mistral" run: poetry run python ./examples/python/simple_example.py From ec9bbca79856def1a5c09dbbb594c920568b1cbe Mon Sep 17 00:00:00 2001 From: Daniel Molnar Date: Wed, 26 Feb 2025 11:46:24 +0100 Subject: [PATCH 39/48] Cosmetics. --- .github/workflows/test_ollama.yml | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test_ollama.yml b/.github/workflows/test_ollama.yml index 9b55ba62c9..dd3de932ca 100644 --- a/.github/workflows/test_ollama.yml +++ b/.github/workflows/test_ollama.yml @@ -44,10 +44,14 @@ jobs: # ollama pull llama3.2 & # ollama pull avr/sfr-embedding-mistral:latest - - name: Start Ollama in a Docker container + - name: Start Ollama container run: | - docker run -d --name ollama -p 11434:11434 ollama/ollama serve --openai - sleep 5 # Give some time for Ollama to start + docker run -d --name ollama -p 11434:11434 ollama/ollama + sleep 5 + docker exec -d ollama bash -c "ollama serve --openai" + + - name: Check Ollama logs + run: docker logs ollama - name: Wait for Ollama to be ready run: | From cabbfd628931bc45124956ef1ddbdda49824ecdd Mon Sep 17 00:00:00 2001 From: Daniel Molnar Date: Wed, 26 Feb 2025 11:57:17 +0100 Subject: [PATCH 40/48] Maybe we could fly without the Hugger. --- .github/workflows/test_ollama.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test_ollama.yml b/.github/workflows/test_ollama.yml index dd3de932ca..16692ae206 100644 --- a/.github/workflows/test_ollama.yml +++ b/.github/workflows/test_ollama.yml @@ -110,5 +110,5 @@ jobs: EMBEDDING_MODEL: "avr/sfr-embedding-mistral:latest" EMBEDDING_ENDPOINT: "http://localhost:11434/v1/embeddings" EMBEDDING_DIMENSIONS: "4096" - HUGGINGFACE_TOKENIZER: "Salesforce/SFR-Embedding-Mistral" +# HUGGINGFACE_TOKENIZER: "Salesforce/SFR-Embedding-Mistral" run: poetry run python ./examples/python/simple_example.py From c329cefcda02242b6d158dece0b245017fcf8051 Mon Sep 17 00:00:00 2001 From: Daniel Molnar Date: Wed, 26 Feb 2025 14:02:29 +0100 Subject: [PATCH 41/48] OHMY. --- .github/workflows/test_ollama.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/test_ollama.yml b/.github/workflows/test_ollama.yml index 16692ae206..ceffa08b02 100644 --- a/.github/workflows/test_ollama.yml +++ b/.github/workflows/test_ollama.yml @@ -104,11 +104,11 @@ jobs: PYTHONFAULTHANDLER: 1 LLM_PROVIDER: "ollama" LLM_API_KEY: "ollama" - LLM_ENDPOINT: "http://localhost:11434/v1/chat/completions" - LLM_MODEL: "llama3" + LLM_ENDPOINT: "http://localhost:11434/v1/" + LLM_MODEL: "llama3.2" EMBEDDING_PROVIDER: "ollama" EMBEDDING_MODEL: "avr/sfr-embedding-mistral:latest" - EMBEDDING_ENDPOINT: "http://localhost:11434/v1/embeddings" + EMBEDDING_ENDPOINT: "http://localhost:11434/v1/" EMBEDDING_DIMENSIONS: "4096" -# HUGGINGFACE_TOKENIZER: "Salesforce/SFR-Embedding-Mistral" + HUGGINGFACE_TOKENIZER: "Salesforce/SFR-Embedding-Mistral" run: poetry run python ./examples/python/simple_example.py From 44f02df0c8a5b6ccad15fa5f453b2fe06878c217 Mon Sep 17 00:00:00 2001 From: Daniel Molnar Date: Wed, 26 Feb 2025 14:16:10 +0100 Subject: [PATCH 42/48] Async it. --- .github/workflows/test_ollama.yml | 1 + cognee/infrastructure/llm/ollama/adapter.py | 45 ++++++++++++++------- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/.github/workflows/test_ollama.yml b/.github/workflows/test_ollama.yml index ceffa08b02..f2e33c5d47 100644 --- a/.github/workflows/test_ollama.yml +++ b/.github/workflows/test_ollama.yml @@ -35,6 +35,7 @@ jobs: - name: Install dependencies run: | poetry install --no-interaction --all-extras + poetry add torch # - name: Install ollama # run: curl -fsSL https://ollama.com/install.sh | sh diff --git a/cognee/infrastructure/llm/ollama/adapter.py b/cognee/infrastructure/llm/ollama/adapter.py index c41f3da7fa..7ec0a83f07 100644 --- a/cognee/infrastructure/llm/ollama/adapter.py +++ b/cognee/infrastructure/llm/ollama/adapter.py @@ -1,10 +1,9 @@ -from typing import Type +from typing import Type, Optional from pydantic import BaseModel import instructor from cognee.infrastructure.llm.llm_interface import LLMInterface -from openai import OpenAI +from openai import AsyncOpenAI # Use AsyncOpenAI for async compatibility import base64 -from pathlib import Path import os @@ -20,7 +19,7 @@ def __init__( model: str, name: str, max_tokens: int, - api_version: str = None, + api_version: Optional[str] = None, ) -> None: self.name = name self.model = model @@ -29,8 +28,9 @@ def __init__( self.max_tokens = max_tokens self.api_version = api_version + # Use AsyncOpenAI for proper async handling self.aclient = instructor.from_openai( - OpenAI(base_url=self.endpoint, api_key=self.api_key), mode=instructor.Mode.JSON + AsyncOpenAI(base_url=self.endpoint, api_key=self.api_key), mode=instructor.Mode.JSON ) async def acreate_structured_output( @@ -38,6 +38,10 @@ async def acreate_structured_output( ) -> BaseModel: """Generate a structured output from the LLM using the provided text and system prompt.""" + # Ensure the function being awaited is actually async + if not callable(getattr(self.aclient.chat.completions, "create", None)): + raise TypeError("self.aclient.chat.completions.create is not callable!") + response = await self.aclient.chat.completions.create( model=self.model, messages=[ @@ -45,33 +49,40 @@ async def acreate_structured_output( {"role": "user", "content": text_input}, ], max_tokens=self.max_tokens, - response_model=response_model, ) - return response + # Ensure response is valid before passing to Pydantic model + if not isinstance(response, dict): + raise ValueError(f"Unexpected response format: {response}") + + return response_model(**response) - def create_transcript(self, input: str): + def create_transcript(self, input_file: str) -> str: """Generate an audio transcript from a user query.""" - if not os.path.isfile(input): - raise FileNotFoundError(f"The file {input} does not exist.") + if not os.path.isfile(input_file): + raise FileNotFoundError(f"The file {input_file} does not exist.") - with open(input, "rb") as audio_file: + with open(input_file, "rb") as audio_file: transcription = self.aclient.audio.transcriptions.create( model="whisper-1", # Ensure the correct model for transcription file=audio_file, language="en", ) + # Ensure the response contains a valid transcript + if not hasattr(transcription, "text"): + raise ValueError("Transcription failed. No text returned.") + return transcription.text - def transcribe_image(self, input: str) -> str: + def transcribe_image(self, input_file: str) -> str: """Transcribe content from an image using base64 encoding.""" - if not os.path.isfile(input): - raise FileNotFoundError(f"The file {input} does not exist.") + if not os.path.isfile(input_file): + raise FileNotFoundError(f"The file {input_file} does not exist.") - with open(input, "rb") as image_file: + with open(input_file, "rb") as image_file: encoded_image = base64.b64encode(image_file.read()).decode("utf-8") response = self.aclient.chat.completions.create( @@ -91,4 +102,8 @@ def transcribe_image(self, input: str) -> str: max_tokens=300, ) + # Ensure response is valid before accessing .choices[0].message.content + if not hasattr(response, "choices") or not response.choices: + raise ValueError("Image transcription failed. No response received.") + return response.choices[0].message.content From 6b490789639851d6fd392adabf4f43dca9027dbc Mon Sep 17 00:00:00 2001 From: Daniel Molnar Date: Wed, 26 Feb 2025 14:25:16 +0100 Subject: [PATCH 43/48] Response model. --- cognee/infrastructure/llm/ollama/adapter.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/cognee/infrastructure/llm/ollama/adapter.py b/cognee/infrastructure/llm/ollama/adapter.py index 7ec0a83f07..fa5023a1c2 100644 --- a/cognee/infrastructure/llm/ollama/adapter.py +++ b/cognee/infrastructure/llm/ollama/adapter.py @@ -38,10 +38,11 @@ async def acreate_structured_output( ) -> BaseModel: """Generate a structured output from the LLM using the provided text and system prompt.""" - # Ensure the function being awaited is actually async + # Ensure the API method is async if not callable(getattr(self.aclient.chat.completions, "create", None)): raise TypeError("self.aclient.chat.completions.create is not callable!") + # Call the API with the expected parameters response = await self.aclient.chat.completions.create( model=self.model, messages=[ @@ -51,11 +52,12 @@ async def acreate_structured_output( max_tokens=self.max_tokens, ) - # Ensure response is valid before passing to Pydantic model + # Ensure the response is valid before passing to Pydantic model if not isinstance(response, dict): raise ValueError(f"Unexpected response format: {response}") - return response_model(**response) + # Use instructor's parse_response method instead of passing response_model in API call + return instructor.parse_response(response, response_model) def create_transcript(self, input_file: str) -> str: """Generate an audio transcript from a user query.""" From fe7da6028b3e3bf836486ce27346e101a8724bf7 Mon Sep 17 00:00:00 2001 From: Daniel Molnar Date: Wed, 26 Feb 2025 14:41:23 +0100 Subject: [PATCH 44/48] Will graph fly. --- cognee/infrastructure/llm/ollama/adapter.py | 46 +++++---------------- 1 file changed, 10 insertions(+), 36 deletions(-) diff --git a/cognee/infrastructure/llm/ollama/adapter.py b/cognee/infrastructure/llm/ollama/adapter.py index fa5023a1c2..1390d812ca 100644 --- a/cognee/infrastructure/llm/ollama/adapter.py +++ b/cognee/infrastructure/llm/ollama/adapter.py @@ -2,7 +2,7 @@ from pydantic import BaseModel import instructor from cognee.infrastructure.llm.llm_interface import LLMInterface -from openai import AsyncOpenAI # Use AsyncOpenAI for async compatibility +from openai import AsyncOpenAI import base64 import os @@ -28,55 +28,29 @@ def __init__( self.max_tokens = max_tokens self.api_version = api_version - # Use AsyncOpenAI for proper async handling - self.aclient = instructor.from_openai( - AsyncOpenAI(base_url=self.endpoint, api_key=self.api_key), mode=instructor.Mode.JSON - ) + # Properly initialize AsyncOpenAI + self.client = AsyncOpenAI(base_url=self.endpoint, api_key=self.api_key) + + # Apply instructor patch (this enables structured output support) + instructor.patch(self.client) async def acreate_structured_output( self, text_input: str, system_prompt: str, response_model: Type[BaseModel] ) -> BaseModel: """Generate a structured output from the LLM using the provided text and system prompt.""" - # Ensure the API method is async - if not callable(getattr(self.aclient.chat.completions, "create", None)): - raise TypeError("self.aclient.chat.completions.create is not callable!") - - # Call the API with the expected parameters - response = await self.aclient.chat.completions.create( + # Directly pass `response_model` inside `.create()` + response = await self.client.chat.completions.create( model=self.model, messages=[ {"role": "system", "content": system_prompt}, {"role": "user", "content": text_input}, ], max_tokens=self.max_tokens, + response_model=response_model, # This works after `instructor.patch(self.client)` ) - # Ensure the response is valid before passing to Pydantic model - if not isinstance(response, dict): - raise ValueError(f"Unexpected response format: {response}") - - # Use instructor's parse_response method instead of passing response_model in API call - return instructor.parse_response(response, response_model) - - def create_transcript(self, input_file: str) -> str: - """Generate an audio transcript from a user query.""" - - if not os.path.isfile(input_file): - raise FileNotFoundError(f"The file {input_file} does not exist.") - - with open(input_file, "rb") as audio_file: - transcription = self.aclient.audio.transcriptions.create( - model="whisper-1", # Ensure the correct model for transcription - file=audio_file, - language="en", - ) - - # Ensure the response contains a valid transcript - if not hasattr(transcription, "text"): - raise ValueError("Transcription failed. No text returned.") - - return transcription.text + return response # Already converted into `response_model` def transcribe_image(self, input_file: str) -> str: """Transcribe content from an image using base64 encoding.""" From a77655a7765e562b54baa062cb3b353fc05792a4 Mon Sep 17 00:00:00 2001 From: Daniel Molnar Date: Wed, 26 Feb 2025 14:51:04 +0100 Subject: [PATCH 45/48] Oops, putting back create transcript. --- cognee/infrastructure/llm/ollama/adapter.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/cognee/infrastructure/llm/ollama/adapter.py b/cognee/infrastructure/llm/ollama/adapter.py index 1390d812ca..7a1701bbf7 100644 --- a/cognee/infrastructure/llm/ollama/adapter.py +++ b/cognee/infrastructure/llm/ollama/adapter.py @@ -52,6 +52,25 @@ async def acreate_structured_output( return response # Already converted into `response_model` + def create_transcript(self, input_file: str) -> str: + """Generate an audio transcript from a user query.""" + + if not os.path.isfile(input_file): + raise FileNotFoundError(f"The file {input_file} does not exist.") + + with open(input_file, "rb") as audio_file: + transcription = self.aclient.audio.transcriptions.create( + model="whisper-1", # ✅ Ensure the correct model for transcription + file=audio_file, + language="en", + ) + + # ✅ Ensure the response contains a valid transcript + if not hasattr(transcription, "text"): + raise ValueError("Transcription failed. No text returned.") + + return transcription.text + def transcribe_image(self, input_file: str) -> str: """Transcribe content from an image using base64 encoding.""" From cfc93e3d7aebf74f963b27dafba70a50677e8eea Mon Sep 17 00:00:00 2001 From: Daniel Molnar Date: Wed, 26 Feb 2025 15:18:24 +0100 Subject: [PATCH 46/48] Clean up adapter. --- cognee/infrastructure/llm/ollama/adapter.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/cognee/infrastructure/llm/ollama/adapter.py b/cognee/infrastructure/llm/ollama/adapter.py index 7a1701bbf7..bf79de1a15 100644 --- a/cognee/infrastructure/llm/ollama/adapter.py +++ b/cognee/infrastructure/llm/ollama/adapter.py @@ -5,6 +5,7 @@ from openai import AsyncOpenAI import base64 import os +import json class OllamaAPIAdapter(LLMInterface): @@ -28,18 +29,14 @@ def __init__( self.max_tokens = max_tokens self.api_version = api_version - # Properly initialize AsyncOpenAI + # ✅ Properly initialize AsyncOpenAI and patch with instructor self.client = AsyncOpenAI(base_url=self.endpoint, api_key=self.api_key) - - # Apply instructor patch (this enables structured output support) - instructor.patch(self.client) + instructor.patch(self.client) # ✅ Patch OpenAI with instructor for structured output async def acreate_structured_output( self, text_input: str, system_prompt: str, response_model: Type[BaseModel] ) -> BaseModel: """Generate a structured output from the LLM using the provided text and system prompt.""" - - # Directly pass `response_model` inside `.create()` response = await self.client.chat.completions.create( model=self.model, messages=[ @@ -47,10 +44,10 @@ async def acreate_structured_output( {"role": "user", "content": text_input}, ], max_tokens=self.max_tokens, - response_model=response_model, # This works after `instructor.patch(self.client)` + response_model=response_model, ) - return response # Already converted into `response_model` + return response_model(response) def create_transcript(self, input_file: str) -> str: """Generate an audio transcript from a user query.""" @@ -60,12 +57,12 @@ def create_transcript(self, input_file: str) -> str: with open(input_file, "rb") as audio_file: transcription = self.aclient.audio.transcriptions.create( - model="whisper-1", # ✅ Ensure the correct model for transcription + model="whisper-1", # Ensure the correct model for transcription file=audio_file, language="en", ) - # ✅ Ensure the response contains a valid transcript + # Ensure the response contains a valid transcript if not hasattr(transcription, "text"): raise ValueError("Transcription failed. No text returned.") From 647d87286af5cba926bf06368de0d50f38676286 Mon Sep 17 00:00:00 2001 From: Daniel Molnar Date: Thu, 27 Feb 2025 12:13:04 +0100 Subject: [PATCH 47/48] Phi4 can respond reasonably. --- .github/workflows/test_ollama.yml | 6 +-- cognee/infrastructure/llm/ollama/adapter.py | 46 ++++++++++----------- 2 files changed, 24 insertions(+), 28 deletions(-) diff --git a/.github/workflows/test_ollama.yml b/.github/workflows/test_ollama.yml index f2e33c5d47..ada006a12b 100644 --- a/.github/workflows/test_ollama.yml +++ b/.github/workflows/test_ollama.yml @@ -69,7 +69,7 @@ jobs: - name: Pull required Ollama models run: | - curl -X POST http://localhost:11434/api/pull -d '{"name": "llama3.2"}' + curl -X POST http://localhost:11434/api/pull -d '{"name": "phi4"}' curl -X POST http://localhost:11434/api/pull -d '{"name": "avr/sfr-embedding-mistral:latest"}' - name: Call ollama API @@ -77,7 +77,7 @@ jobs: curl -X POST http://localhost:11434/v1/chat/completions \ -H "Content-Type: application/json" \ -d '{ - "model": "llama3.2", + "model": "phi4", "stream": false, "messages": [ { "role": "system", "content": "You are a helpful assistant." }, @@ -106,7 +106,7 @@ jobs: LLM_PROVIDER: "ollama" LLM_API_KEY: "ollama" LLM_ENDPOINT: "http://localhost:11434/v1/" - LLM_MODEL: "llama3.2" + LLM_MODEL: "phi4" EMBEDDING_PROVIDER: "ollama" EMBEDDING_MODEL: "avr/sfr-embedding-mistral:latest" EMBEDDING_ENDPOINT: "http://localhost:11434/v1/" diff --git a/cognee/infrastructure/llm/ollama/adapter.py b/cognee/infrastructure/llm/ollama/adapter.py index bf79de1a15..ac6b91af43 100644 --- a/cognee/infrastructure/llm/ollama/adapter.py +++ b/cognee/infrastructure/llm/ollama/adapter.py @@ -1,53 +1,49 @@ -from typing import Type, Optional +from typing import Type from pydantic import BaseModel import instructor from cognee.infrastructure.llm.llm_interface import LLMInterface -from openai import AsyncOpenAI +from cognee.infrastructure.llm.config import get_llm_config +from openai import OpenAI import base64 import os -import json class OllamaAPIAdapter(LLMInterface): - """Adapter for an Ollama API LLM provider using instructor with an OpenAI backend.""" - - MAX_RETRIES = 5 - - def __init__( - self, - endpoint: str, - api_key: str, - model: str, - name: str, - max_tokens: int, - api_version: Optional[str] = None, - ) -> None: + """Adapter for a Generic API LLM provider using instructor with an OpenAI backend.""" + + def __init__(self, endpoint: str, api_key: str, model: str, name: str, max_tokens: int): self.name = name self.model = model self.api_key = api_key self.endpoint = endpoint self.max_tokens = max_tokens - self.api_version = api_version - # ✅ Properly initialize AsyncOpenAI and patch with instructor - self.client = AsyncOpenAI(base_url=self.endpoint, api_key=self.api_key) - instructor.patch(self.client) # ✅ Patch OpenAI with instructor for structured output + self.aclient = instructor.from_openai( + OpenAI(base_url=self.endpoint, api_key=self.api_key), mode=instructor.Mode.JSON + ) async def acreate_structured_output( self, text_input: str, system_prompt: str, response_model: Type[BaseModel] ) -> BaseModel: """Generate a structured output from the LLM using the provided text and system prompt.""" - response = await self.client.chat.completions.create( + + response = self.aclient.chat.completions.create( model=self.model, messages=[ - {"role": "system", "content": system_prompt}, - {"role": "user", "content": text_input}, + { + "role": "user", + "content": f"Use the given format to extract information from the following input: {text_input}", + }, + { + "role": "system", + "content": system_prompt, + }, ], - max_tokens=self.max_tokens, + max_retries=5, response_model=response_model, ) - return response_model(response) + return response def create_transcript(self, input_file: str) -> str: """Generate an audio transcript from a user query.""" From 01bb8cbf0cafde849af458a23c664a69baea4f58 Mon Sep 17 00:00:00 2001 From: Daniel Molnar Date: Fri, 28 Feb 2025 19:06:09 +0100 Subject: [PATCH 48/48] Beefy runner. --- .github/workflows/test_ollama.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test_ollama.yml b/.github/workflows/test_ollama.yml index ada006a12b..030d6e3e13 100644 --- a/.github/workflows/test_ollama.yml +++ b/.github/workflows/test_ollama.yml @@ -9,7 +9,8 @@ jobs: run_simple_example_test: - runs-on: ubuntu-latest + # needs 16 Gb RAM for phi4 + runs-on: buildjet-4vcpu-ubuntu-2204 # services: # ollama: # image: ollama/ollama