Skip to content

Commit

Permalink
update python examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Ami11111 committed Sep 10, 2024
1 parent 075638e commit fe005f2
Show file tree
Hide file tree
Showing 16 changed files with 71 additions and 103 deletions.
17 changes: 7 additions & 10 deletions example/ColBERT_reranker_example/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,18 @@ def create_test_env(self, schema: dict):
schema[self.inner_col_txt] = {"type": "varchar"}
schema[self.inner_col_float] = {"type": "tensorarray,128,float"}
schema[self.inner_col_bit] = {"type": "tensorarray,128,bit"}
import infinity
from infinity import NetworkAddress
import infinity.index as index
from infinity.common import ConflictType
# NOTICE: the following statement is for local infinity
self.infinity_obj = infinity.connect("/var/infinity")
import infinity_embedded as infinity
# enable the following statement to use remote infinity
# self.infinity_obj = infinity.connect(LOCAL_HOST)
self.infinity_obj.drop_database(self.test_db_name, ConflictType.Ignore)
#import infinity
self.infinity_obj = infinity.connect()
self.infinity_obj.drop_database(self.test_db_name, infinity.common.ConflictType.Ignore)
self.colbert_test_db = self.infinity_obj.create_database(self.test_db_name)
self.colbert_test_table = self.colbert_test_db.create_table(self.test_table_name, schema, ConflictType.Error)
self.colbert_test_table = self.colbert_test_db.create_table(self.test_table_name, schema, infinity.common.ConflictType.Error)
# NOTICE: the following statement is for english text
self.colbert_test_table.create_index("test_ft_index",
index.IndexInfo(self.inner_col_txt, index.IndexType.FullText),
ConflictType.Error)
infinity.index.IndexInfo(self.inner_col_txt, infinity.index.IndexType.FullText),
infinity.common.ConflictType.Error)
# please enable the following statement to use chinese text
# self.colbert_test_table.create_index("test_ft_index",
# index.IndexInfo(self.inner_col_txt, index.IndexType.FullText,
Expand Down
10 changes: 4 additions & 6 deletions example/delete_update_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@
This example is to connect local infinity instance, create table, insert data, delete and update data.
'''

import infinity
import infinity_embedded as infinity
#import infinity
import sys

try:
# open a local directory to store the data
infinity_instance = infinity.connect("/var/infinity")

# connect to server with 127.0.0.1
# infinity_instance = infinity.connect(infinity.common.LOCAL_HOST)
# open a local directory(default = "/var/infinity") or connect to server(default = NetworkAddress("127.0.0.1", 23817)) to store the data
infinity_instance = infinity.connect()

# 'default_db' is the default database
db_instance = infinity_instance.get_database("default_db")
Expand Down
10 changes: 4 additions & 6 deletions example/export_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,17 @@
This example is about connecting local infinity instance, creating table, ing data, importing file into a table, and exporting table's data
'''

import infinity
import infinity_embedded as infinity
#import infinity
import os
import sys

current_path = os.path.abspath(__file__)
project_directory = os.path.dirname(current_path)

try:
# open a local directory to store the data
infinity_instance = infinity.connect("/var/infinity")

# connect to server with 127.0.0.1
# infinity_instance = infinity.connect(infinity.common.LOCAL_HOST)
# open a local directory(default = "/var/infinity") or connect to server(default = NetworkAddress("127.0.0.1", 23817)) to store the data
infinity_instance = infinity.connect()

# 'default_db' is the default database
db_instance = infinity_instance.get_database("default_db")
Expand Down
10 changes: 4 additions & 6 deletions example/filter_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@
# limitations under the License.


import infinity
import infinity_embedded as infinity
#import infinity
import sys

try:
# Open a local directory to store the data
infinity_instance = infinity.connect("/var/infinity")

# connect to server with 127.0.0.1
# infinity_instance = infinity.connect(infinity.common.LOCAL_HOST)
# open a local directory(default = "/var/infinity") or connect to server(default = NetworkAddress("127.0.0.1", 23817)) to store the data
infinity_instance = infinity.connect()

