iT邦幫忙

2024 iThome 鐵人賽

DAY 11
0
生成式 AI

生成式 AI 時代下的 Azure Machine Learning系列 第 11

Day11-寫程式串接 Serverless 的 Llama-3.1-8B-Instruct 之寫程式篇

  • 分享至 

  • xImage
  •  

今天開始來寫程式,串接部署在 Azure ML 上面的 Llama-3.1-8B-Instruct。

使用 curl

我們可以單純使用下面這個 curl 來呼叫。

curl -X POST https://meta-llama-3-1-8b-instruct-dkdel.westus3.models.ai.azure.com/v1/chat/completions \
-H "Authorization: Bearer xx" \
-H "Content-Type: application/json" \
-d '{
    "messages": [
        {
            "role": "user",
            "content": "你好,我大魔術熊貓工程師。"
        }
    ]
    ,"stream": true
}'

生成式 AI 時代下的 Azure Machine Learning 教學圖文

其實可以用 curl 來呼叫的話,對大部份的工程師來說,已經很足夠了。你可以用你自己熟悉的程式語言和框架來做 web API 的請求。

接著我們來使用 LangChain 吧!

使用 LangChain

目前 LangChain 的如果使用 AzureMLOnlineEndpoint,會出現一個 bug,導致無法正確呼叫部署在 Azure ML 上的 model。

我已經了 PR 給 LangChain 團隊了,應該過一陣子就會 merge 並且能正常使用了。
https://github.com/langchain-ai/langchain/pull/26683

所以在被 merge 之前,可以用我的 PR 的程式碼,剛好也可以學一下 poetry 的進階使用。

Poetry 是可以把 GitHub 網址的程式碼,安裝成套件的,而我們這個還要指定的特定子目錄。

我們把 pyproject.toml 的 langchain-community 的地方程式碼修改如下:

langchain-community = { git = "https://github.com/Ko-Ko-Kirk/langchain.git", subdirectory = "libs/community", branch = "fix/serverless-api-400-bug" }

然後使用指令 poetry update langchain-community

這裡會等上一段時間來解析依賴。如果你安裝失敗,可以直接把虛擬環境砍掉再重裝一次。

好了之後,接著建立檔案 day11.py,複製貼上下面的程式碼。

from langchain_community.llms.azureml_endpoint import (
    AzureMLOnlineEndpoint,
    CustomOpenAIContentFormatter,
)
from langchain_core.messages import HumanMessage, SystemMessage, AIMessage

llm = AzureMLOnlineEndpoint(
    endpoint_url="https://Meta-Llama-3-1-8B-Instruct-dkdel.westus3.models.ai.azure.com/v1/chat/completions/",
    endpoint_api_type='serverless',
    endpoint_api_key="xx",
    content_formatter=CustomOpenAIContentFormatter(),
    model_kwargs={"temperature": 0.8},

)

messages = [
    SystemMessage(
        content="You are a helpful assistant! Your name is Bob."
    ),
    HumanMessage(
        content="What is your name? Do you know me?"
    ),
    AIMessage(
        content="Hello there! My name is Bob, and I'm here to help you with anything you need. I'm a friendly assistant, and I'm happy to chat with you.As for knowing you, I don't think we've met before."
    ),
    HumanMessage(
        content="My name is Alice. So what is your name? "
    ),
]


response = llm.invoke(messages)

print(response)

上面的程式碼裡,我們使用了一連串的對話機制,讓 AI 記得你是誰。實務上在做歷史訊息,也是這麼做的。請務必記得,它是 stateless 的。

大約會得到類似下面的結果:

Hello Alice! I'm glad we were able to clarify that. Now that we've established who's who, I'll introduce myself properly. My name is Bob, and I'll be your friendly assistant for as long as you need me. I don't have personal memories or knowledge of individuals, so I won't be able to recall a previous conversation or encounter. Each time you interact with me, it's a fresh start! How can I help you today, Alice?

今天就是 LangChain 接 Azure ML 的基本教學了。沒想到寫個鐵人賽可以順便幫 LangChain 解 bug😂😂


上一篇
Day10-寫程式串接 Serverless 的 Llama-3.1-8B-Instruct 之虛擬環境篇
下一篇
Day12-部署多模態模型 Phi-3.5 Vision-Instruct
系列文
生成式 AI 時代下的 Azure Machine Learning13
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言