Skip to content

Commit

Permalink
Renamed SessionProperties to ChatSessionProperties
Browse files Browse the repository at this point in the history
  • Loading branch information
alistar-andrei committed Aug 8, 2024
1 parent 13798f4 commit 30f4f49
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ namespace FoundationaLLM.Common.Models.Chat
/// <summary>
/// The session properties object.
/// </summary>
public class SessionProperties
public class ChatSessionProperties
{
/// <summary>
/// The session name.
/// </summary>
[JsonPropertyName("session_name")]
public required string SessionName { get; set; }
[JsonPropertyName("name")]
public required string Name { get; set; }
}
}
8 changes: 4 additions & 4 deletions src/dotnet/Core/Interfaces/ICoreService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ public interface ICoreService
/// Creates a new chat session.
/// </summary>
/// <param name="instanceId">The instance Id.</param>
/// <param name="sessionProperties">The session properties.</param>
Task<Session> CreateNewChatSessionAsync(string instanceId, SessionProperties sessionProperties);
/// <param name="chatSessionProperties">The session properties.</param>
Task<Session> CreateNewChatSessionAsync(string instanceId, ChatSessionProperties chatSessionProperties);

/// <summary>
/// Rename the chat session from its default (eg., "New Chat") to the summary provided by OpenAI.
/// </summary>
/// <param name="instanceId">The instance id.</param>
/// <param name="sessionId">The session id to rename.</param>
/// <param name="sessionProperties">The session properties.</param>
Task<Session> RenameChatSessionAsync(string instanceId, string sessionId, SessionProperties sessionProperties);
/// <param name="chatSessionProperties">The session properties.</param>
Task<Session> RenameChatSessionAsync(string instanceId, string sessionId, ChatSessionProperties chatSessionProperties);

/// <summary>
/// Delete a chat session and related messages.
Expand Down
12 changes: 6 additions & 6 deletions src/dotnet/Core/Services/CoreService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,26 @@ public async Task<List<Message>> GetChatSessionMessagesAsync(string instanceId,
}

/// <inheritdoc/>
public async Task<Session> CreateNewChatSessionAsync(string instanceId, SessionProperties sessionProperties)
public async Task<Session> CreateNewChatSessionAsync(string instanceId, ChatSessionProperties chatSessionProperties)
{
ArgumentException.ThrowIfNullOrEmpty(sessionProperties.SessionName);
ArgumentException.ThrowIfNullOrEmpty(chatSessionProperties.Name);

Session session = new()
{
Name = sessionProperties.SessionName,
Name = chatSessionProperties.Name,
Type = _sessionType,
UPN = _callContext.CurrentUserIdentity?.UPN ?? throw new InvalidOperationException("Failed to retrieve the identity of the signed in user when creating a new chat session.")
};
return await _cosmosDbService.InsertSessionAsync(session);
}

/// <inheritdoc/>
public async Task<Session> RenameChatSessionAsync(string instanceId, string sessionId, SessionProperties sessionProperties)
public async Task<Session> RenameChatSessionAsync(string instanceId, string sessionId, ChatSessionProperties chatSessionProperties)
{
ArgumentNullException.ThrowIfNull(sessionId);
ArgumentException.ThrowIfNullOrEmpty(sessionProperties.SessionName);
ArgumentException.ThrowIfNullOrEmpty(chatSessionProperties.Name);

return await _cosmosDbService.UpdateSessionNameAsync(sessionId, sessionProperties.SessionName);
return await _cosmosDbService.UpdateSessionNameAsync(sessionId, chatSessionProperties.Name);
}

