Skip to content

Commit

Permalink
Refactor abstractions (autogenhub#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
kostapetan authored Apr 9, 2024
1 parent 2bece0d commit 65a262b
Show file tree
Hide file tree
Showing 21 changed files with 150 additions and 74 deletions.
14 changes: 14 additions & 0 deletions Microsoft.AI.Agents.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "seed-memory", "samples\seed
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkflowsApp", "samples\WorkflowsApp\WorkflowsApp.csproj", "{3EE3F061-CC63-41B0-84BA-C95A256E18A9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AI.Agents.Orleans", "src\Microsoft.AI.Agents.Orleans\Microsoft.AI.Agents.Orleans.csproj", "{12381ECC-7ECD-4D13-911B-83A07BB58134}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AI.Agents.Dapr", "src\Microsoft.AI.Agents.Dapr\Microsoft.AI.Agents.Dapr.csproj", "{6CCA9961-D498-4E59-8781-D94AE8228200}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -41,6 +45,14 @@ Global
{3EE3F061-CC63-41B0-84BA-C95A256E18A9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3EE3F061-CC63-41B0-84BA-C95A256E18A9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3EE3F061-CC63-41B0-84BA-C95A256E18A9}.Release|Any CPU.Build.0 = Release|Any CPU
{12381ECC-7ECD-4D13-911B-83A07BB58134}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{12381ECC-7ECD-4D13-911B-83A07BB58134}.Debug|Any CPU.Build.0 = Debug|Any CPU
{12381ECC-7ECD-4D13-911B-83A07BB58134}.Release|Any CPU.ActiveCfg = Release|Any CPU
{12381ECC-7ECD-4D13-911B-83A07BB58134}.Release|Any CPU.Build.0 = Release|Any CPU
{6CCA9961-D498-4E59-8781-D94AE8228200}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6CCA9961-D498-4E59-8781-D94AE8228200}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6CCA9961-D498-4E59-8781-D94AE8228200}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6CCA9961-D498-4E59-8781-D94AE8228200}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -52,5 +64,7 @@ Global
{F02CC33B-B479-480B-B886-25ED0E18494A} = {DADD59B8-4B8B-4F2B-BFEB-0F93B8A6AA55}
{A1EB6145-FE32-45EC-8A1B-A76268B6DFDE} = {17FFA72B-390A-4BF6-A277-2099897D2132}
{3EE3F061-CC63-41B0-84BA-C95A256E18A9} = {17FFA72B-390A-4BF6-A277-2099897D2132}
{12381ECC-7ECD-4D13-911B-83A07BB58134} = {7EC6823E-99FB-403F-9941-0CB08C6BFFD4}
{6CCA9961-D498-4E59-8781-D94AE8228200} = {7EC6823E-99FB-403F-9941-0CB08C6BFFD4}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Microsoft.AI.Agents.Abstractions;
using Microsoft.AI.Agents.Orleans;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Memory;
using Orleans.Runtime;
using Orleans.Streams;

namespace Microsoft.AI.DevTeam;

Expand All @@ -17,7 +17,7 @@ public Architect([PersistentState("state", "messages")] IPersistentState<AgentSt
{
}

public override Task HandleEvent(Event item, StreamSequenceToken? token)
public override Task HandleEvent(Event item)
{
return Task.CompletedTask;
}
Expand Down
4 changes: 2 additions & 2 deletions samples/gh-flow/src/Microsoft.AI.DevTeam/Agents/AzureGenie.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.AI.Agents.Abstractions;
using Microsoft.AI.Agents.Orleans;
using Microsoft.AI.DevTeam.Events;
using Orleans.Streams;

namespace Microsoft.AI.DevTeam;

Expand All @@ -15,7 +15,7 @@ public AzureGenie( IManageAzure azureService)
_azureService = azureService;
}

