Skip to content

Commit

Permalink
Python: fixes and release (#855)
Browse files Browse the repository at this point in the history
* Fix lint errors
* Bump version number
* Add retry loop to integration tests
* Enable all integration tests: some tests will fail if OpenAI/Azure are
throttling.
  • Loading branch information
dluc authored May 8, 2023
1 parent 3fae7c4 commit de3436d
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 46 deletions.
2 changes: 1 addition & 1 deletion python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "semantic-kernel"
version = "0.2.6.dev"
version = "0.2.7.dev"
description = ""
authors = ["Microsoft <[email protected]>"]
readme = "pip/README.md"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from logging import Logger
from typing import Optional


from semantic_kernel.connectors.ai.ai_exception import AIException
from semantic_kernel.connectors.ai.complete_request_settings import (
CompleteRequestSettings,
Expand Down Expand Up @@ -48,11 +47,13 @@ def __init__(
self._log = log if log is not None else NullLogger()

try:
import transformers
import torch
import transformers
except (ImportError, ModuleNotFoundError):
raise ImportError("Please ensure that torch and transformers are installed to use HuggingFaceTextCompletion")

raise ImportError(
"Please ensure that torch and transformers are installed to use HuggingFaceTextCompletion"
)

self.device = (
"cuda:" + device if device >= 0 and torch.cuda.is_available() else "cpu"
)
Expand All @@ -75,6 +76,7 @@ async def complete_async(
"""
try:
import transformers

generation_config = transformers.GenerationConfig(
temperature=request_settings.temperature,
top_p=request_settings.top_p,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from logging import Logger
from typing import List, Optional

from numpy import array, ndarray

from semantic_kernel.connectors.ai.ai_exception import AIException
Expand Down Expand Up @@ -37,11 +38,13 @@ def __init__(
self._log = log if log is not None else NullLogger()

try:
import torch
import sentence_transformers
except (ImportError):
raise ImportError("Please ensure that torch and sentence-transformers are installed to use HuggingFaceTextEmbedding")

import torch
except ImportError:
raise ImportError(
"Please ensure that torch and sentence-transformers are installed to use HuggingFaceTextEmbedding"
)

self.device = (
"cuda:" + device if device >= 0 and torch.cuda.is_available() else "cpu"
)
Expand Down
1 change: 1 addition & 0 deletions python/semantic_kernel/kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

T = TypeVar("T")


class Kernel(KernelBase, KernelExtensions):
_log: Logger
_skill_collection: SkillCollectionBase
Expand Down
80 changes: 56 additions & 24 deletions python/tests/integration/completions/e2e_text_completion.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
import logging
import time

import semantic_kernel as sk

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger()


async def retry(func, retries=15, delay=1):
for i in range(retries):
try:
result = str(await func())
if "Error" in result:
raise ValueError(result)
return result
except Exception as e:
logger.error(f"Retry {i + 1}: {e}")
if i == retries - 1: # Last retry
raise
time.sleep(delay)


async def summarize_function_test(kernel: sk.Kernel):
# Define semantic function using SK prompt template language
Expand Down Expand Up @@ -33,68 +53,80 @@ async def summarize_function_test(kernel: sk.Kernel):
print()

# Summarize input string and print
summary = await kernel.run_async(tldr_function, input_str=text_to_summarize)

summary = await retry(
lambda: kernel.run_async(tldr_function, input_str=text_to_summarize)
)
output = str(summary).strip()
print(f"Summary using input string: '{output}'")
assert len(output.split(" ")) == 5
assert "humans" in output or "Humans" in output or "preserve" in output
assert len(output) < 100

# Summarize input as context variable and print
context_vars = sk.ContextVariables(text_to_summarize)
summary = await kernel.run_async(tldr_function, input_vars=context_vars)

summary = await retry(
lambda: kernel.run_async(tldr_function, input_vars=context_vars)
)
output = str(summary).strip()
print(f"Summary using context variables: '{output}'")
assert len(output.split(" ")) == 5
assert "humans" in output or "Humans" in output or "preserve" in output
assert len(output) < 100

# Summarize input context and print
context = kernel.create_new_context()
context["input"] = text_to_summarize
summary = await kernel.run_async(tldr_function, input_context=context)

summary = await retry(
lambda: kernel.run_async(tldr_function, input_context=context)
)
output = str(summary).strip()
print(f"Summary using input context: '{output}'")
assert len(output.split(" ")) == 5
assert "humans" in output or "Humans" in output or "preserve" in output
assert len(output) < 100

# Summarize input context with additional variables and print
context = kernel.create_new_context()
context["input"] = text_to_summarize
context_vars = sk.ContextVariables("4) All birds are robots.")
summary = await kernel.run_async(
tldr_function, input_context=context, input_vars=context_vars
summary = await retry(
lambda: kernel.run_async(
tldr_function, input_context=context, input_vars=context_vars
)
)

output = str(summary).strip()
print(f"Summary using context and additional variables: '{output}'")
assert len(output.split(" ")) == 5
assert "humans" in output or "Humans" in output or "preserve" in output
assert len(output) < 100

# Summarize input context with additional input string and print
context = kernel.create_new_context()
context["input"] = text_to_summarize
summary = await kernel.run_async(
tldr_function, input_context=context, input_str="4) All birds are robots."
summary = await retry(
lambda: kernel.run_async(
tldr_function, input_context=context, input_str="4) All birds are robots."
)
)

output = str(summary).strip()
print(f"Summary using context and additional string: '{output}'")
assert len(output.split(" ")) == 5
assert "humans" in output or "Humans" in output or "preserve" in output
assert len(output) < 100

# Summarize input context with additional variables and string and print
context = kernel.create_new_context()
context["input"] = text_to_summarize
context_vars = sk.ContextVariables(variables={"input2": "4) All birds are robots."})
summary = await kernel.run_async(
tldr_function,
input_context=context,
input_vars=context_vars,
input_str="new text",
summary = await retry(
lambda: kernel.run_async(
tldr_function,
input_context=context,
input_vars=context_vars,
input_str="new text",
)
)

output = str(summary).strip()
print(
f"Summary using context, additional variables, and additional string: '{output}'"
)
assert len(output.split(" ")) == 5
assert "humans" in output or "Humans" in output or "preserve" in output
assert len(output) < 100


async def simple_summarization(kernel: sk.Kernel):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@


@pytest.mark.asyncio
@pytest.mark.xfail(
raises=AssertionError,
reason="Azure OpenAI may throttle requests, preventing this test from passing",
)
async def test_azure_chat_completion_with_skills():
kernel = sk.Kernel()

Expand All @@ -27,6 +23,10 @@ async def test_azure_chat_completion_with_skills():
deployment_name, api_key, endpoint = sk.azure_openai_settings_from_dot_env()
deployment_name = "gpt-4"

print("* Service: Azure OpenAI Chat Completion")
print(f"* Endpoint: {endpoint}")
print(f"* Deployment: {deployment_name}")

# Configure LLM service
kernel.add_chat_service(
"chat_completion",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ async def test_azure_text_completion_with_skills():
deployment_name, api_key, endpoint = sk.azure_openai_settings_from_dot_env()
deployment_name = "text-davinci-003"

print("* Service: Azure OpenAI Text Completion")
print(f"* Endpoint: {endpoint}")
print(f"* Deployment: {deployment_name}")

# Configure LLM service
kernel.add_text_completion_service(
"text_completion",
Expand Down
8 changes: 4 additions & 4 deletions python/tests/integration/completions/test_oai_chat_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@


@pytest.mark.asyncio
@pytest.mark.xfail(
raises=AssertionError,
reason="OpenAI may throttle requests, preventing this test from passing",
)
async def test_oai_chat_service_with_skills():
kernel = sk.Kernel()

Expand All @@ -25,6 +21,10 @@ async def test_oai_chat_service_with_skills():
# Load credentials from .env file
api_key, org_id = sk.openai_settings_from_dot_env()

print("* Service: OpenAI Chat Completion")
print("* Endpoint: OpenAI")
print("* Model: gpt-3.5-turbo")

kernel.add_chat_service(
"chat-gpt", sk_oai.OpenAIChatCompletion("gpt-3.5-turbo", api_key, org_id)
)
Expand Down
8 changes: 4 additions & 4 deletions python/tests/integration/completions/test_oai_text_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@


@pytest.mark.asyncio
@pytest.mark.xfail(
raises=AssertionError,
reason="OpenAI may throttle requests, preventing this test from passing",
)
async def test_oai_text_completion_with_skills():
kernel = sk.Kernel()

Expand All @@ -25,6 +21,10 @@ async def test_oai_text_completion_with_skills():
# Load credentials from .env file
api_key, org_id = sk.openai_settings_from_dot_env()

print("* Service: OpenAI Text Completion")
print("* Endpoint: OpenAI")
print("* Model: text-davinci-003")

kernel.add_chat_service(
"davinci-003", sk_oai.OpenAITextCompletion("text-davinci-003", api_key, org_id)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@


@pytest.mark.asyncio
# @pytest.mark.xfail(raises=AssertionError, reason="OpenAI may throttle requests, preventing this test from passing")
async def test_oai_embedding_service_with_memories():
kernel = sk.Kernel()

Expand Down

0 comments on commit de3436d

Please sign in to comment.