Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
SLKhadeeja committed Nov 12, 2024
1 parent eddb176 commit e3c89ad
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 89 deletions.
2 changes: 1 addition & 1 deletion examples/python/book-search/book_search/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from ahnlich_client_py.clients import AhnlichAIClient
from ahnlich_client_py.internals import ai_query
from ahnlich_client_py.internals import ai_query
63 changes: 38 additions & 25 deletions examples/python/book-search/book_search/split_book.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,48 @@

BASE_DIR = Path(__file__).resolve().parent.parent


def load_epub(file_path):

book = epub.read_epub(file_path)
return book

book = epub.read_epub(file_path)
return book


def split_into_chapters(book):
paragraphs = []
for chapter_num, item in enumerate(book.get_items_of_type(ebooklib.ITEM_DOCUMENT), start=1):
paragraph_num = 1
content = item.get_body_content()
soup = BeautifulSoup(content, 'html.parser')

for p in soup.find_all('p'):
text = p.text.strip().replace('\n', '')
if text:
paragraphs.append((
ai_query.StoreInput__RawString(text),
{
"chapter": ai_query.MetadataValue__RawString(f'{chapter_num}'),
"paragraph": ai_query.MetadataValue__RawString(f'{paragraph_num}')}
))
paragraph_num += 1
return paragraphs
paragraphs = []
for chapter_num, item in enumerate(
book.get_items_of_type(ebooklib.ITEM_DOCUMENT), start=1
):
paragraph_num = 1
content = item.get_body_content()
soup = BeautifulSoup(content, "html.parser")

for p in soup.find_all("p"):
text = p.text.strip().replace("\n", "")
if text:
paragraphs.append(
(
ai_query.StoreInput__RawString(text),
{
"chapter": ai_query.MetadataValue__RawString(
f"{chapter_num}"
),
"paragraph": ai_query.MetadataValue__RawString(
f"{paragraph_num}"
),
},
)
)
paragraph_num += 1
return paragraphs


def process_epub(file_path):
book = load_epub(file_path)
chapters = split_into_chapters(book)
return chapters
book = load_epub(file_path)
chapters = split_into_chapters(book)
return chapters


def get_book():
result = process_epub(f"{BASE_DIR}/book_search/animal_farm.epub")
return result
result = process_epub(f"{BASE_DIR}/book_search/animal_farm.epub")
return result
112 changes: 60 additions & 52 deletions examples/python/book-search/search_actions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
from concurrent.futures import Future, ThreadPoolExecutor
from functools import partial
from typing import List
from concurrent.futures import ThreadPoolExecutor, Future

from ahnlich_client_py.clients import AhnlichAIClient
from ahnlich_client_py.config import AhnlichPoolSettings
Expand All @@ -14,71 +14,79 @@
"query_model": ai_query.AIModel__BGEBaseEnV15(),
"index_model": ai_query.AIModel__BGEBaseEnV15(),
"predicates": ["chapter", "paragraph"],
"error_if_exists": False
"error_if_exists": False,
}

pool_setting = AhnlichPoolSettings()
pool_setting.max_pool_size = 35
ai_client = AhnlichAIClient(address="127.0.0.1", port=1370, connect_timeout_sec=600, pool_settings=pool_setting)
ai_client = AhnlichAIClient(
address="127.0.0.1", port=1370, connect_timeout_sec=600, pool_settings=pool_setting
)


def set_client(inputs):
response = ai_client.set(
store_name=ai_store_payload_with_predicates["store_name"],
inputs=inputs,
preprocess_action=ai_query.PreprocessAction__RawString(
ai_query.StringAction__ErrorIfTokensExceed()
),
)
response = ai_client.set(
store_name=ai_store_payload_with_predicates["store_name"],
inputs=inputs,
preprocess_action=ai_query.PreprocessAction__RawString(
ai_query.StringAction__ErrorIfTokensExceed()
),
)

print(response)
print(response)


def create_tasks():
store_inputs = get_book()
return [partial(set_client, store_inputs[i:i+10]) for i in range(0, len(store_inputs), 10)]
store_inputs = get_book()
return [
partial(set_client, store_inputs[i : i + 10])
for i in range(0, len(store_inputs), 10)
]


def insert_book():
ai_client.drop_store(
store_name=ai_store_payload_with_predicates["store_name"],
error_if_not_exists=False
)
ai_client.create_store(**ai_store_payload_with_predicates)

task_definitions = create_tasks()
thread_tasks: List[Future] = []
with ThreadPoolExecutor(max_workers=30) as executor:
for partial_func in task_definitions:
thread_tasks.append(executor.submit(partial_func))

