Commit Graph
163 Commits
Author SHA1 Message Date
John Liu 37f2760fdd Python: Fix SKFunction.invoke_stream_async (#3986)
### 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.
-->

The `invoke_stream_async` method on a native `SKFunction` is not
functional. The following sample code should work:
```python
import asyncio

import semantic_kernel as sk
from semantic_kernel.skill_definition import sk_function


class StreamNativeSkill:
    @sk_function(name="stream")
    async def stream(self, input: str) -> None: # This should also work: -> AsyncGenerator
        for i in range(5):
            await asyncio.sleep(1)
            yield input + str(i)


async def main():
    kernel = sk.Kernel()

    skill = kernel.import_skill(StreamNativeSkill(), "stream_skill")
    native_stream_func = skill["stream"]

    async for chunk in native_stream_func.invoke_stream_async("HELLO!"):
        print(chunk)


if __name__ == "__main__":
    asyncio.run(main())
```

And should output:
```
HELLO!0
HELLO!1
HELLO!2
HELLO!3
HELLO!4
```

But now it only shows:
```
HELLO!
```

### Description

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

Add four new `DelegateTypes` for native asynchronous generator functions
for different types of parameters:
- OutAsyncGenerator: No parameter, has `yield`
- InStringOutAsyncGenerator: One `str` parameter, has `yield`
- InContextOutAsyncGenerator: One `SKContext` parameter, has `yield`
- InStringAndContextOutAsyncGenerator: Both `str` and `SKContext`
parameters, has `yield`

Along with these four new types, add four new `DelegateInference`
functions to detect the types:
- infer_out_async_generator
- infer_in_string_out_async_generator
- infer_in_context_out_async_generator
- infer_in_string_and_context_out_async_generator

And because the async generator functions needs to use
`inspect.isasyncgenfunction` function to check instead of
`inspect.iscoroutinefunction`, add another parameter `is_asyncgenfunc`
to all the infer functions.

And finally, fix `SKFunction._invoke_native_stream_async` so that it can
correctly iterating from an async generator function.

### 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 😄
2023-12-08 14:48:11 +00:00
Eduard van ValkenburgandEvan Mattson 61b385defd Python: moved telemetry from pkg_resource to importlib (#4098)
### 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.
-->
Noticed a message saying pkg_resources.get_distribution was deprecated
so moved getting the version to importlib metadata
### 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 😄

Co-authored-by: Evan Mattson <[email protected]>
2023-12-08 14:36:41 +00:00
Evan MattsonandEvan Mattson a204be8faf Python: Bump Python version to 0.4.1. Make TextMemorySkill attributes ClassVar. (#4001)
Bump Python version to 0.4.1. Make TextMemorySkill attributes ClassVar.

### Motivation and Context

Bumping Python version to 0.4.1 for next release. Fix TextMemorySkill
attribute error to make it be a ClassVar after upgrading to Pydantic
2.0.

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

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

---------

Co-authored-by: Evan Mattson <[email protected]>
2023-12-05 18:28:02 +00:00
Evan MattsonandEvan Mattson 15f13b285d Python: allow default_headers configuration and users to pass in custom AzureOpenAI/OpenAI clients. (#3903)
### Motivation and Context

Currently, users in Python have no ability to configure default_headers
for the AzureOpenAI/OpenAI clients we use. Additionally, if a user wants
to set up their own AzureOpenAI/OpenAI client, this isn't possible. This
PR addresses #2895.

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

Allow users to set default_headers for Chat/Text/Embedding Completion
classes for both OpenAI and AzureOpenAI. Additionally, allow users to
pass in a customer OpenAI/AzureOpenAI client, if desired. Update tests
accordingly.

<!-- 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 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: Evan Mattson <[email protected]>
2023-12-05 17:05:13 +00:00
Eduard van Valkenburg 0982e3f271 Python: extra tests and a fix for the base_url (#3967)
### 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 the issue flagged by @sarah-widder in #3919, 
and added tests for base_url scenario's.

### 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 😄
2023-12-05 15:01:04 +00:00
Eduard van Valkenburg e0259c9e2c Python: fix deployment name parameter when using base_url (#3880)
### 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.
-->
Small fix, deployment_name is always needed, also when using base_url
for openai.

### 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 😄
2023-12-01 15:56:49 +00:00
Eduard van Valkenburg fdd16a7c7c Python: readded user agent telemetry to openai classes (#3881)
### 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.
-->
During the reworking of the OpenAI classes for openai sdk v1, the
user-agent telemetry was not ported over, readding that here.

### 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 😄
2023-12-01 15:56:39 +00:00
Eduard van Valkenburg 0868068f9b Python: Upgrade to Pydantic 2+ (#3723)
### 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.
-->
Upgraded pydantic to 2.0+, much cleaner, less complexity in different
types of base models.

### 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 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 😄
2023-11-30 19:34:08 +00:00
+17 c765cb1109 Python: Make SK compatible with OpenAI 1.0 (#3555)
Need to update SK Python to integrate the breaking changes from
openai-python 1.0

https://github.com/microsoft/semantic-kernel/issues/3330

This PR replaces the old one that was merging to main
https://github.com/microsoft/semantic-kernel/pull/3417

---------

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

### 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 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 😄

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: Joowon <[email protected]>
Co-authored-by: Roger Barreto <[email protected]>
Co-authored-by: Dmytro Struk <[email protected]>
Co-authored-by: Mark Wallace <[email protected]>
Co-authored-by: Ben Thomas <[email protected]>
Co-authored-by: Ben Thomas <[email protected]>
Co-authored-by: Devis Lucato <[email protected]>
Co-authored-by: Aayush Kataria <[email protected]>
Co-authored-by: Abby Harrison <[email protected]>
Co-authored-by: Abby Harrison <[email protected]>
Co-authored-by: Stephen Toub <[email protected]>
Co-authored-by: Teresa Hoang <[email protected]>
Co-authored-by: Chris <[email protected]>
Co-authored-by: Gil LaHaye <[email protected]>
Co-authored-by: Gina Triolo <[email protected]>
Co-authored-by: Tao Chen <[email protected]>
Co-authored-by: Lee Miller <[email protected]>
Co-authored-by: Jennifer Marsman <[email protected]>
Co-authored-by: Weihan Li <[email protected]>
Co-authored-by: SergeyMenshykh <[email protected]>
Co-authored-by: Diego Colombo <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Alex Chao <achao@achao>
Co-authored-by: Eduard van Valkenburg <[email protected]>
Co-authored-by: Evan Mattson <[email protected]>
Co-authored-by: Evan Mattson <[email protected]>
2023-11-29 06:44:10 +00:00
rom212andEvan Mattson 712cbafa10 Python: extended notebook 06 to showcase Azure AI Search for semantic memory (#3573)
### 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.
-->

Sample notebook 06-memory-and-embeddings demonstrated how semantic
kernel lets users add memory to their kernels and also work around
ever-growing contexts that eventually exceed the maximum token limit. It
did so by using the in RAM VolatileMemoryStore and teased the
possibility of offloading this to an external vector store. With this
pull request, we want to provide guidance and repeatable steps so that
users can actually perform semantic search by leveraging an external
vector store, Azure AI Search (formerly known as Azure Cognitive Search

### Description

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

The changes consist in new cells added at the tail end of the existing
notebook. We register a new memory store, external this time and show
case how the rest of the code can be reused as-is. We add the capability
of providing access credentials to Azure AI Search in environment
variables as well.

### 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
- [x] I didn't break anyone 😄

---------

Co-authored-by: Evan Mattson <[email protected]>
2023-11-27 17:18:16 +00:00
dcad9f7ba5 Python: Adding Azure CosmosDB Mongo vCore as a datastore. (#2990)
### Motivation and Context

I have added Azure CosmosDB MongoDB vCore as a data store. MongoDB vCore
now supports vector search on embeddings, and it could be used to
seamlessly integrate your AI-based applications with your data stored in
the Azure CosmosDB. More details about Mongo vCore can be found here:
https://learn.microsoft.com/en-us/azure/cosmos-db/mongodb/vcore/vector-search.

Issue https://github.com/microsoft/semantic-kernel/issues/2375

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, by providing the following
information:
-->
1. Why is this change required? **MongoDB vCore now supports vector
search on embeddings, and it could be used to seamlessly integrate your
AI-based applications with your data stored in the Azure CosmosDB.**
2. What problem does it solve? This adds a new memory store MongoDB
vCore(Azure CosmosDB)


### Description

<!-- Describe your changes, the overall approach, and the underlying
design.
These notes will help you understand 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 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]>
Co-authored-by: Abby Harrison <[email protected]>
Co-authored-by: Mark Wallace <[email protected]>
2023-11-14 22:55:55 +00:00
Mark WallaceandDevis Lucato 6f7cd6a9f7 Python: Add user agent to OpenAI and OpenAPI headers (#3488)
### Motivation and Context

Replaces: https://github.com/microsoft/semantic-kernel/pull/3074

This PR adds the User-Agent header "Semantic Kernel" to calls made to
OpenAI/Azure OpenAI and OpenAPI calls.

### Description

* Addition of telemetry.py file containing telemetry constants
* ability to disable Azure/OpenAI telemetry with an environment variable
called AZURE_TELEMETRY_DISABLED

### 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 😄

---------

Co-authored-by: Devis Lucato <[email protected]>
2023-11-14 18:50:09 +00:00
194f2401eb Python: sync C# for Pre/Post RunAsync Handlers (#3063)
### 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.
-->
#3038 

### 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 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: Roger Barreto <[email protected]>
Co-authored-by: Dmytro Struk <[email protected]>
2023-11-13 21:45:04 +00:00
Adarsh AcharyaandDmytro Struk 965c23d374 Python: Fixed type error in plan.run_next_step_async method (#3231)
### Description

This PR fixes #3219 

### Contribution Checklist

- [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 😄

---------

Co-authored-by: Dmytro Struk <[email protected]>
2023-10-19 15:01:30 +00:00
d8a2254685 Python: strings don't have contains method in python (#3229)
### 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?
- A substring in string check was being done using a '.contains' method
which doesn't exist in python.
  2. What problem does it solve?
- avoids the error: Error occurred while running plan step: 'str' object
has no attribute 'contains'", AttributeError("'str' object has no
attribute 'contains'"))
  3. What scenario does it contribute to?
  - stepwise planner execution
  7. If it fixes an open issue, please link to the issue here.

### Description

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

I changed the substring in string check to the proper method for python.

### 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 😄

---------

Co-authored-by: Kevin Kent <[email protected]>
Co-authored-by: Dmytro Struk <[email protected]>
2023-10-19 14:37:23 +00:00
7d31513d3f Python: Context Variables like dict (#3039)
### 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.
-->
It's shame sometimes it shines through that some of the earliest code in
the python code base was a direct port from C#, like with the .get
method on the context variables, made the change here to have the exact
some signature as the default python dict.
Also replaces contains_key with __contains__ so that 'in' keyword can be
used, just like a normal dict.
Fixes #2962 

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Changed the .get method
Replaced the contains_key method with __contains__
Made changes in samples, tests and planner to deal with that.

### 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
- [ ] I didn't break anyone 😄

---------

Co-authored-by: Devis Lucato <[email protected]>
Co-authored-by: Devis Lucato <[email protected]>
2023-10-18 17:22:55 +00:00
Adarsh AcharyaandDmytro Struk 988f65e41e Python: Prevent "device" override on "device_map" in HuggingFace Pipeline (#3111)
### Description

Fixes #3110 

### Contribution Checklist

- [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 😄

---------

Co-authored-by: Dmytro Struk <[email protected]>
2023-10-13 17:07:28 +00:00
JoowonandDmytro Struk b45c35c687 Python: improve ai service usability for text/chat completion (#3172)
### 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.
-->
fix #3171

### 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 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: Dmytro Struk <[email protected]>
2023-10-13 16:54:32 +00:00
Eduard van ValkenburgandAbby Harrison 85ee34b924 Python: Implement Function calling for Chat (#2356)
### 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 built support for function calling into SK!

Related to/fixes: 
- #2315 
- #2175 
- #1450 

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
This implementation builds on top of all the existing pieces, but did
require some major work, so feel free to comment on where that is or is
not appropriate.

- Added a `ChatMessage` class to capture the relevant pieces of function
calling (name and content)
- Added a `complete_chat_with_functions_async` into
OpenAIChatCompletions class
- Added a `function_call` field to ChatRequestSettings class
- Added several helper functions and smaller changes
- Added a sample with updated core_skill that uses function calling to
demonstrate
- Added a second sample that shows how to use function_calling with
non-sk functions.

### 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 😄

---------

Co-authored-by: Abby Harrison <[email protected]>
2023-10-12 17:34:22 +00:00
John LiuandDmytro Struk 98797a9978 Python: Allow action planner to exclude skills or functions (#3096)
### 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.
-->

Allow ActionPlanner to exclude skills and functions when generating the
plan. This is very useful and other planner types all have this
functionality.

Solve #3072

### Description

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

- Add `ActionPlannerConfig` class for three settings:
  - `excluded_skills`
  - `excluded_functions`
  - `max_tokens`
- Filter out available skills and functions in
`ActionPlanner.list_of_functions` method.
- Rename class attribute "ActionPlanner._skill_name" to
"ActionPlanner.RESTRICTED_SKILL_NAME" just like other planners and
update its value to "ActionPlanner". The `this.*` skills defined in
`skprompt.txt` must also be updated.
- Add the missing `__init__.py`:
`semantic_kernel/planning/action_planner/__init__.py`

### 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 😄

---------

Co-authored-by: Dmytro Struk <[email protected]>
2023-10-10 17:25:34 +00:00
John LiuandDmytro Struk 812bc235cc Python: Fix ActionPlanner logging: Value of the parameter (#3037)
### 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.
-->
Fix the bug so that logging works correctly.

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Fix the logging function in `ActionPlanner` to correctly show the
parameters' value of the generated plan.

### 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 😄

Co-authored-by: Dmytro Struk <[email protected]>
2023-10-04 09:15:27 +00:00
8ce66fe9a9 Python: Adding MongoDB Atlas Vector Search Connector (#2818)
### Motivation and Context
Resolves: #2591 

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

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

* Added in the `MongoDBAtlasMemoryStore`. A memory store abstraction
that facilitates connections between a MongoDB Atlas cluster to conduct
[Atlas Vector
Search](https://www.mongodb.com/products/platform/atlas-vector-search)
on the Microsoft Semantic Kernel.

* Leverages our async python driver
[motor](https://motor.readthedocs.io/en/stable/) to keep with the
heuristic of asynchronous code

### 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 😄

### Callout
* When writing my test, I noticed there's a habit of other tests
conducting a `try` on imports. Grepping through those implementations, I
see some of them [encapsulate their respective libraries in function
calls rather than doing direct imports in the
module.](https://github.com/microsoft/semantic-kernel/blob/main/python/semantic_kernel/connectors/memory/chroma/chroma_memory_store.py#L55-L63)
Is this the general code pattern we should follow? Or is it fine to have
our imports at the top of the module fine?

---------

Co-authored-by: Steven Silvester <[email protected]>
Co-authored-by: Abby Harrison <[email protected]>
Co-authored-by: Abby Harrison <[email protected]>
2023-10-02 21:25:28 +00:00
b6c32ed114 Python: Fixed prompt_template_config.from_dict method overwriting default CompletionConfig param values (#2853)
Fixes #2851 

### Description
dict.get('key') returns None if the key isn't present in the dict, Hence
the config params were getting assigned None.

### Contribution Checklist

- [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 😄

---------

Co-authored-by: Abby Harrison <[email protected]>
Co-authored-by: Abby Harrison <[email protected]>
2023-09-28 16:53:19 +00:00
JibandAbby Harrison 56bcccfab6 Python: Make semantic_kernel.NullLogger match logging.Logger function signatures (#2892)
### 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. #2889 
2. Allows Contributors and Devs to seamlessly use `NullLogger` or
generalized `Logger` class without having to do attribute checks
3. See details in #2889 
4. #2889 

### Description

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

* I added in a `_NullerMeta` metaclass to the `NullLogger` to iterate
over all the function signatures in the `Logger` class and have them
issue no returns.
* This is backward compatible and breaks no existing implementations.

### 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 😄

---------

Co-authored-by: Abby Harrison <[email protected]>
2023-09-27 19:30:22 +00:00
Eduard van ValkenburgandAbby Harrison eb9ecba63f Python: simple implementation of token usage (#2844)
### 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.
-->
Added token usage tracking to the OpenAI classes, similar to how that is
done in dotnet.
Similar approach as #2526 but for Python
TODO: add a similar construct to python for the SKContext.LastResult.

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Added three fields to the OpenAI Chat and Text completion classes.
After doing a call, check if 'usage" is in the response and then update
the fields, can be read through properties.

### 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 😄

---------

Co-authored-by: Abby Harrison <[email protected]>
2023-09-27 02:59:32 +00:00
Mark Karle 45f492aa89 Python: Enforcing return typehints on native functions (#2948)
### 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 solves [this
issue](https://github.com/microsoft/semantic-kernel/issues/1069) where
native functions are not returning the expected result. In python,
return typehints are optional. However, the semantic kernel requires a
return typehint to determine how to handle the function. For that
reason, we're now enforcing return typehints and raising an exception
when one is not provided. This may break existing void functions but
none in our repo are affected.

Fixes #1069 
### 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
- [] I didn't break anyone 😄

## Summary
This pull request includes two sets of changes. First, it fixes the
return type annotations in three core skills: file_io_skill,
text_memory_skill, and wait_skill. Specifically, the write_async method
in file_io_skill and the save_async method in text_memory_skill now have
a return type of None, and the wait method in wait_skill now has a
return type of None. Additionally, the
import_native_skill_from_directory method in kernel.py has been
refactored to simplify the code. Second, this pull request adds error
handling for functions with no return type in the delegate inference
module. Previously, if a function did not specify a return type, the
DelegateType could not be inferred, leading to errors. This pull request
adds a check for functions with no return type and raises a
KernelException with an appropriate error message. Additionally, this
pull request includes some minor changes to unit tests for native
functions.

## Changes
- Fixed return type annotation for write_async method in file_io_skill
- Fixed return type annotation for save_async method in
text_memory_skill
- Fixed return type annotation for wait method in wait_skill
- Refactored import_native_skill_from_directory method in kernel.py
- Added error handling for functions with no return type in the delegate
inference module
- Added a check for functions with no return type and raised a
KernelException with an appropriate error message
- Made minor changes to unit tests for native functions

---
*Powered by [Microsoft Semantic
Kernel](https://github.com/microsoft/semantic-kernel)*
2023-09-27 01:24:42 +00:00
Lee MillerandShawn Callegari 8fcd7c0c8b .Net: Fix XML escaping in skprompt.txt examples (#2827)
Update the examples in skprompt.txt to correctly escape XML characters
and improve the clarity of the comments.

Fixes #2246

### 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 😄

---------

Co-authored-by: Shawn Callegari <[email protected]>
2023-09-14 22:38:43 +00:00
Abby Harrison 1b883eac5e Python: Add init file to connectors/memory (#2769)
### Motivation and Context
This PR addresses inconsistencies in auto-doc generation.

### 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 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 😄
2023-09-11 17:20:31 +00:00
Chris f2f8d5a58a Python - Azure Cognitive Search Connector - Remove redeclaration (#2740)
### Motivation and Context

Customer reported credentials passed into connector are ignored:

https://github.com/microsoft/semantic-kernel/issues/2701

### Description

Local variable declaration taking precedence over function parameters.

### Contribution Checklist

- [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 😄
2023-09-07 19:15:51 +00:00
Abby Harrison 7313258072 Python: add chat system message to complete settings (#2726)
### Motivation and Context
This PR parallels work done in .NET for
https://github.com/microsoft/semantic-kernel/pull/2671 and closes
https://github.com/microsoft/semantic-kernel/issues/2722.

This PR allows users to set the chat system message using the semantic
function prompt template.

### Description

* added new `chat_system_message` parameter to
`complete_request_setttings` (defaults to None) and
`chat_request_settings `(defults to "Assistant is a large language
model.")
* added handling of this new parameter to the `chat_prompt_template`
* added corresponding tests
* added a small helper function for deserializing prompt_configs - token
selection biases need to be deserialized to ints, otherwise json loads
deserializes the keys in the set to be strings.

Unrelated 
* added missing __init__ file to the weaviate connector's directory

### 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 😄
2023-09-06 16:01:41 +00:00
Adarsh AcharyaandLee Miller 72736904ca Python: Fix attribute error in basic_planner.py under class Plan (#2720)
Fixes #2703 
Fixes #2660

### Description
Resolved attribute error in the Plan class under basic_planner.py. 

### Contribution Checklist

- [ ] 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
- [ ] All unit tests pass, and I have added new tests where possible

Unit test results : 

![Screenshot
(454)](https://github.com/microsoft/semantic-kernel/assets/132294330/059b852a-229e-4317-a2e4-bab16ac78e4a)

Co-authored-by: Lee Miller <[email protected]>
2023-09-05 23:09:41 +00:00
Eduard van ValkenburgandAbby Harrison e1595f4b30 Python: fixes for openapi sample (#2655)
### 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 tried the openapi sample, didn't work because the context variables
check if the values are strings, otherwise breaks. Now using strings,
and parsing those in the openapi runner.

### Description
Parse the required values in the openapi runner and updated the sample
to use a string of a dict.

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

---------

Co-authored-by: Abby Harrison <[email protected]>
2023-09-01 00:44:29 +00:00
Karol ŻakandAbby Harrison 90ca3d93ee Python: Error fix for HuggingFaceTextEmbedding when trying to run on GPU (#2629)
### Motivation and Context

I run into error while trying to initialize `HuggingFaceTextEmbedding`
with `device=0`.

```
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 552, in _run_script
    exec(code, module.__dict__)
  File "/app/app/app.py", line 329, in <module>
    asyncio.run(main())
  File "/usr/local/lib/python3.9/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/local/lib/python3.9/asyncio/base_events.py", line 647, in run_until_complete
    return future.result()
  File "/app/app/app.py", line 220, in main
    kernel = init_kernel()
  File "/usr/local/lib/python3.9/site-packages/streamlit/runtime/caching/cache_utils.py", line 211, in wrapper
    return cached_func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/streamlit/runtime/caching/cache_utils.py", line 240, in __call__
    return self._get_or_create_cached_value(args, kwargs)
  File "/usr/local/lib/python3.9/site-packages/streamlit/runtime/caching/cache_utils.py", line 266, in _get_or_create_cached_value
    return self._handle_cache_miss(cache, value_key, func_args, func_kwargs)
  File "/usr/local/lib/python3.9/site-packages/streamlit/runtime/caching/cache_utils.py", line 320, in _handle_cache_miss
    computed_value = self._info.func(*func_args, **func_kwargs)
  File "/app/app/app.py", line 148, in init_kernel
    sk_hf.HuggingFaceTextEmbedding("sentence-transformers/all-MiniLM-L6-v2", device=0),
  File "/usr/local/lib/python3.9/site-packages/semantic_kernel/connectors/ai/hugging_face/services/hf_text_embedding.py", line 49, in __init__
    "cuda:" + device if device >= 0 and torch.cuda.is_available() else "cpu"
TypeError: can only concatenate str (not "int") to str
```

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


I replace `"cuda:" + device` with `"cuda:" + str(device)`

<!-- 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 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]>
2023-08-31 23:49:49 +00:00
Sneha bdc7258938 Python: fix incorrect token_selection_biases assignments (#2644)
### 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 #2565 , I addressed a problem where `token_selection_biases` was not
included in the `PromptTemplateConfig`. However, I mistakenly wrote
`config.token_selection_biases` in instances where it should be
`config.completion.token_selection_biases` to assign to the actual
`CompletionConfig` structure.

### Description

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

Changed these faulty lines to be
`config.completion.token_selection_biases = token_selection_biases`

### 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 😄
2023-08-31 21:37:52 +00:00
73afea986a Python: added settings function to load direct into constructor (#2069)
### 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.
-->
Noticed that another commonly edited environment setting for api_version
was missing from the settings util. And want to pass the env to the
constructor.

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Added api_version to the settings util so that that can be controlled
through .env as well.
Added convenience function that outputs a dict that can be passed
directly into Completion constructors.
Updated sample to use that.

### 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
- [ ] I didn't break anyone 😄

---------

Co-authored-by: Abby Harrison <[email protected]>
Co-authored-by: Lee Miller <[email protected]>
2023-08-31 20:59:45 +00:00
edf35de85d Python: Redis memory connector (#2132)
### 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. .NET supports Redis memory while Python does not
2. Match up Python features w/ .NET
3. Users can utilize a Redis backend for memories
4. Closes #1981

### Description

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

Implemented a memory connector for Redis and made unit tests for it.
Also added .env configuration for a connection string.

### 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: Dmytro Struk <[email protected]>
Co-authored-by: Abby Harrison <[email protected]>
Co-authored-by: Abby Harrison <[email protected]>
2023-08-31 20:38:16 +00:00
4b2b93d8b0 Python: Update .env with weaviate (#2148)
Included weaviate env variables

---------

Co-authored-by: Dmytro Struk <[email protected]>
Co-authored-by: Abby Harrison <[email protected]>
Co-authored-by: Abby Harrison <[email protected]>
Co-authored-by: Lee Miller <[email protected]>
2023-08-25 23:06:36 +00:00
Sneha a311422a52 Python: Include token_selection_biases in PromptTemplateConfig (#2565)
### 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.
-->

`token_selection_biases` was missing from constructors in
`PromptTemplateConfig` and `CompleteRequestSettings`

Closes #2564 

### Description

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

Added `token_selection_biases` into the config template and defaults to
an empty dictionary

### 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 😄
2023-08-24 21:20:53 +00:00
Karol ŻakandAbby Harrison 601c9de124 Python: Add HF model and pipeline kwargs to allow model loading customization (#2547)
### 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 was trying to use this library to load and use [HF Falcon
model](https://huggingface.co/tiiuae/falcon-7b-instruct). Unfortunately
in order to make it work I needed to pass in some additional arguments
such as `torch_dtype`, `cache_dir` or `tokenizer`. It was not possible
with current implementation so I decided to add `model_kwargs` and
`pipeline_kwargs` to the `HuggingFaceTextCompletion` class init function
so now, with the changes I introduced I can successfully load any
`falcon` models by using this code:
```python
import semantic_kernel as sk
import semantic_kernel.connectors.ai.hugging_face as sk_hf
import torch
from transformers import AutoTokenizer


cache_dir = "/mnt/tmp"
model_name = "tiiuae/falcon-7b-instruct"

tokenizer = AutoTokenizer.from_pretrained(
    pretrained_model_name_or_path=model_name,
    cache_dir=cache_dir,
    trust_remote_code=True)

hf_model = sk_hf.HuggingFaceTextCompletion(
    model_name,
    task="text-generation",
    model_kwargs={
        "cache_dir": cache_dir,
        "torch_dtype": torch.bfloat16
    },
    pipeline_kwargs={
        "tokenizer": tokenizer,
        "trust_remote_code": True,
        "device": 0,
    }
)
```

### Description

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

The changes I introduced add `model_kwargs` and `pipeline_kwargs` to
`HuggingFaceTextCompletion` class so that these arguments are then
passed to the underlying `transformers.pipeline()` function and thus
allow more customization options.

### 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 😄

---------

Co-authored-by: Abby Harrison <[email protected]>
2023-08-24 18:24:12 +00:00
f251e654ec Python: Enable serialization for sk/orchestration (#2369)
SKFunction has been excluded for now.

Handles issues arising due to many circular imports Update
DelegateInference to handle some cases where annotation is given as a
string to avoid circular imports like in FileIOSkill.

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

### 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 😄

---------

Co-authored-by: Abby Harrison <[email protected]>
Co-authored-by: Mark Karle <[email protected]>
Co-authored-by: Abby Harrison <[email protected]>
Co-authored-by: Lee Miller <[email protected]>
Co-authored-by: Devis Lucato <[email protected]>
2023-08-24 16:05:39 +00:00
4e72b9d6ca Python: add restore method to chat_prompt_template (#2495)
### 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 order to add ways to save and restore a running conversation this
adds a restore method and a property to the ChatPromptTemplate class.

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
1. Add messages property which provides a view to the role,
message.template tuple
2. Add restore classmethod that takes the same input as a regular init,
but adds a list of tuples, it then loops through that list and adds each
message.

### 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 😄

---------

Co-authored-by: Abby Harrison <[email protected]>
Co-authored-by: Devis Lucato <[email protected]>
2023-08-23 23:52:56 +00:00
Julio Carlos Menendez b228346936 Python: Add missing parameter logger on inheriting classes and makes it optional (#2537)
### Motivation and Context
Makes the `logger` parameter optional in
`TextCompletionClientBase.complete_async`,
`TextCompletionClientBase.complete_stream_async`,
`ChatCompletionClientBase.complete_chat_async`, and
`ChatCompletionClientBase.complete_chat_stream_async` to match the
current usage across the project where it's not passed.
It also adds that `logger` optional parameter to the inheriting classes
where needed to match the base class method signatures.

Resolves #2509

### 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 😄
2023-08-23 23:48:12 +00:00
3881a3137f Python: Adding USearch memory connector (#2358)
### Motivation and Context

The integration of [USearch](https://github.com/unum-cloud/usearch) as a
memory connector to Semantic Kernel (SK).

### Description
     
The USearch `Index` does not natively have the ability to store
different collections, and it only stores embeddings without other
attributes like `MemoryRecord`.

The `USearchMemoryStore` class encapsulates these capabilities. It uses
the USearch `Index` to store a collection of embeddings under unique
IDs, with original collection names mapped to those IDs. Other
`MemoryRecord ` attributes are stored in a `pyarrow.Table`, which is
mapped to each collection.

It's important to note the current behavior when a user removes a record
or upserts a new one with an existing ID: the old row is not removed
from the `pyarrow.Table`. This is done for performance reasons but could
lead to the table growing in size.

By default, `USearchMemoryStore` operates as an in-memory store. To
enable persistence, you must set the persist mode with calling
appropriate `__init__ `, supplying a path to the directory for the
persist files. For each collection, two files will be created:
`{collection_name}.usearch` and `{collection_name}.parquet`. Changes
will only be dumped to the disk when `close_async` is called. Due to the
interface provided by the base class `MemoryStoreBase`, this happens
implicitly when using a context manager, or it may be called explicitly.

Since collection names are used to store files on disk, all names are
converted to lowercase.

To ensure efficient use of memory, you should call `close_async`.
---------

Co-authored-by: Abby Harrison <[email protected]>
Co-authored-by: Abby Harrison <[email protected]>
Co-authored-by: Devis Lucato <[email protected]>
2023-08-23 16:18:36 -07:00
4bc5ff716d Python: provide methods to register single native function to the kernel (#2390)
### 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.
-->
solve #2321 

### 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 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]>
Co-authored-by: Abby Harrison <[email protected]>
Co-authored-by: Lee Miller <[email protected]>
2023-08-23 22:59:08 +00:00
05afd29ef6 Python: add stepwise planner (#2223)
### 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.
-->
implement stepwise planner #2221

### 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 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]>
Co-authored-by: Lee Miller <[email protected]>
Co-authored-by: Abby Harrison <[email protected]>
2023-08-23 19:12:52 +00:00
98d2d9fa2e Python: ACS should use Hnsw for vector indexing (#2534)
### Motivation and Context

This PR builds on https://github.com/microsoft/semantic-kernel/pull/2435
- the previous PR updates azure-search to the latest package , where it
is recommended to use HnswVectorSearchAlgorithmConfiguration. The
previous implementation appears to be in a semi-deprecated state.

This new flow is the publicly recommended use for ACS according to
https://github.com/Azure/cognitive-search-vector-pr/tree/main/demo-python/code.

### Description
- update `azure-search-documents` dependency from `11.4.0b6 -> 11.4.0b8`
- index configuration changed from `VectorSearchAlgorithmConfiguration
-> HnswVectorSearchAlgorithmConfiguration`
- update `search_client.search()` call in `get_nearest_matches()` to be
consistent with the new index api

### 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: Kinfey <[email protected]>
Co-authored-by: Lee Miller <[email protected]>
Co-authored-by: Kinfey <[email protected]>
2023-08-22 23:56:49 +00:00
Abby Harrison f0ab0fbd40 Python: Revert "Python: Update Azure Cognitive Search Class Change VectorSearchAlgorithmConfi…" (#2531)
Reverts microsoft/semantic-kernel#2435

This PR caused a breaking change to the ACS memory store's
get_nearest_matches method
2023-08-22 19:49:09 +00:00
59cbbdb2af Python: Add Google PaLM connector with chat completion and text embedding (#2258)
### Motivation and Context

Implementation of Google PaLM connector with chat completion, text
embedding and three example files to demonstrate their functionality.
Closes #2098
I also opened a PR for text completion #2076

<!-- 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
**What's new:**
1. Implemented Google Palm connector with chat completion and text
embedding
2. Added 3 example files to `python/samples/kernel-syntax-examples`
3. The example files show PaLM chat in action with skills, memory, and
just with user messages. The memory example uses PaLM embedding as well
5. Added integration tests to test chat with skills and embedding with
kernel.memory functions
6. Added unit tests to verify successful class initialization and
correct behavior from class functions
  
**There are some important differences between google palm chat and open
ai chat:**

1. PaLM has two functions for chatting, `chat` and `reply`. The chat
function in google's genai library starts a new conversation, and reply
continues the conversation. Reply is an attribute of the response object
returned by chat. So an instance of the `GooglePalmChatCompletion` class
needs a way to determine which function to use, which is why I
introduced a private attribute to store the response object. See
https://developers.generativeai.google/api/python/google/generativeai/types/ChatResponse
2. PaLM does not use system messages. Instead, the chat function takes a
parameter called context. It serves the same purpose, to prime the
assistant with certain behaviors and information. So when the user
passes a system message to `complete_chat_async`, it is passed to `chat`
as the context parameter.
3. Semantic memory works with the chat service as long as the user
creates a chat prompt template. The prompt containing the memory needs
to be added to the chat prompt template as a system message. See
`python\samples\kernel-syntax-examples\google_palm_chat_with_memory.py`
for more details. If the only purpose of `complete_async` in
`GooglePalmChatCompletion` is to send memories + user messages to the
chat service as a text prompt, then `complete_async` is not fulfilling
its intended purpose. A possible solution would be to send the text
prompt as a request to the text service within `complete_async`.

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

Currently no warnings, there was 1 warning when first installing genai
with `poetry add google.generativeai==v0.1.0rc2` from within poetry
shell: "The locked version 0.1.0rc2 for google-generativeai is a yanked
version. Reason for being yanked: Release is marked as supporting Py3.8,
but in practice it requires 3.9". We would need to require later
versions of python to fix it.

---------

Co-authored-by: Abby Harrison <[email protected]>
Co-authored-by: Abby Harrison <[email protected]>
Co-authored-by: Abby Harrison <[email protected]>
2023-08-21 23:56:44 +00:00
JoowonandAbby Harrison e42f7fabe8 Python fix native function default parameter (#2474)
### 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.
-->
solve #2473  and add unit test to prevent this from happening again. 

### 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 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]>
2023-08-21 22:54:02 +00:00
KinfeyandLee Miller 8a7e84ccbc Python: Update Azure Cognitive Search Class Change VectorSearchAlgorithmConfi… (#2435)
…guration to HnswVectorSearchAlgorithmConfiguration

### 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?
  
Azure Cognitive Search SDK Change VectorSearchAlgorithmConfiguration to
HwsnVectorSearchAlgorithmConfiguration
  
  2. What problem does it solve?
  
ImportError: cannot import name 'VectorSearchAlgorithmConfiguration'
from 'azure.search.documents.indexes.models'
  
  3. What scenario does it contribute to?

Python SDK Vector DB Change

  5. If it fixes an open issue, please link to the issue here.
  

https://github.com/Azure/cognitive-search-vector-pr/blob/main/demo-python/code/azure-search-vector-python-sample.ipynb

### 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 😄

---------

Co-authored-by: Lee Miller <[email protected]>
2023-08-21 15:37:54 +00:00