# 'default_db' is the default database
db_instance = infinity_instance.get_database("default_db")
Expand Down
10 changes: 4 additions & 6 deletions example/fulltext_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@
This example is to connect local infinity instance, create table, insert data, search the data
"""

import infinity
import infinity_embedded as infinity
#import infinity
import sys

try:
# open a local directory to store the data
infinity_instance = infinity.connect("/var/infinity")

# connect to server with 127.0.0.1
# infinity_instance = infinity.connect(infinity.common.LOCAL_HOST)
# open a local directory(default = "/var/infinity") or connect to server(default = NetworkAddress("127.0.0.1", 23817)) to store the data
infinity_instance = infinity.connect()

# 'default_db' is the default database
db_instance = infinity_instance.get_database("default_db")
Expand Down
10 changes: 4 additions & 6 deletions example/fulltext_search_zh.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
This example is to connect local infinity instance, create table, insert data, search the data which Chinese words
"""

import infinity
import infinity_embedded as infinity
#import infinity
import sys

"""
Expand All @@ -25,11 +26,8 @@
"""

try:
# open a local directory to store the data
infinity_instance = infinity.connect("/var/infinity")

# connect to server with 127.0.0.1
# infinity_instance = infinity.connect(infinity.common.LOCAL_HOST)
# open a local directory(default = "/var/infinity") or connect to server(default = NetworkAddress("127.0.0.1", 23817)) to store the data
infinity_instance = infinity.connect()

