Skip to content

Commit

Permalink
Merge branch 'feature/cog-971-preparing-swe-bench-run' of https://git…
Browse files Browse the repository at this point in the history
…hub.com/topoteretes/cognee into feature/cog-971-preparing-swe-bench-run
  • Loading branch information
hajdul88 committed Jan 10, 2025
2 parents 06e8d22 + 16155f0 commit c163e35
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/profiling.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ jobs:
# chmod +x cognee/api/v1/cognify/code_graph_pipeline.py
# # Run Scalene
# poetry run pyinstrument --renderer json -o head_results.json cognee/api/v1/cognify/code_graph_pipeline.py
#

# # Compare profiling results
# - name: Compare profiling results
# run: |
Expand Down
12 changes: 12 additions & 0 deletions cognee/api/v1/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,18 @@ def set_relational_db_config(config_dict: dict):
message=f"'{key}' is not a valid attribute of the config."
)

@staticmethod
def set_graph_db_config(config_dict: dict) -> None:
"""
Updates the graph db config with values from config_dict.
"""
graph_db_config = get_graph_config()
for key, value in config_dict.items():
if hasattr(graph_db_config, key):
object.__setattr__(graph_db_config, key, value)
else:
raise AttributeError(message=f"'{key}' is not a valid attribute of the config.")

@staticmethod
def set_vector_db_config(config_dict: dict):
"""
Expand Down
3 changes: 3 additions & 0 deletions cognee/api/v1/search/search_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
from cognee.tasks.graph import query_graph_connections
from cognee.tasks.summarization import query_summaries
from cognee.tasks.completion import query_completion
from cognee.tasks.completion import graph_query_completion


class SearchType(Enum):
SUMMARIES = "SUMMARIES"
INSIGHTS = "INSIGHTS"
CHUNKS = "CHUNKS"
COMPLETION = "COMPLETION"
GRAPH_COMPLETION = "GRAPH_COMPLETION"


async def search(
Expand Down Expand Up @@ -65,6 +67,7 @@ async def specific_search(query_type: SearchType, query: str, user) -> list:
SearchType.INSIGHTS: query_graph_connections,
SearchType.CHUNKS: query_chunks,
SearchType.COMPLETION: query_completion,
SearchType.GRAPH_COMPLETION: graph_query_completion,
}

search_task = search_tasks.get(query_type)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Answer the question using the provided context. If the provided context is not connected to the question, just answer "The provided knowledge base does not contain the answer to the question". Be as brief as possible.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The question is: `{{ question }}`
and here is the context provided with a set of relationships from a knowledge graph separated by \n---\n each represented as node1 -- relation -- node2 triplet: `{{ context }}`
1 change: 1 addition & 0 deletions cognee/tasks/completion/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .query_completion import query_completion
from .graph_query_completion import graph_query_completion
46 changes: 46 additions & 0 deletions cognee/tasks/completion/graph_query_completion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from cognee.infrastructure.databases.vector import get_vector_engine
from cognee.tasks.completion.exceptions import NoRelevantDataFound
from cognee.infrastructure.llm.get_llm_client import get_llm_client
from cognee.infrastructure.llm.prompts import read_query_prompt, render_prompt
from cognee.modules.retrieval.brute_force_triplet_search import brute_force_triplet_search


def retrieved_edges_to_string(retrieved_edges: list) -> str:
edge_strings = []
for edge in retrieved_edges:
node1_string = edge.node1.attributes.get("text") or edge.node1.attributes.get("name")
node2_string = edge.node2.attributes.get("text") or edge.node2.attributes.get("name")
edge_string = edge.attributes["relationship_type"]
edge_str = f"{node1_string} -- {edge_string} -- {node2_string}"
edge_strings.append(edge_str)
return "\n---\n".join(edge_strings)


async def graph_query_completion(query: str) -> list:
"""
Parameters:
- query (str): The query string to compute.
Returns:
- list: Answer to the query.
"""
found_triplets = await brute_force_triplet_search(query, top_k=5)

if len(found_triplets) == 0:
raise NoRelevantDataFound

args = {
"question": query,
"context": retrieved_edges_to_string(found_triplets),
}
user_prompt = render_prompt("graph_context_for_question.txt", args)
system_prompt = read_query_prompt("answer_simple_question_restricted.txt")

llm_client = get_llm_client()
computed_answer = await llm_client.acreate_structured_output(
text_input=user_prompt,
system_prompt=system_prompt,
response_model=str,
)

return [computed_answer]
4 changes: 2 additions & 2 deletions cognee/tasks/repo_processor/expand_dependency_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
from cognee.infrastructure.engine import DataPoint
from cognee.shared.CodeGraphEntities import CodeFile, CodePart
from cognee.tasks.repo_processor.extract_code_parts import extract_code_parts

import logging

logger = logging.getLogger("task:repo_processor")
logger = logging.getLogger(__name__)



def _add_code_parts_nodes_and_edges(code_file: CodeFile, part_type, code_parts) -> None:
Expand Down
3 changes: 1 addition & 2 deletions cognee/tasks/repo_processor/extract_code_parts.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from typing import Dict, List
import parso

import logging

logger = logging.getLogger("task:repo_processor")
logger = logging.getLogger(__name__)


def _extract_parts_from_module(module, parts_dict: Dict[str, List[str]]) -> Dict[str, List[str]]:
Expand Down
3 changes: 1 addition & 2 deletions cognee/tasks/repo_processor/get_local_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
import jedi
import parso
from parso.tree import BaseNode

import logging

logger = logging.getLogger("task:repo_processor")
logger = logging.getLogger(__name__)


@contextmanager
Expand Down
2 changes: 1 addition & 1 deletion cognee/tasks/repo_processor/get_source_code_chunks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from cognee.infrastructure.engine import DataPoint
from cognee.shared.CodeGraphEntities import CodeFile, CodePart, SourceCodeChunk

logger = logging.getLogger("task:get_source_code_chunks")
logger = logging.getLogger(__name__)


def _count_tokens(tokenizer: tiktoken.Encoding, source_code: str) -> int:
Expand Down
4 changes: 3 additions & 1 deletion cognee/tasks/repo_processor/top_down_repo_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import parso
from tqdm import tqdm

from . import logger
import logging

logger = logging.getLogger(__name__)

_NODE_TYPE_MAP = {
"funcdef": "func_def",
Expand Down
12 changes: 6 additions & 6 deletions examples/python/dynamic_steps_example.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import cognee
import asyncio
import logging
from cognee.modules.retrieval.brute_force_triplet_search import brute_force_triplet_search
from cognee.modules.retrieval.brute_force_triplet_search import format_triplets

from cognee.api.v1.search import SearchType
from cognee.shared.utils import setup_logging

job_1 = """
Expand Down Expand Up @@ -185,14 +185,14 @@ async def main(enable_steps):

# Step 4: Query insights
if enable_steps.get("retriever"):
results = await brute_force_triplet_search(
"Who has the most experience with graphic design?"
search_results = await cognee.search(
SearchType.GRAPH_COMPLETION, query_text="Who has experience in design tools?"
)
print(format_triplets(results))
print(search_results)


if __name__ == "__main__":
setup_logging(logging.ERROR)
setup_logging(logging.INFO)

rebuild_kg = True
retrieve = True
Expand Down

0 comments on commit c163e35

Please sign in to comment.