semantic-kernel 0.2.3.dev0__py3-none-any.whl → 0.2.5.dev0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- semantic_kernel/{ai → connectors/ai}/chat_completion_client_base.py +1 -1
- semantic_kernel/connectors/ai/hugging_face/__init__.py +10 -0
- semantic_kernel/connectors/ai/hugging_face/services/hf_text_completion.py +95 -0
- semantic_kernel/connectors/ai/hugging_face/services/hf_text_embedding.py +63 -0
- semantic_kernel/connectors/ai/open_ai/__init__.py +29 -0
- semantic_kernel/{ai → connectors/ai}/open_ai/services/azure_chat_completion.py +2 -2
- semantic_kernel/{ai → connectors/ai}/open_ai/services/azure_text_completion.py +2 -2
- semantic_kernel/{ai → connectors/ai}/open_ai/services/azure_text_embedding.py +2 -2
- semantic_kernel/{ai → connectors/ai}/open_ai/services/open_ai_chat_completion.py +36 -4
- semantic_kernel/{ai → connectors/ai}/open_ai/services/open_ai_text_completion.py +10 -6
- semantic_kernel/{ai → connectors/ai}/open_ai/services/open_ai_text_embedding.py +2 -2
- semantic_kernel/{ai → connectors/ai}/text_completion_client_base.py +4 -2
- semantic_kernel/core_skills/__init__.py +2 -1
- semantic_kernel/core_skills/http_skill.py +106 -0
- semantic_kernel/core_skills/time_skill.py +11 -1
- semantic_kernel/kernel.py +60 -30
- semantic_kernel/kernel_base.py +6 -10
- semantic_kernel/kernel_config.py +118 -109
- semantic_kernel/kernel_exception.py +4 -4
- semantic_kernel/kernel_extensions/import_skills.py +44 -0
- semantic_kernel/kernel_extensions/memory_configuration.py +10 -12
- semantic_kernel/memory/memory_query_result.py +36 -6
- semantic_kernel/memory/memory_record.py +52 -11
- semantic_kernel/memory/memory_store_base.py +74 -5
- semantic_kernel/memory/semantic_text_memory.py +92 -10
- semantic_kernel/memory/semantic_text_memory_base.py +1 -2
- semantic_kernel/memory/volatile_memory_store.py +251 -25
- semantic_kernel/orchestration/context_variables.py +5 -5
- semantic_kernel/orchestration/sk_function.py +93 -109
- semantic_kernel/orchestration/sk_function_base.py +21 -57
- semantic_kernel/semantic_functions/prompt_template_config.py +2 -2
- semantic_kernel/template_engine/blocks/code_block.py +2 -2
- semantic_kernel/text/__init__.py +17 -0
- semantic_kernel/text/function_extension.py +23 -0
- semantic_kernel/text/text_chunker.py +250 -0
- {semantic_kernel-0.2.3.dev0.dist-info → semantic_kernel-0.2.5.dev0.dist-info}/METADATA +19 -17
- {semantic_kernel-0.2.3.dev0.dist-info → semantic_kernel-0.2.5.dev0.dist-info}/RECORD +43 -40
- semantic_kernel/ai/embeddings/embedding_index_base.py +0 -20
- semantic_kernel/ai/open_ai/__init__.py +0 -27
- semantic_kernel/memory/storage/data_entry.py +0 -36
- semantic_kernel/memory/storage/data_store_base.py +0 -36
- semantic_kernel/memory/storage/volatile_data_store.py +0 -62
- /semantic_kernel/{ai → connectors/ai}/ai_exception.py +0 -0
- /semantic_kernel/{ai → connectors/ai}/chat_request_settings.py +0 -0
- /semantic_kernel/{ai → connectors/ai}/complete_request_settings.py +0 -0
- /semantic_kernel/{ai → connectors/ai}/embeddings/embedding_generator_base.py +0 -0
- {semantic_kernel-0.2.3.dev0.dist-info → semantic_kernel-0.2.5.dev0.dist-info}/WHEEL +0 -0
|
@@ -5,7 +5,7 @@ from logging import Logger
|
|
|
5
5
|
from typing import TYPE_CHECKING, List, Tuple
|
|
6
6
|
|
|
7
7
|
if TYPE_CHECKING:
|
|
8
|
-
from semantic_kernel.ai.chat_request_settings import ChatRequestSettings
|
|
8
|
+
from semantic_kernel.connectors.ai.chat_request_settings import ChatRequestSettings
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
class ChatCompletionClientBase(ABC):
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Copyright (c) Microsoft. All rights reserved.
|
|
2
|
+
|
|
3
|
+
from semantic_kernel.connectors.ai.hugging_face.services.hf_text_completion import (
|
|
4
|
+
HuggingFaceTextCompletion,
|
|
5
|
+
)
|
|
6
|
+
from semantic_kernel.connectors.ai.hugging_face.services.hf_text_embedding import (
|
|
7
|
+
HuggingFaceTextEmbedding,
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
__all__ = ["HuggingFaceTextCompletion", "HuggingFaceTextEmbedding"]
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# Copyright (c) Microsoft. All rights reserved.
|
|
2
|
+
|
|
3
|
+
from logging import Logger
|
|
4
|
+
from typing import Optional
|
|
5
|
+
|
|
6
|
+
import torch
|
|
7
|
+
from transformers import pipeline
|
|
8
|
+
|
|
9
|
+
from semantic_kernel.connectors.ai.ai_exception import AIException
|
|
10
|
+
from semantic_kernel.connectors.ai.complete_request_settings import (
|
|
11
|
+
CompleteRequestSettings,
|
|
12
|
+
)
|
|
13
|
+
from semantic_kernel.connectors.ai.text_completion_client_base import (
|
|
14
|
+
TextCompletionClientBase,
|
|
15
|
+
)
|
|
16
|
+
from semantic_kernel.utils.null_logger import NullLogger
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class HuggingFaceTextCompletion(TextCompletionClientBase):
|
|
20
|
+
_model_id: str
|
|
21
|
+
_task: str
|
|
22
|
+
_device: int
|
|
23
|
+
_log: Logger
|
|
24
|
+
|
|
25
|
+
def __init__(
|
|
26
|
+
self,
|
|
27
|
+
model_id: str,
|
|
28
|
+
device: Optional[int] = -1,
|
|
29
|
+
task: Optional[str] = None,
|
|
30
|
+
log: Optional[Logger] = None,
|
|
31
|
+
) -> None:
|
|
32
|
+
"""
|
|
33
|
+
Initializes a new instance of the HuggingFaceTextCompletion class.
|
|
34
|
+
|
|
35
|
+
Arguments:
|
|
36
|
+
model_id {str} -- Hugging Face model card string, see
|
|
37
|
+
https://huggingface.co/models
|
|
38
|
+
device {Optional[int]} -- Device to run the model on, -1 for CPU, 0+ for GPU.
|
|
39
|
+
task {Optional[str]} -- Model completion task type, options are:
|
|
40
|
+
- summarization: takes a long text and returns a shorter summary.
|
|
41
|
+
- text-generation: takes incomplete text and returns a set of completion candidates.
|
|
42
|
+
- text2text-generation (default): takes an input prompt and returns a completion.
|
|
43
|
+
text2text-generation is the default as it behaves more like GPT-3+.
|
|
44
|
+
log {Optional[Logger]} -- Logger instance.
|
|
45
|
+
|
|
46
|
+
Note that this model will be downloaded from the Hugging Face model hub.
|
|
47
|
+
"""
|
|
48
|
+
self._model_id = model_id
|
|
49
|
+
self._task = "text2text-generation" if task is None else task
|
|
50
|
+
self._log = log if log is not None else NullLogger()
|
|
51
|
+
self.device = (
|
|
52
|
+
"cuda:" + device if device >= 0 and torch.cuda.is_available() else "cpu"
|
|
53
|
+
)
|
|
54
|
+
self.generator = pipeline(
|
|
55
|
+
task=self._task, model=self._model_id, device=self.device
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
async def complete_async(
|
|
59
|
+
self, prompt: str, request_settings: CompleteRequestSettings
|
|
60
|
+
) -> str:
|
|
61
|
+
"""
|
|
62
|
+
Completes a prompt using the Hugging Face model.
|
|
63
|
+
|
|
64
|
+
Arguments:
|
|
65
|
+
prompt {str} -- Prompt to complete.
|
|
66
|
+
request_settings {CompleteRequestSettings} -- Request settings.
|
|
67
|
+
|
|
68
|
+
Returns:
|
|
69
|
+
str -- Completion result.
|
|
70
|
+
"""
|
|
71
|
+
try:
|
|
72
|
+
result = self.generator(
|
|
73
|
+
prompt,
|
|
74
|
+
num_return_sequences=1,
|
|
75
|
+
temperature=request_settings.temperature,
|
|
76
|
+
top_p=request_settings.top_p,
|
|
77
|
+
max_length=request_settings.max_tokens,
|
|
78
|
+
pad_token_id=50256, # EOS token
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
if self._task == "text-generation" or self._task == "text2text-generation":
|
|
82
|
+
return result[0]["generated_text"]
|
|
83
|
+
|
|
84
|
+
elif self._task == "summarization":
|
|
85
|
+
return result[0]["summary_text"]
|
|
86
|
+
|
|
87
|
+
else:
|
|
88
|
+
raise AIException(
|
|
89
|
+
AIException.ErrorCodes.InvalidConfiguration,
|
|
90
|
+
"Unsupported hugging face pipeline task: only \
|
|
91
|
+
text-generation, text2text-generation, and summarization are supported.",
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
except Exception as e:
|
|
95
|
+
raise AIException("Hugging Face completion failed", e)
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# Copyright (c) Microsoft. All rights reserved.
|
|
2
|
+
|
|
3
|
+
from logging import Logger
|
|
4
|
+
from typing import List, Optional
|
|
5
|
+
|
|
6
|
+
import torch
|
|
7
|
+
from numpy import array, ndarray
|
|
8
|
+
from sentence_transformers import SentenceTransformer
|
|
9
|
+
|
|
10
|
+
from semantic_kernel.connectors.ai.ai_exception import AIException
|
|
11
|
+
from semantic_kernel.connectors.ai.embeddings.embedding_generator_base import (
|
|
12
|
+
EmbeddingGeneratorBase,
|
|
13
|
+
)
|
|
14
|
+
from semantic_kernel.utils.null_logger import NullLogger
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class HuggingFaceTextEmbedding(EmbeddingGeneratorBase):
|
|
18
|
+
_model_id: str
|
|
19
|
+
_device: int
|
|
20
|
+
_log: Logger
|
|
21
|
+
|
|
22
|
+
def __init__(
|
|
23
|
+
self,
|
|
24
|
+
model_id: str,
|
|
25
|
+
device: Optional[int] = -1,
|
|
26
|
+
log: Optional[Logger] = None,
|
|
27
|
+
) -> None:
|
|
28
|
+
"""
|
|
29
|
+
Initializes a new instance of the HuggingFaceTextEmbedding class.
|
|
30
|
+
|
|
31
|
+
Arguments:
|
|
32
|
+
model_id {str} -- Hugging Face model card string, see
|
|
33
|
+
https://huggingface.co/sentence-transformers
|
|
34
|
+
device {Optional[int]} -- Device to run the model on, -1 for CPU, 0+ for GPU.
|
|
35
|
+
log {Optional[Logger]} -- Logger instance.
|
|
36
|
+
|
|
37
|
+
Note that this model will be downloaded from the Hugging Face model hub.
|
|
38
|
+
"""
|
|
39
|
+
self._model_id = model_id
|
|
40
|
+
self._log = log if log is not None else NullLogger()
|
|
41
|
+
self.device = (
|
|
42
|
+
"cuda:" + device if device >= 0 and torch.cuda.is_available() else "cpu"
|
|
43
|
+
)
|
|
44
|
+
self.generator = SentenceTransformer(
|
|
45
|
+
model_name_or_path=self._model_id, device=self.device
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
async def generate_embeddings_async(self, texts: List[str]) -> ndarray:
|
|
49
|
+
"""
|
|
50
|
+
Generates embeddings for a list of texts.
|
|
51
|
+
|
|
52
|
+
Arguments:
|
|
53
|
+
texts {List[str]} -- Texts to generate embeddings for.
|
|
54
|
+
|
|
55
|
+
Returns:
|
|
56
|
+
ndarray -- Embeddings for the texts.
|
|
57
|
+
"""
|
|
58
|
+
try:
|
|
59
|
+
self._log.info(f"Generating embeddings for {len(texts)} texts")
|
|
60
|
+
embeddings = self.generator.encode(texts)
|
|
61
|
+
return array(embeddings)
|
|
62
|
+
except Exception as e:
|
|
63
|
+
raise AIException("Hugging Face embeddings failed", e)
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# Copyright (c) Microsoft. All rights reserved.
|
|
2
|
+
|
|
3
|
+
from semantic_kernel.connectors.ai.open_ai.services.azure_chat_completion import (
|
|
4
|
+
AzureChatCompletion,
|
|
5
|
+
)
|
|
6
|
+
from semantic_kernel.connectors.ai.open_ai.services.azure_text_completion import (
|
|
7
|
+
AzureTextCompletion,
|
|
8
|
+
)
|
|
9
|
+
from semantic_kernel.connectors.ai.open_ai.services.azure_text_embedding import (
|
|
10
|
+
AzureTextEmbedding,
|
|
11
|
+
)
|
|
12
|
+
from semantic_kernel.connectors.ai.open_ai.services.open_ai_chat_completion import (
|
|
13
|
+
OpenAIChatCompletion,
|
|
14
|
+
)
|
|
15
|
+
from semantic_kernel.connectors.ai.open_ai.services.open_ai_text_completion import (
|
|
16
|
+
OpenAITextCompletion,
|
|
17
|
+
)
|
|
18
|
+
from semantic_kernel.connectors.ai.open_ai.services.open_ai_text_embedding import (
|
|
19
|
+
OpenAITextEmbedding,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
__all__ = [
|
|
23
|
+
"OpenAITextCompletion",
|
|
24
|
+
"OpenAIChatCompletion",
|
|
25
|
+
"OpenAITextEmbedding",
|
|
26
|
+
"AzureTextCompletion",
|
|
27
|
+
"AzureChatCompletion",
|
|
28
|
+
"AzureTextEmbedding",
|
|
29
|
+
]
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
from logging import Logger
|
|
5
5
|
from typing import Any, Optional
|
|
6
6
|
|
|
7
|
-
from semantic_kernel.ai.open_ai.services.open_ai_chat_completion import (
|
|
7
|
+
from semantic_kernel.connectors.ai.open_ai.services.open_ai_chat_completion import (
|
|
8
8
|
OpenAIChatCompletion,
|
|
9
9
|
)
|
|
10
10
|
|
|
@@ -24,7 +24,7 @@ class AzureChatCompletion(OpenAIChatCompletion):
|
|
|
24
24
|
ad_auth=False,
|
|
25
25
|
) -> None:
|
|
26
26
|
"""
|
|
27
|
-
Initialize an AzureChatCompletion
|
|
27
|
+
Initialize an AzureChatCompletion service.
|
|
28
28
|
|
|
29
29
|
You must provide:
|
|
30
30
|
- A deployment_name, endpoint, and api_key (plus, optionally: ad_auth)
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
from logging import Logger
|
|
5
5
|
from typing import Any, Optional
|
|
6
6
|
|
|
7
|
-
from semantic_kernel.ai.open_ai.services.open_ai_text_completion import (
|
|
7
|
+
from semantic_kernel.connectors.ai.open_ai.services.open_ai_text_completion import (
|
|
8
8
|
OpenAITextCompletion,
|
|
9
9
|
)
|
|
10
10
|
|
|
@@ -24,7 +24,7 @@ class AzureTextCompletion(OpenAITextCompletion):
|
|
|
24
24
|
ad_auth=False,
|
|
25
25
|
) -> None:
|
|
26
26
|
"""
|
|
27
|
-
Initialize an AzureTextCompletion
|
|
27
|
+
Initialize an AzureTextCompletion service.
|
|
28
28
|
|
|
29
29
|
You must provide:
|
|
30
30
|
- A deployment_name, endpoint, and api_key (plus, optionally: ad_auth)
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
from logging import Logger
|
|
5
5
|
from typing import Any, Optional
|
|
6
6
|
|
|
7
|
-
from semantic_kernel.ai.open_ai.services.open_ai_text_embedding import (
|
|
7
|
+
from semantic_kernel.connectors.ai.open_ai.services.open_ai_text_embedding import (
|
|
8
8
|
OpenAITextEmbedding,
|
|
9
9
|
)
|
|
10
10
|
|
|
@@ -24,7 +24,7 @@ class AzureTextEmbedding(OpenAITextEmbedding):
|
|
|
24
24
|
ad_auth=False,
|
|
25
25
|
) -> None:
|
|
26
26
|
"""
|
|
27
|
-
Initialize an AzureTextEmbedding
|
|
27
|
+
Initialize an AzureTextEmbedding service.
|
|
28
28
|
|
|
29
29
|
You must provide:
|
|
30
30
|
- A deployment_name, endpoint, and api_key (plus, optionally: ad_auth)
|
|
@@ -3,13 +3,21 @@
|
|
|
3
3
|
from logging import Logger
|
|
4
4
|
from typing import Any, List, Optional, Tuple
|
|
5
5
|
|
|
6
|
-
from semantic_kernel.ai.ai_exception import AIException
|
|
7
|
-
from semantic_kernel.ai.chat_completion_client_base import
|
|
8
|
-
|
|
6
|
+
from semantic_kernel.connectors.ai.ai_exception import AIException
|
|
7
|
+
from semantic_kernel.connectors.ai.chat_completion_client_base import (
|
|
8
|
+
ChatCompletionClientBase,
|
|
9
|
+
)
|
|
10
|
+
from semantic_kernel.connectors.ai.chat_request_settings import ChatRequestSettings
|
|
11
|
+
from semantic_kernel.connectors.ai.complete_request_settings import (
|
|
12
|
+
CompleteRequestSettings,
|
|
13
|
+
)
|
|
14
|
+
from semantic_kernel.connectors.ai.text_completion_client_base import (
|
|
15
|
+
TextCompletionClientBase,
|
|
16
|
+
)
|
|
9
17
|
from semantic_kernel.utils.null_logger import NullLogger
|
|
10
18
|
|
|
11
19
|
|
|
12
|
-
class OpenAIChatCompletion(ChatCompletionClientBase):
|
|
20
|
+
class OpenAIChatCompletion(ChatCompletionClientBase, TextCompletionClientBase):
|
|
13
21
|
_model_id: str
|
|
14
22
|
_api_key: str
|
|
15
23
|
_org_id: Optional[str] = None
|
|
@@ -116,3 +124,27 @@ class OpenAIChatCompletion(ChatCompletionClientBase):
|
|
|
116
124
|
# TODO: tracking on token counts/etc.
|
|
117
125
|
|
|
118
126
|
return response.choices[0].message.content
|
|
127
|
+
|
|
128
|
+
async def complete_async(
|
|
129
|
+
self, prompt: str, request_settings: CompleteRequestSettings
|
|
130
|
+
) -> str:
|
|
131
|
+
"""
|
|
132
|
+
Completes the given prompt. Returns a single string completion.
|
|
133
|
+
Cannot return multiple completions. Cannot return logprobs.
|
|
134
|
+
|
|
135
|
+
Arguments:
|
|
136
|
+
prompt {str} -- The prompt to complete.
|
|
137
|
+
request_settings {CompleteRequestSettings} -- The request settings.
|
|
138
|
+
|
|
139
|
+
Returns:
|
|
140
|
+
str -- The completed text.
|
|
141
|
+
"""
|
|
142
|
+
prompt_to_message = [("user", prompt)]
|
|
143
|
+
chat_settings = ChatRequestSettings(
|
|
144
|
+
temperature=request_settings.temperature,
|
|
145
|
+
top_p=request_settings.top_p,
|
|
146
|
+
presence_penalty=request_settings.presence_penalty,
|
|
147
|
+
frequency_penalty=request_settings.frequency_penalty,
|
|
148
|
+
max_tokens=request_settings.max_tokens,
|
|
149
|
+
)
|
|
150
|
+
return await self.complete_chat_async(prompt_to_message, chat_settings)
|
|
@@ -3,9 +3,13 @@
|
|
|
3
3
|
from logging import Logger
|
|
4
4
|
from typing import Any, Optional
|
|
5
5
|
|
|
6
|
-
from semantic_kernel.ai.ai_exception import AIException
|
|
7
|
-
from semantic_kernel.ai.complete_request_settings import
|
|
8
|
-
|
|
6
|
+
from semantic_kernel.connectors.ai.ai_exception import AIException
|
|
7
|
+
from semantic_kernel.connectors.ai.complete_request_settings import (
|
|
8
|
+
CompleteRequestSettings,
|
|
9
|
+
)
|
|
10
|
+
from semantic_kernel.connectors.ai.text_completion_client_base import (
|
|
11
|
+
TextCompletionClientBase,
|
|
12
|
+
)
|
|
9
13
|
from semantic_kernel.utils.null_logger import NullLogger
|
|
10
14
|
|
|
11
15
|
|
|
@@ -50,7 +54,7 @@ class OpenAITextCompletion(TextCompletionClientBase):
|
|
|
50
54
|
|
|
51
55
|
return openai
|
|
52
56
|
|
|
53
|
-
async def
|
|
57
|
+
async def complete_async(
|
|
54
58
|
self, prompt: str, request_settings: CompleteRequestSettings
|
|
55
59
|
) -> str:
|
|
56
60
|
"""
|
|
@@ -79,14 +83,14 @@ class OpenAITextCompletion(TextCompletionClientBase):
|
|
|
79
83
|
if request_settings.number_of_responses != 1:
|
|
80
84
|
raise AIException(
|
|
81
85
|
AIException.ErrorCodes.InvalidRequest,
|
|
82
|
-
"
|
|
86
|
+
"complete_async only supports a single completion, "
|
|
83
87
|
f"but {request_settings.number_of_responses} were requested",
|
|
84
88
|
)
|
|
85
89
|
|
|
86
90
|
if request_settings.logprobs != 0:
|
|
87
91
|
raise AIException(
|
|
88
92
|
AIException.ErrorCodes.InvalidRequest,
|
|
89
|
-
"
|
|
93
|
+
"complete_async does not support logprobs, "
|
|
90
94
|
f"but logprobs={request_settings.logprobs} was requested",
|
|
91
95
|
)
|
|
92
96
|
|
|
@@ -5,8 +5,8 @@ from typing import Any, List, Optional
|
|
|
5
5
|
|
|
6
6
|
from numpy import array, ndarray
|
|
7
7
|
|
|
8
|
-
from semantic_kernel.ai.ai_exception import AIException
|
|
9
|
-
from semantic_kernel.ai.embeddings.embedding_generator_base import (
|
|
8
|
+
from semantic_kernel.connectors.ai.ai_exception import AIException
|
|
9
|
+
from semantic_kernel.connectors.ai.embeddings.embedding_generator_base import (
|
|
10
10
|
EmbeddingGeneratorBase,
|
|
11
11
|
)
|
|
12
12
|
from semantic_kernel.utils.null_logger import NullLogger
|
|
@@ -5,12 +5,14 @@ from logging import Logger
|
|
|
5
5
|
from typing import TYPE_CHECKING
|
|
6
6
|
|
|
7
7
|
if TYPE_CHECKING:
|
|
8
|
-
from semantic_kernel.ai.complete_request_settings import
|
|
8
|
+
from semantic_kernel.connectors.ai.complete_request_settings import (
|
|
9
|
+
CompleteRequestSettings,
|
|
10
|
+
)
|
|
9
11
|
|
|
10
12
|
|
|
11
13
|
class TextCompletionClientBase(ABC):
|
|
12
14
|
@abstractmethod
|
|
13
|
-
async def
|
|
15
|
+
async def complete_async(
|
|
14
16
|
self,
|
|
15
17
|
prompt: str,
|
|
16
18
|
settings: "CompleteRequestSettings",
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
# Copyright (c) Microsoft. All rights reserved.
|
|
2
2
|
|
|
3
3
|
from semantic_kernel.core_skills.file_io_skill import FileIOSkill
|
|
4
|
+
from semantic_kernel.core_skills.http_skill import HttpSkill
|
|
4
5
|
from semantic_kernel.core_skills.text_memory_skill import TextMemorySkill
|
|
5
6
|
from semantic_kernel.core_skills.text_skill import TextSkill
|
|
6
7
|
from semantic_kernel.core_skills.time_skill import TimeSkill
|
|
7
8
|
|
|
8
|
-
__all__ = ["TextMemorySkill", "TextSkill", "FileIOSkill", "TimeSkill"]
|
|
9
|
+
__all__ = ["TextMemorySkill", "TextSkill", "FileIOSkill", "TimeSkill", "HttpSkill"]
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
# Copyright (c) Microsoft. All rights reserved.
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
|
|
5
|
+
import aiohttp
|
|
6
|
+
|
|
7
|
+
from semantic_kernel.orchestration.sk_context import SKContext
|
|
8
|
+
from semantic_kernel.skill_definition import sk_function, sk_function_context_parameter
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class HttpSkill:
|
|
12
|
+
"""
|
|
13
|
+
A skill that provides HTTP functionality.
|
|
14
|
+
|
|
15
|
+
Usage:
|
|
16
|
+
kernel.import_skill(HttpSkill(), "http")
|
|
17
|
+
|
|
18
|
+
Examples:
|
|
19
|
+
|
|
20
|
+
{{http.getAsync $url}}
|
|
21
|
+
{{http.postAsync $url}}
|
|
22
|
+
{{http.putAsync $url}}
|
|
23
|
+
{{http.deleteAsync $url}}
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
@sk_function(description="Makes a GET request to a uri", name="getAsync")
|
|
27
|
+
async def get_async(self, url: str) -> str:
|
|
28
|
+
"""
|
|
29
|
+
Sends an HTTP GET request to the specified URI and returns
|
|
30
|
+
the response body as a string.
|
|
31
|
+
params:
|
|
32
|
+
uri: The URI to send the request to.
|
|
33
|
+
returns:
|
|
34
|
+
The response body as a string.
|
|
35
|
+
"""
|
|
36
|
+
if not url:
|
|
37
|
+
raise ValueError("url cannot be `None` or empty")
|
|
38
|
+
|
|
39
|
+
async with aiohttp.ClientSession() as session:
|
|
40
|
+
async with session.get(url, raise_for_status=True) as response:
|
|
41
|
+
return await response.text()
|
|
42
|
+
|
|
43
|
+
@sk_function(description="Makes a POST request to a uri", name="postAsync")
|
|
44
|
+
@sk_function_context_parameter(name="body", description="The body of the request")
|
|
45
|
+
async def post_async(self, url: str, context: SKContext) -> str:
|
|
46
|
+
"""
|
|
47
|
+
Sends an HTTP POST request to the specified URI and returns
|
|
48
|
+
the response body as a string.
|
|
49
|
+
params:
|
|
50
|
+
url: The URI to send the request to.
|
|
51
|
+
context: Contains the body of the request
|
|
52
|
+
returns:
|
|
53
|
+
The response body as a string.
|
|
54
|
+
"""
|
|
55
|
+
if not url:
|
|
56
|
+
raise ValueError("url cannot be `None` or empty")
|
|
57
|
+
|
|
58
|
+
_, body = context.variables.get("body")
|
|
59
|
+
|
|
60
|
+
headers = {"Content-Type": "application/json"}
|
|
61
|
+
data = json.dumps(body)
|
|
62
|
+
async with aiohttp.ClientSession() as session:
|
|
63
|
+
async with session.post(
|
|
64
|
+
url, headers=headers, data=data, raise_for_status=True
|
|
65
|
+
) as response:
|
|
66
|
+
return await response.text()
|
|
67
|
+
|
|
68
|
+
@sk_function(description="Makes a PUT request to a uri", name="putAsync")
|
|
69
|
+
@sk_function_context_parameter(name="body", description="The body of the request")
|
|
70
|
+
async def put_async(self, url: str, context: SKContext) -> str:
|
|
71
|
+
"""
|
|
72
|
+
Sends an HTTP PUT request to the specified URI and returns
|
|
73
|
+
the response body as a string.
|
|
74
|
+
params:
|
|
75
|
+
url: The URI to send the request to.
|
|
76
|
+
returns:
|
|
77
|
+
The response body as a string.
|
|
78
|
+
"""
|
|
79
|
+
if not url:
|
|
80
|
+
raise ValueError("url cannot be `None` or empty")
|
|
81
|
+
|
|
82
|
+
_, body = context.variables.get("body")
|
|
83
|
+
|
|
84
|
+
headers = {"Content-Type": "application/json"}
|
|
85
|
+
data = json.dumps(body)
|
|
86
|
+
async with aiohttp.ClientSession() as session:
|
|
87
|
+
async with session.put(
|
|
88
|
+
url, headers=headers, data=data, raise_for_status=True
|
|
89
|
+
) as response:
|
|
90
|
+
return await response.text()
|
|
91
|
+
|
|
92
|
+
@sk_function(description="Makes a DELETE request to a uri", name="deleteAsync")
|
|
93
|
+
async def delete_async(self, url: str) -> str:
|
|
94
|
+
"""
|
|
95
|
+
Sends an HTTP DELETE request to the specified URI and returns
|
|
96
|
+
the response body as a string.
|
|
97
|
+
params:
|
|
98
|
+
uri: The URI to send the request to.
|
|
99
|
+
returns:
|
|
100
|
+
The response body as a string.
|
|
101
|
+
"""
|
|
102
|
+
if not url:
|
|
103
|
+
raise ValueError("url cannot be `None` or empty")
|
|
104
|
+
async with aiohttp.ClientSession() as session:
|
|
105
|
+
async with session.delete(url, raise_for_status=True) as response:
|
|
106
|
+
return await response.text()
|
|
@@ -32,7 +32,7 @@ class TimeSkill:
|
|
|
32
32
|
{{time.timeZoneName}} => PST
|
|
33
33
|
"""
|
|
34
34
|
|
|
35
|
-
@sk_function(description="Get the current
|
|
35
|
+
@sk_function(description="Get the current date.")
|
|
36
36
|
def date(self) -> str:
|
|
37
37
|
"""
|
|
38
38
|
Get the current date
|
|
@@ -43,6 +43,16 @@ class TimeSkill:
|
|
|
43
43
|
now = datetime.datetime.now()
|
|
44
44
|
return now.strftime("%A, %d %B, %Y")
|
|
45
45
|
|
|
46
|
+
@sk_function(description="Get the current date.")
|
|
47
|
+
def today(self) -> str:
|
|
48
|
+
"""
|
|
49
|
+
Get the current date
|
|
50
|
+
|
|
51
|
+
Example:
|
|
52
|
+
{{time.today}} => Sunday, 12 January, 2031
|
|
53
|
+
"""
|
|
54
|
+
return self.date()
|
|
55
|
+
|
|
46
56
|
@sk_function(description="Get the current date and time in the local time zone")
|
|
47
57
|
def now(self) -> str:
|
|
48
58
|
"""
|