Search code examples
c#azureazure-openaiopenai-assistants-api

How to get the Azure assistant to use the vector store using SDK


Seeking help getting the assistant to use the vector store using the library below:

Azure OpenAI: OpenAI Assistants client library for .NET - version 1.0.0-beta.4

For some context:

I've created a Vector store as well as an Assistant within Azure AI Foundry -> Azure OpenAI Service

Using the SDK (link above) and the assistantID I'm trying to run a thread but it's unable to do the file search.

How do I hook up the vector store?

Code

Also tried linking files to the assistant, but it has a limitation of up to 20 files.


Solution

  • I've only done this by creating the vector store in code as well as it is easier and less code but alright, lets go:

    var fileClient = client.GetOpenAIFileClient();
    var vectorStoreClient = client.GetVectorStoreClient();
    var assistantClient = client.GetAssistantClient();
    
    var stores = vectorStoreClient.GetVectorStores();
    var store = stores.First(s => s.Name == "AssistantVectorStore_28651");
    
    var uploadResult = await fileClient.UploadFileAsync(@"..\deploy\assets\go-from-the-beginning.pdf", FileUploadPurpose.Assistants);
    await vectorStoreClient.AddFileToVectorStoreAsync(store.Id, uploadResult.Value.Id, true);
    
    var assistant = await assistantClient.CreateAssistantAsync(
        azureOpenAIModelId,
        new AssistantCreationOptions()
        {
            Tools = { new FileSearchToolDefinition() },
            ToolResources = new()
            {
                FileSearch = new()
                {
                    VectorStoreIds = { store.Id }
                }
            }
        }
    );
    

    Some points of interest: when you create an assistant vector store the Azure AI Foundry portal shows you the name, like AssistantVectorStore_28651 but not the Id. But, we need to get the Id to reference the store. That is why we need this:

    var stores = vectorStoreClient.GetVectorStores();
    var store = stores.First(s => s.Name == "AssistantVectorStore_28651");
    

    Then you need to upload the file using a OpenAIFileClient and add that file to the vectorstore:

    var uploadResult = await fileClient.UploadFileAsync(@"..\deploy\assets\go-from-the-beginning.pdf", FileUploadPurpose.Assistants);
    await vectorStoreClient.AddFileToVectorStoreAsync(store.Id, uploadResult.Value.Id, true);
    

    Then finally create the assistant with the proper tool setup. Then you can ask questions about the document.

    But, like I said, it is easier to create the vector store in code:

    var fileClient = client.GetOpenAIFileClient();
    var assistantClient = client.GetAssistantClient();
    
    var uploadResult = await fileClient.UploadFileAsync(@"..\deploy\assets\go-from-the-beginning.pdf", FileUploadPurpose.Assistants);
    
    var assistant = await assistantClient.CreateAssistantAsync(
        azureOpenAIModelId,
        new AssistantCreationOptions()
        {
            Tools = { new FileSearchToolDefinition() },
            ToolResources = new()
            {
                FileSearch = new()
                {
                    NewVectorStores = { new VectorStoreCreationHelper([uploadResult.Value.Id]) }
                }
            }
        }
    );
    

    A working demo can be found in this repo. I've tested the code above based on that notebook.

    Do note, I've used this NuGet package for all the code above: https://www.nuget.org/packages/Azure.AI.OpenAI/2.1.0-beta.2. For all I know this package supersedes the one you are using.