Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add Lint to CI (Ruff & Black) #63

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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
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
Expand Down Expand Up @@ -60,6 +60,11 @@ jobs:
- name: Install dependencies
run: poetry install --extras "all"

- name: Run Lint
run: |
poetry run ruff check ./graphrag_sdk/
poetry run black ./graphrag_sdk/ --check

- name: Wait for Ollama to be ready
run: |
until curl -s http://localhost:11434; do
Expand Down
24 changes: 14 additions & 10 deletions examples/movies/demo-movies.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"\n",
"# Load environment variables\n",
"load_dotenv()\n",
"logging.disable(logging.CRITICAL)\n"
"logging.disable(logging.CRITICAL)"
]
},
{
Expand All @@ -57,7 +57,7 @@
"falkor_host = \"\"\n",
"falkor_port = None\n",
"falkor_username = \"\"\n",
"falkor_password = \"\"\n"
"falkor_password = \"\""
]
},
{
Expand All @@ -75,12 +75,14 @@
"metadata": {},
"outputs": [],
"source": [
"urls = [\"https://www.rottentomatoes.com/m/side_by_side_2012\",\n",
"\"https://www.rottentomatoes.com/m/matrix\",\n",
"\"https://www.rottentomatoes.com/m/matrix_revolutions\",\n",
"\"https://www.rottentomatoes.com/m/matrix_reloaded\",\n",
"\"https://www.rottentomatoes.com/m/speed_1994\",\n",
"\"https://www.rottentomatoes.com/m/john_wick_chapter_4\"]\n",
"urls = [\n",
" \"https://www.rottentomatoes.com/m/side_by_side_2012\",\n",
" \"https://www.rottentomatoes.com/m/matrix\",\n",
" \"https://www.rottentomatoes.com/m/matrix_revolutions\",\n",
" \"https://www.rottentomatoes.com/m/matrix_reloaded\",\n",
" \"https://www.rottentomatoes.com/m/speed_1994\",\n",
" \"https://www.rottentomatoes.com/m/john_wick_chapter_4\",\n",
"]\n",
"\n",
"sources = [URL(url) for url in urls]"
]
Expand Down Expand Up @@ -136,7 +138,7 @@
" host=falkor_host,\n",
" port=falkor_port,\n",
" username=falkor_username,\n",
" password=falkor_password\n",
" password=falkor_password,\n",
")\n",
"kg.process_sources(sources)"
]
Expand Down Expand Up @@ -190,7 +192,9 @@
"answer = chat.send_message(\"Who is the director of the movie Side by Side?\")\n",
"print(f\"Q: {answer['question']} \\nA: {answer['response']}\\n\")\n",
"\n",
"answer = chat.send_message(\"Order the directors that you mentioned in all of our conversation by lexical order.\")\n",
"answer = chat.send_message(\n",
" \"Order the directors that you mentioned in all of our conversation by lexical order.\"\n",
")\n",
"print(f\"Q: {answer['question']} \\nA: {answer['response']}\\n\")"
]
}
Expand Down
24 changes: 19 additions & 5 deletions examples/trip/demo_orchestrator_trip.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@
"from graphrag_sdk.agents.kg_agent import KGAgent\n",
"from graphrag_sdk.models.openai import OpenAiGenerativeModel\n",
"from graphrag_sdk import (\n",
" Ontology, Entity, Relation, Attribute, AttributeType, KnowledgeGraph, KnowledgeGraphModelConfig\n",
" Ontology,\n",
" Entity,\n",
" Relation,\n",
" Attribute,\n",
" AttributeType,\n",
" KnowledgeGraph,\n",
" KnowledgeGraphModelConfig,\n",
")\n",
"\n",
"# Load environment variables\n",
Expand Down Expand Up @@ -402,7 +408,9 @@
"orchestrator.register_agent(attractions_agent)\n",
"\n",
"# Query the orchestrator\n",
"runner = orchestrator.ask(\"Create a two-day itinerary for a trip to Rome. Please don't ask me any questions. Just provide the best itinerary you can.\")"
"runner = orchestrator.ask(\n",
" \"Create a two-day itinerary for a trip to Rome. Please don't ask me any questions. Just provide the best itinerary you can.\"\n",
")"
]
},
{
Expand Down Expand Up @@ -464,7 +472,9 @@
}
],
"source": [
"runner = orchestrator.ask(\"Please tell me only the name of the restaurant for dinner at the first day that you mention in the itinerary of the trip\")\n",
"runner = orchestrator.ask(\n",
" \"Please tell me only the name of the restaurant for dinner at the first day that you mention in the itinerary of the trip\"\n",
")\n",
"print(runner.output)"
]
},
Expand Down Expand Up @@ -510,7 +520,9 @@
}
],
"source": [
"runner = orchestrator.ask(\"Can you change this restaurant to another one and give me the updated itinerary?\")\n",
"runner = orchestrator.ask(\n",
" \"Can you change this restaurant to another one and give me the updated itinerary?\"\n",
")\n",
"print(runner.output)"
]
},
Expand All @@ -528,7 +540,9 @@
}
],
"source": [
"runner = orchestrator.ask(\"Please tell me attraction on the morning at the first day that you mention in the itinerary of the trip\")\n",
"runner = orchestrator.ask(\n",
" \"Please tell me attraction on the morning at the first day that you mention in the itinerary of the trip\"\n",
")\n",
"print(runner.output)"
]
}
Expand Down
2 changes: 1 addition & 1 deletion graphrag_sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@
"Relation",
"Attribute",
"AttributeType",
]
]
2 changes: 1 addition & 1 deletion graphrag_sdk/agents/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .agent import Agent

