Files
semantic-kernel/python/samples/kernel-syntax-examples/bing_search_plugin.py
T
Evan Mattson 3553d623b1 Python: Fix create func from prompt missing args (#5339)
### Motivation and Context

There were missing function_name and plugin_name args from the create
function from prompt method in several kernel examples.

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

Adding in the missing parameters.

<!-- 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 15:12:09 +00:00

83 lines
2.6 KiB
Python

# Copyright (c) Microsoft. All rights reserved.
import os
from dotenv import load_dotenv
import semantic_kernel as sk
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion
from semantic_kernel.connectors.search_engine import BingConnector
from semantic_kernel.core_plugins import WebSearchEnginePlugin
load_dotenv()
async def main():
kernel = sk.Kernel()
deployment, key, endpoint, api_version = sk.azure_openai_settings_from_dot_env(include_api_version=True)
service_id = "chat-gpt"
kernel.add_service(
AzureChatCompletion(
service_id=service_id,
deployment_name=deployment,
api_key=key,
endpoint=endpoint,
api_version=api_version,
),
)
connector = BingConnector(api_key=os.getenv("BING_API_KEY"))
web_plugin = kernel.import_plugin_from_object(WebSearchEnginePlugin(connector), "WebSearch")
print("---------------- Question 1 -----------------\n")
query = "Which country receives the most rain per year?"
search = web_plugin["search"]
result = await kernel.invoke(search, query=query)
print(f"Question: {query}\n")
print(f"Answer: {result}\n")
print("---------------- Question 2 -----------------\n")
prompt = """
Answer the question using only the data that is provided in the data section.
Do not use any prior knowledge to answer the question.
Data: {{WebSearch.search "What is semantic kernel?"}}
Question: {{$question}}?
Answer:
"""
req_settings = kernel.get_service("chat-gpt").get_prompt_execution_settings_class()(service_id=service_id)
req_settings.temperature = 0.2
prompt_template_config = sk.PromptTemplateConfig(
template=prompt,
name="qna",
template_format="semantic-kernel",
execution_settings=req_settings,
)
question = "What is Semantic Kernel?"
qna = kernel.create_function_from_prompt(
function_name="qna",
plugin_name="WebSearch",
prompt_template_config=prompt_template_config,
)
result = await qna.invoke(kernel, question=question, num_results=10, offset=0)
print(f"Question: {question}\n")
print(f"Answer: {result}\n")
"""
Output:
Semantic Kernel is an open-source SDK that lets you easily combine AI services like OpenAI,
Azure OpenAI, and Hugging Face with conventional programming languages like C# and Python.
By doing so, you can create AI apps that combine the best of both worlds.
Semantic Kernel is at the center of the copilot stack.
"""
if __name__ == "__main__":
import asyncio
asyncio.run(main())