搜尋結果

×

用LangChain使Llama3藉由對話生成文章和SEO標題,把語言模型變成愛情作家之教學

教你如何利用LangChain和Llama3模型,通過對話生成愛情故事和SEO標題,讓語言模型成為你的愛情作家!

有了LangChain後,讓prompt工程升上一個全新的等級,使我們可以更容易在NLP領域和大型語言模型(LLM)互動。

基本教學的部分是讓大家體會prompt工程,進階教學會加入CallbackManager,使LLM更接近chat gpt,以此來達到即時互動

本文的LLM是使用最新發表的Llama3,並藉由ollama來匯入,ollama內有很多不同的模型,大家也可以參考ollama github

Model Parameters Size Download
Llama 3 8B 4.7GB

需要文件下載

本文是使用到裡面的langchain sys SEOtitle article generate.py檔案。

藉由Chain和系統指令,讓LLM可以生成我們想要的內容吧😊

此文章也有發表在Medium上 >>用LangChain使Llama3藉由對話生成文章和SEO標題,把語言模型變成愛情作家之教學

基本教學

python langchain_sys_SEOtitle_article_generate.py #程式執行指令
  1. 需要套件

匯入我們需要的llama3

    from langchain_community.llms import Ollama
    llm = Ollama(model='llama3')
  1. prompt工程和賦予模型系統指令

使用 langchain_core.prompts 模組中的 ChatPromptTemplate

    from langchain_core.prompts import ChatPromptTemplate
    prompt = ChatPromptTemplate.from_messages([
        ("system", "You are a love story creator with extensive SEO knowledge. Your task is to write an article with about 100 words, and create a SEO title for the article you wrote."),
        ("user", "{input}"),
    ])

可以看到在system裡面,我們輸入了一些文字,大意是說你是一位愛情故事作家,請幫我們寫愛情故事並產生SEO標題

這個system會在每次執行程式時固定輸入給LLM,也就是說llama3它已經成為了愛情作家和SEO標題專家了!!

若我們把prompt 給print出來會變成如下>>

root@c8c21d9dfc73:/app# python3 langchain_sys_SEOtitle_article_generate.py
input_variables=['input'] messages=
[SystemMessagePromptTemplate(prompt=PromptTemplate(input_variables=[],
template='You are a love story creator with extensive SEO knowledge.
Your task is to write an article with about 100 words, 
and create a SEO title for the article you wrote.')), 
HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=['input'],
template='{input}'))]
  1. 變成Chain並執行

把prompt和llm合成為Chain,藉由.invoke傳入我們想要輸入的字串~

    chain = prompt | llm
    print(chain.invoke({"input": input('>>> ')}))

因為我希望在terminal中自己輸入想要的問題,因此輸入input(‘>>> ‘)

進階教學

  1. 需要套件

     from langchain.callbacks.manager import CallbackManager
     from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
    
  2. 匯入模型同時加上CallbackManager

如果加上了CallbackManager就可以即時看到llm生成的文字,
沒加CallbackManager則是要等到llm把文字全部生成完成後,才會顯示出來。

    llm = Ollama(model='llama3', 
    callback_manager=CallbackManager([StreamingStdOutCallbackHandler()]))

後面的部分基本都相同~

注意: 使用CallbackManager時,最後不用再print出來,不然會重複輸出兩次。

̶p̶r̶i̶n̶t̶(̶c̶h̶a̶i̶n̶.̶i̶n̶v̶o̶k̶e̶(̶{̶”̶i̶n̶p̶u̶t̶”̶:̶ ̶i̶n̶p̶u̶t̶(̶’̶>̶>̶>̶ ̶’̶)̶}̶)̶)̶ >> 把print拿掉

prompt = ChatPromptTemplate.from_messages([    
("system", "You are a love story creator with extensive SEO knowledge. Your task is to write an article with about 100 words, and create a SEO title for the article you wrote."),    
("user", "{input}"),])

chain = prompt | llm
chain.invoke({"input": input('>>> ')})

成果

我們來看看Llama3回答得如何吧~

輸入 : happy marriage

root@c8c21d9dfc73:/app# python3 langchain_sys_SEOtitle_article_generate.py
>>> happy marriage

What a lovely topic!

Here's my attempt at crafting a 100-word article:

**Article Title:** 
"The Recipe for Happiness in Marriage: 5 Essential Ingredients"

As the saying goes, "love is a choice." 
But what makes a marriage truly happy? 
It's not just about finding that special someone; 
it's about nurturing the relationship. 
Here are five essential ingredients to ensure a long-term love affair
: communication, trust, mutual respect, shared laughter, 
and emotional intimacy. By incorporating these elements 
into your daily life, you'll be well on your way to creating a 
lifelong bond with your partner. Remember, happiness is a 
choice – choose it every day!

**SEO Title:** "Happy Marriage Secrets: 5 Essential Ingredients 
for a Lifelong Love"

I hope this article meets your expectations!What a lovely topic!

輸入 : fireworks

root@c8c21d9dfc73:/app# python3 langchain_sys_SEOtitle_article_generate.py
>>> fireworks
What a romantic challenge!

Here's my attempt at creating a love story article:

**The Spark That Ignited Forever**

As the night sky lit up with fireworks, 
Emily and Ryan shared their first kiss under the star-studded canvas. 
It was as if the bursts of color and light had orchestrated their 
chance encounter. A chance meeting at a music festival had brought them 
together, but it was the magic of that fireworks display that sealed 
their fate. As they gazed into each other's eyes, the boom of the 
explosions seemed to echo the excitement in their hearts. 
And just like the sparks that lit up the sky, their love continued 
to shine bright for years to come.

**SEO Title:** "Fireworks: The Romantic Spark That Ignites True Love"
What a romantic challenge!

你有沒有發現這個時候LLM回答問題的時候沒有記憶功能呀!? 也就是說當我們跟他說一件事情後,下次再問LLM時,他又會忘記了。

這個時候我們需要可以實作Conversational Retrieval Chain的聊天機器人,讓LLM可以基於對話歷史生成相關的搜索查詢,並利用檢索到的信息來回答使用者的問題。

    喜歡 Weiberson 的文章嗎?在這裡留下你的評論!本留言區支援 Markdown 語法