Skip to content

Commit e78242c

Browse files
kshitijcodeawharrison-28lemillermicrosoftdluc
authored
Python : Adding Sample for Azure Cognitive Search Memory (microsoft#2510)
### Motivation and Context This PR resolves microsoft#2493 ### Description The ACS implementation is different from VoltalileMemoryStore and we don't have any samples for Azure Cognitive Search Memory Store. I want to add Python Sample for using AzureCognitiveSearchMemoryStore with OpenAI embeddings. ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [x] The code builds clean without any errors or warnings - [x] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [x] All unit tests pass, and I have added new tests where possible - [x] I didn't break anyone 😄 --------- Co-authored-by: Abby Harrison <[email protected]> Co-authored-by: Lee Miller <[email protected]> Co-authored-by: Devis Lucato <[email protected]>
1 parent 4e72b9d commit e78242c

1 file changed

Lines changed: 101 additions & 0 deletions

File tree

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Copyright (c) Microsoft. All rights reserved.
2+
3+
import asyncio
4+
5+
from dotenv import dotenv_values
6+
7+
import semantic_kernel as sk
8+
from semantic_kernel.connectors.ai.open_ai import (
9+
AzureTextCompletion,
10+
AzureTextEmbedding,
11+
)
12+
from semantic_kernel.connectors.memory.azure_cognitive_search import (
13+
AzureCognitiveSearchMemoryStore,
14+
)
15+
16+
COLLECTION_NAME = "acs-index-sample"
17+
18+
19+
async def populate_memory(kernel: sk.Kernel) -> None:
20+
# Add some documents to the ACS semantic memory
21+
await kernel.memory.save_information_async(
22+
COLLECTION_NAME, id="info1", text="My name is Andrea"
23+
)
24+
await kernel.memory.save_information_async(
25+
COLLECTION_NAME, id="info2", text="I currently work as a tour guide"
26+
)
27+
await kernel.memory.save_information_async(
28+
COLLECTION_NAME, id="info3", text="I've been living in Seattle since 2005"
29+
)
30+
await kernel.memory.save_information_async(
31+
COLLECTION_NAME,
32+
id="info4",
33+
text="I visited France and Italy five times since 2015",
34+
)
35+
await kernel.memory.save_information_async(
36+
COLLECTION_NAME, id="info5", text="My family is from New York"
37+
)
38+
39+
40+
async def search_acs_memory_questions(kernel: sk.Kernel) -> None:
41+
questions = [
42+
"what's my name",
43+
"where do I live?",
44+
"where's my family from?",
45+
"where have I traveled?",
46+
"what do I do for work",
47+
]
48+
49+
for question in questions:
50+
print(f"Question: {question}")
51+
result = await kernel.memory.search_async(COLLECTION_NAME, question)
52+
print(f"Answer: {result[0].text}\n")
53+
54+
55+
async def main() -> None:
56+
kernel = sk.Kernel()
57+
58+
config = dotenv_values(".env")
59+
60+
AZURE_COGNITIVE_SEARCH_ENDPOINT = config["AZURE_COGNITIVE_SEARCH_ENDPOINT"]
61+
AZURE_COGNITIVE_SEARCH_ADMIN_KEY = config["AZURE_COGNITIVE_SEARCH_ADMIN_KEY"]
62+
AZURE_OPENAI_API_KEY = config["AZURE_OPENAI_API_KEY"]
63+
AZURE_OPENAI_ENDPOINT = config["AZURE_OPENAI_ENDPOINT"]
64+
vector_size = 1536
65+
66+
# Setting up OpenAI services for text completion and text embedding
67+
kernel.add_text_completion_service(
68+
"dv",
69+
AzureTextCompletion(
70+
deployment_name="text-embedding-ada-002",
71+
endpoint=AZURE_OPENAI_ENDPOINT,
72+
api_key=AZURE_OPENAI_API_KEY,
73+
),
74+
)
75+
kernel.add_text_embedding_generation_service(
76+
"ada",
77+
AzureTextEmbedding(
78+
deployment_name="text-embedding-ada-002",
79+
endpoint=AZURE_OPENAI_ENDPOINT,
80+
api_key=AZURE_OPENAI_API_KEY,
81+
),
82+
)
83+
84+
connector = AzureCognitiveSearchMemoryStore(
85+
vector_size, AZURE_COGNITIVE_SEARCH_ENDPOINT, AZURE_COGNITIVE_SEARCH_ADMIN_KEY
86+
)
87+
88+
# Register the memory store with the kernel
89+
kernel.register_memory_store(memory_store=connector)
90+
91+
print("Populating memory...")
92+
await populate_memory(kernel)
93+
94+
print("Asking questions... (manually)")
95+
await search_acs_memory_questions(kernel)
96+
97+
await connector.close_async()
98+
99+
100+
if __name__ == "__main__":
101+
asyncio.run(main())

0 commit comments

Comments
 (0)