發表文章

LLM及LangChain開發筆記大綱

圖片
https://www.researchgate.net/figure/LLM-LangChain-technology-roadmap_fig5_380721469 關於服用以下文章學習交流請留意 請留意!! 筆記(1)~筆記(8)都是介紹和LLM 採用OpenAI GPT做使用的提示詞技巧示範 LLM及LangChain開發筆記(3)~LLM及LangChain開發筆記(8)用的程式都有承先啟後 筆記(9)後續的才是偏重在LangChain部分的開發學習筆記相關介紹 LLM及LangChain開發筆記(1)_LangChain與LLM觀念與相關術語簡介Chains(鏈)跟Prompts(提示) https://coolmandiary.blogspot.com/2025/05/langchain1langchainllmchainsprompts.html LLM及LangChain開發筆記(2)_何謂提示詞工程(Prompt Engineering)_8個常見提示詞工程技巧 https://coolmandiary.blogspot.com/2025/05/langchain2prompt-engineering8.html LLM及LangChain開發筆記(3)_OpenAPI金鑰配置_Model的I/O_提示詞、分割符、摘要與指定輸出格式_token耗用 https://coolmandiary.blogspot.com/2025/05/langchain3openapimodeliotoken.html LLM及LangChain開發筆記(4)_Zero-Shot和Few-Shot Prompting通過搜索進行自我詢問 https://coolmandiary.blogspot.com/2025/05/langchain4zero-shotfew-shot-prompting.html LLM及LangChain開發筆記(5)_思維鍊CoT(Chain-of-Thought Prompt) https://coolmandiary.blogspot.com/2025/05/langchain5cotchain-of-thought-prompt.html LLM及LangChain開發筆記(6)_聊天機器人函數改寫_模擬對話歷史扮演某風格助理對話 h...

LLM及LangChain開發筆記(8)_對抗攻擊_Prompt Injection(提示詞注入)_洩漏與越獄

圖片
針對提示詞,若給有心人士使用,可能會變成兩面刃。 因此關於對放攻擊的研究與機制也逐漸進步,LLM現在也會做一些基本防範。(以下提示詞僅用於研究學習用途) 生成式AI生成根據給予的prompt去生出內容,然而prompt當中潛藏混淆視聽的內容,導致AI回復出現幻覺、有害訊息等等,甚至會在公共場合、醫療、宣傳、企業去生成有害的資訊,甚至政治敏感內容,造成決策疏失等諸多風險。 對抗攻擊-提示詞注入 透過分割符號、忽略等字眼來混淆AI去跳過指令描述,甚至用欺騙語法提供給AI。 以下是目前GPT已修復此BUG了,因此沒被混淆。 範例提示詞程式 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 import openai import os from dotenv import load_dotenv, find_dotenv _ = load_dotenv(find_dotenv()) # read local .env file openai.api_key = os.getenv( 'OPENAI_API_KEY' ) def get_completion (prompt, model= "gpt-3.5-turbo" ): messages = [{ "role" : "user" , "content" : prompt}] response = openai.ChatCompletion.create( model=model, messages=messages, temperature= 0 , # this is the degree of randomness of the model's output ) return response.choices[ 0 ].message[ "content" ] prompt = f """ 將下面的句子...

LLM及LangChain開發筆記(7)_聊天機器人函數改寫_自動點餐機器人_結合OpenAI GPT-3.5 API 和 Panel GUI 工具

圖片
  範例程式碼 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 import openai import os from dotenv import load_dotenv, find_dotenv _ = load_dotenv(find_dotenv()) # read local .env file openai.api_key = os.getenv( 'OPENAI_API_KEY' ) def get_completion_from_messages (messages, model= "gpt-3.5-turbo" , temperature= 0 ): response = openai.ChatCompletion.create( model=model, messages=messages, temperature=temperature, # this is the degree of randomness of the model's output ) # print(str(response.choices[0].message)) return response.choices[ 0 ].message[ "content" ] def merge_histories (_): prompt = chat.value_input chat.value = '' retailshop.append({ 'role' : 'user' , 'co...

