Skip to content

Commit

Permalink
Merge pull request #2064 from solliancenet/jdh-auth-ratings
Browse files Browse the repository at this point in the history
Auth and ratings improvements
  • Loading branch information
ciprianjichici authored Dec 12, 2024
2 parents c99e619 + 91cb599 commit fbd2611
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/dotnet/Core/Interfaces/ICoreService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ Task<ResourceProviderUpsertResult<AttachmentFile>> UploadAttachment(
/// <param name="id">The message id to rate.</param>
/// <param name="sessionId">The session id to which the message belongs.</param>
/// <param name="rating">The rating and optional comments to assign to the message.</param>
Task<Message> RateMessageAsync(string instanceId, string id, string sessionId, MessageRatingRequest rating);
Task RateMessageAsync(string instanceId, string id, string sessionId, MessageRatingRequest rating);

/// <summary>
/// Returns the completion prompt for a given session and completion prompt id.
Expand Down
4 changes: 2 additions & 2 deletions src/dotnet/Core/Services/CoreService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -661,12 +661,12 @@ public async Task<List<Message>> GetChatSessionMessagesAsync(string instanceId,
}

/// <inheritdoc/>
public async Task<Message> RateMessageAsync(string instanceId, string id, string sessionId, MessageRatingRequest rating)
public async Task RateMessageAsync(string instanceId, string id, string sessionId, MessageRatingRequest rating)
{
ArgumentNullException.ThrowIfNull(id);
ArgumentNullException.ThrowIfNull(sessionId);

return await _cosmosDBService.PatchSessionsItemPropertiesAsync<Message>(
await _cosmosDBService.PatchSessionsItemPropertiesAsync<Message>(
id,
sessionId,
new Dictionary<string, object?>
Expand Down
2 changes: 1 addition & 1 deletion src/dotnet/CoreAPI/Controllers/SessionsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public async Task<IEnumerable<Message>> GetChatSessionMessages(string instanceId
/// <param name="sessionId">The id of the session to which the message belongs.</param>
/// <param name="ratingRequest">The rating and optional comments to assign to the message.</param>
[HttpPost("{sessionId}/message/{messageId}/rate", Name = "RateMessage")]
public async Task<Message> RateMessage(string instanceId, string messageId, string sessionId, [FromBody] MessageRatingRequest ratingRequest) =>
public async Task RateMessage(string instanceId, string messageId, string sessionId, [FromBody] MessageRatingRequest ratingRequest) =>
await _coreService.RateMessageAsync(instanceId, messageId, sessionId, ratingRequest);

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public async Task<IEnumerable<Conversation>> GetAllChatSessionsAsync()
}

/// <inheritdoc/>
public async Task<Message> RateMessageAsync(string sessionId, string messageId, MessageRatingRequest rating)
public async Task RateMessageAsync(string sessionId, string messageId, MessageRatingRequest rating)
{
if (string.IsNullOrWhiteSpace(sessionId))
{
Expand All @@ -149,10 +149,6 @@ public async Task<Message> RateMessageAsync(string sessionId, string messageId,
{
throw new Exception($"Failed to rate message. Status code: {responseMessage.StatusCode}. Reason: {responseMessage.ReasonPhrase}");
}

var responseContent = await responseMessage.Content.ReadAsStringAsync();
var message = JsonSerializer.Deserialize<Message>(responseContent, SerializerOptions);
return message ?? throw new InvalidOperationException("The returned Message is invalid.");
}

/// <inheritdoc/>
Expand Down
5 changes: 2 additions & 3 deletions src/dotnet/CoreClient/CoreClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,15 @@ public async Task<string> CreateChatSessionAsync(ChatSessionProperties chatSessi
return sessionId;
}

public async Task<Message> RateMessageAsync(string sessionId, string messageId, MessageRatingRequest rating)
public async Task RateMessageAsync(string sessionId, string messageId, MessageRatingRequest rating)
{
if (string.IsNullOrWhiteSpace(sessionId))
throw new ArgumentException("A session ID must be provided when rating a message.");
if (string.IsNullOrWhiteSpace(messageId))
throw new ArgumentException("A message ID must be provided when rating a message.");
if (rating == null)
throw new ArgumentException("A rating must be provided when rating a message.");
var message = await _coreRestClient.Sessions.RateMessageAsync(sessionId, messageId, rating);
return message;
await _coreRestClient.Sessions.RateMessageAsync(sessionId, messageId, rating);
}

/// <inheritdoc/>
Expand Down
3 changes: 1 addition & 2 deletions src/dotnet/CoreClient/Interfaces/ICoreClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ Task<Message> AttachFileAndAskQuestionAsync(Stream fileStream, string fileName,
/// <param name="sessionId">The chat session ID that contains the message to rate.</param>
/// <param name="messageId">The ID of the message to rate.</param>
/// <param name="rating">The rating and optional comments to assign to the message.</param>
/// <returns>Returns the Message object, including its updated rating.</returns>
Task<Message> RateMessageAsync(string sessionId, string messageId, MessageRatingRequest rating);
Task RateMessageAsync(string sessionId, string messageId, MessageRatingRequest rating);

/// <summary>
/// Retrieves agents available to the user for orchestration and session-based requests.
Expand Down
3 changes: 1 addition & 2 deletions src/dotnet/CoreClient/Interfaces/ISessionRESTClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public interface ISessionRESTClient
/// <param name="sessionId">The chat session ID that contains the message to rate.</param>
/// <param name="messageId">The ID of the message to rate.</param>
/// <param name="rating">The rating and optional comments to assign to the message.</param>
/// <returns>Returns the Message object, including its updated rating.</returns>
Task<Message> RateMessageAsync(string sessionId, string messageId, MessageRatingRequest rating);
Task RateMessageAsync(string sessionId, string messageId, MessageRatingRequest rating);

/// <summary>
/// Creates a new session with the specified name.
Expand Down
18 changes: 9 additions & 9 deletions src/ui/ManagementPortal/js/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,15 @@ export default {
);
},

async deleteRoleAssignment(roleAssignmentId): void {
return await this.fetch(
`/instances/${this.instanceId}/providers/FoundationaLLM.Authorization/roleAssignments/${roleAssignmentId}?api-version=${this.apiVersion}`,
{
method: 'DELETE',
},
);
},

/*
Role Definitions
*/
Expand All @@ -705,15 +714,6 @@ export default {
)) as RoleAssignment[];
},

async deleteRoleAssignment(roleAssignmentId): void {
return await this.fetch(
`/instances/${this.instanceId}/providers/FoundationaLLM.Authorization/roleDefinitions/${roleAssignmentId}?api-version=${this.apiVersion}`,
{
method: 'DELETE',
},
);
},

/*
Users
*/
Expand Down
19 changes: 17 additions & 2 deletions src/ui/ManagementPortal/pages/security/role-assignments/create.vue
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,20 @@
<!-- Type -->
<div id="aria-principal-type" class="mb-2">Principal Type:</div>
<div style="display: flex; gap: 16px">
<InputText
<!-- <InputText
v-model="principal.object_type"
readonly
placeholder="Browse for selection"
type="text"
class="w-50"
aria-labelledby="aria-principal-type"
/> -->
<Dropdown
v-model="principal.object_type"
:options="principalTypeOptions"
placeholder="--Select--"
class="mb-2 w-100"
aria-labelledby="aria-principal-type"
/>
</div>

Expand Down Expand Up @@ -100,7 +107,6 @@
<div style="display: flex; gap: 16px">
<InputText
v-model="roleAssignment.principal_id"
readonly
placeholder="Browse for selection"
type="text"
class="w-50"
Expand Down Expand Up @@ -384,6 +390,14 @@ export default {
errors.push('Please specify a role.');
}
if (!this.roleAssignment.principal_type) {
if (this.principal.object_type) {
this.roleAssignment.principal_type = this.principal.object_type;
} else {
errors.push('Please specify a principal type.');
}
}
if (errors.length > 0) {
throw errors.join('\n');
}
Expand All @@ -392,6 +406,7 @@ export default {
let successMessage = null as null | string;
try {
this.loadingStatusText = 'Saving role assignment...';
await api.createRoleAssignment({
...this.roleAssignment,
name: uuidv4(),
Expand Down
19 changes: 2 additions & 17 deletions tests/dotnet/Core.Client.Tests/CoreClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public async Task CreateChatSessionAsync_WithName_CreatesAndRenamesSession()
}

[Fact]
public async Task RateMessageAsync_ShouldReturnMessage_WhenSuccessful()
public async Task RateMessageAsync_ShouldSucceed()
{
// Arrange
var sessionId = "test-session-id";
Expand All @@ -47,26 +47,11 @@ public async Task RateMessageAsync_ShouldReturnMessage_WhenSuccessful()
Rating = true,
Comments = "Great response!"
};
var expectedMessage = new Message
{
Id = messageId,
RatingComments = "This is a test message.",
Rating = ratingRequest.Rating
};

_coreRestClient.Sessions
.RateMessageAsync(sessionId, messageId, ratingRequest)
.Returns(Task.FromResult(expectedMessage));

// Act
var result = await _coreClient.RateMessageAsync(sessionId, messageId, ratingRequest);
await _coreClient.RateMessageAsync(sessionId, messageId, ratingRequest);

// Assert
Assert.NotNull(result);
Assert.Equal(expectedMessage.Id, result.Id);
Assert.Equal(expectedMessage.RatingComments, result.RatingComments);
Assert.Equal(expectedMessage.Rating, result.Rating);

await _coreRestClient.Sessions.Received(1).RateMessageAsync(sessionId, messageId, ratingRequest);
}

Expand Down

0 comments on commit fbd2611

Please sign in to comment.