Files
semantic-kernel/python/samples/kernel-syntax-examples/rag_with_text_memory_plugin.py
T
Eduard van Valkenburg 192414180e Python: small fix for running prompt without any execution settings (#5687)
### Motivation and Context

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->
I noticed that when running a piece without any execution settings, it
errored out, while it should use 'default' service with the default
settings for that service, that is now fixed.
Small updates to the rag example.
Added TODO: to replace the embedding_generator service in
SemanticTextMemory with Kernel and the same service_selector pattern as
for KernelFunctions.

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

### 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 😄
2024-03-28 11:33:29 +00:00

41 lines
1.4 KiB
Python

# Copyright (c) Microsoft. All rights reserved.
import asyncio
import semantic_kernel as sk
import semantic_kernel.connectors.ai.open_ai as sk_oai
from semantic_kernel.core_plugins.text_memory_plugin import TextMemoryPlugin
from semantic_kernel.memory.semantic_text_memory import SemanticTextMemory
from semantic_kernel.memory.volatile_memory_store import VolatileMemoryStore
from semantic_kernel.utils.settings import openai_settings_from_dot_env
async def main():
kernel = sk.Kernel()
api_key, org_id = openai_settings_from_dot_env()
service_id = "default"
kernel.add_service(
sk_oai.OpenAIChatCompletion(service_id=service_id, ai_model_id="gpt-3.5-turbo", api_key=api_key, org_id=org_id)
)
embedding_gen = sk_oai.OpenAITextEmbedding(
service_id="ada", ai_model_id="text-embedding-ada-002", api_key=api_key, org_id=org_id
)
kernel.add_service(embedding_gen)
memory = SemanticTextMemory(storage=VolatileMemoryStore(), embeddings_generator=embedding_gen)
kernel.import_plugin_from_object(TextMemoryPlugin(memory), "memory")
await memory.save_information(collection="generic", id="info1", text="My budget for 2024 is $100,000")
result = await kernel.invoke_prompt(
function_name="budget",
plugin_name="BudgetPlugin",
prompt="{{memory.recall 'budget by year'}} What is my budget for 2024?",
)
print(result)
if __name__ == "__main__":
asyncio.run(main())