Skip to content
This repository has been archived by the owner on Sep 19, 2024. It is now read-only.

2 introduce llm model abstraction #5

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .github/workflows/spellcheck.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: spellcheck
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
check-spelling:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Check Spelling
uses: rojopolis/spellcheck-github-actions@0.33.1
with:
config_path: .spellcheck.yml
task_name: Markdown
58 changes: 58 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: Run tests

on:
push:
pull_request:

jobs:
test:
permissions:
contents: "read"
id-token: "write"
runs-on: ubuntu-latest
services:
falkordb:
image: falkordb/falkordb:latest
ports:
- 6379:6379
steps:
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: 3.x # Update with desired Python version

- name: Cache Poetry virtualenv
id: cache
uses: actions/cache@v4
with:
path: ~/.poetry/virtualenvs
key: ${{ runner.os }}-poetry-${{ hashFiles('poetry.lock') }}
restore-keys: |
${{ runner.os }}-poetry-

- id: "auth"
uses: "google-github-actions/auth@v2"
with:
credentials_json: ${{ secrets.GCP_SA_KEY }}

- name: "Set up Cloud SDK"
uses: "google-github-actions/setup-gcloud@v2"
with:
version: ">= 363.0.0"

- name: Install Poetry
if: steps.cache.outputs.cache-hit != true
run: |
curl -sSL https://install.python-poetry.org | python3 -

- name: Install dependencies
run: poetry install

