Skip to content

Commit

Permalink
20240124/update pyv4 code 2 (#1581)
Browse files Browse the repository at this point in the history
* Call wvc classes from submodules

* test updates

* add markers to tests; update tests:

* Update pytest.ini

* rename tests to pyv4

* Update python-v4.py; add to test suite

* update Sort syntax

* Update chained Sort syntax

* Add generative examples to tests
  • Loading branch information
databyjp authored Jan 25, 2024
1 parent 131782d commit 3505192
Show file tree
Hide file tree
Showing 22 changed files with 294 additions and 109 deletions.
80 changes: 62 additions & 18 deletions _includes/code/client-libraries/python_v4.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

assert client.is_ready()

client.close()

"""
# EmbeddedInstantiationBasic
import weaviate
Expand All @@ -17,8 +19,10 @@
client = weaviate.connect_to_embedded(
port=8085,
grpc_port=50055
) # Connect with default parameters
)

assert client.is_ready()
client.close()

# WCSInstantiation
import weaviate
Expand All @@ -31,20 +35,22 @@
# END WCSInstantiation

assert client.is_ready()
client.close()

# WCSwOIDCInstantiation
import weaviate

client = weaviate.connect_to_wcs(
cluster_url=os.getenv("WCS_DEMO_URL"), # Replace with your WCS URL
auth_credentials=weaviate.AuthClientPassword(
auth_credentials=weaviate.auth.AuthClientPassword(
username=os.getenv("WCS_USERNAME"), # Your WCS username
password=os.getenv("WCS_PASSWORD") # Your WCS password
)
)
# END WCSwOIDCInstantiation

assert client.is_ready()
client.close()

# CustomInstantiationBasic
import weaviate
Expand All @@ -64,16 +70,20 @@
# END CustomInstantiationBasic

assert client.is_ready()
client.close()

# DirectInstantiationBasic
# # DirectInstantiationBasic
import weaviate

client = weaviate.WeaviateClient(
weaviate.ConnectionParams.from_url("http://localhost:8080", 50051)
weaviate.connect.ConnectionParams.from_url("http://localhost:8080", 50051)
)

client.connect() # When directly instantiating, you need to connect manually
# END DirectInstantiationBasic

assert client.is_ready()
client.close()

# LocalInstantiationWithHeaders
import weaviate
Expand All @@ -85,21 +95,27 @@
# END LocalInstantiationWithHeaders

assert client.is_ready()
client.close()

# LocalInstantiationWithTimeout
import weaviate

client = weaviate.connect_to_local(port=8080, grpc_port=50051, timeout=(5, 15))
client = weaviate.connect_to_local(
port=8080,
grpc_port=50051,
additional_config=weaviate.config.AdditionalConfig(timeout=(5, 15))
)
# END LocalInstantiationWithTimeout

assert client.is_ready()
client.close()

# DirectInstantiationFull
import weaviate
import os

client = weaviate.WeaviateClient(
connection_params=weaviate.ConnectionParams.from_params(
connection_params=weaviate.connect.ConnectionParams.from_params(
http_host="localhost",
http_port="8099",
http_secure=False,
Expand All @@ -111,29 +127,35 @@
additional_headers={
"X-OpenAI-Api-Key": os.getenv("OPENAI_APIKEY")
},
additional_config=weaviate.AdditionalConfig(
additional_config=weaviate.config.AdditionalConfig(
startup_period=10,
timeout=(5, 15)
),
)

client.connect() # When directly instantiating, you need to connect manually
# END DirectInstantiationFull

assert client.is_ready()
client.close()

# =====================================================================================
# Collection instantiation
# =====================================================================================

client = weaviate.connect_to_local()
client.collections.delete("TestArticle")
assert not client.collections.exists("TestArticle")

# START CreateCollectionExample
import weaviate
import weaviate.classes as wvc

client = weaviate.connect_to_local()

# END CreateCollectionExample

# Delete any existing collections
client.collections.delete("TestArticle")
assert not client.collections.exists("TestArticle")

# START CreateCollectionExample
collection = client.collections.create(
name="TestArticle",
vectorizer_config=wvc.config.Configure.Vectorizer.text2vec_cohere(),
Expand Down Expand Up @@ -163,6 +185,7 @@
client.collections.delete(cname)
assert not client.collections.exists(cname)

client.close()

# START CreateCollectionWithRefsExample
import weaviate
Expand Down Expand Up @@ -248,7 +271,7 @@
"question": "This is the capital of Australia."
},
references={ # For adding cross-references
"hasCategory": wvc.data.Reference.to(uuids=[target_uuid])
"hasCategory": [target_uuid]
}
)
# END CreateObjectExample
Expand Down Expand Up @@ -293,7 +316,7 @@
properties=properties,
# highlight-start
references={
"hasCategory": wvc.data.Reference.to(uuids=target_uuid)
"hasCategory": target_uuid
},
# highlight-end
uuid=generate_uuid5(properties)
Expand All @@ -317,16 +340,23 @@
questions = client.collections.get("JeopardyQuestion")

response = questions.data.delete_many(
where=Filter.by_property(prop="question").equal("Test Question")
where=Filter.by_property(name="question").equal("Test Question")
)
# END DeleteManyExample

# =====================================================================================
# Query examples
# =====================================================================================

client = weaviate.connect_to_local(
headers={"X-OpenAI-Api-Key": os.getenv("OPENAI_APIKEY")},
client.close()

# Connect to WCS instance for query examples
client = weaviate.connect_to_wcs(
cluster_url=os.getenv("WCS_DEMO_URL"),
auth_credentials=weaviate.auth.AuthApiKey(os.getenv("WCS_DEMO_RO_KEY")),
headers={
"X-OpenAI-Api-Key": os.getenv("OPENAI_APIKEY"),
}
)

# START BM25QueryExample
Expand Down Expand Up @@ -362,7 +392,14 @@
print(o.properties) # All properties by default
print(o.references) # References not returned by default
print(o.uuid) # UUID included by default
# END BM25QueryDefaultReturnsExample
"""
# Don't actually show vector when script runs
# START BM25QueryDefaultReturnsExample
print(o.vector) # No vector
# END BM25QueryDefaultReturnsExample
"""
# START BM25QueryDefaultReturnsExample
print(o.metadata) # No metadata
# END BM25QueryDefaultReturnsExample

Expand All @@ -381,7 +418,14 @@
print(o.properties) # Selected properties only
print(o.references) # Selected references
print(o.uuid) # UUID included by default
# END BM25QueryCustomReturnsExample
"""
# Don't actually show vector when script runs
# START BM25QueryCustomReturnsExample
print(o.vector) # With vector
# END BM25QueryCustomReturnsExample
"""
# START BM25QueryCustomReturnsExample
print(o.metadata) # With selected metadata
# END BM25QueryCustomReturnsExample

Expand Down Expand Up @@ -426,7 +470,7 @@
# START AggregateCountExample
questions = client.collections.get("JeopardyQuestion")
response = questions.aggregate.over_all(
filters=wvc.query.Filter.by_property(prop="question").like("*animal*"),
filters=wvc.query.Filter.by_property(name="question").like("*animal*"),
total_count=True
)

Expand Down Expand Up @@ -480,7 +524,7 @@
return_metrics=wvc.Metrics("points").integer(mean=True)
)

for o in response:
for o in response.groups:
print(o)
# END AggregateGroupbyExample

Expand Down
22 changes: 18 additions & 4 deletions _includes/code/generative.groupedtask.examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,32 @@
}
)

publications = client.collections.get("Publication")
# END-ANY
client.close()

client = weaviate.connect_to_wcs(
cluster_url=os.getenv("WCS_DEMO_URL"),
auth_credentials=weaviate.auth.AuthApiKey(os.getenv("WCS_DEMO_RO_KEY")),
headers={
"X-OpenAI-Api-Key": os.getenv("OPENAI_APIKEY"),
}
)

# START-ANY
reviews = client.collections.get("WineReview")

# instruction for the generative module
generate_prompt = "Explain why these magazines or newspapers are about finance"
generate_prompt = "Explain what occasion these wines might be good for."

response = publications.generate.near_text(
query="magazine or newspaper about finance",
response = reviews.generate.near_text(
query="dry red wine",
grouped_task=generate_prompt,
limit=5
)

print(response.generated) # "Grouped task" generations are attributes of the entire response
for o in response.objects:
print(o.properties) # To inspect the retrieved object

client.close()
# END-ANY
24 changes: 19 additions & 5 deletions _includes/code/generative.singleprompt.examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,32 @@
}
)

articles = client.collections.get("Article")
# END-ANY
client.close()

client = weaviate.connect_to_wcs(
cluster_url=os.getenv("WCS_DEMO_URL"),
auth_credentials=weaviate.auth.AuthApiKey(os.getenv("WCS_DEMO_RO_KEY")),
headers={
"X-OpenAI-Api-Key": os.getenv("OPENAI_APIKEY"),
}
)

# START-ANY
reviews = client.collections.get("WineReview")

# instruction for the generative module
generate_prompt = "Describe the following as a Facebook Ad: {summary}"
generate_prompt = "Describe the following as a Facebook Ad: {review_body}"

response = articles.generate.near_text(
query="Italian food",
response = reviews.generate.near_text(
query="fruity white wine",
single_prompt=generate_prompt,
limit=5
limit=3
)

for o in response.objects:
print(o.generated) # "Single prompt" generations are attributes of each object
print(o.properties) # To inspect the retrieved object

client.close()
# END-ANY
12 changes: 6 additions & 6 deletions _includes/code/graphql.additional.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_gqlresponse(response_in, gqlresponse_in):
article=client.collections.get("JeopardyQuestion")
response = article.query.fetch_objects(
# highlight-start
sort=Sort(prop="answer", ascending=True),
sort=Sort.by_property(name="answer", ascending=True),
# highlight-end
limit=3
)
Expand All @@ -55,10 +55,10 @@ def test_gqlresponse(response_in, gqlresponse_in):
# ==========================================

# START MultiplePropSorting Python
article=client.collections.get("JeopardyQuestion")
response = article.query.fetch_objects(
sort=[Sort(prop="points", ascending=False),
Sort(prop="answer", ascending=True)],
questions=client.collections.get("JeopardyQuestion")
response = questions.query.fetch_objects(
# Note: To sort by multiple properties, chain the relevant `by_xxx` methods.
sort=Sort.by_property(name="points", ascending=False).by_property(name="answer", ascending=True),
limit=3
)

Expand All @@ -81,7 +81,7 @@ def test_gqlresponse(response_in, gqlresponse_in):
article=client.collections.get("JeopardyQuestion")
response = article.query.fetch_objects(
return_metadata=wvc.query.MetadataQuery(creation_time=True),
sort=Sort(ascending=True, prop="_creationTimeUnix"),
sort=Sort.by_property(name="_creationTimeUnix", ascending=True),
limit=3
)

Expand Down
14 changes: 14 additions & 0 deletions _includes/code/graphql.filters.nearText.generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@
import os

client = weaviate.connect_to_local()
# END-ANY

# Actual client instantiation
client.close()

client = weaviate.connect_to_wcs(
cluster_url=os.getenv("WCS_DEMO_URL"),
auth_credentials=weaviate.auth.AuthApiKey(os.getenv("WCS_DEMO_RO_KEY")),
headers={
"X-OpenAI-Api-Key": os.getenv("OPENAI_APIKEY"),
}
)

# START-ANY

publications = client.collections.get("Publication")

Expand Down
14 changes: 14 additions & 0 deletions _includes/code/graphql.filters.nearText.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@
}
)

# END-ANY

# Actual client instantiation
client.close()

client = weaviate.connect_to_wcs(
cluster_url=os.getenv("WCS_DEMO_URL"),
auth_credentials=weaviate.auth.AuthApiKey(os.getenv("WCS_DEMO_RO_KEY")),
headers={
"X-OpenAI-Api-Key": os.getenv("OPENAI_APIKEY"),
}
)

# START-ANY
publications = client.collections.get("Publication")

response = publications.query.near_text(
Expand Down
1 change: 0 additions & 1 deletion _includes/code/howto/manage-data.cross-refs.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ def del_props(client: WeaviateClient, uuid_to_update: str, collection_name: str,
client = weaviate.connect_to_local(
headers={"X-OpenAI-Api-Key": os.getenv("OPENAI_APIKEY")}
)
old_client = weaviate.Client("http://localhost:8080")


# ===========================================================
Expand Down
Loading

0 comments on commit 3505192

Please sign in to comment.