Skip to content

Commit

Permalink
Add AI communication interface
Browse files Browse the repository at this point in the history
Add AI interaction capability to the web interface.

* **AgentDocs/AssistantFileSearch/Program.cs**
  - Add `HandleAIInteraction` method to handle AI interactions via a web interface.
  - Integrate the method with the existing logic and use `OpenAIAssistantAgent` for AI communication.

* **app.py**
  - Add an endpoint `/ai-interact` to interact with the AI using the method from `AgentDocs/AssistantFileSearch/Program.cs`.
  - Ensure the endpoint handles POST requests and returns AI responses in JSON format.

* **config.json**
  - Add configuration settings for AI interaction, such as API keys and endpoints.
  • Loading branch information
Bryan-Roe committed Dec 22, 2024
1 parent 8c7d80d commit fddbafd
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 2 deletions.
95 changes: 95 additions & 0 deletions AgentDocs/AssistantFileSearch/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,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))
]);
}
}
}
14 changes: 12 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@

from fastapi import FastAPI
from fastapi import FastAPI, Request
from pydantic import BaseModel
import asyncio
import AgentDocs.AssistantFileSearch.Program as assistant

app = FastAPI()

class AIRequest(BaseModel):
user_input: str

@app.get("/")
def greet_json():
return {"Hello": "World!"}

@app.post("/ai-interact")
async def ai_interact(request: Request, ai_request: AIRequest):
user_input = ai_request.user_input
response = await assistant.HandleAIInteraction(user_input)
return {"response": response}
11 changes: 11 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,16 @@
"dataset": "your_parquet_export_dataset_here",
"hfToken": "your_parquet_export_hf_token_here",
"secret": "your_parquet_export_secret_here"
},
"aiInteraction": {
"openaiApiKey": "your_openai_api_key_here",
"azureOpenaiEndpoint": "your_azure_openai_endpoint_here",
"azureOpenaiApiKey": "your_azure_openai_api_key_here",
"azureOpenaiChatModelDeployment": "your_azure_openai_chat_model_deployment_here",
"azureBlobStorageEndpoint": "your_azure_blob_storage_endpoint_here",
"azureCognitiveServicesEndpoint": "your_azure_cognitive_services_endpoint_here",
"cosmosDbEndpoint": "your_cosmos_db_endpoint_here",
"keyVaultEndpoint": "your_key_vault_endpoint_here",
"azureDevOpsOrganizationUrl": "your_azure_devops_organization_url_here"
}
}

0 comments on commit fddbafd

Please sign in to comment.