Skip to content

Commit

Permalink
Merge pull request #654 from Bryan-Roe-ai/Bryan-Roe/add-ai-communication
Browse files Browse the repository at this point in the history
Bryan roe/add ai communication
  • Loading branch information
Bryan-Roe authored Dec 26, 2024
2 parents 7179c4e + 273bd23 commit 618d434
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
}
},
"tasks": {
"test": "dotnet test --no-build --verbosity normal && python -m pytest && ./mvnw -B -Pbug-check -Pcompile-jdk17 test --file java/pom.xml && npm test",
"build": "dotnet restore && dotnet build --no-restore && pip install -r requirements.txt && ./mvnw -B package --file java/pom.xml && npm install && npm run build",
"build": "npm install && mvn install && dotnet restore && dotnet build && pip install -r requirements.txt && docker build -t my-image .",
"test": "1. dotnet test --no-build --verbosity normal\n2. cd python && poetry run pytest\n3. cd java && ./mvnw -B test --file pom.xml\n4. npm test",
"launch": "npm start"
Expand Down
15 changes: 15 additions & 0 deletions .env.local.template
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,18 @@ PUBLIC_APP_DATA_DISCLAIMER=#set to 1 to enable disclaimers about model outputs
API_KEY=#your_api_key_here
DATABASE_URL=#your_database_url_here
SECRET_KEY=#your_secret_key_here

# OpenAI settings
OpenAISettings:ApiKey=#your_openai_api_key_here
OpenAISettings:ChatModel=#your_openai_chat_model_here

# Azure OpenAI settings
AzureOpenAISettings:Endpoint=#your_azure_openai_endpoint_here
AzureOpenAISettings:ChatModelDeployment=#your_azure_openai_chat_model_deployment_here

# Additional settings
AZURE_BLOB_STORAGE_ENDPOINT=#your_azure_blob_storage_endpoint_here
AZURE_COGNITIVE_SERVICES_ENDPOINT=#your_azure_cognitive_services_endpoint_here
COSMOS_DB_ENDPOINT=#your_cosmos_db_endpoint_here
KEY_VAULT_ENDPOINT=#your_key_vault_endpoint_here
AZURE_DEVOPS_ORGANIZATION_URL=#your_azure_devops_organization_url_here
95 changes: 95 additions & 0 deletions AgentDocs/AssistantCodeInterpreter/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,99 @@ private static void HandleMergeRequestComments()
{
// Implement logic to process comments for optimization or merge
}

// Method to handle AI interactions via a web interface
public static async Task<string> HandleAIInteraction(string userInput)
{
// Load configuration from environment variables or user secrets.
Settings settings = new();

OpenAIClientProvider clientProvider =
OpenAIClientProvider.ForAzureOpenAI(
new AzureCliCredential(),
new Uri(settings.AzureOpenAI.Endpoint));

Console.WriteLine("Creating store...");
VectorStoreClient storeClient = clientProvider.Client.GetVectorStoreClient();
VectorStore store = await storeClient.CreateVectorStoreAsync();

// Retain file references.
Dictionary<string, OpenAIFileInfo> fileReferences = [];

Console.WriteLine("Uploading files...");
FileClient fileClient = clientProvider.Client.GetFileClient();
foreach (string fileName in _fileNames)
{
OpenAIFileInfo fileInfo = await fileClient.UploadFileAsync(fileName, FileUploadPurpose.Assistants);
await storeClient.AddFileToVectorStoreAsync(store.Id, fileInfo.Id);
fileReferences.Add(fileInfo.Id, fileInfo);
}

Console.WriteLine("Defining agent...");
OpenAIAssistantAgent agent =
await OpenAIAssistantAgent.CreateAsync(
clientProvider,
new OpenAIAssistantDefinition(settings.AzureOpenAI.ChatModelDeployment)
{
Name = "SampleAssistantAgent",
Instructions =
"""
The document store contains the text of fictional stories.
Always analyze the document store to provide an answer to the user's question.
Never rely on your knowledge of stories not included in the document store.
Always format response using markdown.
""",
EnableFileSearch = true,
VectorStoreId = store.Id,
},
new Kernel());

Console.WriteLine("Creating thread...");
string threadId = await agent.CreateThreadAsync();

Console.WriteLine("Ready!");

try
{
await agent.AddChatMessageAsync(threadId, new ChatMessageContent(AuthorRole.User, userInput));
Console.WriteLine();

List<StreamingAnnotationContent> footnotes = [];
string response = string.Empty;
await foreach (StreamingChatMessageContent chunk in agent.InvokeStreamingAsync(threadId))
{
// Capture annotations for footnotes
footnotes.AddRange(chunk.Items.OfType<StreamingAnnotationContent>());

// Append chunk content to response
response += chunk.Content.ReplaceUnicodeBrackets();
}

Console.WriteLine();

// Render footnotes for captured annotations.
if (footnotes.Count > 0)
{
response += "\n\n";
foreach (StreamingAnnotationContent footnote in footnotes)
{
response += $"#{footnote.Quote.ReplaceUnicodeBrackets()} - {fileReferences[footnote.FileId!].Filename} (Index: {footnote.StartIndex} - {footnote.EndIndex})\n";
}
}

return response;
}
finally
{
Console.WriteLine();
Console.WriteLine("Cleaning-up...");
await Task.WhenAll(
[
agent.DeleteThreadAsync(threadId),
agent.DeleteAsync(),
storeClient.DeleteVectorStoreAsync(store.Id),
..fileReferences.Select(fileReference => fileClient.DeleteFileAsync(fileReference.Key))
]);
}
}
}
9 changes: 9 additions & 0 deletions AgentDocs/Common/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class Settings
private CosmosDBSettings cosmosDB;
private KeyVaultSettings keyVault;
private AzureDevOpsSettings azureDevOps;
private GeneralSettings general;