for task in thread_tasks:
try:
_ = task.result()
except Exception as exc:
print(f'Exception gotten from task {task}... {exc}')

print("Cleaning up Connection Pool...")
ai_client.cleanup()
print("Shut down!....")
ai_client.drop_store(
store_name=ai_store_payload_with_predicates["store_name"],
error_if_not_exists=False,
)
ai_client.create_store(**ai_store_payload_with_predicates)

task_definitions = create_tasks()
thread_tasks: List[Future] = []
with ThreadPoolExecutor(max_workers=30) as executor:
for partial_func in task_definitions:
thread_tasks.append(executor.submit(partial_func))

for task in thread_tasks:
try:
_ = task.result()
except Exception as exc:
print(f"Exception gotten from task {task}... {exc}")

print("Cleaning up Connection Pool...")
ai_client.cleanup()
print("Shut down!....")


def run_get_simn_text(input_query):
ai_client = AhnlichAIClient(address="127.0.0.1", port=1370)
builder = ai_client.pipeline()
builder.get_sim_n(
store_name=ai_store_payload_with_predicates["store_name"],
search_input=ai_query.StoreInput__RawString(input_query),
closest_n=5,
algorithm=ai_query.Algorithm__CosineSimilarity(),
)
return builder.exec()
ai_client = AhnlichAIClient(address="127.0.0.1", port=1370)
builder = ai_client.pipeline()
builder.get_sim_n(
store_name=ai_store_payload_with_predicates["store_name"],
search_input=ai_query.StoreInput__RawString(input_query),
closest_n=5,
algorithm=ai_query.Algorithm__CosineSimilarity(),
)
return builder.exec()


def search_phrase():
input_query = input("Please enter the phrase: ")
response = run_get_simn_text(input_query)
for result in response.results[0].value.value:
print(f'Chapter {result[1]["chapter"].value}')
print(f'Paragraph {result[1]["paragraph"].value}')
print(result[0].value)
print('\n')

ai_client.cleanup()
input_query = input("Please enter the phrase: ")
response = run_get_simn_text(input_query)
for result in response.results[0].value.value:
print(f'Chapter {result[1]["chapter"].value}')
print(f'Paragraph {result[1]["paragraph"].value}')
print(result[0].value)
print("\n")

ai_client.cleanup()
2 changes: 1 addition & 1 deletion sdk/ahnlich-client-py/ahnlich_client_py/clients/ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def create_store(
index_model=index_model,
predicates=predicates,
non_linear_indices=non_linear_indices,
error_if_exists=error_if_exists
error_if_exists=error_if_exists,
)
return self.process_request(builder.to_server_query())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
from contextlib import _GeneratorContextManager
from ipaddress import IPv4Address

from generic_connection_pool.exceptions import ConnectionPoolClosedError
from generic_connection_pool.threading import ConnectionPool

from ahnlich_client_py.config import AhnlichPoolSettings
from ahnlich_client_py.exceptions import AhnlichClientException
from ahnlich_client_py.internals import (
Expand All @@ -16,6 +13,8 @@
pool_wrapper,
protocol,
)
from generic_connection_pool.exceptions import ConnectionPoolClosedError
from generic_connection_pool.threading import ConnectionPool


class BaseClient:
Expand Down
1 change: 0 additions & 1 deletion sdk/ahnlich-client-py/ahnlich_client_py/libs.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import typing

import numpy as np

from ahnlich_client_py import exceptions as ah_exceptions
from ahnlich_client_py.internals import db_query
from ahnlich_client_py.internals import serde_types as st
Expand Down
1 change: 0 additions & 1 deletion sdk/ahnlich-client-py/ahnlich_client_py/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import time

import pytest

from ahnlich_client_py import clients, config, db_query
from ahnlich_client_py.libs import create_store_key

Expand Down
3 changes: 1 addition & 2 deletions sdk/ahnlich-client-py/demo_embed.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import io
from urllib.request import urlopen

from PIL import Image

from ahnlich_client_py.clients import AhnlichAIClient
from ahnlich_client_py.internals import ai_query
from PIL import Image

ai_store_payload_no_predicates = {
"store_name": "Diretnan Stores",
Expand Down
5 changes: 2 additions & 3 deletions sdk/ahnlich-client-py/demo_tracing.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import os
import traceback

from ahnlich_client_py.clients import AhnlichAIClient
from ahnlich_client_py.internals import ai_query
from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor

from ahnlich_client_py.clients import AhnlichAIClient
from ahnlich_client_py.internals import ai_query

ai_store_payload_no_predicates = {
"store_name": "Diretnan Stores",
"query_model": ai_query.AIModel__AllMiniLML6V2(),
Expand Down

0 comments on commit e3c89ad

Please sign in to comment.