/// <inheritdoc/>
Expand Down
12 changes: 6 additions & 6 deletions src/dotnet/CoreAPI/Controllers/SessionsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,20 @@ public async Task<CompletionPrompt> GetCompletionPrompt(string instanceId, strin
/// Creates a new chat session.
/// </summary>
/// <param name="instanceId">The id of the instance.</param>
/// <param name="sessionProperties">The session properties.</param>
/// <param name="chatSessionProperties">The session properties.</param>
[HttpPost(Name = "CreateNewChatSession")]
public async Task<Session> CreateNewChatSession(string instanceId, [FromBody] SessionProperties sessionProperties) =>
await _coreService.CreateNewChatSessionAsync(instanceId, sessionProperties);
public async Task<Session> CreateNewChatSession(string instanceId, [FromBody] ChatSessionProperties chatSessionProperties) =>
await _coreService.CreateNewChatSessionAsync(instanceId, chatSessionProperties);

/// <summary>
/// Rename the chat session.
/// </summary>
/// <param name="instanceId">The id of the instance.</param>
/// <param name="sessionId">The id of the session to rename.</param>
/// <param name="sessionProperties">The session properties.</param>
/// <param name="chatSessionProperties">The session properties.</param>
[HttpPost("{sessionId}/rename", Name = "RenameChatSession")]
public async Task<Session> RenameChatSession(string instanceId, string sessionId, [FromBody] SessionProperties sessionProperties) =>
await _coreService.RenameChatSessionAsync(instanceId, sessionId, sessionProperties);
public async Task<Session> RenameChatSession(string instanceId, string sessionId, [FromBody] ChatSessionProperties chatSessionProperties) =>
await _coreService.RenameChatSessionAsync(instanceId, sessionId, chatSessionProperties);

/// <summary>
/// Delete a chat session and related messages.
Expand Down
10 changes: 5 additions & 5 deletions src/dotnet/CoreClient/Clients/RESTClients/SessionRESTClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ internal class SessionRESTClient(
private readonly string _instanceId = instanceId ?? throw new ArgumentNullException(nameof(instanceId));

/// <inheritdoc/>
public async Task<string> CreateSessionAsync(SessionProperties sessionProperties)
public async Task<string> CreateSessionAsync(ChatSessionProperties chatSessionProperties)
{
var coreClient = await GetCoreClientAsync();
var responseSession = await coreClient.PostAsync(
$"instances/{_instanceId}/sessions",
JsonContent.Create(sessionProperties));
JsonContent.Create(chatSessionProperties));

if (responseSession.IsSuccessStatusCode)
{
Expand All @@ -38,16 +38,16 @@ public async Task<string> CreateSessionAsync(SessionProperties sessionProperties
}

/// <inheritdoc/>
public async Task<string> RenameChatSession(string sessionId, SessionProperties sessionProperties)
public async Task<string> RenameChatSession(string sessionId, ChatSessionProperties chatSessionProperties)
{
var coreClient = await GetCoreClientAsync();
var response = await coreClient.PostAsync(
$"instances/{_instanceId}/sessions/{sessionId}/rename",
JsonContent.Create(sessionProperties));
JsonContent.Create(chatSessionProperties));

if (response.IsSuccessStatusCode)
{
return sessionProperties.SessionName;
return chatSessionProperties.Name;
}

throw new Exception($"Failed to rename chat session. Status code: {response.StatusCode}. Reason: {response.ReasonPhrase}");
Expand Down
18 changes: 9 additions & 9 deletions src/dotnet/CoreClient/CoreClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,29 +54,29 @@ public CoreClient(
_coreRestClient = new CoreRESTClient(coreUri, credential, instanceId, options);

/// <inheritdoc/>
public async Task<string> CreateChatSessionAsync(SessionProperties sessionProperties)
public async Task<string> CreateChatSessionAsync(ChatSessionProperties chatSessionProperties)
{
if (string.IsNullOrWhiteSpace(sessionProperties.SessionName))
if (string.IsNullOrWhiteSpace(chatSessionProperties.Name))
throw new ArgumentException("A session name must be provided when creating a new session.");

var sessionId = await _coreRestClient.Sessions.CreateSessionAsync(sessionProperties);
var sessionId = await _coreRestClient.Sessions.CreateSessionAsync(chatSessionProperties);
return sessionId;
}

/// <inheritdoc/>
public async Task<Completion> GetCompletionWithSessionAsync(string? sessionId, SessionProperties? sessionProperties,
public async Task<Completion> GetCompletionWithSessionAsync(string? sessionId, ChatSessionProperties? chatSessionProperties,
string userPrompt, string agentName)
{
if (string.IsNullOrWhiteSpace(sessionId))
{
if (sessionProperties == null)
if (chatSessionProperties == null)
{
throw new ArgumentException(
"The completion request must contain a session name if no session Id is provided. " +
"A new session will be created with the provided session name.");
}

sessionId = await CreateChatSessionAsync(sessionProperties);
sessionId = await CreateChatSessionAsync(chatSessionProperties);
}

var orchestrationRequest = new CompletionRequest
Expand Down Expand Up @@ -132,7 +132,7 @@ public async Task<Completion> GetCompletionAsync(CompletionRequest completionReq

/// <inheritdoc/>
public async Task<Completion> AttachFileAndAskQuestionAsync(Stream fileStream, string fileName, string contentType,
string agentName, string question, bool useSession, string? sessionId, SessionProperties? sessionProperties)
string agentName, string question, bool useSession, string? sessionId, ChatSessionProperties? chatSessionProperties)
{
if (fileStream == null)
{
Expand All @@ -145,14 +145,14 @@ public async Task<Completion> AttachFileAndAskQuestionAsync(Stream fileStream, s
{
if (string.IsNullOrWhiteSpace(sessionId))
{
if (sessionProperties == null)
if (chatSessionProperties == null)
{
throw new ArgumentException(
"The completion request must contain a session name if no session Id is provided. " +
"A new session will be created with the provided session name.");
}

sessionId = await CreateChatSessionAsync(sessionProperties);
sessionId = await CreateChatSessionAsync(chatSessionProperties);
}

var orchestrationRequest = new CompletionRequest
Expand Down
8 changes: 4 additions & 4 deletions src/dotnet/CoreClient/Interfaces/ICoreClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public interface ICoreClient
/// </summary>
/// <param name="sessionProperties">The session properties.</param>
/// <returns>The new chat session ID.</returns>
Task<string> CreateChatSessionAsync(SessionProperties sessionProperties);
Task<string> CreateChatSessionAsync(ChatSessionProperties sessionProperties);

/// <summary>
/// Runs a single completion request with an agent using the Core API and a chat session.
Expand All @@ -30,7 +30,7 @@ public interface ICoreClient
/// <param name="agentName">The name of the FoundationaLLM agent that will handle the
/// completion request.</param>
/// <returns>A completion from the designated FoundationaLLM agent.</returns>
Task<Completion> GetCompletionWithSessionAsync(string? sessionId, SessionProperties? sessionProperties,
Task<Completion> GetCompletionWithSessionAsync(string? sessionId, ChatSessionProperties? sessionProperties,
string userPrompt, string agentName);

/// <summary>
Expand Down Expand Up @@ -83,11 +83,11 @@ Task<Completion> GetCompletionWithSessionAsync(string? sessionId, SessionPropert
/// false, no session is created and the sessionless orchestration flow is used.</param>
/// <param name="sessionId">The ID of an existing session. If null or empty, a new session
/// is created first.</param>
/// <param name="sessionProperties">Optional session properties.</param>
/// <param name="chatSessionProperties">Optional session properties.</param>
/// <returns>A completion from the designated FoundationaLLM agent.</returns>
/// <returns>A completion from the designated FoundationaLLM agent.</returns>
Task<Completion> AttachFileAndAskQuestionAsync(Stream fileStream, string fileName, string contentType,
string agentName, string question, bool useSession, string? sessionId, SessionProperties? sessionProperties);
string agentName, string question, bool useSession, string? sessionId, ChatSessionProperties? chatSessionProperties);

/// <summary>
/// Returns the chat messages related to an existing session.
Expand Down
8 changes: 4 additions & 4 deletions src/dotnet/CoreClient/Interfaces/ISessionRESTClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ public interface ISessionRESTClient
/// <summary>
/// Creates a new session with the specified name.
/// </summary>
/// <param name="sessionProperties">The session properties.</param>
/// <param name="chatSessionProperties">The session properties.</param>
/// <returns>Returns the new Session ID.</returns>
Task<string> CreateSessionAsync(SessionProperties sessionProperties);
Task<string> CreateSessionAsync(ChatSessionProperties chatSessionProperties);

/// <summary>
/// Renames a chat session.
/// </summary>
/// <param name="sessionId">The chat session ID.</param>
/// <param name="sessionProperties">The session properties.</param>
/// <param name="chatSessionProperties">The session properties.</param>
/// <returns></returns>
Task<string> RenameChatSession(string sessionId, SessionProperties sessionProperties);
Task<string> RenameChatSession(string sessionId, ChatSessionProperties chatSessionProperties);

/// <summary>
/// Gets a completion prompt by session ID and completion prompt ID.
Expand Down
26 changes: 13 additions & 13 deletions tests/dotnet/Core.Client.Tests/CoreClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ public CoreClientTests()
public async Task CreateChatSessionAsync_WithName_CreatesAndRenamesSession()
{
// Arrange
var sessionProperties = new SessionProperties() { SessionName = "TestSession" };
var chatSessionProperties = new ChatSessionProperties() { Name = "TestSession" };
var sessionId = "session-id";
_coreRestClient.Sessions.CreateSessionAsync(sessionProperties).Returns(Task.FromResult(sessionId));
_coreRestClient.Sessions.CreateSessionAsync(chatSessionProperties).Returns(Task.FromResult(sessionId));

// Act
var result = await _coreClient.CreateChatSessionAsync(sessionProperties);
var result = await _coreClient.CreateChatSessionAsync(chatSessionProperties);

// Assert
Assert.Equal(sessionId, result);
await _coreRestClient.Sessions.Received(1).CreateSessionAsync(sessionProperties);
await _coreRestClient.Sessions.Received(1).CreateSessionAsync(chatSessionProperties);
}

[Fact]
Expand All @@ -42,18 +42,18 @@ public async Task GetCompletionWithSessionAsync_WithNewSession_CreatesSessionAnd
// Arrange
var userPrompt = "Hello, World!";
var agentName = "TestAgent";
var sessionProperties = new SessionProperties() { SessionName = "TestSession" };
var chatSessionProperties = new ChatSessionProperties() { Name = "TestSession" };
var sessionId = "new-session-id";
var completion = new Completion();
_coreRestClient.Sessions.CreateSessionAsync(sessionProperties).Returns(Task.FromResult(sessionId));
_coreRestClient.Sessions.CreateSessionAsync(chatSessionProperties).Returns(Task.FromResult(sessionId));
_coreRestClient.Completions.GetChatCompletionAsync(Arg.Any<CompletionRequest>()).Returns(Task.FromResult(completion));

// Act
var result = await _coreClient.GetCompletionWithSessionAsync(null, sessionProperties, userPrompt, agentName);
var result = await _coreClient.GetCompletionWithSessionAsync(null, chatSessionProperties, userPrompt, agentName);

// Assert
Assert.Equal(completion, result);
await _coreRestClient.Sessions.Received(1).CreateSessionAsync(sessionProperties);
await _coreRestClient.Sessions.Received(1).CreateSessionAsync(chatSessionProperties);
await _coreRestClient.Completions.GetChatCompletionAsync(Arg.Is<CompletionRequest>(
r => r.SessionId == sessionId && r.AgentName == agentName && r.UserPrompt == userPrompt));
}
Expand Down Expand Up @@ -100,21 +100,21 @@ public async Task AttachFileAndAskQuestionAsync_UsesSession_UploadsFileAndSendsS
var contentType = "text/plain";
var agentName = "TestAgent";
var question = "What is this file about?";
var sessionProperties = new SessionProperties() { SessionName = "TestSession" };
var chatSessionProperties = new ChatSessionProperties() { Name = "TestSession" };
var sessionId = "session-id";
var objectId = "object-id";
var completion = new Completion();
_coreRestClient.Attachments.UploadAttachmentAsync(fileStream, fileName, contentType).Returns(Task.FromResult(objectId));
_coreRestClient.Sessions.CreateSessionAsync(sessionProperties).Returns(Task.FromResult(sessionId));
_coreRestClient.Sessions.CreateSessionAsync(chatSessionProperties).Returns(Task.FromResult(sessionId));
_coreRestClient.Completions.GetChatCompletionAsync(Arg.Any<CompletionRequest>()).Returns(Task.FromResult(completion));

// Act
var result = await _coreClient.AttachFileAndAskQuestionAsync(fileStream, fileName, contentType, agentName, question, true, null, sessionProperties);
var result = await _coreClient.AttachFileAndAskQuestionAsync(fileStream, fileName, contentType, agentName, question, true, null, chatSessionProperties);

// Assert
Assert.Equal(completion, result);
await _coreRestClient.Attachments.Received(1).UploadAttachmentAsync(fileStream, fileName, contentType);
await _coreRestClient.Sessions.Received(1).CreateSessionAsync(sessionProperties);
await _coreRestClient.Sessions.Received(1).CreateSessionAsync(chatSessionProperties);
await _coreRestClient.Completions.GetChatCompletionAsync(Arg.Is<CompletionRequest>(
r => r.AgentName == agentName && r.SessionId == sessionId && r.UserPrompt == question && r.Attachments.Contains(objectId)));
}
Expand All @@ -126,7 +126,7 @@ public async Task AttachFileAndAskQuestionAsync_ThrowsException_WhenFileStreamIs
await Assert.ThrowsAsync<ArgumentNullException>(() =>
_coreClient.AttachFileAndAskQuestionAsync(
null!, "file.txt", "text/plain", "agent", "question", true, "session-id",
new SessionProperties() { SessionName = "session-name" }));
new ChatSessionProperties() { Name = "session-name" }));
}

[Fact]
Expand Down
Loading

0 comments on commit 30f4f49

Please sign in to comment.