Skip to content

Commit

Permalink
Implement a sync run method - experiment 1
Browse files Browse the repository at this point in the history
  • Loading branch information
stellasia committed Jan 2, 2025
1 parent 324fd2c commit a2aea05
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
Empty file.
5 changes: 5 additions & 0 deletions src/neo4j_graphrag/experimental/pipeline/kg_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
)
from neo4j_graphrag.generation.prompts import ERExtractionTemplate
from neo4j_graphrag.llm.base import LLMInterface
from neo4j_graphrag.utils import run_sync


class SimpleKGPipeline:
Expand Down Expand Up @@ -124,3 +125,7 @@ async def run_async(
PipelineResult: The result of the pipeline execution.
"""
return await self.runner.run({"file_path": file_path, "text": text})

def run(self, file_path: Optional[str] = None, text: Optional[str] = None) -> PipelineResult:
"""Run pipeline synchronously"""
return run_sync(self, file_path=file_path, text=text)
25 changes: 25 additions & 0 deletions src/neo4j_graphrag/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,35 @@
from __future__ import annotations

from typing import Optional
import asyncio
import concurrent.futures


def validate_search_query_input(
query_text: Optional[str] = None, query_vector: Optional[list[float]] = None
) -> None:
if not (bool(query_vector) ^ bool(query_text)):
raise ValueError("You must provide exactly one of query_vector or query_text.")


def run_sync(function, *args, **kwargs):
with concurrent.futures.ThreadPoolExecutor() as executor:
future = executor.submit(lambda: asyncio.run(function(*args, **kwargs)))
return_value = future.result()
return return_value


if __name__ == "__main__":
async def async_run(char: str, repeat: int = 2) -> str:
await asyncio.sleep(5)
return char * repeat

async def async_run_multiple(char, n=10):
return await asyncio.gather(*[
async_run(char)
for _ in range(n)
])

print(
run_sync(async_run_multiple, "abc")
)

0 comments on commit a2aea05

Please sign in to comment.