Files
semantic-kernel/python/samples/kernel-syntax-examples/rag_with_text_memory_plugin.py
T
Evan Mattson d7820df9c2 Python: Add function name and plugin name to invoke prompt. Add unit tests. (#5318)
### Motivation and Context

The kernel.invoke_prompt was missing the function name and plugin name
args and was failing.

<!-- 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.
-->

### Description

This PR:
- Adds the required arguments. Closes #5313 
- Adds unit tests. 

<!-- 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-06 08:38:47 +00:00

37 lines
1.2 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.memory.semantic_text_memory import SemanticTextMemory
async def main():
kernel = sk.Kernel()
api_key, org_id = sk.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=sk.memory.VolatileMemoryStore(), embeddings_generator=embedding_gen)
kernel.import_plugin_from_object(sk.core_plugins.TextMemoryPlugin(memory), "TextMemoryPlugin")
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="{{recall 'budget by year'}} What is my budget for 2024?",
)
print(result)
if __name__ == "__main__":
asyncio.run(main())