Skip to content

Commit

Permalink
Add history support to AzCLI agent (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
daxian-dbw authored Feb 22, 2024
1 parent da5ce16 commit b4edca6
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ if ($LASTEXITCODE -eq 0) {
}

if ($LASTEXITCODE -eq 0) {
Write-Host "`n[Build the Azure agent ...]`n" -ForegroundColor Green
Write-Host "`n[Build the Azure agents ...]`n" -ForegroundColor Green
$az_csproj = GetProjectFile $az_agent_dir
dotnet publish $az_csproj -c $Configuration -o $az_out_dir
}
Expand Down
9 changes: 9 additions & 0 deletions shell/ShellCopilot.Azure.Agent/AzCLI/AzCLIAgent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text;
using System.Text.Json;
using Azure.Identity;
using ShellCopilot.Abstraction;

Expand Down Expand Up @@ -76,6 +77,14 @@ public async Task<bool> Chat(string input, IShell shell)
}

var data = azResponse.Data[0];
var history = _chatService.ChatHistory;
while (history.Count > Utils.HistoryCount - 2)
{
history.RemoveAt(0);
}
history.Add(new ChatMessage { Role = "user", Content = input });
history.Add(new ChatMessage { Role = "assistant", Content = JsonSerializer.Serialize(data, Utils.JsonOptions) });

_text.Clear();
_text.AppendLine(data.Description).AppendLine();

Expand Down
11 changes: 10 additions & 1 deletion shell/ShellCopilot.Azure.Agent/AzCLI/AzCLIChatService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@ internal class AzCLIChatService : IDisposable

private readonly HttpClient _client;
private readonly string[] _scopes;
private readonly List<ChatMessage> _chatHistory;
private AccessToken? _accessToken;

internal AzCLIChatService()
{
_client = new HttpClient();
_scopes = ["api://62009369-df36-4df2-b7d7-b3e784b3ed55/"];
_chatHistory = [];
_accessToken = null;
}

internal List<ChatMessage> ChatHistory => _chatHistory;

public void Dispose()
{
_client.Dispose();
Expand Down Expand Up @@ -51,7 +55,12 @@ private void RefreshToken(CancellationToken cancellationToken)

private HttpRequestMessage PrepareForChat(string input)
{
var requestData = new Query { Question = input, Top_num = 1 };
var requestData = new Query
{
Question = input,
History = _chatHistory,
Top_num = 1
};
var json = JsonSerializer.Serialize(requestData, Utils.JsonOptions);

var content = new StringContent(json, Encoding.UTF8, "application/json");
Expand Down
1 change: 1 addition & 0 deletions shell/ShellCopilot.Azure.Agent/AzCLI/AzCLISchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace ShellCopilot.Azure.CLI;
internal class Query
{
public string Question { get; set; }
public List<ChatMessage> History { get; set; }
public int Top_num { get; set; }
}

Expand Down
4 changes: 4 additions & 0 deletions shell/ShellCopilot.Azure.Agent/AzPS/AzPSChatService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ internal void AddResponseToHistory(string response)
{
if (!string.IsNullOrEmpty(response))
{
while (_chatHistory.Count > Utils.HistoryCount - 1)
{
_chatHistory.RemoveAt(0);
}
_chatHistory.Add(new ChatMessage() { Role = "assistant", Content = response });
}
}
Expand Down
6 changes: 0 additions & 6 deletions shell/ShellCopilot.Azure.Agent/AzPS/AzPSSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@

namespace ShellCopilot.Azure.PowerShell;

internal class ChatMessage
{
public string Role { get; set; }
public string Content { get; set; }
}

internal class Query
{
public List<ChatMessage> Messages { get; set; }
Expand Down
11 changes: 11 additions & 0 deletions shell/ShellCopilot.Azure.Agent/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ static Utils()
}

internal static JsonSerializerOptions JsonOptions => s_jsonOptions;

/// <summary>
/// Keep 3 conversation iterations as the context information.
/// </summary>
internal const int HistoryCount = 6;
}

internal class RefreshTokenException : Exception
Expand All @@ -25,3 +30,9 @@ internal RefreshTokenException(string message, Exception innerException)
{
}
}

internal class ChatMessage
{
public string Role { get; set; }
public string Content { get; set; }
}

0 comments on commit b4edca6

Please sign in to comment.