__all__ = ['Agent']
__all__ = ["Agent"]
1 change: 0 additions & 1 deletion graphrag_sdk/agents/agent.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from abc import ABC, abstractmethod
from graphrag_sdk.models.model import GenerativeModelChatSession


class AgentResponseCode:
Expand Down
3 changes: 1 addition & 2 deletions graphrag_sdk/agents/kg_agent.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from graphrag_sdk.kg import KnowledgeGraph
from .agent import Agent
from graphrag_sdk.models import GenerativeModelChatSession


class KGAgent(Agent):
Expand Down Expand Up @@ -136,7 +135,7 @@ def run(self, params: dict) -> str:

"""
output = self.chat_session.send_message(params["prompt"])
return output['response']
return output["response"]

def __repr__(self):
"""
Expand Down
27 changes: 13 additions & 14 deletions graphrag_sdk/attribute.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
from graphrag_sdk.fixtures.regex import *
import logging
import re

Expand Down Expand Up @@ -42,18 +41,18 @@ def from_string(txt: str):


class Attribute:
""" Represents an attribute of an entity or relation in the ontology.

Args:
name (str): The name of the attribute.
attr_type (AttributeType): The type of the attribute.
unique (bool): Whether the attribute is unique.
required (bool): Whether the attribute is required.

Examples:
>>> attr = Attribute("name", AttributeType.STRING, True, True)
>>> print(attr)
name: "string!*"
"""Represents an attribute of an entity or relation in the ontology.

Args:
name (str): The name of the attribute.
attr_type (AttributeType): The type of the attribute.
unique (bool): Whether the attribute is unique.
required (bool): Whether the attribute is required.

Examples:
>>> attr = Attribute("name", AttributeType.STRING, True, True)
>>> print(attr)
name: "string!*"
"""

def __init__(
Expand Down Expand Up @@ -158,4 +157,4 @@ def __str__(self) -> str:
Returns:
str: A string representation of the Attribute object.
"""
return f"{self.name}: \"{self.type}{'!' if self.unique else ''}{'*' if self.required else ''}\""
return f'{self.name}: "{self.type}{"!" if self.unique else ""}{"*" if self.required else ""}"'
49 changes: 29 additions & 20 deletions graphrag_sdk/chat_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,17 @@ class ChatSession:
>>> chat_session.send_message("What is the capital of France?")
"""

def __init__(self, model_config: KnowledgeGraphModelConfig, ontology: Ontology, graph: Graph,
cypher_system_instruction: str, qa_system_instruction: str,
cypher_gen_prompt: str, qa_prompt: str, cypher_gen_prompt_history: str):
def __init__(
self,
model_config: KnowledgeGraphModelConfig,
ontology: Ontology,
graph: Graph,
cypher_system_instruction: str,
qa_system_instruction: str,
cypher_gen_prompt: str,
qa_prompt: str,
cypher_gen_prompt_history: str,
):
"""
Initializes a new ChatSession object.

