Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
c572e27
Draft ollama test
Vasilije1990 Feb 19, 2025
2de364b
Ollama test end to end
Vasilije1990 Feb 20, 2025
8ebd9a7
Ollama test end to end
Vasilije1990 Feb 20, 2025
9d0d96e
Fix ollama
Vasilije1990 Feb 21, 2025
b4088be
Fix ollama
Vasilije1990 Feb 21, 2025
b670697
Fix ollama
Vasilije1990 Feb 21, 2025
bfe039d
Fix ollama
Vasilije1990 Feb 21, 2025
6bc4f6a
Fix ollama
Vasilije1990 Feb 21, 2025
96adcfb
Fix ollama
Vasilije1990 Feb 21, 2025
c06c28d
Fix ollama
Vasilije1990 Feb 21, 2025
edd681f
Fix ollama
Vasilije1990 Feb 21, 2025
02b0109
Fix ollama
Vasilije1990 Feb 21, 2025
a91e83e
Fix ollama
Vasilije1990 Feb 21, 2025
326c418
Fix ollama
Vasilije1990 Feb 21, 2025
92602aa
Fix ollama
Vasilije1990 Feb 22, 2025
f2d0909
Fix ollama
Vasilije1990 Feb 22, 2025
97465f1
Fix ollama
Vasilije1990 Feb 22, 2025
73662b8
Fix ollama
Vasilije1990 Feb 22, 2025
90d96aa
Fix ollama
Vasilije1990 Feb 22, 2025
3a88b94
Fix ollama
Vasilije1990 Feb 22, 2025
11442df
Fix ollama
Vasilije1990 Feb 22, 2025
1dfb0dd
Fix ollama
Vasilije1990 Feb 22, 2025
4c4723b
Fix ollama
Vasilije1990 Feb 22, 2025
846c45e
Fix ollama
Vasilije1990 Feb 22, 2025
2c0bfc8
Fix ollama
Vasilije1990 Feb 22, 2025
62d2d76
Add helm chart for deployment
Vasilije1990 Feb 24, 2025
57630ee
Merge branch 'dev' into add_helm_to_core
Vasilije1990 Feb 24, 2025
0516d6e
Update cognee/infrastructure/llm/ollama/adapter.py
borisarzentar Feb 26, 2025
bacfb01
Merge branch 'dev' into add_helm_to_core
Vasilije1990 Mar 4, 2025
6e17fdf
check
Vasilije1990 Mar 4, 2025
a9f8ab4
Merge remote-tracking branch 'origin/add_helm_to_core' into add_helm_…
Vasilije1990 Mar 4, 2025
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
83 changes: 62 additions & 21 deletions .github/workflows/test_ollama.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ jobs:

run_simple_example_test:

runs-on: ubuntu-latest
services:
ollama:
image: ollama/ollama
ports:
- 11434:11434

# needs 16 Gb RAM for phi4
runs-on: buildjet-4vcpu-ubuntu-2204
# services:
# ollama:
# image: ollama/ollama
# ports:
# - 11434:11434


steps:
- name: Checkout repository
Expand All @@ -36,21 +39,32 @@ jobs:
run: |
poetry install --no-interaction --all-extras

- 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
poetry add torch

# - 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 container
run: |
curl -d '{"model": "llama3.2", "stream": false, "prompt":"Whatever I say, asnwer with Yes"}' http://localhost:11434/api/generate
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: |
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
Expand All @@ -60,6 +74,31 @@ jobs:
echo "Ollama failed to start"
exit 1

- name: Pull required Ollama models
run: |
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
run: |
curl -X POST http://localhost:11434/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "phi4",
"stream": false,
"messages": [
{ "role": "system", "content": "You are a helpful assistant." },
{ "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: Dump Docker logs
run: |
docker ps
Expand All @@ -72,13 +111,15 @@ jobs:
GRAPHISTRY_USERNAME: ${{ secrets.GRAPHISTRY_USERNAME }}
GRAPHISTRY_PASSWORD: ${{ secrets.GRAPHISTRY_PASSWORD }}
PYTHONFAULTHANDLER: 1
LLM_API_KEY: "ollama"

LLM_PROVIDER: "ollama"
LLM_ENDPOINT: "http://127.0.0.1:11434/api/generate"
LLM_MODEL: "ollama/llama3.2"
LLM_API_KEY: "ollama"
LLM_ENDPOINT: "http://localhost:11434/v1/"
LLM_MODEL: "phi4"
EMBEDDING_PROVIDER: "ollama"
EMBEDDING_MODEL: "avr/sfr-embedding-mistral:latest"
EMBEDDING_ENDPOINT: "http://127.0.0.1:11434/api/embeddings"
EMBEDDING_ENDPOINT: "http://localhost:11434/v1/"
EMBEDDING_DIMENSIONS: "4096"
HUGGINGFACE_TOKENIZER: "Salesforce/SFR-Embedding-Mistral"
run: poetry run python ./examples/python/simple_example.py
run: poetry run python ./examples/python/simple_example.py

79 changes: 37 additions & 42 deletions cognee/infrastructure/llm/ollama/adapter.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
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

import os

class OllamaAPIAdapter(LLMInterface):
"""Adapter for a Ollama API LLM provider using instructor with an OpenAI backend."""
Expand All @@ -17,21 +15,13 @@ class OllamaAPIAdapter(LLMInterface):

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix formatting issue.

Add a space after the equals sign.

-        self.api_version= api_version
+        self.api_version = api_version
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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
Expand Down Expand Up @@ -60,52 +50,57 @@ 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.")
def create_transcript(self, input_file: str) -> str:
"""Generate an audio transcript from a user query."""

# with open(input, 'rb') as audio_file:
# audio_data = audio_file.read()
if not os.path.isfile(input_file):
raise FileNotFoundError(f"The file {input_file} does not exist.")

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,
)
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
return transcription.text

def transcribe_image(self, input) -> BaseModel:
with open(input, "rb") as image_file:
def transcribe_image(self, input_file: str) -> str:
"""Transcribe content from an image using base64 encoding."""

if not os.path.isfile(input_file):
raise FileNotFoundError(f"The file {input_file} does not exist.")

Comment on lines +54 to +78
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Initialize missing transcription_model attribute.

The transcription_model attribute used in create_transcript is not initialized in the constructor.

Add the following to the constructor:

 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.transcription_model = model  # or add a new parameter if a different model is needed for transcription
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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 __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.transcription_model = model # or add a new parameter if a different model is needed for transcription

with open(input_file, "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,
)

# 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

Loading