public AzureOpenAISettings AzureOpenAI => this.azureOpenAI ??= this.GetSettings<Settings.AzureOpenAISettings>();
public OpenAISettings OpenAI => this.openAI ??= this.GetSettings<Settings.OpenAISettings>();
Expand All @@ -28,6 +29,7 @@ public class Settings
public CosmosDBSettings CosmosDB => this.cosmosDB ??= this.GetSettings<Settings.CosmosDBSettings>();
public KeyVaultSettings KeyVault => this.keyVault ??= this.GetSettings<Settings.KeyVaultSettings>();
public AzureDevOpsSettings AzureDevOps => this.azureDevOps ??= this.GetSettings<Settings.AzureDevOpsSettings>();
public GeneralSettings General => this.general ??= this.GetSettings<Settings.GeneralSettings>();

public class OpenAISettings
{
Expand Down Expand Up @@ -79,6 +81,13 @@ public class AzureDevOpsSettings
public string OrganizationUrl { get; set; } = string.Empty;
}

public class GeneralSettings
{
public string ApiKey { get; set; } = string.Empty;
public string DatabaseUrl { get; set; } = string.Empty;
public string SecretKey { get; set; } = string.Empty;
}

private TSettings GetSettings<TSettings>() =>
this.configRoot.GetRequiredSection(typeof(TSettings).Name).Get<TSettings>()!;

Expand Down
15 changes: 15 additions & 0 deletions semantic-kernel/.env.local.template
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,18 @@ PUBLIC_APP_DATA_DISCLAIMER=#set to 1 to enable disclaimers about model outputs
API_KEY=#your_api_key_here
DATABASE_URL=#your_database_url_here
SECRET_KEY=#your_secret_key_here

# OpenAI settings
OpenAISettings:ApiKey=#your_openai_api_key_here
OpenAISettings:ChatModel=#your_openai_chat_model_here

# Azure OpenAI settings
AzureOpenAISettings:Endpoint=#your_azure_openai_endpoint_here
AzureOpenAISettings:ChatModelDeployment=#your_azure_openai_chat_model_deployment_here

# Additional settings
AZURE_BLOB_STORAGE_ENDPOINT=#your_azure_blob_storage_endpoint_here
AZURE_COGNITIVE_SERVICES_ENDPOINT=#your_azure_cognitive_services_endpoint_here
COSMOS_DB_ENDPOINT=#your_cosmos_db_endpoint_here
KEY_VAULT_ENDPOINT=#your_key_vault_endpoint_here
AZURE_DEVOPS_ORGANIZATION_URL=#your_azure_devops_organization_url_here

0 comments on commit 618d434

Please sign in to comment.