Skip to content

Commit 4277528

Browse files
Merge branch 'microsoft:main' into update-modelid
2 parents f2cab02 + 9db47e5 commit 4277528

24 files changed

Lines changed: 1369 additions & 1211 deletions

‎dotnet/Directory.Packages.props‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
<PackageVersion Include="Moq" Version="[4.18.4]" />
4747
<PackageVersion Include="System.Threading.Channels" Version="8.0.0" />
4848
<PackageVersion Include="System.Threading.Tasks.Dataflow" Version="8.0.0" />
49-
<PackageVersion Include="xunit" Version="2.6.3" />
49+
<PackageVersion Include="xunit" Version="2.6.4" />
5050
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.6" />
5151
<PackageVersion Include="xretry" Version="1.9.0" />
5252
<PackageVersion Include="coverlet.collector" Version="6.0.0" />
@@ -91,7 +91,7 @@
9191
<PrivateAssets>all</PrivateAssets>
9292
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
9393
</PackageReference>
94-
<PackageVersion Include="xunit.analyzers" Version="1.7.0" />
94+
<PackageVersion Include="xunit.analyzers" Version="1.8.0" />
9595
<PackageReference Include="xunit.analyzers">
9696
<PrivateAssets>all</PrivateAssets>
9797
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

‎dotnet/src/SemanticKernel.Core/TemplateEngine/Blocks/NamedArgBlock.cs‎

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

3+
using System;
34
using System.Text.RegularExpressions;
45
using Microsoft.Extensions.Logging;
56

@@ -35,21 +36,14 @@ internal sealed class NamedArgBlock : Block, ITextRendering
3536
public NamedArgBlock(string? text, ILoggerFactory? logger = null)
3637
: base(NamedArgBlock.TrimWhitespace(text), logger)
3738
{
38-
var argParts = this.Content.Split(Symbols.NamedArgBlockSeparator);
39-
if (argParts.Length != 2)
39+
if (!TryGetNameAndValue(this.Content, out string argName, out string argValue))
4040
{
4141
this.Logger.LogError("Invalid named argument `{Text}`", text);
4242
throw new KernelException($"A function named argument must contain a name and value separated by a '{Symbols.NamedArgBlockSeparator}' character.");
4343
}
4444

45-
this.Name = argParts[0];
46-
this._argNameAsVarBlock = new VarBlock($"{Symbols.VarPrefix}{argParts[0]}");
47-
var argValue = argParts[1];
48-
if (argValue.Length == 0)
49-
{
50-
this.Logger.LogError("Invalid named argument `{Text}`", text);
51-
throw new KernelException($"A function named argument must contain a quoted value or variable after the '{Symbols.NamedArgBlockSeparator}' character.");
52-
}
45+
this.Name = argName;
46+
this._argNameAsVarBlock = new VarBlock($"{Symbols.VarPrefix}{argName}");
5347

5448
if (argValue[0] == Symbols.VarPrefix)
5549
{
@@ -61,6 +55,34 @@ public NamedArgBlock(string? text, ILoggerFactory? logger = null)
6155
}
6256
}
6357

58+
/// <summary>
59+
/// Attempts to extract the name and value of a named argument block from a string
60+
/// </summary>
61+
/// <param name="text">String from which to extract a name and value</param>
62+
/// <param name="name">Name extracted from argument block, when successful. Empty string otherwise.</param>
63+
/// <param name="value">Value extracted from argument block, when successful. Empty string otherwise.</param>
64+
/// <returns>true when a name and value are successfully extracted from the given text, false otherwise</returns>
65+
internal static bool TryGetNameAndValue(string? text, out string name, out string value)
66+
{
67+
name = string.Empty;
68+
value = string.Empty;
69+
70+
if (!string.IsNullOrEmpty(text))
71+
{
72+
string[] argBlockParts = text!.Split(new char[] { Symbols.NamedArgBlockSeparator }, StringSplitOptions.RemoveEmptyEntries);
73+
74+
if (argBlockParts.Length == 2)
75+
{
76+
name = argBlockParts[0];
77+
value = argBlockParts[1];
78+
79+
return true;
80+
}
81+
}
82+
83+
return false;
84+
}
85+
6486
/// <summary>
6587
/// Gets the rendered value of the function argument. If the value is a <see cref="ValBlock"/>, the value stays the same.
6688
/// If the value is a <see cref="VarBlock"/>, the value of the variable is determined by the arguments passed in.