LLM及LangChain開發筆記(6)_聊天機器人函數改寫_模擬對話歷史扮演某風格助理對話

圖片
程式 import openai import os from dotenv import load_dotenv, find_dotenv _ = load_dotenv(find_dotenv()) # read local .env file openai . api_key = os . getenv( 'OPENAI_API_KEY' ) def get_completion_from_messages (messages, model = "gpt-3.5-turbo" , temperature = 0 ): response = openai . ChatCompletion . create( model = model, messages = messages, temperature = temperature, # this is the degree of randomness of the model's output ) # print(str(response.choices[0].message)) return response . choices[ 0 ] . message[ "content" ] 透過一組「模擬對話歷史」來與 GPT 模型互動,並要求模型扮演莎士比亞風格的助理來繼續對話。 提示詞範例 messages = [ { 'role' : 'system' , 'content' : '你是一位說話風格像莎士比亞的助手。' }, { 'role' : 'user' , 'content' : '講個笑話給我聽' }, { 'role' : 'assistant' , 'content' : '小雞為什麼要過馬路' }, { 'role' : 'user...

LLM及LangChain開發筆記(5)_思維鍊CoT(Chain-of-Thought Prompt)

圖片
  https://www.promptingguide.ai/techniques/cot 思維鍊CoT(Chain-of-Thought Prompt)提示: 由於較複雜邏輯困難問題只用一般的prompt容易出錯,因此多出了CoT的技巧。 可透過中間推理步驟實踐的複雜推理能力,讓LLM將一個問題拆解為多個步驟,最終給出答案。引導LLM逐步去分解問題,展現推理過程,進而提高模型在邏輯推理、數學計算跟多步驟問題處裡的成效。 Chain-of-Thought Prompting Elicits Reasoning in Large Language Models https://arxiv.org/abs/2201.11903 CoT範例 prompt = f """ 這組數中的奇數相加會得到一個偶數:4、8、9、15、12、2、1。 回答: 將所有奇數相加(9、15、1),得到 25。答案為 錯(False)。 這組數中的奇數相加會得到一個偶數:15、32、5、13、82、7、1。 回答: """ response = get_completion(prompt) print (response) prompt = f """ 問題: 小明有 5 個蘋果;他又買了兩袋蘋果,每袋有 3 個蘋果;問小明現在共有多少個蘋果? 答案: """ response = get_completion(prompt) print (response) prompt = f """ 問題: 寵物店有 64 隻寵物,賣掉 28 隻,把剩下的放到籠子裡,每個籠子放 4 隻,問共需要多少個籠子? """ response = get_completion(prompt) print (response) prompt = f """ 問題: 小明在網上買好吃的,他買了 3 個蘋果、5 顆梨和 7 個橘子,每個橘子還配送了一只袋子,請問小明共有多少個水果? 答案: 小明買了 3 個蘋果、5 顆梨和 7 個橘子。所以他總共買了 3...

LLM及LangChain開發筆記(4)_Zero-Shot和Few-Shot Prompting通過搜索進行自我詢問

