Skip to content

Commit 2a1c486

Browse files
committed
Update to M.E.AI.Abstractions 9.9
1 parent d61044c commit 2a1c486

File tree

2 files changed

+45
-18
lines changed

2 files changed

+45
-18
lines changed

src/GenerativeAI.Microsoft/Extensions/MicrosoftExtensions.cs

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ from c in m.Contents
3333
let p = c.ToPart(options)
3434
where p is not null
3535
select p).ToArray();
36-
if (systemParts.Length > 0)
36+
if (systemParts.Length > 0 || !string.IsNullOrWhiteSpace(options?.Instructions))
3737
{
38-
request.SystemInstruction = new Content(systemParts, Roles.System);
38+
request.SystemInstruction = new Content(systemParts.Concat([new Part { Text = options!.Instructions }]), Roles.System);
3939
}
4040

4141
request.Contents = (from m in chatMessages
@@ -45,24 +45,51 @@ where p is not null
4545
where p is not null
4646
select p).ToArray(), m.Role == ChatRole.Assistant ? Roles.Model : Roles.User)).ToList();
4747

48-
var functionDeclarations = options?.Tools?.OfType<AIFunction>().Select(f =>
49-
new FunctionDeclaration()
48+
if (options?.Tools is not null)
49+
{
50+
List<FunctionDeclaration>? functionDeclarations = null;
51+
List<Tool>? tools = null;
52+
foreach (var tool in options.Tools)
5053
{
51-
Name = f.Name,
52-
Description = f.Description,
53-
Parameters = ParseFunctionParameters(f.JsonSchema),
54+
switch (tool)
55+
{
56+
case AIFunctionDeclaration f:
57+
(functionDeclarations ??= []).Add(new()
58+
{
59+
Name = f.Name,
60+
Description = f.Description,
61+
Parameters = ParseFunctionParameters(f.JsonSchema),
62+
});
63+
break;
64+
65+
case HostedWebSearchTool ws:
66+
(tools ??= []).Add(new Tool
67+
{
68+
GoogleSearch = new GoogleSearchTool()
69+
});
70+
break;
71+
72+
case HostedCodeInterpreterTool ci:
73+
(tools ??= []).Add(new Tool
74+
{
75+
CodeExecution = new CodeExecutionTool(),
76+
});
77+
break;
78+
}
5479
}
55-
).ToList();
5680

57-
if (functionDeclarations != null && functionDeclarations.Count > 0)
58-
{
59-
request.Tools = new List<Tool>()
81+
if (functionDeclarations is not null)
6082
{
61-
new Tool
83+
(tools ??= []).Add(new Tool
6284
{
6385
FunctionDeclarations = functionDeclarations.ToList()
64-
}
65-
};
86+
});
87+
}
88+
89+
if (tools is not null)
90+
{
91+
request.Tools = tools;
92+
}
6693
}
6794

6895
return request;
@@ -564,7 +591,7 @@ public static IList<AIContent> ToAiContents(this List<Part>? parts, ChatOptions?
564591
if (objectSchema == null && !string.IsNullOrEmpty(parameterName) && !string.IsNullOrEmpty(functionName) && options?.Tools != null)
565592
{
566593
// Try to get the schema for this object parameter
567-
var function = options.Tools.OfType<AIFunction>().FirstOrDefault(f => f.Name == functionName);
594+
var function = options.Tools.OfType<AIFunctionDeclaration>().FirstOrDefault(f => f.Name == functionName);
568595
if (function?.JsonSchema != null)
569596
{
570597
var schemaNode = JsonSerializer.SerializeToNode(function.JsonSchema);
@@ -697,7 +724,7 @@ public static IList<AIContent> ToAiContents(this List<Part>? parts, ChatOptions?
697724
JsonNode? itemSchema = null;
698725
if (!string.IsNullOrEmpty(parameterName) && !string.IsNullOrEmpty(functionName) && options?.Tools != null)
699726
{
700-
var function = options.Tools.OfType<AIFunction>().FirstOrDefault(f => f.Name == functionName);
727+
var function = options.Tools.OfType<AIFunctionDeclaration>().FirstOrDefault(f => f.Name == functionName);
701728
if (function?.JsonSchema != null)
702729
{
703730
var schemaNode = JsonSerializer.SerializeToNode(function.JsonSchema);
@@ -747,7 +774,7 @@ public static IList<AIContent> ToAiContents(this List<Part>? parts, ChatOptions?
747774
if (!string.IsNullOrEmpty(parameterName) && !string.IsNullOrEmpty(functionName) && options?.Tools != null)
748775
{
749776
// Find the function in the tools
750-
var function = options.Tools.OfType<AIFunction>().FirstOrDefault(f => f.Name == functionName);
777+
var function = options.Tools.OfType<AIFunctionDeclaration>().FirstOrDefault(f => f.Name == functionName);
751778
if (function?.JsonSchema != null)
752779
{
753780
// Parse the schema to check the parameter's format

src/GenerativeAI.Microsoft/GenerativeAI.Microsoft.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<ProjectReference Include="..\GenerativeAI\GenerativeAI.csproj" />
3333
</ItemGroup>
3434
<ItemGroup>
35-
<PackageReference Include="Microsoft.Extensions.AI.Abstractions" Version="9.8.0" />
35+
<PackageReference Include="Microsoft.Extensions.AI.Abstractions" Version="9.9.0" />
3636
</ItemGroup>
3737

3838
</Project>

0 commit comments

Comments
 (0)