‎dotnet/src/SemanticKernel.Core/TemplateEngine/CodeTokenizer.cs‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -322,14 +322,13 @@ private static bool CanBeEscaped(char c)
322322
Justification = "Does not throw an exception by design.")]
323323
private static bool IsValidNamedArg(string tokenContent)
324324
{
325-
try
325+
if (NamedArgBlock.TryGetNameAndValue(tokenContent, out string _, out string _))
326326
{
327327
var tokenContentAsNamedArg = new NamedArgBlock(tokenContent);
328-
return tokenContentAsNamedArg.IsValid(out var error);
329-
}
330-
catch
331-
{
332-
return false;
328+
329+
return tokenContentAsNamedArg.IsValid(out string _);
333330
}
331+
332+
return false;
334333
}
335334
}

‎dotnet/src/SemanticKernel.UnitTests/TemplateEngine/Blocks/NamedArgBlockTests.cs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,11 @@ public void ArgValueNeedsQuoteOrDollarSignPrefix()
115115
public void ArgNameShouldBeNonEmpty()
116116
{
117117
// Arrange
118-
var target = new NamedArgBlock("='b'");
118+
static NamedArgBlock funcToTest() => new("='b'");
119119

120120
// Act + Assert
121-
Assert.False(target.IsValid(out var error));
122-
Assert.Equal("A named argument must have a name", error);
121+
KernelException exception = Assert.Throws<KernelException>(funcToTest);
122+
Assert.Equal("A function named argument must contain a name and value separated by a '=' character.", exception.Message);
123123
}
124124

125125
[Fact]

‎dotnet/src/SemanticKernel.UnitTests/TemplateEngine/CodeTokenizerTests.cs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,12 @@ public void ItThrowsWhenSeparatorsAreMissing(string template)
209209
}
210210

211211
[Theory]
212-
[InlineData("f a =", "A function named argument must contain a quoted value or variable after the '=' character.")]
213-
[InlineData("f a='b' arg2", "A function named argument must contain a name and value separated by a '=' character.")]
214-
public void ItThrowsWhenArgValueIsMissing(string template, string expectedErrorMessage)
212+
[InlineData("f a =")]
213+
[InlineData("f a='b' arg2")]
214+
public void ItThrowsWhenArgValueIsMissing(string template)
215215
{
216216
// Act & Assert
217217
var exception = Assert.Throws<KernelException>(() => this._target.Tokenize(template));
218-
Assert.Equal(expectedErrorMessage, exception.Message);
218+
Assert.Equal("A function named argument must contain a name and value separated by a '=' character.", exception.Message);
219219
}
220220
}

