paint-brush
100 ngày AI, Ngày 18: Phát triển RAG bằng Microsoft Semantic Kerneltừ tác giả@sindamnataraj
1,042 lượt đọc
1,042 lượt đọc

100 ngày AI, Ngày 18: Phát triển RAG bằng Microsoft Semantic Kernel

từ tác giả Nataraj5m2024/04/05
Read on Terminal Reader

dài quá đọc không nổi

Trong bài đăng này, chúng tôi xem xét việc tạo RAG bằng Semantic Kernel của Microsoft.
featured image - 100 ngày AI, Ngày 18: Phát triển RAG bằng Microsoft Semantic Kernel
Nataraj HackerNoon profile picture

Nè mọi người! Tôi là Nataraj cũng giống như bạn, tôi rất thích thú với những tiến bộ gần đây của trí tuệ nhân tạo. Nhận thấy rằng mình cần phải theo kịp mọi sự phát triển đang diễn ra, tôi quyết định bắt tay vào hành trình học tập cá nhân, do đó 100 ngày của AI đã được sinh ra! Với loạt bài này, tôi sẽ tìm hiểu về LLM và chia sẻ ý tưởng, thử nghiệm, ý kiến, xu hướng và bài học thông qua các bài đăng trên blog của mình. Bạn có thể theo dõi hành trình trên HackerNoon đây hoặc trang web cá nhân của tôi đây . Trong bài viết hôm nay, chúng ta sẽ xem xét cách phát triển RAG bằng Semantic Kernel của Microsoft.

Tạo tăng cường truy xuất (RAG) là một trong những ứng dụng phổ biến nhất đang được phát triển bằng các LLM khác nhau. Trước đây chúng tôi đã khám phá cách phát triển RAG bằng langchain . Trong bài đăng này, chúng tôi sẽ tạo RAG bằng Semantic Kernel của Microsoft .


Để làm theo, bạn sẽ cần API AI mở.

Bước 1: Khởi tạo hạt nhân ngữ nghĩa

Bước đầu tiên là khởi tạo hạt nhân ngữ nghĩa và thông báo cho hạt nhân rằng chúng ta muốn sử dụng tính năng hoàn thành trò chuyện của Open AI và mô hình nhúng của Open AI mà sau này chúng ta sẽ sử dụng để tạo các phần nhúng. Chúng tôi cũng sẽ thông báo cho kernel rằng chúng tôi muốn sử dụng kho lưu trữ bộ nhớ sẽ là Chroma DB trong trường hợp của chúng tôi. Lưu ý rằng chúng tôi cũng đang hướng dẫn Kernel rằng kho lưu trữ bộ nhớ này cần phải liên tục.


 kernel = sk.Kernel() kernel.add_text_completion_service("openai", OpenAIChatCompletion("gpt-4",api_key)) kernel.add_text_embedding_generation_service("openai-embedding", OpenAITextEmbedding("text-embedding-ada-002", api_key)) # chrome db kernel.register_memory_store(memory_store=ChromaMemoryStore(persist_directory='mymemories2')) print("Made two new services attached to the kernel and made a Chroma memory store that's persistent.")

Bước 2 - Tạo phần nhúng

