Skip to content

Commit

Permalink
Python: docs update + ruff config + fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dluc committed Apr 13, 2023
1 parent 5fc05b2 commit e2e5e2d
Show file tree
Hide file tree
Showing 26 changed files with 145 additions and 93 deletions.
35 changes: 18 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Semantic Kernel is available to explore AI and build apps with C# and Python:
<img src="https://user-images.githubusercontent.com/371009/230673733-7a447d30-b48e-46e1-bd84-2b321c90649e.png" style="margin-right:12px" height="30"/>
<a href="python/README.md">Using Semantic Kernel in Python</a>.
</div>
<br/>

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


## Samples apps ⚡
## Sample apps ⚡

The repository includes some sample applications, with a React frontend and
a backend web service using Semantic Kernel.
Expand All @@ -89,28 +90,28 @@ Follow the links for more information and instructions about running these apps.
| [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. |
| [Copilot Chat Sample App](samples/apps/copilot-chat-app/README.md) | Build your own chat experience based on Semantic Kernel. |

**Please note:**
**Requirements:**

- You will need an
[Open AI API Key](https://openai.com/api/) or
[Azure Open AI service key](https://learn.microsoft.com/azure/cognitive-services/openai/quickstart?pivots=rest-api)
to get started.
- There are a few software requirements you may need to satisfy before running examples and notebooks:
1. [Azure Functions Core Tools](https://learn.microsoft.com/azure/azure-functions/functions-run-local)
used for running the kernel as a local API, required by the web apps.
2. [Yarn](https://yarnpkg.com/getting-started/install) used for installing
web apps' dependencies.
3. Semantic Kernel supports .NET Standard 2.1 and it's recommended using .NET 6+. However, some of
the examples in the repository require [.NET 7](https://dotnet.microsoft.com/download) and the VS Code
[Polyglot extension](https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.dotnet-interactive-vscode)
to run the notebooks.

## Notebooks ⚡

For a more hands-on overview, you can also check out the Jupyter C# and Python notebooks, starting
- [Azure Functions Core Tools](https://learn.microsoft.com/azure/azure-functions/functions-run-local)
are required to run the kernel as a local web service, used by the sample web apps.
- [.NET 6](https://dotnet.microsoft.com/download/dotnet/6.0). If you have .NET 7 installe, Azure Function
Tools will still require .NET 6, so we suggest installing both.
- [Yarn](https://yarnpkg.com/getting-started/install) is used for installing web apps' dependencies.


## Jupyter Notebooks ⚡

For a more hands-on overview, you can also check out the C# and Python Jupyter notebooks, starting
from here:
* [Getting Started with C# notebook](samples/notebooks/dotnet/Getting-Started-Notebook.ipynb)
* [Getting Started with Python notebook](samples/notebooks/python/00-getting-started-notebook.ipynb)
* [Getting Started with C# notebook](samples/notebooks/dotnet/00-getting-started.ipynb)
* [Getting Started with Python notebook](samples/notebooks/python/00-getting-started.ipynb)

**Requirements:** C# notebooks require [.NET 7](https://dotnet.microsoft.com/download)
and the VS Code [Polyglot extension](https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.dotnet-interactive-vscode).

## Contributing and Community

Expand Down
17 changes: 14 additions & 3 deletions dotnet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ Make sure you have an

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

dotnet add package Microsoft.SemanticKernel --prerelease

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

See [here](../samples/notebooks/dotnet/README.md) for the full list with setup
instructions.
See [here](../samples/notebooks/dotnet/README.md) for the full list, with
requirements and setup instructions.

1. [Getting started](00-getting-started.ipynb)
2. [Loading and configuring Semantic Kernel](01-basic-loading-the-kernel.ipynb)
3. [Running AI prompts from file](02-running-prompts-from-file.ipynb)
4. [Creating Semantic Functions at runtime (i.e. inline functions)](03-semantic-function-inline.ipynb)
5. [Using Context Variables to Build a Chat Experience](04-context-variables-chat.ipynb)
6. [Creating and Executing Plans](05-using-the-planner.ipynb)
7. [Building Memory with Embeddings](06-memory-and-embeddings.ipynb)
8. [Creating images with DALL-E 2](07-DALL-E-2.ipynb)
9. [Chatting with ChatGPT and Images](08-chatGPT-with-DALL-E-2.ipynb)
4 changes: 2 additions & 2 deletions python/.env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
OPENAI_API_KEY=""
OPENAI_ORG_ID=""
AZURE_OPENAI_API_KEY=""
AZURE_OPENAI_ENDPOINT=""
AZURE_OPENAI_DEPLOYMENT_NAME=""
AZURE_OPENAI_ENDPOINT=""
AZURE_OPENAI_API_KEY=""
11 changes: 9 additions & 2 deletions python/DEV_SETUP.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ Copy those keys into a `.env` file (see the `.env.example` file):
```
OPENAI_API_KEY=""
OPENAI_ORG_ID=""
AZURE_OPENAI_API_KEY=""
AZURE_OPENAI_DEPLOYMENT_NAME=""
AZURE_OPENAI_ENDPOINT=""
AZURE_OPENAI_API_KEY=""
```

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

# Pipeline checks
# Tools and scripts

## Pipeline checks

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

poetry run pre-commit run -c .conf/.pre-commit-config.yaml -a

## Running ruff

poetry run ruff check .
16 changes: 8 additions & 8 deletions python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ Copy those keys into a `.env` file (see the `.env.example` file):
```
OPENAI_API_KEY=""
OPENAI_ORG_ID=""
AZURE_OPENAI_API_KEY=""
AZURE_OPENAI_ENDPOINT=""
AZURE_OPENAI_DEPLOYMENT_NAME=""
AZURE_OPENAI_ENDPOINT=""
AZURE_OPENAI_API_KEY=""
```

# Running a prompt
Expand Down Expand Up @@ -92,12 +92,12 @@ get started with the Semantic Kernel.

Python notebooks:

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

# Frequently asked questions

Expand Down
3 changes: 3 additions & 0 deletions python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ pytest-asyncio = "^0.21.0"
[tool.isort]
profile = "black"

[tool.ruff]
line-length = 120

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
7 changes: 7 additions & 0 deletions python/semantic_kernel/orchestration/sk_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ def skills(self) -> ReadOnlySkillCollectionBase:
"""
return self._skill_collection

@skills.setter
def skills(self, value: ReadOnlySkillCollectionBase) -> None:
"""
Set the value of skills collection
"""
self._skill_collection = value

@property
def log(self) -> Logger:
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright (c) Microsoft. All rights reserved.

import os
from typing import List, Tuple

from pytest import mark, raises
Expand All @@ -11,9 +12,9 @@

def _get_template_language_tests() -> List[Tuple[str, str]]:
path = __file__
path = path[: path.rfind("/")]
path = os.path.dirname(path)

with open(f"{path}/tests.txt", "r") as file:
with open(os.path.join(path, "tests.txt"), "r") as file:
content = file.readlines()

key = ""
Expand Down
62 changes: 39 additions & 23 deletions python/tests/unit/core_skills/test_file_io_skill.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@ def test_can_be_imported():
@pytest.mark.asyncio
async def test_can_read_async():
skill = FileIOSkill()
with tempfile.NamedTemporaryFile(mode="w", delete=True) as fp:
fp.write("Hello, world!")
fp.flush()
fp = None
try:
with tempfile.NamedTemporaryFile(mode="w", delete=False) as fp:
fp.write("Hello, world!")
fp.flush()

content = await skill.read_async(fp.name)
assert content == "Hello, world!"
content = await skill.read_async(fp.name)
assert content == "Hello, world!"
finally:
if fp is not None:
os.remove(fp.name)


@pytest.mark.asyncio
Expand All @@ -46,34 +51,45 @@ async def test_cannot_read_async():
@pytest.mark.asyncio
async def test_can_write():
skill = FileIOSkill()
with tempfile.NamedTemporaryFile(mode="r", delete=True) as fp:
context_variables = ContextVariables()
fp = None
try:
with tempfile.NamedTemporaryFile(mode="r", delete=False) as fp:
context_variables = ContextVariables()

context_variables.set("path", fp.name)
context_variables.set("content", "Hello, world!")
context_variables.set("path", fp.name)
context_variables.set("content", "Hello, world!")

context = SKContext(context_variables, None, None, None)
context = SKContext(context_variables, None, None, None)

await skill.write_async(context)
await skill.write_async(context)

content = fp.read()
content = fp.read()

assert content == "Hello, world!"
assert content == "Hello, world!"
finally:
if fp is not None:
os.remove(fp.name)


@pytest.mark.asyncio
async def test_cannot_write():
with tempfile.TemporaryDirectory() as temp_dir:
skill = FileIOSkill()
os.chmod(temp_dir, 0o500)
skill = FileIOSkill()
fp = None
try:
with tempfile.NamedTemporaryFile(mode="r", delete=False) as fp:
os.chmod(fp.name, 0o500)

temp_file = os.path.join(temp_dir, "test.txt")
context_variables = ContextVariables()
context_variables = ContextVariables()

context_variables.set("path", temp_file)
context_variables.set("content", "Hello, world!")
context_variables.set("path", fp.name)
context_variables.set("content", "Hello, world!")

context = SKContext(context_variables, None, None, None)
context = SKContext(context_variables, None, None, None)

with pytest.raises(PermissionError):
await skill.write_async(context)
with pytest.raises(PermissionError):
await skill.write_async(context)

os.chmod(fp.name, 0o777)
finally:
if fp is not None:
os.remove(fp.name)
6 changes: 3 additions & 3 deletions samples/dotnet/kernel-syntax-examples/Example14_Memory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public static async Task RunAsync()
{
["https://github.com/microsoft/semantic-kernel/blob/main/README.md"]
= "README: Installation, getting started, and how to contribute",
["https://github.com/microsoft/semantic-kernel/blob/main/samples/notebooks/dotnet/2-running-prompts-from-file.ipynb"]
["https://github.com/microsoft/semantic-kernel/blob/main/samples/notebooks/dotnet/02-running-prompts-from-file.ipynb"]
= "Jupyter notebook describing how to pass prompts from a file to a semantic skill or function",
["https://github.com/microsoft/semantic-kernel/blob/main/samples/notebooks/dotnet/Getting-Started-Notebook.ipynb"]
["https://github.com/microsoft/semantic-kernel/blob/main/samples/notebooks/dotnet/00-getting-started.ipynb"]
= "Jupyter notebook describing how to get started with the Semantic Kernel",
["https://github.com/microsoft/semantic-kernel/tree/main/samples/skills/ChatSkill/ChatGPT"]
= "Sample demonstrating how to create a chat skill interfacing with ChatGPT",
Expand Down Expand Up @@ -99,7 +99,7 @@ Files added.
Relevance: 0.8233838875772925
Result 2:
URL: : https://github.com/microsoft/semantic-kernel/blob/main/samples/dotnet-jupyter-notebooks/Getting-Started-Notebook.ipynb
URL: : https://github.com/microsoft/semantic-kernel/blob/main/samples/dotnet-jupyter-notebooks/00-getting-started.ipynb
Title : Jupyter notebook describing how to get started with the Semantic Kernel
Relevance: 0.7920580994199448
Expand Down
2 changes: 1 addition & 1 deletion samples/notebooks/dotnet/0-AI-settings.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
"metadata": {},
"source": [
"Now that your environment is all set up, let's dive into\n",
"[how to do basic loading of the Semantic Kernel](1-basic-loading-the-kernel.ipynb)."
"[how to do basic loading of the Semantic Kernel](01-basic-loading-the-kernel.ipynb)."
]
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@
"Sample app learning examples:\n",
"- [Simple chat summary](../../apps/chat-summary-webapp-react/README.md) (**Recommended**) – learn how basic semantic functions can be added to an app\n",
"- [Book creator](../../apps/book-creator-webapp-react/README.md) – learn how Planner and chaining of semantic functions can be used in your app\n",
"- [Authentication and APIs](../../dotnet/graph-api-skills/README.md) – learn how to connect to external API's with authentication while using Semantic Kernel"
"- [Authentication and APIs](../../dotnet/graph-api-skills/README.md) – learn how to connect to external API's with authentication while using Semantic Kernel",
"- [Copilot Chat](../../apps/copilot-chat-app/d) – Build your own chatbot based on Semantic Kernel"
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Great, now that you're familiar with setting up the Semantic Kernel, let's see [how we can use it to run prompts](2-running-prompts-from-file.ipynb)."
"Great, now that you're familiar with setting up the Semantic Kernel, let's see [how we can use it to run prompts](02-running-prompts-from-file.ipynb)."
]
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Great, now that you know how to load a skill from disk, let's show how you can [create and run a semantic function inline.](./3-semantic-function-inline.ipynb)"
"Great, now that you know how to load a skill from disk, let's show how you can [create and run a semantic function inline.](./03-semantic-function-inline.ipynb)"
]
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"The [previous notebook](./2-running-prompts-from-file.ipynb)\n",
"The [previous notebook](./02-running-prompts-from-file.ipynb)\n",
"showed how to define a semantic function using a prompt template stored on a file.\n",
"\n",
"In this notebook, we'll show how to use the Semantic Kernel to define functions inline with your C# code. This can be useful in a few scenarios:\n",
Expand Down
Loading

0 comments on commit e2e5e2d

Please sign in to comment.