Expand All @@ -45,21 +53,22 @@ def __init__(self, model_config: KnowledgeGraphModelConfig, ontology: Ontology,
self.model_config = model_config
self.graph = graph
self.ontology = ontology
cypher_system_instruction = cypher_system_instruction.format(ontology=str(ontology.to_json()))
cypher_system_instruction = cypher_system_instruction.format(
ontology=str(ontology.to_json())
)


self.cypher_prompt = cypher_gen_prompt
self.qa_prompt = qa_prompt
self.cypher_prompt_with_history = cypher_gen_prompt_history

self.cypher_chat_session = (
model_config.cypher_generation.with_system_instruction(
cypher_system_instruction
).start_chat()
)
self.qa_chat_session = model_config.qa.with_system_instruction(
qa_system_instruction
).start_chat()
qa_system_instruction
).start_chat()
self.last_answer = None

def send_message(self, message: str):
Expand All @@ -71,9 +80,9 @@ def send_message(self, message: str):

Returns:
dict: The response to the message in the following format:
{"question": message,
"response": answer,
"context": context,
{"question": message,
"response": answer,
"context": context,
"cypher": cypher}
"""
cypher_step = GraphQueryGenerationStep(
Expand All @@ -82,7 +91,7 @@ def send_message(self, message: str):
ontology=self.ontology,
last_answer=self.last_answer,
cypher_prompt=self.cypher_prompt,
cypher_prompt_with_history=self.cypher_prompt_with_history
cypher_prompt_with_history=self.cypher_prompt_with_history,
)

(context, cypher) = cypher_step.run(message)
Expand All @@ -92,8 +101,8 @@ def send_message(self, message: str):
"question": message,
"response": "I am sorry, I could not find the answer to your question",
"context": None,
"cypher": None
}
"cypher": None,
}

qa_step = QAStep(
chat_session=self.qa_chat_session,
Expand All @@ -102,10 +111,10 @@ def send_message(self, message: str):

answer = qa_step.run(message, cypher, context)
self.last_answer = answer

return {
"question": message,
"response": answer,
"context": context,
"cypher": cypher
}
"question": message,
"response": answer,
"context": context,
"cypher": cypher,
}
2 changes: 1 addition & 1 deletion graphrag_sdk/document.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Document():
class Document:
"""
Common class containing text extracted from a source
"""
Expand Down
6 changes: 1 addition & 5 deletions graphrag_sdk/document_loaders/jsonl.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ def load(self) -> Iterator[Document]:
num_documents = num_rows // self.rows_per_document
for i in range(num_documents):
content = "\n".join(
rows[
i
* self.rows_per_document : (i + 1)
* self.rows_per_document
]
rows[i * self.rows_per_document : (i + 1) * self.rows_per_document]
)
yield Document(content)
22 changes: 10 additions & 12 deletions graphrag_sdk/document_loaders/pdf.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from typing import Iterator
from graphrag_sdk.document import Document

class PDFLoader():

class PDFLoader:
"""
Load PDF
"""
Expand All @@ -15,11 +16,11 @@ def __init__(self, path: str) -> None:
"""

try:
import pypdf
except ImportError:
raise ImportError(
"pypdf package not found, please install it with " "`pip install pypdf`"
)
__import__("pypdf")
except ModuleNotFoundError:
raise ModuleNotFoundError(
"pypdf package not found, please install it with `pip install pypdf`"
)

self.path = path

Expand All @@ -30,11 +31,8 @@ def load(self) -> Iterator[Document]:
Returns:
Iterator[Document]: document iterator
"""
from pypdf import PdfReader # pylint: disable=import-outside-toplevel

from pypdf import PdfReader # pylint: disable=import-outside-toplevel

reader = PdfReader(self.path)
yield from [
Document(page.extract_text())
for page in reader.pages
]
yield from [Document(page.extract_text()) for page in reader.pages]
9 changes: 4 additions & 5 deletions graphrag_sdk/document_loaders/text.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from typing import Iterator
from graphrag_sdk.document import Document

class TextLoader():

class TextLoader:
"""
Load Text
"""
Expand All @@ -24,7 +25,5 @@ def load(self) -> Iterator[Document]:
Iterator[Document]: document iterator
"""

with open(self.path, 'r') as f:
yield Document(
f.read()
)
with open(self.path, "r") as f:
yield Document(f.read())
Loading
Loading