Skip to content

Commit e2e5e2d

Browse files
committed
Python: docs update + ruff config + fix tests
1 parent 5fc05b2 commit e2e5e2d

26 files changed

Lines changed: 145 additions & 93 deletions

README.md

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Semantic Kernel is available to explore AI and build apps with C# and Python:
5454
<img src="https://user-images.githubusercontent.com/371009/230673733-7a447d30-b48e-46e1-bd84-2b321c90649e.png" style="margin-right:12px" height="30"/>
5555
<a href="python/README.md">Using Semantic Kernel in Python</a>.
5656
</div>
57+
<br/>
5758

5859
The quickest way to get started with the basics is to get an API key
5960
(OpenAI or Azure OpenAI)
@@ -74,7 +75,7 @@ For Python:
7475
5. Run the python script.
7576

7677

77-
## Samples apps ⚡
78+
## Sample apps ⚡
7879

7980
The repository includes some sample applications, with a React frontend and
8081
a backend web service using Semantic Kernel.
@@ -89,28 +90,28 @@ Follow the links for more information and instructions about running these apps.
8990
| [GitHub repository Q&A](samples/apps/github-qna-webapp-react/README.md) | Use embeddings and memory to store recent data and allow you to query against it. |
9091
| [Copilot Chat Sample App](samples/apps/copilot-chat-app/README.md) | Build your own chat experience based on Semantic Kernel. |
9192

92-
**Please note:**
93+
**Requirements:**
9394

