Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일

(시리즈 글이 6개 있습니다.)
.NET Framework: 2116. C# - OpenAI API 사용 - 지원 모델 목록
; https://www.sysnet.pe.kr/2/0/13344

닷넷: 2165. C# - Azure OpenAI API를 이용해 ChatGPT처럼 동작하는 콘솔 응용 프로그램 제작
; https://www.sysnet.pe.kr/2/0/13451

닷넷: 2166. C# - Azure OpenAI API를 이용해 사용자가 제공하는 정보를 대상으로 검색하는 방법
; https://www.sysnet.pe.kr/2/0/13452

닷넷: 2167. C# - Qdrant Vector DB를 이용한 Embedding 벡터 값 보관/조회 (Azure OpenAI)
; https://www.sysnet.pe.kr/2/0/13454

닷넷: 2168. C# - Azure.AI.OpenAI 패키지로 OpenAI 사용
; https://www.sysnet.pe.kr/2/0/13455

닷넷: 2169. C# - OpenAI를 사용해 PDF 데이터를 대상으로 OpenAI 챗봇 작성
; https://www.sysnet.pe.kr/2/0/13456




C# - Qdrant Vector DB를 이용한 Embedding 벡터 값 보관/조회 (Azure OpenAI)

지난 글에서,

C# - Azure OpenAI API를 이용해 사용자가 제공하는 정보를 대상으로 검색하는 방법
; https://www.sysnet.pe.kr/2/0/13452

GitHub 이슈 데이터 정보를 벡터 변환 후 로컬 파일에 저장해 재사용을 했는데요, 사실 간단한 경우라면 몰라도 거의 이런 식으로 사용하는 경우는 없을 것입니다.

그보다는 DB를 활용하게 될 텐데요, 이번 글에서 소개하는 Qdrant가 바로 그런 벡터 데이터베이스 중의 하나입니다.

Qdrant
; https://youtu.be/xEFO1sQ2bUc?t=28371

그리고 .NET Conf 2023의 "Build Intelligent Apps with .NET and Azure" 동영상에서 이에 대한 사용법이 나옵니다. ^^ 역시 이번에도, 해당 강의 내용을 그대로 베껴 보겠습니다.




자, 그럼 지난 글에서 GitHub로부터 가져온 이슈 데이터를 Embedding 과정을 거쳐 벡터로 변환을 해 파일로 저장했는데요,

C# - Azure OpenAI API를 이용해 사용자가 제공하는 정보를 대상으로 검색하는 방법
; https://www.sysnet.pe.kr/2/0/13452

이번에는 Qdrant DB에 저장을 해보겠습니다. 이를 위해 docker로 qdrant 컨테이너를 하나 띄워 두시고,

docker run -p 6333:6333 -p 6334:6334 qdrant/qdrant

NuGet으로부터 Qdrant.Client를 참조 후 인스턴스를 생성합니다.

// Install-Package Azure.AI.OpenAI -Pre
// Install-Package Microsoft.DotNet.Interactive.AIUtilities -Pre
// Install-Package Qdrant.Client -Pre

string azureOpenAIKey = "...[azure openai key]..."; // 초기화 참고
string azureOpenAIEndpoint = "...[azure openai endpoint]...";
var embeddingDeployment = "my-embedding";

OpenAIClient openAIClient = new OpenAIClient(new System.Uri(azureOpenAIEndpoint), new AzureKeyCredential(azureOpenAIKey));

string qdrantHost = "localhost";
string collectionName = "github_issues";

QdrantClient qdrantClient = new QdrantClient(qdrantHost, 6334, false);

이후 동작은 지난 글에서 파일로 벡터 데이터를 저장했던 코드를 DB에 저장하게만 바꾸면 됩니다.

private static async Task EmbedAllIssuesAndSaveToDBAsync(
    QdrantClient qdrantClient, string collectionName, OpenAIClient openAIClient, string embeddingDeployment)
{
    GitHubIssue[]? issues = await LoadIssuesFromFileAsync("issues.json");
    if (issues == null)
    {
        Console.WriteLine("Failed to load issues.json");
        return;
    }

    var collections = await qdrantClient.ListCollectionsAsync();
    if (collections.Contains(collectionName))
    {
        // await qdrantClient.DeleteCollectionAsync(collectionName);
        return;
    }

    var issuesWithChunksColleciton =
        issues.Select(issue => new IssueWithChunks(issue, new()))
            .ToArray();

    var tokenizer = await Tokenizer.CreateAsync(TokenizerModel.ada2);

    foreach (var item in issuesWithChunksColleciton)
    {
        var fullText = item.Issue.Text;
        if (string.IsNullOrWhiteSpace(fullText))
        {
            continue;
        }

        var chunks = tokenizer.ChunkByTokenCountWithOverlap(fullText, 3000, 50)
            .Select(t =>
            $"""
            Title: {item.Issue.Title}

            {t}
            """).Chunk(16)
            .ToArray();

        foreach (var chunk in chunks)
        {
            var embeddingResponse = await openAIClient.GetEmbeddingsAsync(
                new EmbeddingsOptions(embeddingDeployment, chunk));

            item.Chunks.AddRange(
                embeddingResponse.Value.Data.Select(d =>
                new TextWithEmbedding(chunk[d.Index], d.Embedding.ToArray())));
        }
    }

    await qdrantClient.CreateCollectionAsync(collectionName,
        new VectorParams { Size = 1536, Distance = Distance.Cosine });

    var vectors = issuesWithChunksColleciton
        .Where(d => d.Chunks.Count > 0)
        .SelectMany(d =>
        d.Chunks.Select(c => new
        {
            Embedding = c.Embedding,
            Text = $"<issuesTitle>{d.Issue.Title}</issueTitle>\n<issueUrl>{d.Issue.Url}</issueUrl><issueContent>{d.Issue.Text}</issueContent>"
        }))
        .ToList();

    var points = vectors.Select(vector =>
    {
        var point = new PointStruct
        {
            Id = new PointId { Uuid = Guid.NewGuid().ToString() },
            Vectors = vector.Embedding,
            Payload =
            {
                ["text"] = vector.Text
            }
        };

        return point;
    }).ToList();

    await qdrantClient.UpsertAsync(collectionName, points);
}

