Skip to content

Commit add31db

Browse files
committed
Add Azure OpenAI integration to Elsa Agents
Introduced a new module `Elsa.Agents.AzureOpenAI` for integrating Azure OpenAI with Elsa Agents. The module supports Azure OpenAI chat completions and is added to the main solution and dependencies. Adjusted related files to accommodate this new feature.
1 parent 89f3e1b commit add31db

8 files changed

Lines changed: 87 additions & 19 deletions

File tree

Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@
175175
<PackageVersion Include="Microsoft.SemanticKernel.Agents.Core" Version="1.68.0"/>
176176
<PackageVersion Include="Microsoft.SemanticKernel.PromptTemplates.Handlebars" Version="1.68.0"/>
177177
<PackageVersion Include="Microsoft.SemanticKernel.Plugins.Memory" Version="1.68.0-alpha"/>
178+
<PackageVersion Include="Microsoft.SemanticKernel.Connectors.AzureOpenAI" Version="1.68.0"/>
178179
<PackageVersion Include="Microsoft.SemanticKernel.Connectors.InMemory" Version="1.68.0-preview"/>
179180
<PackageVersion Include="Microsoft.SemanticKernel.Connectors.OpenAI" Version="1.68.0"/>
180181
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0"/>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Microsoft.SemanticKernel;
2+
3+
namespace Elsa.Agents.AzureOpenAI;
4+
5+
public static class AgentsFeatureExtensions
6+
{
7+
public static AgentsFeature AddAzureOpenAIChatCompletion(this AgentsFeature feature,
8+
string deploymentName,
9+
string endpoint,
10+
string apiKey,
11+
string? serviceId = null,
12+
string? modelId = null,
13+
string? apiVersion = null,
14+
HttpClient? httpClient = null,
15+
string name = "Azure OpenAI Chat Completion")
16+
{
17+
return feature.AddServiceDescriptor(new()
18+
{
19+
Name = name,
20+
ConfigureKernel = kernel => kernel.Services.AddAzureOpenAIChatCompletion(deploymentName, endpoint, apiKey, serviceId, modelId, apiVersion, httpClient)
21+
});
22+
}
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<Description>Provides Azure OpenAI integration with Elsa Agents</Description>
5+
<PackageTags>elsa extension module agents azure openai</PackageTags>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.SemanticKernel.Connectors.AzureOpenAI"/>
10+
</ItemGroup>
11+
12+
<ItemGroup Label="Elsa" Condition="'$(UseProjectReferences)' == 'true'">
13+
<ProjectReference Include="..\..\..\..\..\elsa-core\src\modules\Elsa.Common\Elsa.Common.csproj"/>
14+
<ProjectReference Include="..\Elsa.Agents\Elsa.Agents.csproj"/>
15+
</ItemGroup>
16+
17+
<ItemGroup Label="Elsa" Condition="'$(UseProjectReferences)' != 'true'">
18+
<PackageReference Include="Elsa.Common"/>
19+
<PackageReference Include="Elsa.Agents"/>
20+
</ItemGroup>
21+
22+
</Project>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
2+
<ConfigureAwait />
3+
</Weavers>

src/modules/agents/Elsa.Agents.Core/Services/AgentInvoker.cs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,23 @@ public async Task<InvokeAgentResult> InvokeAgentAsync(string agentName, IDiction
5858
var renderedPrompt = await promptTemplate.RenderAsync(agent.Kernel, kernelArguments, cancellationToken);
5959

6060
chatHistory.AddUserMessage(renderedPrompt);
61-
chatHistory.AddSystemMessage(
62-
""""
63-
You are a function that returns *only* JSON.
6461

65-
Rules:
66-
- Return a single valid JSON object.
67-
- Do not add explanations.
68-
- Do not add code fences.
69-
- Do not prefix with ```json or any other markers.
70-
- Output must start with { and end with }.
71-
72-
If there's a problem with the JSON input, include the exact JSON input in your response for troubleshooting.
73-
"""");
62+
if (executionSettings.ResponseFormat == "json_object")
63+
{
64+
chatHistory.AddSystemMessage(
65+
""""
66+
You are a function that returns *only* JSON.
67+
68+
Rules:
69+
- Return a single valid JSON object.
70+
- Do not add explanations.
71+
- Do not add code fences.
72+
- Do not prefix with ```json or any other markers.
73+
- Output must start with { and end with }.
74+
75+
If there's a problem with the JSON input, include the exact JSON input in your response for troubleshooting.
76+
"""");
77+
}
7478

7579
// Get response from agent
7680
var response = await agent.InvokeAsync(chatHistory, cancellationToken: cancellationToken).LastOrDefaultAsync(cancellationToken);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Microsoft.SemanticKernel;
2+
3+
namespace Elsa.Agents.OpenAI;
4+
5+
public static class AgentsFeatureExtensions
6+
{
7+
public static AgentsFeature AddOpenAIChatCompletion(this AgentsFeature feature,
8+
string modelId,
9+
string apiKey,
10+
string? orgId = null,
11+
string? serviceId = null,
12+
string name = "OpenAI Chat Completion")
13+
{
14+
return feature.AddServiceDescriptor(new()
15+
{
16+
Name = name,
17+
ConfigureKernel = kernel => kernel.Services.AddOpenAIChatCompletion(modelId, apiKey, orgId, serviceId)
18+
});
19+
}
20+
}

src/modules/agents/Elsa.Agents.OpenAI/Class1.cs

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/modules/agents/Elsa.Agents.OpenAI/Elsa.Agents.OpenAI.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111

1212
<ItemGroup Label="Elsa" Condition="'$(UseProjectReferences)' == 'true'">
1313
<ProjectReference Include="..\..\..\..\..\elsa-core\src\modules\Elsa.Common\Elsa.Common.csproj"/>
14-
<ProjectReference Include="..\Elsa.Agents.Core\Elsa.Agents.Core.csproj"/>
14+
<ProjectReference Include="..\Elsa.Agents\Elsa.Agents.csproj"/>
1515
</ItemGroup>
1616

1717
<ItemGroup Label="Elsa" Condition="'$(UseProjectReferences)' != 'true'">
1818
<PackageReference Include="Elsa.Common"/>
19-
<PackageReference Include="Elsa.Agents.Core"/>
19+
<PackageReference Include="Elsa.Agents"/>
2020
</ItemGroup>
2121

2222
</Project>

0 commit comments

Comments
 (0)