The assistants documentation has the code to link to a vector database in python, is it possible to be implemented in C#.

Saket Arora 0 Reputation points
2024-06-30T13:40:35.9033333+00:00

I want to implement the following functionality in C#, Is it possible to achieve that?

from openai import AzureOpenAI client = AzureOpenAI( api_key=os.getenv("AZURE_OPENAI_API_KEY"), api_version="2024-05-01-preview", azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT") ) # Create a vector store called "Financial Statements" vector_store = client.beta.vector_stores.create(name="Financial Statements") # Ready the files for upload to OpenAI file_paths = ["mydirectory/myfile1.pdf", "mydirectory/myfile2.txt"] file_streams = [open(path, "rb") for path in file_paths] # Use the upload and poll SDK helper to upload the files, add them to the vector store, # and poll the status of the file batch for completion. file_batch = client.beta.vector_stores.file_batches.upload_and_poll( vector_store_id=vector_store.id, files=file_streams ) # You can print the status and the file counts of the batch to see the result of this operation. print(file_batch.status) print(file_batch.file_counts)

Azure OpenAI Service
Azure OpenAI Service
An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
2,529 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. YutongTie-MSFT 47,991 Reputation points
    2024-06-30T21:50:37.12+00:00

    Hello @Saket Arora

    Thanks for reaching out to us, I think you are looking for this document - https://video2.skills-academy.com/en-us/dotnet/api/overview/azure/ai.openai.assistants-readme?view=azure-dotnet-preview

    You can follow above document to implement your code but please be aware that this is only the beta version.

    A quick sample for your is here, please adjust it according to your need -

    using Azure;
    using Azure.AI.OpenAI;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Threading.Tasks;
    class Program
    {
        static async Task Main(string[] args)
        {
            string apiKey = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY");
            string endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT");
            var client = new OpenAIClient(new OpenAIClientOptions
            {
                ApiKey = new AzureKeyCredential(apiKey),
                Endpoint = endpoint
            });
            // Create a vector store
            string vectorStoreName = "Financial Statements";
            VectorStore vectorStore = await client.VectorStores.CreateAsync(vectorStoreName);
            Console.WriteLine($"Created vector store with ID: {vectorStore.Id}");
            // Prepare files for upload
            string[] filePaths = new string[] { "mydirectory/myfile1.pdf", "mydirectory/myfile2.txt" };
            List<Stream> fileStreams = new List<Stream>();
            foreach (var filePath in filePaths)
            {
                FileStream fileStream = new FileStream(filePath, FileMode.Open);
                fileStreams.Add(fileStream);
            }
            // Upload files and poll for completion
            FileBatch fileBatch = await client.VectorStores.UploadAndPollFileBatchAsync(
                vectorStoreId: vectorStore.Id,
                files: fileStreams);
            // Output status and file counts
            Console.WriteLine($"File batch status: {fileBatch.Status}");
            Console.WriteLine($"File counts: {fileBatch.FileCounts}");
            // Dispose file streams
            foreach (var fileStream in fileStreams)
            {
                fileStream.Dispose();
            }
        }
    }
    
    
    

    More samples from official doc for your reference - https://video2.skills-academy.com/en-us/dotnet/api/overview/azure/ai.openai.assistants-readme?view=azure-dotnet-preview#usage

    I hope this helps!

    Regards,

    Yutong

    -Please kindly accept the answer if you feel helpful to support the community, thanks a lot.