mirror of
https://github.com/microsoft/semantic-kernel.git
synced 2026-10-12 11:19:56 +00:00
3ce874bea3d5fc9847ac6f31fa7a963fa09babdd
7
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
2733a5574f |
Python: Import OpenAPI documents into the semantic kernel (#2297)
### 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. --> This allows us to import OpenAPI documents, including ChatGPT plugins, into the Semantic Kernel. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> - The interface reads the operationIds of the openapi spec into a skill: ```python from semantic_kernel.connectors.openapi import register_openapi_skill skill = register_openapi_skill(kernel=kernel, skill_name="test", openapi_document="url/or/path/to/openapi.yamlorjson") skill['operationId'].invoke_async() ``` - Parse an OpenAPI document - For each operation in the document, create a function that will execute the operation - Add all those operations to a skill in the kernel - Modified `import_skill` to accept a dictionary of functions instead of just class so that we can import dynamically created functions - Created unit tests TESTING: I've been testing this with the following ChatGPT plugins: - [Semantic Kernel Starter's Python Flask plugin](https://github.com/microsoft/semantic-kernel-starters/tree/main/sk-python-flask) - [ChatGPT's example retrieval plugin](https://github.com/openai/chatgpt-retrieval-plugin/blob/main/docs/providers/azuresearch/setup.md) - This one was annoying to setup. I didn't get the plugin functioning, but I was able to send the right API requests - Also, their openapi file was invalid. The "servers" attribute is misindented - [Google ChatGPT plugin](https://github.com/Sogody/google-chatgpt-plugin) - [Chat TODO plugin](https://github.com/lencx/chat-todo-plugin) - This openapi file is also invalid. I checked with an online validator. I had to remove"required" from the referenced request objects' properties: https://github.com/lencx/chat-todo-plugin/blob/main/openapi.yaml#L85 Then I used this python file to test the examples: ```python import asyncio import logging import semantic_kernel as sk from semantic_kernel import ContextVariables, Kernel from semantic_kernel.connectors.ai.open_ai import AzureTextCompletion from semantic_kernel.connectors.openapi.sk_openapi import register_openapi_skill # Example usage chatgpt_retrieval_plugin = { "openapi": # location of the plugin's openapi.yaml file, "payload": { "queries": [ { "query": "string", "filter": { "document_id": "string", "source": "email", "source_id": "string", "author": "string", "start_date": "string", "end_date": "string", }, "top_k": 3, } ] }, "operation_id": "query_query_post", } sk_python_flask = { "openapi": # location of the plugin's openapi.yaml file, "path_params": {"skill_name": "FunSkill", "function_name": "Joke"}, "payload": {"input": "dinosaurs"}, "operation_id": "executeFunction", } google_chatgpt_plugin = { "openapi": # location of the plugin's openapi.yaml file, "query_params": {"q": "dinosaurs"}, "operation_id": "searchGet", } todo_plugin_add = { "openapi": # location of the plugin's openapi.yaml file, "path_params": {"username": "markkarle"}, "payload": {"todo": "finish this"}, "operation_id": "addTodo", } todo_plugin_get = { "openapi": # location of the plugin's openapi.yaml file, "path_params": {"username": "markkarle"}, "operation_id": "getTodos", } todo_plugin_delete = { "openapi": # location of the plugin's openapi.yaml file, "path_params": {"username": "markkarle"}, "payload": {"todo_idx": 0}, "operation_id": "deleteTodo", } plugin = todo_plugin_get # set this to the plugin you want to try logger = logging.getLogger(__name__) logger.addHandler(logging.StreamHandler()) logger.setLevel(logging.DEBUG) kernel = Kernel(log=logger) deployment, api_key, endpoint = sk.azure_openai_settings_from_dot_env() kernel.add_text_completion_service( "dv", AzureTextCompletion(deployment, endpoint, api_key) ) skill = register_openapi_skill( kernel=kernel, skill_name="test", openapi_document=plugin["openapi"] ) context_variables = ContextVariables(variables=plugin) result = asyncio.run( skill[plugin["operation_id"]].invoke_async(variables=context_variables) ) print(result) ``` ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [ ] The code builds clean without any errors or warnings - [ ] 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 - [ ] All unit tests pass, and I have added new tests where possible - [ ] I didn't break anyone 😄 --------- Co-authored-by: Abby Harrison <[email protected]> |
||
|
|
0d37271499 |
Python: Action Planner implementation (#1948)
### 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. --> This PR is a first draft for implementation of ActionPlanner in Python. I try to follow the C# implementation as much as possible. Fixes: #1917 ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> - I have implemented ActionPlanner in the `python/semantic_kernel/planning/action_planner` directory. The implementation is in a file called `action_planner.py` and the prompt used by the planner (a replica of the prompt used in C#) is found in `skprompt.txt` in the same directory. - The functions `good_examples`, `edge_case_examples` and `list_of_functions` are used by the prompt to populate examples and list of available functions in the kernel. - After creating the plan, I parse the JSON generated by the planner by using regex matching to extract out pure JSON as implemented in #1496 - Based on whether or not the planner was able to identify a function, I create a Plan instance and return it. ### Here is some example code for using the planner ```python import logging import semantic_kernel as sk from semantic_kernel.planning import ActionPlanner from semantic_kernel.connectors.ai.open_ai import ( OpenAIChatCompletion, OpenAITextCompletion ) from semantic_kernel.core_skills import ( MathSkill, FileIOSkill, TimeSkill, TextSkill ) async def main(): kernel = sk.Kernel() api_key, org_id = sk.openai_settings_from_dot_env() kernel.add_text_completion_service("dv", OpenAITextCompletion("text-davinci-003", api_key, org_id)) kernel.import_skill(MathSkill(), "math") kernel.import_skill(FileIOSkill(), "fileIO") kernel.import_skill(TimeSkill(), "time") kernel.import_skill(TextSkill(), "text") planner = ActionPlanner(kernel) ask = "What is the sum of 110 and 990?" plan = await planner.create_plan_async(goal=ask) result = await plan.invoke_async() print(result.result) if __name__ == "__main__": import asyncio asyncio.run(main()) ``` ### Queries - In the C# implementation, when populating the function parameters in the plan object, the `plan.parameters` variable is updated with the function parameters. When I did the same in python, these parameters weren't being passed onto the function in `invoke_async` method of the plan instance. The parameters are only being passed when I update them in the `plan.state` variable. This code is in `line 121` of `action_planner.py`. What is the right way to do this? - Some skills / plugins require one of the inputs to be passed as the `input` variable and not in the context. How should this be handled in the planner?  **OR**  ### Issues that I faced - The `text-davinci-002` text-completion model regularly output incorrectly formatted JSON with commas missing. For example ```json {""plan"":{ ""rationale"": ""The list contains a function that allows for writing to files, so the plan is to use that function to write the desired content to the file 'output.txt'."", ""function"": ""fileIO.writeAsync"", ""parameters"": { ""input"": null, ""content"": ""A brief history of computer science"" ""path"": ""output.txt"" }}} ``` Pending Items - [x] Create unit tests - [x] Send the function parameters to the planner in list of available functions with/without input parameter. ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [x] The code builds clean without any errors or warnings - [x] The PR follows SK Contribution Guidelines (https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) - [x] The code follows the .NET coding conventions (https://learn.microsoft.com/dotnet/csharp/fundamentals/coding-style/coding-conventions) verified with `dotnet format` - [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: Abby Harrison <[email protected]> Co-authored-by: Shawn Callegari <[email protected]> |
||
|
|
a2da17d336 |
Python: google connector (#2071)
### 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. --> Implementation of Google search connector for web search skill. Fixes: #1862 Sample usage can be found in `python/samples/kernel-syntax-examples/google_search_skill.py` ### 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#dev-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: Abby Harrison <[email protected]> |
||
|
|
484b39d144 |
Python: Add sequential planner to python (#1916)
### 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. --> fixes #1915 ### 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: --> - [ ] The code builds clean without any errors or warnings - [ ] The PR follows SK Contribution Guidelines (https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) - [ ] The code follows the .NET coding conventions (https://learn.microsoft.com/dotnet/csharp/fundamentals/coding-style/coding-conventions) verified with `dotnet format` - [ ] All unit tests pass, and I have added new tests where possible - [ ] I didn't break anyone 😄 --------- Co-authored-by: Abby Harrison <[email protected]> Co-authored-by: Abby Harrison <[email protected]> Co-authored-by: Shawn Callegari <[email protected]> |
||
|
|
eab7a8f63a |
Python: implemented web search engine skill with bing connector (#1813)
### 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.
-->
In this PR, I have tried my hand at an implementation of web search
engine skill in python semantic kernel using the Bing Web Search API.
### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
In the semantic kernel directory, I have added a new directory called
web_skills (To replicate Skills.Web from C#) and added the web search
skill here. For now, I have implemented web search using the bing web
search API. If this approach is fine, then I can implement the same with
the google search API too. I have tried to stick with similar naming
conventions as used in the C# implementation with matching context
parameters and arguments.
I can also add some unit tests for the connectors and the search skill,
and add something like exponential backoff to avoid rate limit errors
while querying the search APIs.
Here is some sample code that checks the working of the search skill.
```python
import os
import semantic_kernel as sk
from semantic_kernel.web_skills.web_search_engine_skill import WebSearchEngineSkill
from semantic_kernel.web_skills.connectors import BingConnector
from semantic_kernel.connectors.ai.open_ai import OpenAITextCompletion
async def main():
kernel = sk.Kernel()
api_key, org_id = sk.openai_settings_from_dot_env()
kernel.add_text_completion_service(
"dv", OpenAITextCompletion("text-davinci-003", api_key, org_id)
)
connector = BingConnector(api_key=os.getenv("BING_API_KEY"))
web_skill = kernel.import_skill(WebSearchEngineSkill(connector), "WebSearch")
prompt = "Who is Leonardo DiCaprio's current girlfriend?"
search_async = web_skill["searchAsync"]
result = await search_async.invoke_async(prompt)
print(result)
"""
Output:
["Celebrity Celebrity News Everything You Need to Know About Leonardo DiCaprio and Camila Morrone's Relationship From the beginning of their romance to today, we track their relationship here. By..."]
"""
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.SearchAsync "What is semantic kernel?"}}
Question: What is semantic kernel?
Answer:
"""
qna = kernel.create_semantic_function(prompt, temperature=0.2)
context = kernel.create_new_context()
context["count"] = "10"
context["offset"] = "0"
result = await qna.invoke_async(context=context)
print(result)
"""
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())
```
### Contribution Checklist
<!-- Before submitting this PR, please make sure: -->
- [x] The code builds clean without any errors or warnings
- [x] The PR follows SK Contribution Guidelines
(https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
- [x] The code follows the .NET coding conventions
(https://learn.microsoft.com/dotnet/csharp/fundamentals/coding-style/coding-conventions)
verified with `dotnet format`
- [ ] 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: Abby Harrison <[email protected]>
|
||
|
|
378a1a949c |
Python: Add Logit-Bias to TextCompletion abstractions (#1880)
### 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. --> 1. Adds the Logit-Bias feature that is in the C# into Python. 2. It adds the token bias configuration to the text and chat completion settings. 3. It contributes to flexibility in prompt engineering by allowing the user to influence the likelihood of specified tokens appearing in the text and chat completion. 4. Closes #1854 ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> We added a dictionary field called token_selection_biases to the classes CompleteRequestSettings and ChatRequestSettings to implement logit bias and included an example to demonstrate functionality. ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [x] The code builds clean without any errors or warnings - [X] The PR follows SK Contribution Guidelines (https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) - [x] The code follows the .NET coding conventions (https://learn.microsoft.com/dotnet/csharp/fundamentals/coding-style/coding-conventions) verified with `dotnet format` - [x] All unit tests pass, and I have added new tests where possible - [X] I didn't break anyone 😄 --------- Co-authored-by: sneha <[email protected]> Co-authored-by: Alisha Maddy <[email protected]> Co-authored-by: Alisha Maddy <[email protected]> Co-authored-by: Abby Harrison <[email protected]> Co-authored-by: Sneha <[email protected]> |
||
|
|
7a3fad9dbb |
Python: move kernel-syntax-examples to python/samples folder (#1650)
Moving kernel-syntax-examples to the python/samples folder. Leaving README behind to point to the new folders (will delete in a few sprints) ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [X] The code builds clean without any errors or warnings - [X] The PR follows SK Contribution Guidelines (https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) - [X] The code follows the .NET coding conventions (https://learn.microsoft.com/dotnet/csharp/fundamentals/coding-style/coding-conventions) verified with `dotnet format` - [X] All unit tests pass, and I have added new tests where possible - [X] I didn't break anyone 😄 Co-authored-by: name <email> |