圖片
  https://portkey.ai/blog/zero-shot-vs-few-shot-prompting Few-Shot Prompting(少量示例提示)  是一種使用自然語言範例來教導大型語言模型(如 GPT)如何完成任務的方法,不需要額外訓練模型。簡單說,就是在 prompt 裡提供幾個範例(通常 1~5 個),讓模型「模仿範例模式」來完成新的任務,其相反就是所謂的zero-shot。 FSP範例 text = f """ 床前明月光,疑是地上霜;舉頭望明月,低頭思故鄉。 """ prompt = f """ 你好,如果下面的句子是關於產品描述,請回答是,並給出產品名稱; \ 如果不是,則回答不是。 {text} """ response = get_completion(prompt) print (response) text = f """ 泡一杯茶很簡單!首先,你需要把水煮開。在水煮開的過程中,拿一個杯子,放入一個茶包。 \ 一旦水夠熱了,就把它倒在茶包上。讓茶包浸泡一會兒。幾分鐘後,取出茶包。如果你喜歡的話,可以加一些糖 \ 或牛奶來調味。就這樣!你就可以享用一杯美味的茶了。 """ prompt = f """ 您將會獲得由反引號分隔的文本。 \ 如果其中包含一系列的指令,請依照以下格式重新撰寫這些指令: 步驟1 - ... 步驟2 - ... … 步驟N - ... 如果文本不包含一系列的指令,請簡單地寫上「未提供步驟」。 ```{text}``` """ response = get_completion(prompt) print (response) 讓AI ChatBot自行判斷是非 讓AI ChatBot自行抽絲剝繭列舉步驟 FSP示範(three-shot、one-shot) prompt = f """ 「毛動」是一種生長在坦尚尼亞的小型毛絨絨的動物。使用「毛動」這個詞的例句...

LLM及LangChain開發筆記(3)_OpenAPI金鑰配置_Model的I/O_提示詞、分割符、摘要與指定輸出格式_token耗用

圖片
先準備好anaconda環境並安裝好python Step1.建立 conda 虛擬環境 打開終端機或 Anaconda Prompt,輸入以下指令: conda create -n langchain03 python=3.10 -y LangChain 0.3 建議搭配 Python 3.10 Step2.啟動虛擬環境 conda activate langchain03 Step3.安裝 LangChain 0.3 版本與必要元件 pip install langchain==0.3.0 pip install notebook pip install openai==0.28 pip install chromadb pip install unstructured 補充為了避免程式碼版本預設抓最新報如下錯,請強制裝0.28版本openai You tried to access openai.ChatCompletion, but this is no longer supported in openai>=1.0.0 - see the README at https://github.com/openai/openai-python for the API. You can run `openai migrate` to automatically upgrade your codebase to use the 1.0.0 interface.  Alternatively, you can pin your installation to the old version, e.g. `pip install openai==0.28` Step4.去OPENAI申請API KEY(先儲值付最低用量5美元) 準備好.env於專案目錄下 程式範例使用openai的gpt-3.5-turbo模型 import openai import os from dotenv import load_dotenv, find_dotenv _ = load_dotenv(find_dotenv()) # read local .env file openai . api_key = os . getenv( 'OPENAI_...

LLM及LangChain開發筆記(2)_何謂提示詞工程(Prompt Engineering)_8個常見提示詞工程技巧

圖片
https://pub.towardsai.net/the-problem-with-prompting-and-what-it-really-is-469ce2c344f6 提示工程是人工智慧中的一個概念,特別是自然語言處理。  在提示工程中,任務的描述會被嵌入到輸入中。 例如,不是隱含地給予模型一定的參數,而是以問題的形式直接輸入。  https://thecreatorsai.com/p/prompt-engineering-is-easy 提示工程的典型工作方式是將一個或多個任務轉換為基於提示的數據集,並通過所謂的「基於提示的學習」來訓練語言模型。 最簡單的形式中,提示可以是問題或說明,例如,要求模型完成一個句子: Prompt: “Complete this sentence: The sky is” Output: “The sky is blue during the day and dark at night.” 通常也可以用「角色+任務+要求+提示」的結構來設計提示詞,比方: 「你是一個歷史學家,請撰寫一篇關於文藝復興時期的藝術見解文章,須包含三個主要藝術家作品的比較」 在更進階一些則可以變形多補充你的動機、要求回復的形式比方語言要繁體中文、英文等等或者檔案格式要是json、XML。 祕訣就在於更多的上下文或詳細的說明可以帶來更好的管理輸出。 通常會帶來如下幾項優點 提升輸出精準度:透過精心設計的提示語,可以引導模型產生更貼切的內容,降低隨機猜測的可能性,提升結果的可信度。 加強輸出可控性:設計提示語時,使用者能有效掌握模型輸出的方向,使內容更符合特定情境與使用需求,保持內容的一致與關聯性。 促進上下文理解力:良好的提示語有助於模型更全面地理解語境與背景,生成與主題緊密相關的資訊,提升應用價值。 維持語氣與風格統一:藉由提示語引導,模型可遵循特定的語調與風格,使產出內容更加一致,符合不同場域的溝通需求。 5個常見提示詞工程技巧 https://www.lennysnewsletter.com/p/five-proven-prompt-engineering-techniques Tactic 1:角色扮演(Role-playing) 讓模型扮演某個特定角色(如老師、律師、顧問),可明確引導它用對應角色的視角與語氣回應,產生更貼近需...