9495
- You will need an
9596
[Open AI API Key](https://openai.com/api/) or
9697
[Azure Open AI service key](https://learn.microsoft.com/azure/cognitive-services/openai/quickstart?pivots=rest-api)
9798
to get started.
98-
- There are a few software requirements you may need to satisfy before running examples and notebooks:
99-
1. [Azure Functions Core Tools](https://learn.microsoft.com/azure/azure-functions/functions-run-local)
100-
used for running the kernel as a local API, required by the web apps.
101-
2. [Yarn](https://yarnpkg.com/getting-started/install) used for installing
102-
web apps' dependencies.
103-
3. Semantic Kernel supports .NET Standard 2.1 and it's recommended using .NET 6+. However, some of
104-
the examples in the repository require [.NET 7](https://dotnet.microsoft.com/download) and the VS Code
105-
[Polyglot extension](https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.dotnet-interactive-vscode)
106-
to run the notebooks.
107-
108-
## Notebooks ⚡
109-
110-
For a more hands-on overview, you can also check out the Jupyter C# and Python notebooks, starting
99+
- [Azure Functions Core Tools](https://learn.microsoft.com/azure/azure-functions/functions-run-local)
100+
are required to run the kernel as a local web service, used by the sample web apps.
101+
- [.NET 6](https://dotnet.microsoft.com/download/dotnet/6.0). If you have .NET 7 installe, Azure Function
102+
Tools will still require .NET 6, so we suggest installing both.
103+
- [Yarn](https://yarnpkg.com/getting-started/install) is used for installing web apps' dependencies.
104+
105+
106+
## Jupyter Notebooks ⚡
107+
108+
For a more hands-on overview, you can also check out the C# and Python Jupyter notebooks, starting
111109
from here:
112-
* [Getting Started with C# notebook](samples/notebooks/dotnet/Getting-Started-Notebook.ipynb)
113-
* [Getting Started with Python notebook](samples/notebooks/python/00-getting-started-notebook.ipynb)
110+
* [Getting Started with C# notebook](samples/notebooks/dotnet/00-getting-started.ipynb)
111+
* [Getting Started with Python notebook](samples/notebooks/python/00-getting-started.ipynb)
112+
113+
**Requirements:** C# notebooks require [.NET 7](https://dotnet.microsoft.com/download)
114+
and the VS Code [Polyglot extension](https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.dotnet-interactive-vscode).
114115

115116
## Contributing and Community
116117

dotnet/README.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ Make sure you have an
1010

1111
Here is a quick example of how to use Semantic Kernel from a C# console app.
1212
First, let's create a new project, targeting .NET 6 or newer, and add the
13-
`Microsoft.SemanticKernel` nuget package:
13+
`Microsoft.SemanticKernel` nuget package to your project from the command prompt
14+
in Visual Studio:
1415

1516
dotnet add package Microsoft.SemanticKernel --prerelease
1617

@@ -101,5 +102,15 @@ Console.WriteLine(output);
101102
The repository contains also a few C# Jupyter notebooks that demonstrates
102103
how to get started with the Semantic Kernel.
103104

104-
See [here](../samples/notebooks/dotnet/README.md) for the full list with setup
105-
instructions.
105+
See [here](../samples/notebooks/dotnet/README.md) for the full list, with
106+
requirements and setup instructions.
107+
108+
1. [Getting started](00-getting-started.ipynb)
109+
2. [Loading and configuring Semantic Kernel](01-basic-loading-the-kernel.ipynb)
110+
3. [Running AI prompts from file](02-running-prompts-from-file.ipynb)
111+
4. [Creating Semantic Functions at runtime (i.e. inline functions)](03-semantic-function-inline.ipynb)
112+
5. [Using Context Variables to Build a Chat Experience](04-context-variables-chat.ipynb)
113+
6. [Creating and Executing Plans](05-using-the-planner.ipynb)
114+
7. [Building Memory with Embeddings](06-memory-and-embeddings.ipynb)
115+
8. [Creating images with DALL-E 2](07-DALL-E-2.ipynb)
116+
9. [Chatting with ChatGPT and Images](08-chatGPT-with-DALL-E-2.ipynb)

python/.env.example

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
OPENAI_API_KEY=""
22
OPENAI_ORG_ID=""
3-
AZURE_OPENAI_API_KEY=""
4-
AZURE_OPENAI_ENDPOINT=""
53
AZURE_OPENAI_DEPLOYMENT_NAME=""
4+
AZURE_OPENAI_ENDPOINT=""
5+
AZURE_OPENAI_API_KEY=""

python/DEV_SETUP.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ Copy those keys into a `.env` file (see the `.env.example` file):
1313
```
1414
OPENAI_API_KEY=""
1515
OPENAI_ORG_ID=""
16-
AZURE_OPENAI_API_KEY=""
16+
AZURE_OPENAI_DEPLOYMENT_NAME=""
1717
AZURE_OPENAI_ENDPOINT=""
18+
AZURE_OPENAI_API_KEY=""
1819
```
1920

2021
We suggest adding a copy of the `.env` file under these folders:
@@ -92,9 +93,15 @@ You should be able to run the example under the [tests](tests) folder.
9293
poetry install
9394
poetry run pytest tests
9495

95-
# Pipeline checks
96+
# Tools and scripts
97+
98+
## Pipeline checks
9699

97100
To run the same checks that run during the GitHub Action build, you can use
98101
this command, from the [python](/python) folder:
99102

100103
poetry run pre-commit run -c .conf/.pre-commit-config.yaml -a
104+
105+
## Running ruff
106+
107+
poetry run ruff check .

python/README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ Copy those keys into a `.env` file (see the `.env.example` file):
1818
```
1919
OPENAI_API_KEY=""
2020
OPENAI_ORG_ID=""
21-
AZURE_OPENAI_API_KEY=""
22-
AZURE_OPENAI_ENDPOINT=""
2321
AZURE_OPENAI_DEPLOYMENT_NAME=""
22+
AZURE_OPENAI_ENDPOINT=""
23+
AZURE_OPENAI_API_KEY=""
2424
```
2525

2626
# Running a prompt
@@ -92,12 +92,12 @@ get started with the Semantic Kernel.
9292

9393
Python notebooks:
9494

95-
* [Getting started with Semantic Kernel](samples/notebooks/python/00-getting-started.ipynb)
96-
* [Loading and configuring Semantic Kernel](samples/notebooks/python/01-basic-loading-the-kernel.ipynb)
97-
* [Running AI prompts from file](samples/notebooks/python/02-running-prompts-from-file.ipynb)
98-
* [Creating Semantic Functions at runtime (i.e. inline functions)](samples/notebooks/python/03-semantic-function-inline.ipynb)
99-
* [Using Context Variables to Build a Chat Experience](samples/notebooks/python/04-context-variables-chat.ipynb)
100-
* [Building Memory with Embeddings](samples/notebooks/python/06-memory-and-embeddings.ipynb)
95+
* [Getting started with Semantic Kernel](../samples/notebooks/python/00-getting-started.ipynb)
96+
* [Loading and configuring Semantic Kernel](../samples/notebooks/python/01-basic-loading-the-kernel.ipynb)
97+
* [Running AI prompts from file](../samples/notebooks/python/02-running-prompts-from-file.ipynb)
98+
* [Creating Semantic Functions at runtime (i.e. inline functions)](../samples/notebooks/python/03-semantic-function-inline.ipynb)
99+
* [Using Context Variables to Build a Chat Experience](../samples/notebooks/python/04-context-variables-chat.ipynb)
100+
* [Building Memory with Embeddings](../samples/notebooks/python/06-memory-and-embeddings.ipynb)
101101

102102
# Frequently asked questions
103103

python/pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ pytest-asyncio = "^0.21.0"
2424
[tool.isort]
2525
profile = "black"
2626

27+
[tool.ruff]
28+
line-length = 120
29+
2730
[build-system]
2831
requires = ["poetry-core"]
2932
build-backend = "poetry.core.masonry.api"

python/semantic_kernel/orchestration/sk_context.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ def skills(self) -> ReadOnlySkillCollectionBase:
131131
"""
132132
return self._skill_collection
133133

134+
@skills.setter
135+
def skills(self, value: ReadOnlySkillCollectionBase) -> None:
136+
"""
137+
Set the value of skills collection
138+
"""
139+
self._skill_collection = value
140+
134141
@property
135142
def log(self) -> Logger:
136143
"""

python/tests/integration/template_engine/test_prompt_template_e2e.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Copyright (c) Microsoft. All rights reserved.
22

3+
import os
34
from typing import List, Tuple
45

56
from pytest import mark, raises
@@ -11,9 +12,9 @@
1112

1213
def _get_template_language_tests() -> List[Tuple[str, str]]:
1314
path = __file__
14-
path = path[: path.rfind("/")]
15+
path = os.path.dirname(path)
1516

16-
with open(f"{path}/tests.txt", "r") as file:
17+
with open(os.path.join(path, "tests.txt"), "r") as file:
1718
content = file.readlines()
1819

1920
key = ""

python/tests/unit/core_skills/test_file_io_skill.py

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,17 @@ def test_can_be_imported():
2323
@pytest.mark.asyncio
2424
async def test_can_read_async():
2525
skill = FileIOSkill()
26-
with tempfile.NamedTemporaryFile(mode="w", delete=True) as fp:
27-
fp.write("Hello, world!")
28-
fp.flush()
26+
fp = None
27+
try:
28+
with tempfile.NamedTemporaryFile(mode="w", delete=False) as fp:
29+
fp.write("Hello, world!")
30+
fp.flush()
2931

30-
content = await skill.read_async(fp.name)
31-
assert content == "Hello, world!"
32+
content = await skill.read_async(fp.name)
33+
assert content == "Hello, world!"
34+
finally:
35+
if fp is not None:
36+
os.remove(fp.name)
3237

3338

3439
@pytest.mark.asyncio
@@ -46,34 +51,45 @@ async def test_cannot_read_async():
4651
@pytest.mark.asyncio
4752
async def test_can_write():
4853
skill = FileIOSkill()
49-
with tempfile.NamedTemporaryFile(mode="r", delete=True) as fp:
50-
context_variables = ContextVariables()
54+
fp = None
55+
try:
56+
with tempfile.NamedTemporaryFile(mode="r", delete=False) as fp:
57+
context_variables = ContextVariables()
5158

52-
context_variables.set("path", fp.name)
53-
context_variables.set("content", "Hello, world!")
59+
context_variables.set("path", fp.name)
60+
context_variables.set("content", "Hello, world!")
5461

55-
context = SKContext(context_variables, None, None, None)
62+
context = SKContext(context_variables, None, None, None)
5663

57-
await skill.write_async(context)
64+
await skill.write_async(context)
5865

59-
content = fp.read()
66+
content = fp.read()
6067

61-
assert content == "Hello, world!"
68+
assert content == "Hello, world!"
69+
finally:
70+
if fp is not None:
71+
os.remove(fp.name)
6272

6373

6474
@pytest.mark.asyncio
6575
async def test_cannot_write():
66-
with tempfile.TemporaryDirectory() as temp_dir:
67-
skill = FileIOSkill()
68-
os.chmod(temp_dir, 0o500)
76+
skill = FileIOSkill()
77+
fp = None
78+
try:
79+
with tempfile.NamedTemporaryFile(mode="r", delete=False) as fp:
80+
os.chmod(fp.name, 0o500)
6981

70-
temp_file = os.path.join(temp_dir, "test.txt")
71-
context_variables = ContextVariables()
82+
context_variables = ContextVariables()
7283

73-
context_variables.set("path", temp_file)
74-
context_variables.set("content", "Hello, world!")
84+
context_variables.set("path", fp.name)
85+
context_variables.set("content", "Hello, world!")
7586

76-
context = SKContext(context_variables, None, None, None)
87+
context = SKContext(context_variables, None, None, None)
7788

78-
with pytest.raises(PermissionError):
79-
await skill.write_async(context)
89+
with pytest.raises(PermissionError):
90+
await skill.write_async(context)
91+
92+
os.chmod(fp.name, 0o777)
93+
finally:
94+
if fp is not None:
95+
os.remove(fp.name)

samples/dotnet/kernel-syntax-examples/Example14_Memory.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public static async Task RunAsync()
1818
{
1919
["https://github.com/microsoft/semantic-kernel/blob/main/README.md"]
2020
= "README: Installation, getting started, and how to contribute",
21-
["https://github.com/microsoft/semantic-kernel/blob/main/samples/notebooks/dotnet/2-running-prompts-from-file.ipynb"]
21+
["https://github.com/microsoft/semantic-kernel/blob/main/samples/notebooks/dotnet/02-running-prompts-from-file.ipynb"]
2222
= "Jupyter notebook describing how to pass prompts from a file to a semantic skill or function",
23-
["https://github.com/microsoft/semantic-kernel/blob/main/samples/notebooks/dotnet/Getting-Started-Notebook.ipynb"]
23+
["https://github.com/microsoft/semantic-kernel/blob/main/samples/notebooks/dotnet/00-getting-started.ipynb"]
2424
= "Jupyter notebook describing how to get started with the Semantic Kernel",
2525
["https://github.com/microsoft/semantic-kernel/tree/main/samples/skills/ChatSkill/ChatGPT"]
2626
= "Sample demonstrating how to create a chat skill interfacing with ChatGPT",
@@ -99,7 +99,7 @@ Files added.
9999
Relevance: 0.8233838875772925
100100
101101
Result 2:
102-
URL: : https://github.com/microsoft/semantic-kernel/blob/main/samples/dotnet-jupyter-notebooks/Getting-Started-Notebook.ipynb
102+
URL: : https://github.com/microsoft/semantic-kernel/blob/main/samples/dotnet-jupyter-notebooks/00-getting-started.ipynb
103103
Title : Jupyter notebook describing how to get started with the Semantic Kernel
104104
Relevance: 0.7920580994199448
105105

0 commit comments

Comments
 (0)