성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] Java - How to use the Foreign Funct...
[정성태] 제가 큰 실수를 했군요. ^^; Delegate를 통한 Bein...
[정성태] Working with Rust Libraries from C#...
[정성태] Detecting blocking calls using asyn...
[정성태] 아쉽게도, 커뮤니티는 아니고 개인 블로그입니다. ^^
[정성태] 질문이 잘 이해가 안 됩니다. 우선, 해당 소스코드에서 ILis...
[양승조
] var대신 dinamic으로 선언해서 해결은 했습니다. 맞는 해...
[양승조
] 또 막혔습니다. ㅠㅠ var list = props[i].Ge...
[양승조
] 아. 감사합니다. 어제는 안됐던것 같은데....정신을 차려야겠네...
[정성태] "props[i].GetValue(props[i])" 코드에서 ...
글쓰기
제목
이름
암호
전자우편
HTML
홈페이지
유형
제니퍼 .NET
닷넷
COM 개체 관련
스크립트
VC++
VS.NET IDE
Windows
Team Foundation Server
디버깅 기술
오류 유형
개발 환경 구성
웹
기타
Linux
Java
DDK
Math
Phone
Graphics
사물인터넷
부모글 보이기/감추기
내용
<div style='display: inline'> <h1 style='font-family: Malgun Gothic, Consolas; font-size: 20pt; color: #006699; text-align: center; font-weight: bold'>C# - OpenAI를 사용해 PDF 데이터를 대상으로 OpenAI 챗봇 작성</h1> <p> 이번 글도 ^^ .NET Conf 2023에 있었던 동영상을 그대로 베끼겠습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Build an Azure OpenAI powered .NET 8 Chat Bot on your data from scratch | .NET Conf 2023 ; <a target='tab' href='https://youtu.be/fYJuokUnucE'>https://youtu.be/fYJuokUnucE</a> </pre> <br /> <hr style='width: 50%' /><br /> <br /> 지난 글에서,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > C# - Azure OpenAI API를 이용해 사용자가 제공하는 정보를 대상으로 검색하는 방법 ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/13452'>https://www.sysnet.pe.kr/2/0/13452</a> C# - Qdrant Vector DB를 이용한 Embedding 벡터 값 보관/조회 (Azure OpenAI) ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/13454'>https://www.sysnet.pe.kr/2/0/13454</a> </pre> <br /> 대략 다음과 같은 처리 순서를 설명했습니다.<br /> <br /> <ol> <li>사용자가 제공하는 문서를 embedding 시켜 벡터로 보관 (대개의 경우 DB에 보관)</li> <li>사용자 입력한 쿼리를 embedding 시키고, 1번 과정에서 저장한 것과 비교해 적절한 문서를 선택(혹은 DB로부터 조회)</li> <li>조회한 문서와 함께 사용자가 입력한 쿼리를 ChatGPT에 전달</li> </ol> <br /> 저 의미에서 보면, PDF 역시 사용자가 제공하는 문서에 불과하므로, 1번 과정을 거쳐 embedding 시켜 벡터로 보관해 두는 작업을 거쳐야 합니다. 자, 그럼 당연히 PDF 문서를 읽는 라이브러리가 필요하겠죠. ^^<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Install-Package itext7 </pre> <br /> 그다음 적절한 PDF 예제 문서가 있어야 하는데, 테스트를 위해 너무 큰 PDF 문서를 지정하면 OpenAI/Azure 사용료만 부과되므로 적절하게 10개 페이지 이하 분량의 PDF를 하나 선택해 주고,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Shared Files PRO - A WordPress plugin by Tammersoft - Sample PDF ; <a target='tab' href='https://www.sharedfilespro.com/shared-files/38/sample.pdf'>https://www.sharedfilespro.com/shared-files/38/sample.pdf</a> </pre> <br /> 다음의 코드를 이용해 페이지 하나 당 Embedding 시킨 벡터 값들을 구해 <a target='tab' href='https://www.sysnet.pe.kr/2/0/13454'>Qdrant에 저장</a>해 둡니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > string qdrantHost = "localhost"; QdrantClient qdrantClient = new QdrantClient(qdrantHost, 6334, false); string collectionName = "pdf_docs"; await EmbedPdfFilesAsync(qdrantClient, collectionName, openAIClient, embeddingDeployment, "sample.pdf"); private static string[] ReadPdfFile(string filePath) { using PdfDocument pdfDoc = new PdfDocument(new PdfReader(filePath)); List<string> pages = new List<string>(); <span style='color: blue; font-weight: bold'>for (int page = 1; page <= pdfDoc.GetNumberOfPages(); page++)</span> { PdfPage pdfPage = pdfDoc.GetPage(page); ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy(); pages.Add(PdfTextExtractor.GetTextFromPage(pdfPage, strategy)); Console.WriteLine($"Page {page}: # of chars = {pages[page - 1].Length}"); } return pages.ToArray(); } private static async Task EmbedPdfFilesAsync( QdrantClient qdrantClient, string collectionName, OpenAIClient openAIClient, string embeddingDeployment, string pdfFile) { var collections = await qdrantClient.ListCollectionsAsync(); if (collections.Contains(collectionName)) { // await qdrantClient.DeleteCollectionAsync(collectionName); return; } string[] pdfPages = ReadPdfFile(pdfFile); var emddedPages = pdfPages.Select(page => new EmbeddedPage(page, [])).ToArray(); var tokenizer = await Tokenizer.CreateAsync(TokenizerModel.ada2); foreach (var (page, index) in emddedPages.WithIndex()) { var fullText = page.Text; if (string.IsNullOrWhiteSpace(fullText)) { continue; } int tokenCount = tokenizer.GetTokenCount(fullText); Console.WriteLine($"Page {index + 1} - # of tokens = {tokenCount}"); var chunks = tokenizer.ChunkByTokenCountWithOverlap(fullText, 3000, 50).Chunk(16).ToArray(); foreach (var chunk in chunks) { <span style='color: blue; font-weight: bold'>var embeddingResponse = await openAIClient.GetEmbeddingsAsync( new EmbeddingsOptions(embeddingDeployment, chunk));</span> page.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 = emddedPages .Where(d => d.Chunks.Count > 0) .SelectMany(d => d.Chunks.Select(c => new { Embedding = c.Embedding, Text = d.Text, })) .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(); <span style='color: blue; font-weight: bold'>await qdrantClient.UpsertAsync(collectionName, points);</span> } </pre> <br /> <a target='tab' href='https://www.sysnet.pe.kr/2/0/13454'>지난번 코드</a>와 비교하면 PDF 데이터로 바뀌었다는 점을 제외하고는 거의 그대로 재사용이 되었습니다.<br /> <br /> 테스트로 사용한 PDF는 5개의 페이지를 포함하고 있는데요, 그래서 위의 코드를 실행하면 다음과 같은 결과를 볼 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Page 1: # of chars = 3062 Page 2: # of chars = 2476 Page 3: # of chars = 2696 Page 4: # of chars = 1647 Page 5: # of chars = 899 Page 1: # of tokens = 617 Page 2: # of tokens = 514 Page 3: # of tokens = 576 Page 4: # of tokens = 342 Page 5: # of tokens = 453 </pre> <br /> 달리 말하면, 웬만한 PDF 페이지는 Token 수가 1,000개를 넘지 않으므로 페이지 단위 정도라면 OpenAI 측의 대화 문맥으로 사용하는데 부담이 없는 수준입니다. (물론, 좀 더 검색 수준을 높이고 싶다면 페이지 단위보다는, 허용이 되는 수준의 장(Chapter) 또는 절(Section) 단위로 문서 구분을 하는 것도 좋을 것입니다.)<br /> <br /> 어쨌든, 위와 같은 상황에서 검색을 해보면,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > <span style='color: blue; font-weight: bold'>string question = "What is the features of Pdf995 Suite solution?";</span> string[] results = await SearchWithQdrantAsync(qdrantClient, collectionName, openAIClient, embeddingDeployment, question, 5); // 5개 페이지인데, limit을 5로 설정했으니 모든 페이지를 반환 results.All((text) =&gt; { Console.WriteLine(text); Console.WriteLine("-----------------------------------"); return true; }); 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); <span style='color: blue; font-weight: bold'>foreach (var result in results) { Console.WriteLine($"Score: {result.Score}"); }</span> return results.Select(r => r.Payload["text"].StringValue).ToArray(); } </pre> <br /> 검색 결과에 따른 개별 문서(위의 예에서는 페이지)별 Cosine 유사도는 다음과 같이 나옵니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Score: 0.9092298 Score: 0.7339479 Score: 0.70261437 Score: 0.70222175 Score: 0.69459313 </pre> <br /> 실제 서비스라면 전체 문서를 모두 검색 결과로 받지는 않을 것이므로, 대략 0.8 이상의 유사도가 나오는 문서를 검색하도록 Qdrant 검색 조건에 주는 것이 좋겠습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > var results = await qdrantClient.SearchAsync(collectionName, embeddingVector, <span style='color: blue; font-weight: bold'>scoreThreshold: 0.8f</span>, limit: (ulong)resultLimit); </pre> <br /> <hr style='width: 50%' /><br /> <br /> 자, 그럼 이렇게 DB를 구축했으니 이후부터는 사용자로부터 질문을 받고, ChatGPT처럼 답하는 코드를 작성할 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > string question = "...[사용자가 입력한 질문]..."; <span style='color: blue; font-weight: bold'>string[] results = await SearchWithQdrantAsync</span>(qdrantClient, collectionName, openAIClient, embeddingDeployment, <span style='color: blue; font-weight: bold'>question</span>, 5); var chatCompletionsOptions = new ChatCompletionsOptions() { DeploymentName = deploymentModel, MaxTokens = 1000, Temperature = 0, // Knowledge Base 조회인 경우, 정확함을 목표로 하게 되므로 0으로 지정 Messages = { new ChatMessage(ChatRole.System, "You are a helpful AI assistant"), <span style='color: blue; font-weight: bold'>new ChatMessage(ChatRole.User, "The following information is from the PDF text: " + string.Join('\n', results)), new ChatMessage(ChatRole.User, question),</span> } }; <span style='color: blue; font-weight: bold'>Response<ChatCompletions> response = openAIClient.GetChatCompletions(chatCompletionsOptions);</span> Console.WriteLine(response.Value.Choices.First().Message.Content); </pre> <br /> 보는 바와 같이, DB로부터 조회한 문서 데이터와 함께 사용자의 질문을 chat model에게 전달해 응답을 받아 처리하고 있습니다. 실제로 실행해 보면 이런 결과를 얻게 됩니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > [질문]: What is the features of Pdf995 Suite solution? </pre> <br /> <div style='BACKGROUND-COLOR: #ccffcc; padding: 10px 10px 5px 10px; MARGIN: 0px 10px 10px 10px; FONT-FAMILY: Malgun Gothic, Consolas, Verdana; COLOR: #005555'> [답변]<br /> <br /> The Pdf995 Suite of products offers the following features:<br /> <br /> - Creation of professional-quality PDF documents<br /> - Easy-to-use interface for creating PDF files<br /> - Network file saving<br /> - Fast user switching on XP<br /> - Citrix/Terminal Server support<br /> ...[생략]...<br /> - Specify PDF document properties<br /> - Control PDF opening mode<br /> - Can be configured to add functionality to Acrobat Distiller<br /> - Free: Creates PDFs without annoying watermarks<br /> - Free: Fully functional, not a trial and does not expire<br /> - Over 5 million satisfied customers<br /> - Over 1000 Enterprise Customers worldwide<br /> <br /> All of these features are available at no cost to the user.<br /> </div><br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > [질문]: What Pdf995 product is for? </pre> <br /> <div style='BACKGROUND-COLOR: #ccffcc; padding: 10px 10px 5px 10px; MARGIN: 0px 10px 10px 10px; FONT-FAMILY: Malgun Gothic, Consolas, Verdana; COLOR: #005555'> [답변]<br /> <br /> The Pdf995 suite of products, which includes Pdf995, PdfEdit995, and Signature995, is a complete solution for document publishing needs. It provides ease of use, flexibility in format, and industry-standard security, all at no cost to the user. Pdf995 makes it easy and affordable to create professional-quality documents in the popular PDF file format. PdfEdit995 offers additional functionality, such as combining documents into a single PDF, automatic link insertion, and PDF conversion to HTML or DOC. Signature995 offers state-of-the-art security and encryption to protect documents and add digital signatures.<br /> </div><br /> <br /> 이 정도면 대충 감이 오시죠? ^^<br /> <br /> (<a target='tab' href='https://www.sysnet.pe.kr/bbs/DownloadAttachment.aspx?fid=2108&boardid=331301885'>첨부 파일은 이 글의 예제 코드를 포함</a>합니다.)<br /> <br /> <hr style='width: 50%' /><br /> <br /> 여러분만의 Knowledge Base를 embedding 시킨 DB가 있다면, 물론 예전에도 <a target='tab' href='https://www.sysnet.pe.kr/2/0/11663'>Elasticsearch</a>와 같은 검색 엔진을 사용할 수 있었지만 OpenAI의 Chat Completion 기능과 함께 연동하면 좀 더 자연스러운 수준의 검색 결과를 받아올 수 있습니다.<br /> <br /> 뭐랄까... 달리 생각하면 해당 DB 하나가 자신의 또 다른 두뇌 저장소라고 봐도 좋을 개념이 된 것입니다.<br /> </p><br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
1405
(왼쪽의 숫자를 입력해야 합니다.)