‎python/notebooks/00-getting-started.ipynb‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"metadata": {},
1717
"outputs": [],
1818
"source": [
19-
"!python -m pip install semantic-kernel==0.4.3.dev0"
19+
"!python -m pip install semantic-kernel==0.4.4.dev0"
2020
]
2121
},
2222
{
@@ -57,7 +57,7 @@
5757
"\n",
5858
"api_key, org_id = sk.openai_settings_from_dot_env()\n",
5959
"\n",
60-
"kernel.add_chat_service(\"chat-gpt\", OpenAIChatCompletion(ai_model_id=\"gpt-3.5-turbo\", api_key=api_key, org_id=org_id))"
60+
"kernel.add_chat_service(\"chat-gpt\", OpenAIChatCompletion(ai_model_id=\"gpt-3.5-turbo-1106\", api_key=api_key, org_id=org_id))"
6161
]
6262
},
6363
{

‎python/notebooks/01-basic-loading-the-kernel.ipynb‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,20 @@
2525
"metadata": {},
2626
"outputs": [],
2727
"source": [
28-
"!python -m pip install semantic-kernel==0.4.3.dev0"
28+
"!python -m pip install semantic-kernel==0.4.4.dev0"
2929
]
3030
},
3131
{
3232
"cell_type": "code",
33-
"execution_count": null,
33+
"execution_count": 1,
3434
"metadata": {},
3535
"outputs": [],
3636
"source": [
3737
"import semantic_kernel as sk\n",
38-
"from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion, OpenAIChatCompletion"
38+
"from semantic_kernel.connectors.ai.open_ai import (\n",
39+
" AzureChatCompletion,\n",
40+
" OpenAIChatCompletion,\n",
41+
")"
3942
]
4043
},
4144
{
@@ -48,7 +51,7 @@
4851
},
4952
{
5053
"cell_type": "code",
51-
"execution_count": null,
54+
"execution_count": 2,
5255
"metadata": {},
5356
"outputs": [],
5457
"source": [
@@ -85,13 +88,13 @@
8588
" )\n",
8689
")\n",
8790
"\n",
88-
"kernel.add_chat_service( # We are adding a text service\n",
89-
" \"OpenAI_chat_gpt\", # The alias we can use in prompt templates' config.json\n",
91+
"kernel.add_chat_service( # We are adding a text service\n",
92+
" \"OpenAI_chat_gpt\", # The alias we can use in prompt templates' config.json\n",
9093
" OpenAIChatCompletion(\n",
91-
" ai_model_id=\"gpt-3.5-turbo\", # OpenAI Model Name\n",
92-
" api_key=\"...your OpenAI API Key...\", # OpenAI API key\n",
93-
" org_id=\"...your OpenAI Org ID...\" # *optional* OpenAI Organization ID\n",
94-
" )\n",
94+
" ai_model_id=\"gpt-3.5-turbo\", # OpenAI Model Name\n",
95+
" api_key=\"...your OpenAI API Key...\", # OpenAI API key\n",
96+
" org_id=\"...your OpenAI Org ID...\", # *optional* OpenAI Organization ID\n",
97+
" ),\n",
9598
")"
9699
]
97100
},
@@ -143,7 +146,7 @@
143146
"name": "python",
144147
"nbconvert_exporter": "python",
145148
"pygments_lexer": "ipython3",
146-
"version": "3.11.6"
149+
"version": "3.10.12"
147150
},
148151
"polyglot_notebook": {
149152
"kernelInfo": {

‎python/notebooks/02-running-prompts-from-file.ipynb‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
"metadata": {},
9090
"outputs": [],
9191
"source": [
92-
"!python -m pip install semantic-kernel==0.4.3.dev0"
92+
"!python -m pip install semantic-kernel==0.4.4.dev0"
9393
]
9494
},
9595
{
@@ -100,7 +100,10 @@
100100
"outputs": [],
101101
"source": [
102102
"import semantic_kernel as sk\n",
103-
"from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion, OpenAIChatCompletion\n",
103+
"from semantic_kernel.connectors.ai.open_ai import (\n",
104+
" AzureChatCompletion,\n",
105+
" OpenAIChatCompletion,\n",
106+
")\n",
104107
"\n",
105108
"kernel = sk.Kernel()\n",
106109
"\n",
@@ -109,7 +112,9 @@
109112
"# Configure AI service used by the kernel\n",
110113
"if useAzureOpenAI:\n",
111114
" deployment, api_key, endpoint = sk.azure_openai_settings_from_dot_env()\n",
112-
" azure_chat_service = AzureChatCompletion(deployment_name=\"turbo\", endpoint=endpoint, api_key=api_key) # set the deployment name to the value of your chat model\n",
115+
" azure_chat_service = AzureChatCompletion(\n",
116+
" deployment_name=\"turbo\", endpoint=endpoint, api_key=api_key\n",
117+
" ) # set the deployment name to the value of your chat model\n",
113118
" kernel.add_chat_service(\"chat_completion\", azure_chat_service)\n",
114119
"else:\n",
115120
" api_key, org_id = sk.openai_settings_from_dot_env()\n",
@@ -192,7 +197,7 @@
192197
"name": "python",
193198
"nbconvert_exporter": "python",
194199
"pygments_lexer": "ipython3",
195-
"version": "3.11.6"
200+
"version": "3.10.12"
196201
}
197202
},
198203
"nbformat": 4,

‎python/notebooks/03-semantic-function-inline.ipynb‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"metadata": {},
5656
"outputs": [],
5757
"source": [
58-
"!python -m pip install semantic-kernel==0.4.3.dev0"
58+
"!python -m pip install semantic-kernel==0.4.4.dev0"
5959
]
6060
},
6161
{
@@ -66,7 +66,10 @@
6666
"outputs": [],
6767
"source": [
6868
"import semantic_kernel as sk\n",
69-
"from semantic_kernel.connectors.ai.open_ai import AzureTextCompletion, OpenAITextCompletion\n",
69+
"from semantic_kernel.connectors.ai.open_ai import (\n",
70+
" AzureTextCompletion,\n",
71+
" OpenAITextCompletion,\n",
72+
")\n",
7073
"\n",
7174
"kernel = sk.Kernel()\n",
7275
"\n",
@@ -75,7 +78,9 @@
7578
"# Configure AI service used by the kernel\n",
7679
"if useAzureOpenAI:\n",
7780
" deployment, api_key, endpoint = sk.azure_openai_settings_from_dot_env()\n",
78-
" azure_text_service = AzureTextCompletion(deployment_name=\"text\", endpoint=endpoint, api_key=api_key) # set the deployment name to the value of your text model\n",
81+
" azure_text_service = AzureTextCompletion(\n",
82+
" deployment_name=\"text\", endpoint=endpoint, api_key=api_key\n",
83+
" ) # set the deployment name to the value of your text model\n",
7984
" kernel.add_text_completion_service(\"dv\", azure_text_service)\n",
8085
"else:\n",
8186
" api_key, org_id = sk.openai_settings_from_dot_env()\n",
@@ -196,7 +201,10 @@
196201
"outputs": [],
197202
"source": [
198203
"import semantic_kernel as sk\n",
199-
"from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion, OpenAIChatCompletion\n",
204+
"from semantic_kernel.connectors.ai.open_ai import (\n",
205+
" AzureChatCompletion,\n",
206+
" OpenAIChatCompletion,\n",
207+
")\n",
200208
"\n",
201209
"kernel = sk.Kernel()\n",
202210
"\n",
@@ -205,7 +213,9 @@
205213
"# Configure AI service used by the kernel\n",
206214
"if useAzureOpenAI:\n",
207215
" deployment, api_key, endpoint = sk.azure_openai_settings_from_dot_env()\n",
208-
" azure_chat_service = AzureChatCompletion(deployment_name=\"turbo\", endpoint=endpoint, api_key=api_key) # set the deployment name to the value of your chat model\n",
216+
" azure_chat_service = AzureChatCompletion(\n",
217+
" deployment_name=\"turbo\", endpoint=endpoint, api_key=api_key\n",
218+
" ) # set the deployment name to the value of your chat model\n",
209219
" kernel.add_chat_service(\"chat_completion\", azure_chat_service)\n",
210220
"else:\n",
211221
" api_key, org_id = sk.openai_settings_from_dot_env()\n",
@@ -241,7 +251,7 @@
241251
"\n",
242252
"summary = tldr_function(text)\n",
243253
"\n",
244-
"print(f\"Output: {summary}\") # Output: Robots must not harm humans."
254+
"print(f\"Output: {summary}\") # Output: Robots must not harm humans."
245255
]
246256
}
247257
],

