Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

pgml chat blog#914

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
santiatpml merged 16 commits intomasterfromsanti-pgml-chat-blog
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
16 commits
Select commitHold shift + click to select a range
39a1734
kicked off blog
santiadavaniAug 9, 2023
1493bbd
minor updates
santiadavaniAug 10, 2023
f6c9d64
First draft for pgml chat blog
santiadavaniAug 11, 2023
0c43943
Updated gitignore
santiadavaniAug 11, 2023
83a3ebc
kicked off blog
santiadavaniAug 9, 2023
e3253fa
minor updates
santiadavaniAug 10, 2023
a938317
First draft for pgml chat blog
santiadavaniAug 11, 2023
505a20f
Updated gitignore
santiadavaniAug 11, 2023
8a3640a
Merge branch 'santi-pgml-chat-blog' of https://github.com/postgresml/…
santiadavaniAug 11, 2023
dd34854
timings benchmark scripts v1
santiadavaniAug 14, 2023
029fa05
Added requirements
santiadavaniAug 14, 2023
296eb30
Added latency results
santiadavaniAug 15, 2023
45e5493
query comparison plot
santiadavaniAug 15, 2023
c5e3dc4
verified rendering locally
santiadavaniAug 15, 2023
08e44b6
updated embeddings and query to reflect e5-large model
santiadavaniAug 16, 2023
be0c239
Intro updates and new image
santiadavaniAug 16, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletionspgml-apps/pgml-chat/README.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,10 +10,9 @@ This tool automates the above two stages and provides a command line interface t
#Prerequisites
Before you begin, make sure you have the following:

