|
| 1 | +# Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +import os |
| 4 | + |
| 5 | +from dotenv import load_dotenv |
| 6 | + |
| 7 | +import semantic_kernel as sk |
| 8 | +from semantic_kernel.connectors.ai.open_ai import OpenAITextCompletion |
| 9 | +from semantic_kernel.connectors.search_engine import GoogleConnector |
| 10 | +from semantic_kernel.core_skills import WebSearchEngineSkill |
| 11 | + |
| 12 | +load_dotenv() |
| 13 | + |
| 14 | + |
| 15 | +async def main(): |
| 16 | + kernel = sk.Kernel() |
| 17 | + api_key, org_id = sk.openai_settings_from_dot_env() |
| 18 | + kernel.add_text_completion_service( |
| 19 | + "dv", OpenAITextCompletion("text-davinci-003", api_key, org_id) |
| 20 | + ) |
| 21 | + |
| 22 | + """ |
| 23 | + Instantiate a Google Connector |
| 24 | + Make sure to have the following keys in a .env file or set as environment variables |
| 25 | + - GOOGLE_API_KEY |
| 26 | + - GOOGLE_SEARCH_ENGINE_ID |
| 27 | +
|
| 28 | + A Google Custom Search API has to be created in order to have an API key and a search engine ID. |
| 29 | + To create a Google Custom Search API, follow the guide - https://developers.google.com/custom-search/v1/overview. |
| 30 | + If you have already created the service, the credentials can be found in the Credentials tab on the page |
| 31 | + https://console.cloud.google.com/apis/api/customsearch.googleapis.com |
| 32 | + """ |
| 33 | + connector = GoogleConnector( |
| 34 | + api_key=os.getenv("GOOGLE_API_KEY"), |
| 35 | + search_engine_id=os.getenv("GOOGLE_SEARCH_ENGINE_ID"), |
| 36 | + ) |
| 37 | + |
| 38 | + # Import the WebSearchEngineSkill and pass the Google Connector to it. |
| 39 | + web_skill = kernel.import_skill(WebSearchEngineSkill(connector), "WebSearch") |
| 40 | + |
| 41 | + # The search query |
| 42 | + prompt = "Who is Leonardo DiCaprio's current girlfriend?" |
| 43 | + search_async = web_skill["searchAsync"] |
| 44 | + |
| 45 | + # By default, only one search result is provided |
| 46 | + result = await search_async.invoke_async(prompt) |
| 47 | + print(result) |
| 48 | + |
| 49 | + """ |
| 50 | + Output: |
| 51 | + ["Celebrity Celebrity News Everything You Need to Know About Leonardo DiCaprio and Camila Morrone's |
| 52 | + Relationship From the beginning of their romance to today, we track their relationship here. By..."] |
| 53 | + """ |
| 54 | + |
| 55 | + # Following example demonstrates the use of the skill within a semantic function |
| 56 | + prompt = """ |
| 57 | + Answer the question using only the data that is provided in the data section. |
| 58 | + Do not use any prior knowledge to answer the question. |
| 59 | + Data: {{WebSearch.SearchAsync "What is semantic kernel?"}} |
| 60 | + Question: What is semantic kernel? |
| 61 | + Answer: |
| 62 | + """ |
| 63 | + |
| 64 | + qna = kernel.create_semantic_function(prompt, temperature=0.2) |
| 65 | + context = kernel.create_new_context() |
| 66 | + |
| 67 | + """ |
| 68 | + Two context parameters can be passed to the search engine skill. |
| 69 | + - num_results controls the number of results returned by the web search. |
| 70 | + - offset controls the number of results to omit. |
| 71 | + """ |
| 72 | + context["num_results"] = "10" |
| 73 | + context["offset"] = "0" |
| 74 | + |
| 75 | + result = await qna.invoke_async(context=context) |
| 76 | + print(result) |
| 77 | + |
| 78 | + """ |
| 79 | + Output: |
| 80 | + Semantic Kernel is an open-source SDK that lets you easily combine AI services like OpenAI, |
| 81 | + Azure OpenAI, and Hugging Face with conventional programming languages like C# and Python. |
| 82 | + By doing so, you can create AI apps that combine the best of both worlds. |
| 83 | + Semantic Kernel is at the center of the copilot stack. |
| 84 | + """ |
| 85 | + |
| 86 | + |
| 87 | +if __name__ == "__main__": |
| 88 | + import asyncio |
| 89 | + |
| 90 | + asyncio.run(main()) |
0 commit comments