‎python/notebooks/04-context-variables-chat.ipynb‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"metadata": {},
2727
"outputs": [],
2828
"source": [
29-
"!python -m pip install semantic-kernel==0.4.3.dev0"
29+
"!python -m pip install semantic-kernel==0.4.4.dev0"
3030
]
3131
},
3232
{
@@ -37,7 +37,10 @@
3737
"outputs": [],
3838
"source": [
3939
"import semantic_kernel as sk\n",
40-
"from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion, OpenAIChatCompletion\n",
40+
"from semantic_kernel.connectors.ai.open_ai import (\n",
41+
" AzureChatCompletion,\n",
42+
" OpenAIChatCompletion,\n",
43+
")\n",
4144
"\n",
4245
"kernel = sk.Kernel()\n",
4346
"\n",
@@ -46,10 +49,16 @@
4649
"# Configure AI service used by the kernel\n",
4750
"if useAzureOpenAI:\n",
4851
" deployment, api_key, endpoint = sk.azure_openai_settings_from_dot_env()\n",
49-
" kernel.add_chat_service(\"chat_completion\", AzureChatCompletion(deployment_name=deployment, endpoint=endpoint, api_key=api_key))\n",
52+
" kernel.add_chat_service(\n",
53+
" \"chat_completion\",\n",
54+
" AzureChatCompletion(deployment_name=deployment, endpoint=endpoint, api_key=api_key),\n",
55+
" )\n",
5056
"else:\n",
5157
" api_key, org_id = sk.openai_settings_from_dot_env()\n",
52-
" kernel.add_chat_service(\"gpt-3.5\", OpenAIChatCompletion(ai_model_id=\"gpt-3.5-turbo\", api_key=api_key, org_id=org_id))\n"
58+
" kernel.add_chat_service(\n",
59+
" \"gpt-3.5\",\n",
60+
" OpenAIChatCompletion(ai_model_id=\"gpt-3.5-turbo\", api_key=api_key, org_id=org_id),\n",
61+
" )"
5362
]
5463
},
5564
{
@@ -98,7 +107,8 @@
98107
" function_name=\"ChatBot\",\n",
99108
" max_tokens=2000,\n",
100109
" temperature=0.7,\n",
101-
" top_p=0.5)"
110+
" top_p=0.5,\n",
111+
")"
102112
]
103113
},
104114
{

0 commit comments

Comments
 (0)