LLM及LangChain開發筆記(1)_LangChain與LLM觀念與相關術語簡介Chains(鏈)跟Prompts(提示)

圖片
https://medium.com/@shivajiofficial5088/what-is-langchain-in-ai-unlocking-complex-task-chaining-with-ease-6b3ac8e27834 https://www.cnblogs.com/angoli/p/17898963.html LangChain 由 Harrison Chase 創建,是一個 Python 庫,為使用 LLM 構建 NLP 應用程式提供開箱即用的支援。 可以連接到各種數據和計算源,並構建在特定於域的數據源、私有存儲庫等上執行 NLP 任務的應用程式。 LangChain與LLM有何區別? LLM(大語言模型): 是一類具有巨大規模和緊急功能的語言模型。 LLM 是 LangChain 的基本組成部分。它本質上是大型語言模型的包裝器,有助於使用特定大型語言模型的功能和能力。 LangChain 是一個用於創建由語言模型驅動的應用程式的框架,但並非統包概念,仍然拆分很多零散的套件。最新版出到0.3版本,不同版本上下支援度也會有落差。 LangChain 中的一些模組: LangChain 有6大核心模塊(除了LLM Model之外另外5個): Models:從不同的LLM和嵌入模型中進行選擇 Prompts:管理LLM輸入 Chains:將LLM與其他組件組合 Document Loaders and Utils/Tools:訪問外部數據 Memory:記住以前的對話 Agents:訪問其他工具 Prompts (提示) 提示是任何 NLP 應用程式的核心。即使在 ChatGPT 運作階段,答案的幫助也取決於提示。 為此,LangChain 提供了可用於格式化輸入和許多其他實用程式的提示範本。 Chains (鏈) LLM 是 LangChain 中的基本單元。而LangChain可以根據特定任務將 LLM 調用連結在一起。 例如,您可能需要從特定 URL 獲取數據,匯總返回的文字,並使用生成的摘要回答問題。 鏈也可以是較簡單的作業模式,比方可能需要讀入使用者輸入,然後使用該輸入來構建提示。在使用該 API 來生成回應。 Document Loaders and Utils(文件載入器和相關工具應用) 提供模型可使用的外部功能,如查詢資料庫、調用外部...

Module 9: Static and Dynamic Application Security Testing (SAST & DAST)

  What is a limitation of DAST compared to SAST? Response: A. DAST can only test static parts of the application B. DAST cannot identify vulnerabilities in the source code not executed during the test C. DAST is more effective at finding vulnerabilities in non-web applications D. DAST replaces all other testing methods Why is it important to integrate SAST in the early stages of development? Response: A. To increase the time it takes to detect vulnerabilities B. To detect vulnerabilities early and reduce the cost of fixing them C. SAST is less effective in the early stages D. To focus only on final testing stages How can combining SAST and DAST improve application security? Response: A. By focusing only on post-deployment testing B. It creates redundancies that increase vulnerability C. By providing a comprehensive view of both static code vulnerabilities and runtime issues D. Combining these tests is discouraged in modern development practices What advantage does DAST provide when...