- name: Run tests
env:
PROJECT_ID: ${{ secrets.PROJECT_ID }}
REGION: ${{ vars.REGION }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
run: poetry run pytest
35 changes: 0 additions & 35 deletions config.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion falkordb_gemini_kg/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .classes.source import Source
Copy link

Choose a reason for hiding this comment

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

Remove unused import Source.

The Source class is imported but not used in this file. Consider removing it.

- from .classes.source import Source
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
from .classes.source import Source
Tools
Ruff

1-1: .classes.source.Source imported but unused; consider removing, adding to __all__, or using a redundant alias

(F401)

from .classes.ontology import Ontology
Copy link

Choose a reason for hiding this comment

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

Remove unused import Ontology.

The Ontology class is imported but not used in this file. Consider removing it.

- from .classes.ontology import Ontology
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
from .classes.ontology import Ontology
Tools
Ruff

2-2: .classes.ontology.Ontology imported but unused; consider removing, adding to __all__, or using a redundant alias

(F401)

from .kg import KnowledgeGraph
Copy link

Choose a reason for hiding this comment

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

Remove unused import KnowledgeGraph.

The KnowledgeGraph class is imported but not used in this file. Consider removing it.

- from .kg import KnowledgeGraph
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
from .kg import KnowledgeGraph
Tools
Ruff

3-3: .kg.KnowledgeGraph imported but unused; consider removing, adding to __all__, or using a redundant alias

(F401)

from .classes.model_config import KnowledgeGraphModelConfig, StepModelConfig
from .classes.model_config import KnowledgeGraphModelConfig
Copy link

Choose a reason for hiding this comment

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

Remove unused import KnowledgeGraphModelConfig.

The KnowledgeGraphModelConfig class is imported but not used in this file. Consider removing it.

- from .classes.model_config import KnowledgeGraphModelConfig
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
from .classes.model_config import KnowledgeGraphModelConfig
Tools
Ruff

4-4: .classes.model_config.KnowledgeGraphModelConfig imported but unused; consider removing, adding to __all__, or using a redundant alias

(F401)

from .steps.create_ontology_step import CreateOntologyStep
Copy link

Choose a reason for hiding this comment

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

Remove unused import CreateOntologyStep.

The CreateOntologyStep class is imported but not used in this file. Consider removing it.

- from .steps.create_ontology_step import CreateOntologyStep
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
from .steps.create_ontology_step import CreateOntologyStep
Tools
Ruff

5-5: .steps.create_ontology_step.CreateOntologyStep imported but unused; consider removing, adding to __all__, or using a redundant alias

(F401)


# Setup Null handler
Expand Down
2 changes: 2 additions & 0 deletions falkordb_gemini_kg/classes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
from .source import Source
Copy link

Choose a reason for hiding this comment

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

Remove unused import Source.

The Source class is imported but not used in this file. Consider removing it.

- from .source import Source
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
from .source import Source
Tools
Ruff

2-2: .source.Source imported but unused; consider removing, adding to __all__, or using a redundant alias

(F401)

from .node import Node
Copy link

Choose a reason for hiding this comment

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

Remove unused import Node.

The Node class is imported but not used in this file. Consider removing it.

- from .node import Node
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
from .node import Node
Tools
Ruff

3-3: .node.Node imported but unused; consider removing, adding to __all__, or using a redundant alias

(F401)

from .edge import Edge
Copy link

Choose a reason for hiding this comment

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

Remove unused import Edge.

The Edge class is imported but not used in this file. Consider removing it.

- from .edge import Edge
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
from .edge import Edge
Tools
Ruff

4-4: .edge.Edge imported but unused; consider removing, adding to __all__, or using a redundant alias

(F401)

from .attribute import Attribute, AttributeType
Copy link

Choose a reason for hiding this comment

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

Remove unused import AttributeType.

The AttributeType class is imported but not used in this file. Consider removing it.

- from .attribute import AttributeType

Committable suggestion was skipped due to low confidence.

Tools
Ruff

5-5: .attribute.Attribute imported but unused; consider removing, adding to __all__, or using a redundant alias

(F401)


5-5: .attribute.AttributeType imported but unused; consider removing, adding to __all__, or using a redundant alias

(F401)


Remove unused import Attribute.

The Attribute class is imported but not used in this file. Consider removing it.

- from .attribute import Attribute, AttributeType
+ from .attribute import AttributeType
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
from .attribute import Attribute, AttributeType
from .attribute import AttributeType
Tools
Ruff

5-5: .attribute.Attribute imported but unused; consider removing, adding to __all__, or using a redundant alias

(F401)


5-5: .attribute.AttributeType imported but unused; consider removing, adding to __all__, or using a redundant alias

(F401)


# Setup Null handler
import logging

logger = logging.getLogger(__name__)
logger.addHandler(logging.NullHandler())
22 changes: 11 additions & 11 deletions falkordb_gemini_kg/classes/attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@
logger = logging.getLogger(__name__)


class _AttributeType:
class AttributeType:
STRING = "string"
NUMBER = "number"
BOOLEAN = "boolean"

@staticmethod
def fromString(txt: str):
if txt.isdigit():
return _AttributeType.NUMBER
return AttributeType.NUMBER
elif txt.lower() in ["true", "false"]:
return _AttributeType.BOOLEAN
return _AttributeType.STRING
return AttributeType.BOOLEAN
return AttributeType.STRING


class Attribute:
def __init__(
self, name: str, attr_type: _AttributeType, unique: bool, required: bool = False
self, name: str, attr_type: AttributeType, unique: bool, required: bool = False
):
self.name = name
self.type = attr_type
Expand All @@ -32,9 +32,9 @@ def __init__(
def from_json(txt: str):
txt = txt if isinstance(txt, dict) else json.loads(txt)
if txt["type"] not in [
_AttributeType.STRING,
_AttributeType.NUMBER,
_AttributeType.BOOLEAN,
AttributeType.STRING,
AttributeType.NUMBER,
AttributeType.BOOLEAN,
]:
raise Exception(f"Invalid attribute type: {txt['type']}")
return Attribute(
Expand All @@ -52,9 +52,9 @@ def from_string(txt: str):
required = "*" in txt

if attr_type not in [
_AttributeType.STRING,
_AttributeType.NUMBER,
_AttributeType.BOOLEAN,
AttributeType.STRING,
AttributeType.NUMBER,
AttributeType.BOOLEAN,
]:
raise Exception(f"Invalid attribute type: {attr_type}")

Expand Down
4 changes: 2 additions & 2 deletions falkordb_gemini_kg/classes/edge.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
import re
import logging
from .attribute import Attribute, _AttributeType
from .attribute import Attribute, AttributeType
from falkordb import Node as GraphNode, Edge as GraphEdge
from falkordb_gemini_kg.fixtures.regex import *
Copy link

Choose a reason for hiding this comment

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

Avoid using star imports.

The star import (from falkordb_gemini_kg.fixtures.regex import *) can lead to undefined names and makes it harder to track dependencies. Import specific classes and functions instead.

- from falkordb_gemini_kg.fixtures.regex import *
+ from falkordb_gemini_kg.fixtures.regex import EDGE_LABEL_REGEX, NODE_LABEL_REGEX, EDGE_REGEX
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
from falkordb_gemini_kg.fixtures.regex import *
from falkordb_gemini_kg.fixtures.regex import EDGE_LABEL_REGEX, NODE_LABEL_REGEX, EDGE_REGEX
Tools
Ruff

6-6: from falkordb_gemini_kg.fixtures.regex import * used; unable to detect undefined names

(F403)


Expand Down Expand Up @@ -58,7 +58,7 @@ def from_graph(edge: GraphEdge, nodes: list[GraphNode]):
[
Attribute(
attr,
_AttributeType.fromString(edge.properties),
AttributeType.fromString(edge.properties),
"!" in edge.properties[attr],
"*" in edge.properties[attr],
)
Expand Down
60 changes: 8 additions & 52 deletions falkordb_gemini_kg/classes/model_config.py
Original file line number Diff line number Diff line change
@@ -1,66 +1,22 @@
from vertexai.generative_models import GenerationConfig


class StepModelGenerationConfig:
def __init__(
self,
temperature: float,
top_p: float,
top_k: int,
candidate_count: int,
max_output_tokens: int,
stop_sequences: list[str],
):
self.temperature = temperature
self.top_p = top_p
self.top_k = top_k
self.candidate_count = candidate_count
self.max_output_tokens = max_output_tokens
self.stop_sequences = stop_sequences

def to_generation_config(self) -> GenerationConfig:
return GenerationConfig(
temperature=self.temperature,
top_p=self.top_p,
top_k=self.top_k,
candidate_count=self.candidate_count,
max_output_tokens=self.max_output_tokens,
stop_sequences=self.stop_sequences,
)


class StepModelConfig:

def __init__(
self, model: str, generation_config: StepModelGenerationConfig | None = None
):
self.model = model
self.generation_config = generation_config
from falkordb_gemini_kg.models import GenerativeModel


class KnowledgeGraphModelConfig:

def __init__(
self,
extract_data: StepModelConfig | None = None,
cypher_generation: StepModelConfig | None = None,
qa: StepModelConfig | None = None,
extract_data: GenerativeModel,
cypher_generation: GenerativeModel,
qa: GenerativeModel,
):
self.extract_data = extract_data
self.cypher_generation = cypher_generation
self.qa = qa

@staticmethod
def from_dict(d: dict):
model = d.get("model")
generation_config = d.get("generation_config")
extract_data = StepModelConfig(model=model, generation_config=generation_config)
cypher_generation = StepModelConfig(
model=model, generation_config=generation_config
)
qa = StepModelConfig(model=model, generation_config=generation_config)
def with_model(model: GenerativeModel):
return KnowledgeGraphModelConfig(
extract_data=extract_data,
cypher_generation=cypher_generation,
qa=qa,
extract_data=model,
cypher_generation=model,
qa=model,
)
4 changes: 2 additions & 2 deletions falkordb_gemini_kg/classes/node.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json
import logging
from .attribute import Attribute, _AttributeType
from .attribute import Attribute, AttributeType
from falkordb import Node as GraphNode

logger = logging.getLogger(__name__)
Expand All @@ -18,7 +18,7 @@ def from_graph(node: GraphNode):
[
Attribute(
attr,
_AttributeType.fromString(node.properties[attr]),
AttributeType.fromString(node.properties[attr]),
"!" in node.properties[attr],
)
for attr in node.properties
Expand Down
6 changes: 3 additions & 3 deletions falkordb_gemini_kg/classes/ontology.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
from falkordb import Graph
from falkordb_gemini_kg.classes.source import AbstractSource
from falkordb_gemini_kg.classes.model_config import StepModelConfig
from falkordb_gemini_kg.models import GenerativeModel
import falkordb_gemini_kg
import logging
from .edge import Edge
Expand All @@ -19,12 +19,12 @@ def __init__(self, nodes: list[Node] = [], edges: list[Edge] = []):
def from_sources(
sources: list[AbstractSource],
boundaries: str,
model_config: StepModelConfig,
model: GenerativeModel,
) -> "Ontology":
step = falkordb_gemini_kg.CreateOntologyStep(
sources=sources,
ontology=Ontology(),
model_config=model_config,
model=model,
)

return step.run(boundaries=boundaries)
Expand Down
3 changes: 0 additions & 3 deletions falkordb_gemini_kg/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,6 @@ def validate_cypher_edge_directions(cypher: str, ontology: Ontology):
if ontology_edge is None:
errors.append(f"Edge {edge_label} not found in ontology")

logger.debug(
f"ontology_edge: {ontology_edge}"
)
if (
not ontology_edge.source.label == source_label
or not ontology_edge.target.label == target_label
Expand Down
8 changes: 5 additions & 3 deletions falkordb_gemini_kg/kg.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def _create_graph_with_sources(self, sources: list[AbstractSource] | None = None
step = ExtractDataStep(
sources=list(sources),
ontology=self.ontology,
model_config=self._model_config.extract_data,
model=self._model_config.extract_data,
graph=self.graph,
)

Expand All @@ -124,16 +124,18 @@ def ask(self, question: str) -> str:
>>> ans = kg.ask("List a few movies in which that actored played in", history)
"""

cypher_chat_session = self._model_config.cypher_generation.start_chat()
cypher_step = GraphQueryGenerationStep(
ontology=self.ontology,
model_config=self._model_config.cypher_generation,
chat_session=cypher_chat_session,
graph=self.graph,
)

(context, cypher) = cypher_step.run(question)

qa_chat_session = self._model_config.qa.start_chat()
qa_step = QAStep(
model_config=self._model_config.qa,
chat_session=qa_chat_session,
)

answer = qa_step.run(question, cypher, context)
Expand Down
1 change: 1 addition & 0 deletions falkordb_gemini_kg/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .model import *
Copy link

Choose a reason for hiding this comment

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

Avoid using wildcard imports.

Using from .model import * can lead to namespace pollution and makes it difficult to track which names are imported. Replace the wildcard import with explicit imports.

- from .model import *
+ from .model import GenerativeModel, GenerativeModelConfig, GenerativeModelChatSession, FinishReason, GenerationResponse
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
from .model import *
from .model import GenerativeModel, GenerativeModelConfig, GenerativeModelChatSession, FinishReason, GenerationResponse
Tools
Ruff

1-1: from .model import * used; unable to detect undefined names

(F403)

Loading