Skip to content

Python: vector store simplificatons and some cleanup #12274

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 2 additions & 4 deletions python/samples/concepts/caching/semantic_caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from semantic_kernel import Kernel
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion, OpenAITextEmbedding
from semantic_kernel.connectors.memory.in_memory import InMemoryStore
from semantic_kernel.data import VectorStore, VectorStoreField, VectorStoreRecordCollection, vectorstoremodel
from semantic_kernel.data.vectors import VectorStore, VectorStoreCollection, VectorStoreField, vectorstoremodel
from semantic_kernel.filters import FilterTypes, FunctionInvocationContext, PromptRenderContext
from semantic_kernel.functions import FunctionResult

Expand Down Expand Up @@ -41,9 +41,7 @@ def __init__(
if vector_store.embedding_generator is None:
raise ValueError("The vector store must have an embedding generator.")
self.vector_store = vector_store
self.collection: VectorStoreRecordCollection[str, CacheRecord] = vector_store.get_collection(
record_type=CacheRecord
)
self.collection: VectorStoreCollection[str, CacheRecord] = vector_store.get_collection(record_type=CacheRecord)
self.score_threshold = score_threshold

async def on_prompt_render(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from semantic_kernel.contents import ChatHistory, ChatMessageContent
from semantic_kernel.core_plugins.math_plugin import MathPlugin
from semantic_kernel.core_plugins.time_plugin import TimePlugin
from semantic_kernel.data import VectorStore, VectorStoreField, VectorStoreRecordCollection, vectorstoremodel
from semantic_kernel.data.vectors import VectorStore, VectorStoreCollection, VectorStoreField, vectorstoremodel

"""
This sample demonstrates how to build a conversational chatbot
Expand Down Expand Up @@ -49,7 +49,7 @@ class ChatHistoryInCosmosDB(ChatHistory):
session_id: str
user_id: str
store: VectorStore
collection: VectorStoreRecordCollection[str, ChatHistoryModel] | None = None
collection: VectorStoreCollection[str, ChatHistoryModel] | None = None

async def create_collection(self, collection_name: str) -> None:
"""Create a collection with the inbuild data model using the vector store.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
)
from pydantic import BaseModel, ConfigDict

from semantic_kernel.data import VectorStoreField, vectorstoremodel
from semantic_kernel.data.vectors import VectorStoreField, vectorstoremodel

"""
The data model used for this sample is based on the hotel data model from the Azure AI Search samples.
Expand Down
22 changes: 13 additions & 9 deletions python/samples/concepts/memory/complex_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@
SqlServerCollection,
WeaviateCollection,
)
from semantic_kernel.data import VectorStoreRecordCollection, vectorstoremodel
from semantic_kernel.data.definitions import VectorStoreField
from semantic_kernel.data.vectors import SearchType, VectorSearch
from semantic_kernel.data.vectors import (
SearchType,
VectorSearchProtocol,
VectorStoreCollection,
VectorStoreField,
vectorstoremodel,
)

# This is a rather complex sample, showing how to use the vector store
# with a number of different collections.
Expand All @@ -46,16 +50,16 @@ class DataModel:
title: Annotated[str, VectorStoreField("data", is_full_text_indexed=True)]
content: Annotated[str, VectorStoreField("data", is_full_text_indexed=True)]
embedding: Annotated[
str | None,
VectorStoreField("vector", dimensions=1536, type_="float"),
list[float] | str | None,
VectorStoreField("vector", dimensions=1536, type="float"),
] = None
id: Annotated[
str,
VectorStoreField(
"key",
),
] = field(default_factory=lambda: str(uuid4()))
tag: Annotated[str | None, VectorStoreField("data", type_="str", is_indexed=True)] = None
tag: Annotated[str | None, VectorStoreField("data", type="str", is_indexed=True)] = None

def __post_init__(self, **kwargs):
if self.embedding is None:
Expand Down Expand Up @@ -94,7 +98,7 @@ def __post_init__(self, **kwargs):
# function which returns the collection.
# Using a function allows for lazy initialization of the collection,
# so that settings for unused collections do not cause validation errors.
collections: dict[str, Callable[[], VectorStoreRecordCollection]] = {
collections: dict[str, Callable[[], VectorStoreCollection]] = {
"ai_search": lambda: AzureAISearchCollection[str, DataModel](record_type=DataModel),
"postgres": lambda: PostgresCollection[str, DataModel](record_type=DataModel),
"redis_json": lambda: RedisJsonCollection[str, DataModel](
Expand Down Expand Up @@ -143,6 +147,7 @@ async def main(collection: str, use_azure_openai: bool):
)
kernel.add_service(embedder)
async with collections[collection]() as record_collection:
assert isinstance(record_collection, VectorSearchProtocol) # nosec
record_collection.embedding_generator = embedder
print_with_color(f"Creating {collection} collection!", Colors.CGREY)
# cleanup any existing collection
Expand Down Expand Up @@ -172,7 +177,7 @@ async def main(collection: str, use_azure_openai: bool):
keys = await record_collection.upsert(records)
print(f" Upserted {keys=}")
print_with_color("Getting records!", Colors.CBLUE)
results = await record_collection.get(top=10, order_by={"field": "content"})
results = await record_collection.get(top=10, order_by="content")
if results:
[print_record(record=result) for result in results]
else:
Expand All @@ -187,7 +192,6 @@ async def main(collection: str, use_azure_openai: bool):
print_with_color("Now we can start searching.", Colors.CBLUE)
print_with_color(" For each type of search, enter a search term, for instance `python`.", Colors.CBLUE)
print_with_color(" Enter exit to exit, and skip or nothing to skip this search.", Colors.CBLUE)
assert isinstance(record_collection, VectorSearch) # nosec
print("-" * 30)
print_with_color(
"This collection supports the following search types: "
Expand Down
2 changes: 1 addition & 1 deletion python/samples/concepts/memory/data_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pandas import DataFrame
from pydantic import BaseModel, Field

from semantic_kernel.data import VectorStoreCollectionDefinition, VectorStoreField, vectorstoremodel
from semantic_kernel.data.vectors import VectorStoreCollectionDefinition, VectorStoreField, vectorstoremodel

# This concept shows the different ways you can create a vector store data model
# using dataclasses, Pydantic, and Python classes.
Expand Down
2 changes: 1 addition & 1 deletion python/samples/concepts/memory/memory_with_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from semantic_kernel.connectors.ai.open_ai import OpenAITextEmbedding
from semantic_kernel.connectors.memory.azure_ai_search import AzureAISearchCollection
from semantic_kernel.data import VectorStoreCollectionDefinition, VectorStoreField
from semantic_kernel.data.vectors import VectorStoreCollectionDefinition, VectorStoreField

definition = VectorStoreCollectionDefinition(
collection_name="pandas_test_index",
Expand Down
2 changes: 1 addition & 1 deletion python/samples/concepts/memory/simple_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from samples.concepts.resources.utils import Colors, print_with_color
from semantic_kernel.connectors.ai.open_ai import OpenAITextEmbedding
from semantic_kernel.connectors.memory import InMemoryCollection
from semantic_kernel.data import VectorStoreField, vectorstoremodel
from semantic_kernel.data.vectors import VectorStoreField, vectorstoremodel

# This is the most basic example of a vector store and collection
# For a more complex example, using different collection types, see "complex_memory.py"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
AzureChatPromptExecutionSettings,
ExtraBody,
)
from semantic_kernel.connectors.memory.azure_cognitive_search.azure_ai_search_settings import AzureAISearchSettings
from semantic_kernel.connectors.memory.azure_ai_search import AzureAISearchSettings
from semantic_kernel.contents import ChatHistory
from semantic_kernel.functions import KernelArguments
from semantic_kernel.prompt_template import InputVariable, PromptTemplateConfig
Expand Down
2 changes: 1 addition & 1 deletion python/samples/concepts/rag/rag_with_vector_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
OpenAITextEmbedding,
)
from semantic_kernel.connectors.memory import InMemoryCollection
from semantic_kernel.data import VectorStoreField, vectorstoremodel
from semantic_kernel.data.vectors import VectorStoreField, vectorstoremodel
from semantic_kernel.functions import KernelArguments

"""
Expand Down
2 changes: 1 addition & 1 deletion python/samples/concepts/rag/self_critique_rag.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion, OpenAITextEmbedding
from semantic_kernel.connectors.memory import AzureAISearchCollection
from semantic_kernel.contents import ChatHistory
from semantic_kernel.data import VectorStoreField, vectorstoremodel
from semantic_kernel.data.vectors import VectorStoreField, vectorstoremodel
from semantic_kernel.functions.kernel_function import KernelFunction

"""
Expand Down
137 changes: 0 additions & 137 deletions python/samples/concepts/search/bing_text_search_as_plugin.py

This file was deleted.

12 changes: 6 additions & 6 deletions python/samples/concepts/search/brave_text_search_as_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from semantic_kernel.connectors.search.brave import BraveSearch
from semantic_kernel.contents import ChatHistory
from semantic_kernel.filters import FilterTypes, FunctionInvocationContext
from semantic_kernel.functions import KernelArguments, KernelParameterMetadata, KernelPlugin
from semantic_kernel.functions import KernelArguments, KernelParameterMetadata

"""
This project demonstrates how to integrate the Brave Search API as a plugin into the Semantic Kernel
Expand All @@ -21,10 +21,10 @@

kernel = Kernel()
kernel.add_service(OpenAIChatCompletion(service_id="chat"))
kernel.add_plugin(
KernelPlugin.from_text_search_with_search(
BraveSearch(),
plugin_name="brave",
kernel.add_function(
plugin_name="brave",
function=BraveSearch().create_search_function(
function_name="brave_search",
description="Get details about Semantic Kernel concepts.",
parameters=[
KernelParameterMetadata(
Expand All @@ -51,7 +51,7 @@
type_object=int,
),
],
)
),
)
chat_function = kernel.add_function(
prompt="{{$chat_history}}{{$user_input}}",
Expand Down
Loading