Skip to content

Commit c7af517

Browse files
committed
Update to tool scenario.
1 parent be90516 commit c7af517

File tree

11 files changed

+523
-312
lines changed

11 files changed

+523
-312
lines changed
Lines changed: 30 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
using System.IO;
5-
using System.Net.Http.Headers;
6-
using System.Net;
74
using Amazon.BedrockRuntime;
85
using Amazon.BedrockRuntime.Model;
96
using Microsoft.Extensions.Logging;
@@ -29,119 +26,50 @@ public BedrockActionsWrapper(IAmazonBedrockRuntime bedrockClient, ILogger<Bedroc
2926
_logger = logger;
3027
}
3128

29+
3230
/// <summary>
3331
/// Sends a Converse request to the Amazon Bedrock Converse API.
3432
/// </summary>
35-
/// <param name="systemContent">The system content for the Converse request.</param>
36-
/// <param name="documentContent">The document content for the Converse request.</param>
37-
/// <param name="inferenceConfiguration">The inference configuration for the Converse request.</param>
38-
/// <param name="toolSpecification">The tool specification for the Converse request.</param>
39-
/// <returns>True if the Converse request was successful, false otherwise.</returns>
40-
public async Task<bool> SendConverseRequestAsync(string systemContent, string documentContent, InferenceConfiguration inferenceConfiguration, ToolSpecification toolSpecification)
41-
{
42-
try
43-
{
44-
var converseRequest = new ConverseRequest
45-
{
46-
SystemContent = systemContent,
47-
DocumentContent = documentContent,
48-
InferenceConfiguration = inferenceConfiguration,
49-
ToolSpecification = toolSpecification
50-
};
51-
52-
var response = await _bedrockClient.ConverseAsync(converseRequest);
53-
54-
if (response.IsSuccessful)
55-
{
56-
_logger.LogInformation("Converse request was successful.");
57-
return true;
58-
}
59-
else
60-
{
61-
_logger.LogError($"Converse request failed with error: {response.Error.Message}");
62-
return false;
63-
}
64-
}
65-
catch (AmazonBedrockRuntimeException ex)
66-
{
67-
_logger.LogError(ex, "Error occurred while sending Converse request.");
68-
return false;
69-
}
70-
}
71-
72-
public async Task<bool> ProcessMessages()
33+
/// <param name="modelId">The Bedrock Model Id.</param>
34+
/// <param name="systemPrompt">A system prompt instruction.</param>
35+
/// <param name="conversation">The array of messages in the conversation.</param>
36+
/// <param name="toolSpec">The specification for a tool.</param>
37+
/// <returns>The response of the model.</returns>
38+
public async Task<ConverseResponse> SendConverseRequestAsync(string modelId, string systemPrompt, List<Message> conversation, ToolSpecification toolSpec)
7339
{
7440
try
7541
{
76-
List<string> messages = new List<string>();
77-
// todo: populate messages list.
78-
foreach (var messageThread in messages)
42+
var request = new ConverseRequest()
7943
{
80-
// create a repo object
81-
var repoDetails = new RepoDetails()
82-
{
83-
Name = repository.Name,
84-
Language = repository.Language,
85-
Url = repository.HtmlUrl,
86-
LastModified = repository.UpdatedAt.Date,
87-
Summary = repository.Description,
88-
OpenIssuesCount = repository.OpenIssuesCount
89-
};
90-
foreach (var repoCriteria in searchConfig.RepoCriteria)
44+
ModelId = modelId,
45+
System = new List<SystemContentBlock>()
46+
{
47+
new SystemContentBlock()
9148
{
92-
var value = repository.GetType().GetProperty(repoCriteria.DataField).GetValue(repository);
93-
if (value is int)
94-
{
95-
repoDetails.AddCriterion(repoCriteria.Name,
96-
repoCriteria.Description, repoCriteria.Weight, (int)value);
97-
}
49+
Text = systemPrompt
9850
}
99-
100-
// Try to get the content of the README file, to be used to generate tool results with Bedrock.
101-
Console.WriteLine($"Fetching README contents for repository {repoDetails.Name}.");
102-
try
51+
},
52+
Messages = conversation,
53+
ToolConfig = new ToolConfiguration()
54+
{
55+
Tools = new List<Tool>()
10356
{
104-
var readme =
105-
await client.Repository.Content.GetReadme(repository.Id);
106-
var readmeContent = readme.Content;
107-
108-
byte[] byteArray =
109-
System.Text.Encoding.ASCII.GetBytes(readmeContent);
110-
MemoryStream stream = new MemoryStream(byteArray);
111-
Thread.Sleep(200);
112-
var summaryResponseTask =
113-
bedrockService.MakeRepoToolRequest(stream, repoDetails, searchConfig);
114-
taskList.Add(summaryResponseTask);
115-
116-
var lastcommit =
117-
await client.Repository.Commit.Get(repository.Id, "HEAD");
118-
119-
Console.WriteLine($"last commit for {repository.Name} at {lastcommit.Commit.Committer.Date.Date}");
120-
repoDetails.LastModified = lastcommit.Commit.Committer.Date.Date;
121-
if (repoDetails.LastModified >
122-
DateTime.Today.AddYears(-yearsToInclude))
123-
{
124-
repoDetailsList.Add(repoDetails);
125-
}
126-
else
57+
new Tool()
12758
{
128-
noNewCommits++;
59+
ToolSpec = toolSpec
12960
}
13061
}
131-
catch (Exception ex)
132-
{
133-
noReadmeCount++;
134-
Console.WriteLine(
135-
$"unable to get readme for {repoDetails.Name}, {ex.Message}");
136-
}
13762
}
138-
else
139-
{
140-
notSdkLanguageCount++;
141-
}
142-
}
63+
};
14364

144-
var summaryResults = await Task.WhenAll(taskList);
145-
Console.WriteLine($"AI results returned true: {summaryResults.Count(s => s)}.");
65+
var response = await _bedrockClient.ConverseAsync(request);
66+
67+
return response;
14668
}
69+
catch (AmazonBedrockRuntimeException ex)
70+
{
71+
_logger.LogError(ex, "Error occurred while sending Converse request.");
72+
throw;
73+
}
74+
}
14775
}

0 commit comments

Comments
 (0)