- PostgresML Database:Spin up a for a free[GPU-powered database](https://postgresml.org/signup)
- PostgresML Database:Sign up for a free[GPU-powered database](https://postgresml.org/signup)
- Python version >=3.8
- OpenAI API key
- Python 3.8+


#Getting started
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
pgml.sql
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
importos
importrequests
fromtimeimporttime
fromrichimportprint
fromdatasetsimportload_dataset
fromtqdm.autoimporttqdm
fromdatasetsimportDataset
fromdotenvimportload_dotenv

load_dotenv(".env")

api_org=os.environ["HF_API_KEY"]
endpoint=os.environ["HF_ENDPOINT"]
# add the api org token to the headers
headers= {
'Authorization':f'Bearer{api_org}'
}

#squad = load_dataset("squad", split='train')
squad=Dataset.from_file("squad-train.arrow")
data=squad.to_pandas()
data=data.drop_duplicates(subset=["context"])
passages=list(data['context'])

total_documents=10000
batch_size=1
passages=passages[:total_documents]

start=time()
foriintqdm(range(0,len(passages),batch_size)):
# find end of batch
i_end=min(i+batch_size,len(passages))
# extract batch
batch=passages[i:i_end]
# generate embeddings for batch via endpoints
res=requests.post(
endpoint,
headers=headers,
json={"inputs":batch}
)

print("Time taken for HF for %d documents = %0.3f"% (len(passages),time()-start))
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
importos
importrequests
fromtimeimporttime
fromrichimportprint
fromdatasetsimportload_dataset
importpinecone
fromtqdm.autoimporttqdm
fromdatasetsimportDataset

api_org=os.environ["HF_API_KEY"]
endpoint=os.environ["HF_ENDPOINT"]
# add the api org token to the headers
headers= {
'Authorization':f'Bearer{api_org}'
}

#squad = load_dataset("squad", split='train')
squad=Dataset.from_file("squad-train.arrow")
data=squad.to_pandas()
data=data.drop_duplicates(subset=["context"])
passages=list(data['context'])

total_documents=10000
batch_size=64
passages=passages[:total_documents]

# connect to pinecone environment
pinecone.init(
api_key=os.environ["PINECONE_API_KEY"],
environment=os.environ["PINECONE_ENVIRONMENT"]
)

index_name='hf-endpoints'

# check if the movie-emb index exists
ifindex_namenotinpinecone.list_indexes():
# create the index if it does not exist
pinecone.create_index(
index_name,
dimension=dim,
metric="cosine"
)

# connect to movie-emb index we created
index=pinecone.Index(index_name)

start=time()
# we will use batches of 64
foriintqdm(range(0,len(passages),batch_size)):
# find end of batch
i_end=min(i+batch_size,len(passages))
# extract batch
batch=passages[i:i_end]
# generate embeddings for batch via endpoints
res=requests.post(
endpoint,
headers=headers,
json={"inputs":batch}
)
emb=res.json()['embeddings']
# get metadata (just the original text)
meta= [{'text':text}fortextinbatch]
# create IDs
ids= [str(x)forxinrange(i,i_end)]
# add all to upsert list
to_upsert=list(zip(ids,emb,meta))
# upsert/insert these records to pinecone
_=index.upsert(vectors=to_upsert)

print("Time taken for HF for %d documents = %0.3f"% (len(passages),time()-start))
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
importos
importrequests
fromtimeimporttime
fromrichimportprint
importpinecone
fromtqdm.autoimporttqdm
fromdatasetsimportDataset
fromdotenvimportload_dotenv
fromstatisticsimportmean

load_dotenv(".env")
api_org=os.environ["HF_API_KEY"]
endpoint=os.environ["HF_ENDPOINT"]
# add the api org token to the headers
headers= {
'Authorization':f'Bearer{api_org}'
}

#squad = load_dataset("squad", split='train')
squad=Dataset.from_file("squad-train.arrow")
data=squad.to_pandas()
data=data.drop_duplicates(subset=["context"])
passages=list(data['context'])

# connect to pinecone environment
pinecone.init(
api_key=os.environ["PINECONE_API_KEY"],
environment=os.environ["PINECONE_ENVIRONMENT"]
)

index_name='hf-endpoints'

# check if the movie-emb index exists
ifindex_namenotinpinecone.list_indexes():
# create the index if it does not exist
pinecone.create_index(
index_name,
dimension=dim,
metric="cosine"
)

# connect to movie-emb index we created
index=pinecone.Index(index_name)


run_times= []
forqueryindata["context"][0:100]:
start=time()
# encode with HF endpoints
res=requests.post(endpoint,headers=headers,json={"inputs":query})
xq=res.json()['embeddings']
# query and return top 5
xc=index.query(xq,top_k=5,include_metadata=True)
_end=time()
run_times.append(_end-start)
print("HF + Pinecone Average query time: %0.3f"%(mean(run_times)))



View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
frompgmlimportDatabase
importos
fromdatasetsimportload_dataset
fromtimeimporttime
fromdotenvimportload_dotenv
fromrichimportprint
importasyncio
fromtqdm.autoimporttqdm

asyncdefmain():
load_dotenv()
conninfo=os.environ.get("DATABASE_URL")
db=Database(conninfo)

collection_name="squad_collection_benchmark"
collection=awaitdb.create_or_get_collection(collection_name)
model_id=awaitcollection.register_model(model_name="intfloat/e5-large")
awaitcollection.generate_embeddings(model_id=model_id)

if__name__=="__main__":
asyncio.run(main())
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
DO $$
DECLARE
curr_idinteger :=0;
batch_sizeinteger:=2;
total_recordsinteger:=10000;
curr_valtext[];-- Use "text[]" instead of "varchar[]"
embed_result json;-- Store the result of the pgml.embed function
BEGIN
LOOP
--BEGIN RAISE NOTICE 'updating % to %', curr_id, curr_id + batch_size; END;
SELECT ARRAY(SELECT chunk::text
FROMsquad_collection_benchmark.chunks
WHERE id BETWEEN curr_id+1AND curr_id+ batch_size)
INTO curr_val;

-- Use the correct syntax to call pgml.embed and store the result
PERFORM embedFROMpgml.embed('intfloat/e5-large', curr_val);

curr_id := curr_id+ batch_size;
EXIT WHEN curr_id>= total_records;
END LOOP;

SELECT ARRAY(SELECT chunk::text
FROMsquad_collection_benchmark.chunks
WHERE id BETWEEN curr_id-batch_sizeAND total_records)
INTO curr_val;

-- Use the correct syntax to call pgml.embed and store the result
PERFORM embedFROMpgml.embed('intfloat/e5-large', curr_val);

END;
$$;
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
frompgmlimportDatabase
importos
fromdatasetsimportload_dataset
fromtimeimporttime
fromdotenvimportload_dotenv
fromrichimportprint
importasyncio
fromtqdm.autoimporttqdm

asyncdefmain():
load_dotenv()
conninfo=os.environ.get("DATABASE_URL")
db=Database(conninfo)

collection_name="squad_collection_benchmark"
collection=awaitdb.create_or_get_collection(collection_name)

data=load_dataset("squad",split="train")
data=data.to_pandas()
data=data.drop_duplicates(subset=["context"])

documents= [
{"id":r["id"],"text":r["context"],"title":r["title"]}
forrindata.to_dict(orient="records")
]

print("Ingesting and chunking documents ..")
total_documents=10000
batch_size=64
embedding_times= []
total_time=0
documents=documents[:total_documents]
foriintqdm(range(0,len(documents),batch_size)):
i_end=min(i+batch_size,len(documents))
batch=documents[i:i_end]
awaitcollection.upsert_documents(batch)
awaitcollection.generate_chunks()
print("Ingesting and chunking completed")

if__name__=="__main__":
asyncio.run(main())
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
frompgmlimportDatabase
importos
fromdatasetsimportload_dataset
fromtimeimporttime
fromdotenvimportload_dotenv
fromrichimportprint
importasyncio
fromtqdm.autoimporttqdm
fromstatisticsimportmean,median

asyncdefmain():
load_dotenv()

conninfo=os.environ.get("DATABASE_URL")
db=Database(conninfo)

collection_name="squad_collection_benchmark"
collection=awaitdb.create_or_get_collection(collection_name)

data=load_dataset("squad",split="train")
data=data.to_pandas()
data=data.drop_duplicates(subset=["context"])
model_id=awaitcollection.register_model(model_name="intfloat/e5-large")
run_times= []
forqueryindata["context"][0:100]:
start=time()
results=awaitcollection.vector_search(query,top_k=5,model_id=model_id)
_end=time()
run_times.append(_end-start)
#print("PGML Query times:")
#print(run_times)
print("PGML Average query time: %0.3f"%mean(run_times))
print("PGML Median query time: %0.3f"%median(run_times))

#await db.archive_collection(collection_name)

if__name__=="__main__":
asyncio.run(main())
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
aiohttp==3.8.5
aiosignal==1.3.1
async-timeout==4.0.3
attrs==23.1.0
black==23.7.0
certifi==2023.7.22
charset-normalizer==3.2.0
click==8.1.6
datasets==2.14.4
dill==0.3.7
dnspython==2.4.2
filelock==3.12.2
frozenlist==1.4.0
fsspec==2023.6.0
huggingface-hub==0.16.4
idna==3.4
loguru==0.7.0
markdown-it-py==3.0.0
mdurl==0.1.2
multidict==6.0.4
multiprocess==0.70.15
mypy-extensions==1.0.0
numpy==1.25.2
packaging==23.1
pandas==2.0.3
pathspec==0.11.2
pgml==0.8.1
pinecone-client==2.2.2
platformdirs==3.10.0
psycopg==3.1.10
psycopg-pool==3.1.7
pyarrow==12.0.1
Pygments==2.16.1
python-dateutil==2.8.2
python-dotenv==1.0.0
pytz==2023.3
PyYAML==6.0.1
requests==2.31.0
rich==13.5.2
six==1.16.0
tomli==2.0.1
tqdm==4.66.1
typing_extensions==4.7.1
tzdata==2023.3
urllib3==2.0.4
xxhash==3.3.0
yarl==1.9.2
Loading

[8]ページ先頭

©2009-2025 Movatter.jp