public override async Task HandleEvent(Event item, StreamSequenceToken? token)
public override async Task HandleEvent(Event item)
{
switch (item.Type)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Microsoft.AI.Agents.Abstractions;
using Microsoft.AI.Agents.Orleans;
using Microsoft.AI.DevTeam.Events;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Memory;
using Orleans.Runtime;
using Orleans.Streams;

namespace Microsoft.AI.DevTeam;

Expand All @@ -20,7 +20,7 @@ public Dev([PersistentState("state", "messages")] IPersistentState<AgentState<De
_logger = logger;
}

public async override Task HandleEvent(Event item, StreamSequenceToken? token)
public async override Task HandleEvent(Event item)
{
switch (item.Type)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Microsoft.AI.Agents.Abstractions;
using Microsoft.AI.Agents.Orleans;
using Microsoft.AI.DevTeam.Events;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Memory;
using Orleans.Runtime;
using Orleans.Streams;

namespace Microsoft.AI.DevTeam;
[ImplicitStreamSubscription(Consts.MainNamespace)]
Expand All @@ -18,7 +18,7 @@ public DeveloperLead([PersistentState("state", "messages")] IPersistentState<Age
_logger = logger;
}

public async override Task HandleEvent(Event item, StreamSequenceToken? token)
public async override Task HandleEvent(Event item)
{
switch (item.Type)
{
Expand Down
4 changes: 2 additions & 2 deletions samples/gh-flow/src/Microsoft.AI.DevTeam/Agents/Hubber.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Text.Json;
using Microsoft.AI.Agents.Abstractions;
using Microsoft.AI.Agents.Orleans;
using Microsoft.AI.DevTeam.Events;
using Orleans.Streams;

namespace Microsoft.AI.DevTeam;

Expand All @@ -16,7 +16,7 @@ public Hubber(IManageGithub ghService)
_ghService = ghService;
}

public override async Task HandleEvent(Event item, StreamSequenceToken? token)
public override async Task HandleEvent(Event item)
{
switch (item.Type)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Microsoft.AI.Agents.Abstractions;
using Microsoft.AI.Agents.Orleans;
using Microsoft.AI.DevTeam.Events;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Memory;
using Orleans.Runtime;
using Orleans.Streams;

namespace Microsoft.AI.DevTeam;

Expand All @@ -19,7 +19,7 @@ public ProductManager([PersistentState("state", "messages")] IPersistentState<Ag
_logger = logger;
}

public async override Task HandleEvent(Event item, StreamSequenceToken? token)
public async override Task HandleEvent(Event item)
{
switch (item.Type)
{
Expand Down
3 changes: 2 additions & 1 deletion samples/gh-flow/src/Microsoft.AI.DevTeam/Agents/Sandbox.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AI.Agents.Abstractions;
using Microsoft.AI.Agents.Orleans;
using Microsoft.AI.DevTeam.Events;
using Orleans.Runtime;
using Orleans.Streams;
Expand All @@ -23,7 +24,7 @@ public Sandbox([PersistentState("state", "messages")] IPersistentState<SandboxMe
_azService = azService;
_state = state;
}
public override async Task HandleEvent(Event item, StreamSequenceToken? token)
public override async Task HandleEvent(Event item)
{
switch(item.Type)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<ItemGroup>
<ProjectReference Include="..\..\..\..\src\Microsoft.AI.Agents\Microsoft.AI.Agents.csproj" />
<ProjectReference Include="..\..\..\..\src\Microsoft.AI.Agents.Orleans\Microsoft.AI.Agents.Orleans.csproj" />
</ItemGroup>

<PropertyGroup>
Expand Down
6 changes: 6 additions & 0 deletions src/Microsoft.AI.Agents.Dapr/Agent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Microsoft.AI.Agents.Dapr;

public class Agent
{

}
17 changes: 17 additions & 0 deletions src/Microsoft.AI.Agents.Dapr/Microsoft.AI.Agents.Dapr.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Dapr.Actors" Version="1.13.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Microsoft.AI.Agents\Microsoft.AI.Agents.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
using Orleans.Runtime;
using Orleans.Streams;

namespace Microsoft.AI.Agents.Abstractions;

public abstract class Agent : Grain, IGrainWithStringKey
{
protected virtual string Namespace { get;set;}
public abstract Task HandleEvent(Event item, StreamSequenceToken? token);
protected async Task PublishEvent(string ns, string id, Event item)
{
var streamProvider = this.GetStreamProvider("StreamProvider");
var streamId = StreamId.Create(ns, id);
var stream = streamProvider.GetStream<Event>(streamId);
await stream.OnNextAsync(item);
}

public async override Task OnActivateAsync(CancellationToken cancellationToken)
{
var streamProvider = this.GetStreamProvider("StreamProvider");
var streamId = StreamId.Create(Namespace, this.GetPrimaryKeyString());
var stream = streamProvider.GetStream<Event>(streamId);

await stream.SubscribeAsync(HandleEvent);
}
}
using Microsoft.AI.Agents.Abstractions;
using Orleans.Runtime;
using Orleans.Streams;

namespace Microsoft.AI.Agents.Orleans;

public abstract class Agent : Grain, IGrainWithStringKey, IAgent
{
protected virtual string Namespace { get;set;}
public abstract Task HandleEvent(Event item);
private async Task HandleEvent(Event item, StreamSequenceToken? token)
{
await HandleEvent(item);
}
public async Task PublishEvent(string ns, string id, Event item)
{
var streamProvider = this.GetStreamProvider("StreamProvider");
var streamId = StreamId.Create(ns, id);
var stream = streamProvider.GetStream<Event>(streamId);
await stream.OnNextAsync(item);
}

public async override Task OnActivateAsync(CancellationToken cancellationToken)
{
var streamProvider = this.GetStreamProvider("StreamProvider");
var streamId = StreamId.Create(Namespace, this.GetPrimaryKeyString());
var stream = streamProvider.GetStream<Event>(streamId);
await stream.SubscribeAsync(HandleEvent);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System.Text;
using Microsoft.AI.Agents.Abstractions;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Microsoft.SemanticKernel.Memory;
using Orleans.Runtime;

namespace Microsoft.AI.Agents.Abstractions;
namespace Microsoft.AI.Agents.Orleans;

public abstract class AiAgent<T> : Agent
public abstract class AiAgent<T> : Agent, IAiAgent
{
public AiAgent(
[PersistentState("state", "messages")] IPersistentState<AgentState<T>> state, ISemanticTextMemory memory, Kernel kernel)
Expand All @@ -19,7 +20,7 @@ public AiAgent(
private readonly ISemanticTextMemory _memory;
private readonly Kernel _kernel;

protected void AddToHistory(string message, ChatUserType userType)
public void AddToHistory(string message, ChatUserType userType)
{
if (_state.State.History == null) _state.State.History = new List<ChatHistoryItem>();
_state.State.History.Add(new ChatHistoryItem
Expand All @@ -30,13 +31,13 @@ protected void AddToHistory(string message, ChatUserType userType)
});
}

protected string AppendChatHistory(string ask)
public string AppendChatHistory(string ask)
{
AddToHistory(ask, ChatUserType.User);
return string.Join("\n", _state.State.History.Select(message => $"{message.UserType}: {message.Message}"));
}

protected virtual async Task<string> CallFunction(string template, KernelArguments arguments, OpenAIPromptExecutionSettings? settings = null)
public virtual async Task<string> CallFunction(string template, KernelArguments arguments, OpenAIPromptExecutionSettings? settings = null)
{
var propmptSettings = (settings == null) ? new OpenAIPromptExecutionSettings { MaxTokens = 18000, Temperature = 0.8, TopP = 1 }
: settings;
Expand All @@ -53,7 +54,7 @@ protected virtual async Task<string> CallFunction(string template, KernelArgumen
/// <param name="index">Knowledge index</param>
/// <param name="arguments">The sk arguments, "input" is the argument </param>
/// <returns></returns>
protected async Task<KernelArguments> AddKnowledge(string instruction, string index, KernelArguments arguments)
public async Task<KernelArguments> AddKnowledge(string instruction, string index, KernelArguments arguments)
{
var documents = _memory.SearchAsync(index, arguments["input"].ToString(), 5);
var kbStringBuilder = new StringBuilder();
Expand All @@ -64,26 +65,4 @@ protected async Task<KernelArguments> AddKnowledge(string instruction, string in
arguments[index] = instruction.Replace($"!{index}!", $"{kbStringBuilder}");
return arguments;
}
}

[Serializable]
public class ChatHistoryItem
{
public string Message { get; set; }
public ChatUserType UserType { get; set; }
public int Order { get; set; }

}

public class AgentState<T>
{
public List<ChatHistoryItem> History { get; set; }
public T Data { get; set; }
}

public enum ChatUserType
{
System,
User,
Agent
}
15 changes: 15 additions & 0 deletions src/Microsoft.AI.Agents.Orleans/Microsoft.AI.Agents.Orleans.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Orleans.Sdk" Version="8.0.0" />
<PackageReference Include="Microsoft.Orleans.Streaming" Version="8.0.0" />
<ProjectReference Include="..\Microsoft.AI.Agents\Microsoft.AI.Agents.csproj" />
</ItemGroup>

</Project>
7 changes: 7 additions & 0 deletions src/Microsoft.AI.Agents/Abstractions/AgentState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Microsoft.AI.Agents.Abstractions;

public class AgentState<T>
{
public List<ChatHistoryItem> History { get; set; }
public T Data { get; set; }
}
10 changes: 10 additions & 0 deletions src/Microsoft.AI.Agents/Abstractions/ChatHistoryItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Microsoft.AI.Agents.Abstractions;

[Serializable]
public class ChatHistoryItem
{
public string Message { get; set; }
public ChatUserType UserType { get; set; }
public int Order { get; set; }

}
8 changes: 8 additions & 0 deletions src/Microsoft.AI.Agents/Abstractions/ChatUserType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Microsoft.AI.Agents.Abstractions;

public enum ChatUserType
{
System,
User,
Agent
}
4 changes: 0 additions & 4 deletions src/Microsoft.AI.Agents/Abstractions/Event.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
namespace Microsoft.AI.Agents.Abstractions
{
[GenerateSerializer]
public class Event
{
[Id(0)]
public string Message { get; set; }
[Id(1)]
public Dictionary<string, string> Data { get; set; }
[Id(2)]
public string Type { get; set; }
}
}
7 changes: 7 additions & 0 deletions src/Microsoft.AI.Agents/Abstractions/IAgent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Microsoft.AI.Agents.Abstractions;

public interface IAgent
{
Task HandleEvent(Event item);
Task PublishEvent(string ns, string id, Event item);
}
Loading

0 comments on commit 65a262b

Please sign in to comment.