項目簡介:
接下來,小李哥將會每天介紹一個基于亞馬遜云科技AWS云計算平臺的全球前沿AI技術解決方案,幫助大家快速了解國際上最熱門的云計算平臺亞馬遜云科技AWS AI最佳實踐,并應用到自己的日常工作里。本次介紹的是如何在Amazon SageMaker上使用大語言模型Meta Llama 7B,提供可擴展和安全的AI解決方案。通過Amazon API Gateway和AWS Lambda將應用程序與AI模型集成。本方案的解決方案架構圖如下:
本方案將主要使用亞馬遜云科技AWS上的大模型/機器學習模型托管服務Amazon SageMaker,下面我們介紹一下該服務。
什么是Amazon SageMaker?
Amazon SageMaker是一款由亞馬遜云科技提供的全面模型托管服務,旨在幫助開發者和數據科學家快速、輕松地構建、訓練和部署機器學習模型。SageMaker原生集成了機器行業熱門的工具和框架,使用戶能夠專注于模型的開發和優化,而無需擔心基礎設施的管理。
通過SageMaker,用戶可以使用預構建的算法和框架,或者將自己的自定義代碼帶入平臺進行訓練。其自動化的數據處理和模型訓練功能,可以大大減少模型開發的時間和復雜性。此外,SageMaker還提供了云托管Jupyter Notebook,用戶可以使用本地熟悉的訓練模型工具,以便無縫從本地遷移到云端模型訓練。
在模型訓練完成后,SageMaker可以將模型部署為托管的推理服務,配置API節點,確保高可用性和可擴展性。借助SageMaker,用戶可以無縫地集成機器學習模型到應用程序中,例如通過API Gateway和AWS Lambda來實現實時推理服務。
Amazon SageMaker還具備內置的監控和調優功能,使得模型的性能優化和管理更加高效。無論是初學者還是專業數據科學家,SageMaker都是一種理想的解決方案,幫助企業快速實現人工智能和機器學習的價值。
本方案包括的內容:
本方案主要包括如下內容:
1.?使用Amazon SageMaker部署基礎AI/ML模型(Meta Llama 7B)作為推理的應用節點
2. 在云原生代碼托管服務AWS Lambda部署代碼以調用SageMaker推理。
3.?使用測試應用程序為已部署的模型進行功能測試。
項目搭建具體步驟:
下面跟著小李哥手把手搭建一個亞馬遜云科技AWS上的生成式AI模型(Meta Llama 7B)的軟件應用,并且測試大模型的多種應用場景下的表現。
1. 首先進入SageMarker
2. 點擊進入Jumpstart->Foundation Models,查看目前SageMaker上現有的開源ML模型
3. 進入Studio,點擊已經創建好的“Open Studio”
4. 點擊“Studio Classic”,再點擊右側Open
5. 打開SageMaker Studio控制臺
6. 下載存放在S3中的ML模型工程代碼:
aws s3 sync s3://<Replace with lab-code bucket name> .
7. 配置ML模型訓練、預測的Python工程環境
8. 在第一個代碼單元中更新和安裝SageMaker SDK
!pip install sagemaker --quiet --upgrade --force-reinstall
9. 配置需要的ML開源模型ID和版本(Meta Llama 7B)
model_id, model_version, = ("huggingface-llm-falcon-7b-instruct-bf16","*",
)
10. 創建SageMaker上的API端點,供應用訪問
%%time
from sagemaker.jumpstart.model import JumpStartModelmy_model = JumpStartModel(model_id=model_id,instance_type="ml.g5.2xlarge" )
predictor = my_model.deploy()
項目測試階段:
11. 利用編寫好的模型提示詞,對部署的模型進行問答測試。
%%timeprompt = "Tell me about Amazon SageMaker."payload = {"inputs": prompt,"parameters": {"do_sample": True,"top_p": 0.9,"temperature": 0.8,"max_new_tokens": 1024,"stop": ["<|endoftext|>", "</s>"]}
}response = predictor.predict(payload)
print(response[0]["generated_text"])
得到測試結果
Amazon SageMaker is a machine learning platform provided by Amazon Web Services that enables users to train and deploy machine learning models without having to build, train, and manage a machine learning infrastructure.
CPU times: user 13.5 ms, sys: 6.66 ms, total: 20.1 ms
Wall time: 1.38 s
12. 對大模型代碼生成功能進行測試:
payload = {"inputs": "Write a program to compute factorial in python:", "parameters":{"max_new_tokens": 200}}
query_endpoint(payload)
得到測試結果
Input: Write a program to compute factorial in python:Output:
Here is a Python program to compute factorial:```python
def factorial(n):if n == 0:return 1else:return n * factorial(n-1)print(factorial(5)) # Output: 120
```
13. 對大模型進行推理、任務完成能力測試
payload = {"inputs": "Building a website can be done in 10 simple steps:","parameters":{"max_new_tokens": 110,"no_repeat_ngram_size": 3}
}
query_endpoint(payload)
得到測試結果
Input: Building a website can be done in 10 simple steps:Output:
1. Choose a domain name
2. Register a domain name
3. Choose a web hosting provider
4. Create a website design
5. Add content to your website
6. Test your website
7. Optimize your website for search engines
8. Promote your website
9. Update your website regularly
10. Monitor your website for security
14. 對大模型代碼進行語言翻譯測試:
payload = {"inputs": """Translate English to French:sea otter => loutre de merpeppermint => menthe poivréeplush girafe => girafe peluchecheese =>""","parameters":{"max_new_tokens": 3}
}query_endpoint(payload)
得到測試結果
Input: Translate English to French:sea otter => loutre de merpeppermint => menthe poivréeplush girafe => girafe peluchecheese =>Output: fromage
15. 對大模型的基于文字的情緒分析能力進行測試
payload = {"inputs": """"I hate it when my phone battery dies."Sentiment: Negative###Tweet: "My day has been :+1:"Sentiment: Positive###Tweet: "This is the link to the article"Sentiment: Neutral###Tweet: "This new music video was incredibile"Sentiment:""","parameters": {"max_new_tokens":2}
}
query_endpoint(payload)
得到測試結果
Input: "I hate it when my phone battery dies."Sentiment: Negative###Tweet: "My day has been :+1:"Sentiment: Positive###Tweet: "This is the link to the article"Sentiment: Neutral###Tweet: "This new music video was incredibile"Sentiment:Output: Positive
16. 對大模型的問答能力進行測試2
payload = {"inputs": "Could you remind me when was the C programming language invented?","parameters":{"max_new_tokens": 50}
}
query_endpoint(payload)
得到測試結果
Input: Could you remind me when was the C programming language invented?Output:
The C programming language was invented in 1972 by Dennis Ritchie at Bell Labs.
17.利用大模型生成食譜
payload = {"inputs": "What is the recipe for a delicious lemon cheesecake?", "parameters":{"max_new_tokens": 400}}
query_endpoint(payload)
得到測試結果
Input: What is the recipe for a delicious lemon cheesecake?Output:
Here is a recipe for a delicious lemon cheesecake:Ingredients:
- 1 1/2 cups graham cracker crumbs
- 4 tablespoons butter, melted
- 2 (8 ounce) packages cream cheese, softened
- 1/2 cup granulated sugar
- 2 eggs
- 1/2 cup lemon juice
- 1/2 teaspoon salt
- 1/2 teaspoon vanilla extract
- 1/2 cup heavy cream
- 1/2 cup granulated sugar
- 1/2 teaspoon lemon zestInstructions:
1. Preheat oven to 350 degrees F.
2. In a medium bowl, mix together the graham cracker crumbs and melted butter. Press the mixture onto the bottom and sides of a 9-inch springform pan.
3. In a large bowl, beat the cream cheese and sugar until smooth. Add the eggs, lemon juice, salt, vanilla, and heavy cream. Beat until well combined.
4. Pour the mixture into the prepared pan.
5. Bake for 30 minutes or until the cheesecake is set.
6. Let cool for 10 minutes before serving.
7. In a small bowl, mix together the lemon zest and sugar. Sprinkle over the cheesecake before serving.
Step 4.2.7 : Test summarization?
18. 測試大模型的文字總結能力
payload = {"inputs":"""Amazon SageMaker is a fully managed machine learning service. With SageMaker, data scientists and developers can quickly and easily build and train machine learning models, and then directly deploy them into a production-ready hosted environment. It provides an integrated Jupyter authoring notebook instance for easy access to your data sources for exploration and analysis, so you don't have to manage servers. It also provides common machine learning algorithms that are optimized to run efficiently against extremely large data in a distributed environment. With native support for bring-your-own-algorithms and frameworks, SageMaker offers flexible distributed training options that adjust to your specific workflows. Deploy a model into a secure and scalable environment by launching it with a few clicks from SageMaker Studio or the SageMaker console. Summarize the article above:""","parameters":{"max_new_tokens":200}}
query_endpoint(payload)
得到測試結果
Input: Amazon SageMaker is a fully managed machine learning service. With SageMaker, data scientists and developers can quickly and easily build and train machine learning models, and then directly deploy them into a production-ready hosted environment. It provides an integrated Jupyter authoring notebook instance for easy access to your data sources for exploration and analysis, so you don't have to manage servers. It also provides common machine learning algorithms that are optimized to run efficiently against extremely large data in a distributed environment. With native support for bring-your-own-algorithms and frameworks, SageMaker offers flexible distributed training options that adjust to your specific workflows. Deploy a model into a secure and scalable environment by launching it with a few clicks from SageMaker Studio or the SageMaker console. Summarize the article above:Output: SageMaker is a cloud-based machine learning platform that provides a range of tools and services to help data scientists and developers build, train, and deploy machine learning models. It offers an integrated Jupyter notebook environment, optimized algorithms, and flexible distributed training options.
19. 完成所有測試后,我們獲取大模型在SageMaker上的URL端點,作為API供應用使用。
以上就是在亞馬遜云科技上部署開源大模型,并且多場景測試的全部步驟。歡迎大家關注小李哥,未來獲取更多國際前沿的生成式AI開發方案。