이렇게 저장한 데이터를 다음과 같이 검색할 수 있습니다.

string question = "Are there any questions for mouse?";

string[] results = await SearchWithQdrantAsync(qdrantClient, collectionName,
    openAIClient, embeddingDeployment,
    question, 16);

results.All((text) =>
{
    Console.WriteLine(text);
    Console.WriteLine("-----------------------------------");
    return true;
});

Console.WriteLine($"Found: {results.Length}");

public static async Task<string[]> SearchWithQdrantAsync(
    QdrantClient qdrantClient, string collectionName,
    OpenAIClient openAIClient, string embeddingDeployment,
    string query, int resultLimit = 1)
{
    var embeddingResponse = await openAIClient.GetEmbeddingsAsync(
                    new EmbeddingsOptions(embeddingDeployment, new[] { query }));

    var embeddingVector = embeddingResponse.Value.Data[0].Embedding.ToArray();

    var results = await qdrantClient.SearchAsync(collectionName, embeddingVector, limit: (ulong)resultLimit);
    return results.Select(r => r.Payload["text"].StringValue).ToArray();
}

참고로, 이것 역시 자연어 검색을 하는 것은 아닙니다. DB를 생성하는 시점의 CreateCollectionAsync 코드를 보면 Distance를 Cosine 옵션으로 주고 있는 것을 볼 수 있는데요, 그러니까 이것도 역시 지난번에 설명한 유사도에 따른 검색에 해당합니다.

어쨌든, 이것으로 .NET Conf 2023에 있었던 "Build Intelligent Apps with .NET and Azure" 내용은 모두 정리했습니다. 해당 동영상의 마지막에는 다음과 같은 학습 자료를 공유하고 있으니 참고하세요. ^^

AI in .NET Collection
; https://aka.ms/ai-dotnet-learn
; https://learn.microsoft.com/en-us/collections/1n31t57k7k6r85

(첨부 파일은 이 글의 예제 코드를 포함합니다.)




그나저나, OpenAI의 ChatGPT는 어떻게 해서 자연어 검색을 할 수 있는 걸까요? 아직 저도 완벽하게 이해하는 것은 아니지만, 대충 어떤 식일지는 짐작이 가는 듯합니다.

가령, 사용자가 질문을 하면, 그에 해당하는 키워드로 기존에 저장해 두었던 스토리지로부터 Vector 검색을 해 적당한 문서를 선별할 것입니다. 그런 다음, 그 문서를 "대화의 문맥"에 저장해 두고, 사용자의 질문을 그 문맥 내에서 다시 수행해 이후 적절한 문장으로 Completion 엔진을 통해 대답하는 식일 것입니다.

따라서, 우리가 가진 별도의 Knowledge base 자료가 있다면 그것을 Storage (VectorDB)에 저장한 후, 사용자가 질의를 하면 그것과 유사도가 높은 문서들을 VectorDB에서 검색한 다음 그 원본 문자열을 담은 문서를 다시 OpenAI API에 "질문"과 함께 전달해 ChatCompletion을 거치면 되는 식일 것입니다.




참고로, 왜 마이크로소프트는 OpenAI 서비스가 있는데, 그걸 굳이 Azure에 올려 Azure OpenAI로 따로 서비스를 하고 있는 걸까요? 사용자 입장에서 Azure OpenAI를 선택하면 어떤 장점이 있을지 궁금하지 않나요? ^^

지금까지의 코드를 보면, 질문뿐만 아니라 GitHub Issue 데이터를 Embedding하기 위해 OpenAI 측에 데이터를 전달해야만 했는데요, 사실 이런 과정이 보안을 중시하는 "기업" 입장에서는 매우 불편할 수가 있습니다. 실제로 얼마 전 삼성 전자가 사내에서 ChatGPT 사용을 금지한 이유가 그것 때문이었습니다.

