Skip to content

Commit

Permalink
Merge pull request #34 from TheDataGuild/refactor/measure-time-decorator
Browse files Browse the repository at this point in the history
Refactor/measure time decorator
  • Loading branch information
Quantisan authored Sep 23, 2023
2 parents 459d5f1 + cf5fd6c commit 25f1315
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
10 changes: 8 additions & 2 deletions mind_palace/index.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
from llama_index.llms import OpenAI
import llama_index as li

from measure import time_function


@time_function
def index_nodes(nodes, model="gpt-3.5-turbo"):
embed_model = "local"
service_context = li.ServiceContext.from_defaults(
llm=OpenAI(model=model),
embed_model="local"
embed_model=embed_model
# Q: how much does a better embedding model help?
)

print(f"Creating an index with {len(nodes)} number of nodes using model {model}")
print(
f"Creating an index with {len(nodes)} number of nodes using embedding model {embed_model} and querying LLM {model}"
)
index = li.VectorStoreIndex(nodes, service_context=service_context)

return index
16 changes: 11 additions & 5 deletions mind_palace/measure.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@


def time_function(func):
start_time = time.time()
result = func()
end_time = time.time()
elapsed_time = end_time - start_time
return result, elapsed_time
"""Decorator to time and print timing of a single function call"""

def timed_func(*args, **kwargs):
start_time = time.time()
result = func(*args, **kwargs)
end_time = time.time()
elapsed_sec = end_time - start_time
print(f"Elapsed time calling {func.__name__}(): {elapsed_sec:.1f} seconds")
return result

return timed_func
14 changes: 10 additions & 4 deletions tests/end_to_end/test_query.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
from tests.context import extract, index, measure
from tests.context import extract, index
import llama_index as li
import pytest


def bootstrap_index(xml_dir):
nodes = extract.seed_nodes(xml_dir)
vector_index, elapsed_time = measure.time_function(lambda: index.index_nodes(nodes))
print(f"Elapsed time {elapsed_time:.1f} seconds: Indexed {len(nodes)} nodes")
return vector_index
return index.index_nodes(nodes)


@pytest.mark.skip(reason="This test takes a minute to run")
def test_bootstrap_index():
xml_dir = "./resources/xmls/12-pdfs-from-steve-aug-22/"
vector_index = bootstrap_index(xml_dir)
assert vector_index is not None


def persist_index(vector_index):
Expand Down

0 comments on commit 25f1315

Please sign in to comment.