Trong ví dụ này, chúng tôi đang tạo RAG có thể trả lời các câu hỏi về phân tích SWOT mà chúng tôi đã tạo cho một doanh nghiệp pizza. Vì vậy, để làm được điều đó, chúng tôi thực hiện phân tích SWOT và lấy các phần nhúng cho chúng, đồng thời lưu trữ các phần nhúng tương ứng trong bộ sưu tập có tên “SWOT” trong kho dữ liệu liên tục mà chúng tôi đã tạo ở bước cuối cùng.


 strength_questions = ["What unique recipes or ingredients does the pizza shop use?","What are the skills and experience of the staff?","Does the pizza shop have a strong reputation in the local area?","Are there any unique features of the shop or its location that attract customers?", "Does the pizza shop have a strong reputation in the local area?", "Are there any unique features of the shop or its location that attract customers?"] weakness_questions = ["What are the operational challenges of the pizza shop? (eg, slow service, high staff turnover)","Are there financial constraints that limit growth or improvements?","Are there any gaps in the product offering?","Are there customer complaints or negative reviews that need to be addressed?"] opportunities_questions = ["Is there potential for new products or services (eg, catering, delivery)?","Are there under-served customer segments or market areas?","Can new technologies or systems enhance the business operations?","Are there partnerships or local events that can be leveraged for marketing?"] threats_questions = ["Who are the major competitors and what are they offering?","Are there potential negative impacts due to changes in the local area (eg, construction, closure of nearby businesses)?","Are there economic or industry trends that could impact the business negatively (eg, increased ingredient costs)?","Is there any risk due to changes in regulations or legislation (eg, health and safety, employment)?"] strengths = [ "Unique garlic pizza recipe that wins top awards","Owner trained in Sicily at some of the best pizzerias","Strong local reputation","Prime location on university campus" ] weaknesses = [ "High staff turnover","Floods in the area damaged the seating areas that are in need of repair","Absence of popular calzones from menu","Negative reviews from younger demographic for lack of hip ingredients" ] opportunities = [ "Untapped catering potential","Growing local tech startup community","Unexplored online presence and order capabilities","Upcoming annual food fair" ] threats = [ "Competition from cheaper pizza businesses nearby","There's nearby street construction that will impact foot traffic","Rising cost of cheese will increase the cost of pizzas","No immediate local regulatory changes but it's election season" ] print("✅ SWOT analysis for the pizza shop is resident in native memory") memoryCollectionName = "SWOT" # lets put these in memory / vector store async def run_storeinmemory_async(): for i in range(len(strengths)): await kernel.memory.save_information_async(memoryCollectionName, id=f"strength-{i}", text=f"Internal business strength (S in SWOT) that makes customers happy and satisfied Q&A: Q: {strength_questions[i]} A: {strengths[i]}") for i in range(len(weaknesses)): await kernel.memory.save_information_async(memoryCollectionName, id=f"weakness-{i}", text=f"Internal business weakness (W in SWOT) that makes customers unhappy and dissatisfied Q&A: Q: {weakness_questions[i]} A: {weaknesses[i]}") for i in range(len(opportunities)): await kernel.memory.save_information_async(memoryCollectionName, id=f"opportunity-{i}", text=f"External opportunity (O in SWOT) for the business to gain entirely new customers Q&A: Q: {opportunities_questions[i]} A: {opportunities[i]}") for i in range(len(threats)): await kernel.memory.save_information_async(memoryCollectionName, id=f"threat-{i}", text=f"External threat (T in SWOT) to the business that impacts its survival Q&A: Q: {threats_questions[i]} A: {threats[i]}") asyncio.run(run_storeinmemory_async()) print("😶‍🌫️ Embeddings for SWOT have been generated and stored in vector db")

Bước 3 – Đặt câu hỏi của bạn

Bây giờ chúng ta đã có các phần nhúng dữ liệu được lưu trữ trong cửa hàng vectơ chrome, chúng ta có thể đặt câu hỏi liên quan đến việc kinh doanh pizza và nhận được câu trả lời.


 #ask questions on swot potential_question = "What are the easiest ways to make more money?" counter = 0 async def run_askquestions_async(): memories = await kernel.memory.search_async(memoryCollectionName, potential_question, limit=5, min_relevance_score=0.5) display(f"### ❓ Potential question: {potential_question}") for memory in memories: if counter == 0: related_memory = memory.text counter += 1 print(f" > 🧲 Similarity result {counter}:\n >> ID: {memory.id}\n Text: {memory.text} Relevance: {memory.relevance}\n") asyncio.run(run_askquestions_async())


Đây là phiên bản được đơn giản hóa hoàn toàn về cách tạo RAG bằng Semantic Kernel. Lựa chọn khung phổ biến nhất để xây dựng hiện nay bằng LLM là langchain và trước đây chúng ta đã thấy cách xây dựng RAG bằng langchain. Mặc dù Langchain phổ biến hơn khi chúng ta thấy ngày càng có nhiều công ty xây dựng công cụ, nhưng sẽ có nhiều công cụ phức tạp hơn và tôi nhận thấy rằng Semantic Kernel có một số tính năng đặc biệt khiến nó nổi bật.


Thế là xong Ngày thứ 18 trong 100 Ngày của AI.


Tôi viết một bản tin có tên Trên mức trung bình, nơi tôi nói về những hiểu biết sâu sắc thứ hai đằng sau mọi thứ đang diễn ra trong ngành công nghệ lớn. Nếu bạn làm trong lĩnh vực công nghệ và không muốn ở mức trung bình, hãy đăng ký theo dõi .


Theo dõi tôi trên Twitter , LinkedIn hoặc HackerNoon để biết thông tin cập nhật mới nhất về 100 ngày của AI hoặc đánh dấu trang này . Nếu bạn làm trong lĩnh vực công nghệ, bạn có thể muốn tham gia cộng đồng các chuyên gia công nghệ của tôi tại đây .