# 'default_db' is the default database
db_instance = infinity_instance.get_database("default_db")
Expand Down
21 changes: 9 additions & 12 deletions example/hybrid_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,13 @@
This example is to connect local infinity instance, create table, insert data, search the data
"""

import infinity
from infinity.common import SparseVector
import infinity_embedded as infinity
#import infinity
import sys

try:
# open a local directory to store the data
infinity_instance = infinity.connect("/var/infinity")

# connect to server with 127.0.0.1
# infinity_instance = infinity.connect(infinity.common.LOCAL_HOST)
# open a local directory(default = "/var/infinity") or connect to server(default = NetworkAddress("127.0.0.1", 23817)) to store the data
infinity_instance = infinity.connect()

# 'default_db' is the default database
db_instance = infinity_instance.get_database("default_db")
Expand Down Expand Up @@ -53,31 +50,31 @@
"num": 1,
"body": r"unnecessary and harmful",
"vec": [1.0, 1.2, 0.8, 0.9],
"sparse": SparseVector([10, 20, 30], [1.1, 2.2, 3.3]),
"sparse": infinity.common.SparseVector([10, 20, 30], [1.1, 2.2, 3.3]),
"year": 2024,
"tensor": [[1.0, 0.0, 0.0, 0.0], [1.1, 0.0, 0.0, 0.0]],
},
{
"num": 2,
"body": r"Office for Harmful Blooms",
"vec": [4.0, 4.2, 4.3, 4.5],
"sparse": SparseVector([40, 50, 60], [4.4, 5.5, 6.6]),
"sparse": infinity.common.SparseVector([40, 50, 60], [4.4, 5.5, 6.6]),
"year": 2023,
"tensor": [[4.0, 0.0, 4.3, 4.5], [4.0, 4.2, 4.4, 5.0]],
},
{
"num": 3,
"body": r"A Bloom filter is a space-efficient probabilistic data structure, conceived by Burton Howard Bloom in 1970, that is used to test whether an element is a member of a set.",
"vec": [4.0, 4.2, 4.3, 4.2],
"sparse": SparseVector([70, 80, 90], [7.7, 8.8, 9.9]),
"sparse": infinity.common.SparseVector([70, 80, 90], [7.7, 8.8, 9.9]),
"year": 2019,
"tensor": [[0.9, 0.1, 0.0, 0.0], [1.1, 0.0, 0.0, 0.0]],
},
{
"num": 4,
"body": r"The American Football Conference (AFC) harm chemical anarchism add test is one of harm chemical the two conferences of the National Football League (NFL). This add test conference and its counterpart, the National Football Conference (NFC), currently contain 16 teams each, making up the 32 teams of the NFL. The current AFC title holder is the New England Patriots.",
"vec": [4.0, 4.2, 4.3, 4.5],
"sparse": SparseVector([20, 80, 90], [7.7, 7.8, 97.9]),
"sparse": infinity.common.SparseVector([20, 80, 90], [7.7, 7.8, 97.9]),
"year": 2018,
"tensor": [[4.0, 4.2, 4.3, 4.5], [4.0, 4.2, 4.3, 4.4]],
},
Expand All @@ -99,7 +96,7 @@
)
.match_dense("vec", [3.0, 2.8, 2.7, 3.1], "float", "cosine", 3)
.match_sparse(
"sparse", SparseVector([0, 20, 80], [1.0, 2.0, 3.0]), "ip", 3
"sparse", infinity.common.SparseVector([0, 20, 80], [1.0, 2.0, 3.0]), "ip", 3
)
.match_text("body", "blooms", 10)
.filter("year < 2024")
Expand Down
10 changes: 4 additions & 6 deletions example/import_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,17 @@
This example is about connecting local infinity instance, creating table, ing data, importing file into a table, and exporting table's data
'''

import infinity
import infinity_embedded as infinity
#import infinity
import os
import sys

current_path = os.path.abspath(__file__)
project_directory = os.path.dirname(current_path)

try:
# open a local directory to store the data
infinity_instance = infinity.connect("/var/infinity")

# connect to server with 127.0.0.1
# infinity_instance = infinity.connect(infinity.common.LOCAL_HOST)
# open a local directory(default = "/var/infinity") or connect to server(default = NetworkAddress("127.0.0.1", 23817)) to store the data
infinity_instance = infinity.connect()

# 'default_db' is the default database
db_instance = infinity_instance.get_database("default_db")
Expand Down
10 changes: 4 additions & 6 deletions example/list_db_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@
This example is about connecting to the local infinity instance, creating table, inserting data, and searching data
'''

import infinity
import infinity_embedded as infinity
#import infinity
import sys

try:
# open a local directory to store the data
infinity_instance = infinity.connect("/var/infinity")

# connect to server with 127.0.0.1
# infinity_instance = infinity.connect(infinity.common.LOCAL_HOST)
# open a local directory(default = "/var/infinity") or connect to server(default = NetworkAddress("127.0.0.1", 23817)) to store the data
infinity_instance = infinity.connect()

infinity_instance.create_database("db1", infinity.common.ConflictType.Ignore)
infinity_instance.create_database("db2", infinity.common.ConflictType.Ignore)
Expand Down
13 changes: 5 additions & 8 deletions example/secondary_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,15 @@
This example is about connecting to the local infinity instance, creating table, inserting data, and searching data
'''

import infinity
import infinity_embedded as infinity
#import infinity
import time
import sys

from infinity import index

try:
# open a local directory to store the data
infinity_instance = infinity.connect("/var/infinity")

# connect to server with 127.0.0.1
# infinity_instance = infinity.connect(infinity.common.LOCAL_HOST)
# open a local directory(default = "/var/infinity") or connect to server(default = NetworkAddress("127.0.0.1", 23817)) to store the data
infinity_instance = infinity.connect()

# 'default_db' is the default database
db_instance = infinity_instance.get_database("default_db")
Expand Down Expand Up @@ -55,7 +52,7 @@
}]
)

table_instance.create_index("index1", index.IndexInfo("id", index.IndexType.Secondary))
table_instance.create_index("index1", infinity.index.IndexInfo("id", infinity.index.IndexType.Secondary))
res = table_instance.filter("id='ID_1'").output(["*"]).to_pl()
print(res)

Expand Down
10 changes: 4 additions & 6 deletions example/simple_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@
This example is about connecting to the local infinity instance, creating table, inserting data, and searching data
'''

import infinity
import infinity_embedded as infinity
#import infinity
import sys

try:
# open a local directory to store the data
infinity_instance = infinity.connect("/var/infinity")

# connect to server with 127.0.0.1
# infinity_instance = infinity.connect(infinity.common.LOCAL_HOST)
# open a local directory(default = "/var/infinity") or connect to server(default = NetworkAddress("127.0.0.1", 23817)) to store the data
infinity_instance = infinity.connect()

# 'default_db' is the default database
db_instance = infinity_instance.get_database("default_db")
Expand Down
19 changes: 8 additions & 11 deletions example/sparse_vector_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,13 @@
This example is to connect local infinity instance, create table, insert data, search the data
'''

import infinity
from infinity.common import SparseVector
import infinity_embedded as infinity
#import infinity
import sys

try:
# open a local directory to store the data
infinity_instance = infinity.connect("/var/infinity")

# connect to server with 127.0.0.1
# infinity_instance = infinity.connect(infinity.common.LOCAL_HOST)
# open a local directory(default = "/var/infinity") or connect to server(default = NetworkAddress("127.0.0.1", 23817)) to store the data
infinity_instance = infinity.connect()

# 'default_db' is the default database
db_instance = infinity_instance.get_database("default_db")
Expand All @@ -46,22 +43,22 @@
{
"num": 1,
"body": r"unnecessary and harmful",
"vec": SparseVector([10, 20, 30], [1.1, 2.2, 3.3])
"vec": infinity.common.SparseVector([10, 20, 30], [1.1, 2.2, 3.3])
},
{
"num": 2,
"body": r"Office for Harmful Blooms",
"vec": SparseVector([40, 50, 60], [4.4, 5.5, 6.6])
"vec": infinity.common.SparseVector([40, 50, 60], [4.4, 5.5, 6.6])
},
{
"num": 3,
"body": r"A Bloom filter is a space-efficient probabilistic data structure, conceived by Burton Howard Bloom in 1970, that is used to test whether an element is a member of a set.",
"vec": SparseVector([70, 80, 90], [7.7, 8.8, 9.9])
"vec": infinity.common.SparseVector([70, 80, 90], [7.7, 8.8, 9.9])
},
]
)

result = table_instance.output(["num", "vec", "_similarity"]).match_sparse("vec", SparseVector([0, 20, 80], [1.0, 2.0, 3.0]), "ip", 3).to_pl()
result = table_instance.output(["num", "vec", "_similarity"]).match_sparse("vec", infinity.common.SparseVector([0, 20, 80], [1.0, 2.0, 3.0]), "ip", 3).to_pl()
print(result)
infinity_instance.disconnect()

Expand Down
10 changes: 4 additions & 6 deletions example/tensor_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@
# limitations under the License.


import infinity
import infinity_embedded as infinity
#import infinity
import sys

try:
# Open a local directory to store the data
infinity_instance = infinity.connect("/var/infinity")

# connect to server with 127.0.0.1
# infinity_instance = infinity.connect(infinity.common.LOCAL_HOST)
# open a local directory(default = "/var/infinity") or connect to server(default = NetworkAddress("127.0.0.1", 23817)) to store the data
infinity_instance = infinity.connect()

# 'default_db' is the default database
db_instance = infinity_instance.get_database("default_db")
Expand Down
10 changes: 4 additions & 6 deletions example/vector_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@
This example is to connect local infinity instance, create table, insert data, search the data
'''

import infinity
import infinity_embedded as infinity
#import infinity
import sys

try:
# open a local directory to store the data
infinity_instance = infinity.connect("/var/infinity")

# connect to server with 127.0.0.1
# infinity_instance = infinity.connect(infinity.common.LOCAL_HOST)
# open a local directory(default = "/var/infinity") or connect to server(default = NetworkAddress("127.0.0.1", 23817)) to store the data
infinity_instance = infinity.connect()

# 'default_db' is the default database
db_instance = infinity_instance.get_database("default_db")
Expand Down
2 changes: 1 addition & 1 deletion python/infinity_embedded/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from infinity_embedded.local_infinity.infinity import LocalInfinityConnection
from infinity_embedded.errors import ErrorCode

def connect(uri) -> InfinityConnection:
def connect(uri = LOCAL_INFINITY_PATH) -> InfinityConnection:
if isinstance(uri, str) and len(uri) != 0:
return LocalInfinityConnection(uri)
else:
Expand Down
Loading

0 comments on commit fe005f2

Please sign in to comment.