'챗GPT 사내금지' 삼성전자, 직원업무 도울 자체 AI 도구 만든다
; https://www.yna.co.kr/view/AKR20230502125400003

이런 문제를 Azure OpenAI가 해결하는데요, 다음의 문서에서 이를 찾아볼 수 있습니다.

Data, privacy, and security for Azure OpenAI Service
; https://learn.microsoft.com/en-us/legal/cognitive-services/openai/data-privacy

Your prompts (inputs) and completions (outputs), your embeddings, and your training data:

are NOT available to other customers.
are NOT available to OpenAI.
are NOT used to improve OpenAI models.
are NOT used to improve any Microsoft or 3rd party products or services.
are NOT used for automatically improving Azure OpenAI models for your use in your resource (The models are stateless, unless you explicitly fine-tune models with your training data).
Your fine-tuned Azure OpenAI models are available exclusively for your use.


만약, Azure OpenAI의 비용이 부담스럽다면, 차선책으로 무료 LLM 모델인 LLaMA(라마)를 이용해 구축하는 방안이 있습니다.




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 11/23/2023]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 



2024-03-15 09시49분
정성태

1  2  3  4  5  6  7  8  9  10  11  12  13  14  [15]  ...
NoWriterDateCnt.TitleFile(s)
13266정성태2/20/20234666오류 유형: 848. .NET Core/5+ - Process terminated. Couldn't find a valid ICU package installed on the system
13265정성태2/18/20234574.NET Framework: 2096. .NET Core/5+ - PublishSingleFile 유형에 대한 runtimeconfig.json 설정
13264정성태2/17/20236141스크립트: 45. 파이썬 - uvicorn 사용자 정의 Logger 작성
13263정성태2/16/20234300개발 환경 구성: 666. 최신 버전의 ilasm.exe/ildasm.exe 사용하는 방법
13262정성태2/15/20235341디버깅 기술: 191. dnSpy를 이용한 (소스 코드가 없는) 닷넷 응용 프로그램 디버깅 방법 [1]
13261정성태2/15/20234680Windows: 224. Visual Studio - 영문 폰트가 Fullwidth Latin Character로 바뀌는 문제
13260정성태2/14/20234477오류 유형: 847. ilasm.exe 컴파일 오류 - error : syntax error at token '-' in ... -inf
13259정성태2/14/20234598.NET Framework: 2095. C# - .NET5부터 도입된 CollectionsMarshal
13258정성태2/13/20234463오류 유형: 846. .NET Framework 4.8 Developer Pack 설치 실패 - 0x81f40001
13257정성태2/13/20234554.NET Framework: 2094. C# - Job에 Process 포함하는 방법 [1]파일 다운로드1
13256정성태2/10/20235316개발 환경 구성: 665. WSL 2의 네트워크 통신 방법 - 두 번째 이야기
13255정성태2/10/20234703오류 유형: 845. gihub - windows2022 이미지에서 .NET Framework 4.5.2 미만의 프로젝트에 대한 빌드 오류
13254정성태2/10/20234605Windows: 223. (WMI 쿼리를 위한) PowerShell 문자열 escape 처리
13253정성태2/9/20235382Windows: 222. C# - 다른 윈도우 프로그램이 실행되었음을 인식하는 방법파일 다운로드1
13252정성태2/9/20234227오류 유형: 844. ssh로 명령어 수행 시 멈춤 현상
13251정성태2/8/20234643스크립트: 44. 파이썬의 3가지 스레드 ID
13250정성태2/8/20236506오류 유형: 843. System.InvalidOperationException - Unable to configure HTTPS endpoint
13249정성태2/7/20235320오류 유형: 842. 리눅스 - You must wait longer to change your password
13248정성태2/7/20234279오류 유형: 841. 리눅스 - [사용자 계정] is not in the sudoers file. This incident will be reported.
13247정성태2/7/20235177VS.NET IDE: 180. Visual Studio - 닷넷 소스 코드 디버깅 중 "Decompile source code"가 동작하는 않는 문제
13246정성태2/6/20234354개발 환경 구성: 664. Hyper-V에 설치한 리눅스 VM의 VHD 크기 늘리는 방법 - 두 번째 이야기
13245정성태2/6/20234924.NET Framework: 2093. C# - PEM 파일을 이용한 RSA 개인키/공개키 설정 방법파일 다운로드1
13244정성태2/5/20234313VS.NET IDE: 179. Visual Studio - External Tools에 Shell 내장 명령어 등록
13243정성태2/5/20235134디버깅 기술: 190. windbg - Win32 API 호출 시점에 BP 거는 방법 [1]
13242정성태2/4/20234591디버깅 기술: 189. ASP.NET Web Application (.NET Framework) 프로젝트의 숨겨진 예외 - System.UnauthorizedAccessException
13241정성태2/3/20234032디버깅 기술: 188. ASP.NET Web Application (.NET Framework) 프로젝트의 숨겨진 예외 - System.IO.FileNotFoundException
1  2  3  4  5  6  7  8  9  10  11  12  13  14  [15]  ...