與 Amazon Bedrock API 無縫集成,在 Ruby 中產生 AI 支援的文字和圖像? + ?.亞馬遜基岩 API。
Amazon Bedrock 是一項完全託管的服務,可透過 API 提供來自領先 AI 新創公司和 Amazon 的 FM,因此您可以從各種 FM 中進行選擇,找到最適合您的使用案例的模型。
將以下行加入應用程式的 Gemfile 中:
gem "ruby-amazon-bedrock" , "~> 0.2.4"
然後執行:
$ bundle install
或安裝:
$ gem install ruby-amazon-bedrock -v 0.2.4
並要求:
require "amazon_bedrock"
為了使用 Amazon Bedrock,您將需要安全憑證,就像任何其他 AWS 服務一樣。從 AWS IAM(身分和存取管理)取得金鑰 https://us-east-1.console.aws.amazon.com/iam
要使用 Bedrock,您必須要求存取 Bedrock 的 FM。為此,您需要擁有正確的 IAM 權限。對於某些模型,您可能首先需要提交用例詳細信息,然後才能請求存取權限。
請注意,透過將ruby-amazon-bedrock
gem 與 Amazon Bedrock 結合使用,您可能會產生與使用 Amazon Bedrock 服務相關的費用。作為用戶,您對因使用 Amazon Bedrock 產生的任何及所有費用承擔全部責任。您可以在此處查看有關定價的更多詳細信息
透過傳遞您的 AWS IAM 憑證來實例化用戶端:
client = RubyAmazonBedrock :: Client . new (
region : "AWS_REGION" ,
access_key_id : "AWS_ACCESS_KEY_ID" ,
secret_access_key : "AWS_SECRET_ACCESS_KEY"
)
或使用您的帳戶憑證將.env.sample
複製到.env
檔案並使用ENV
變數。注意:為了使用rspec
執行測試,需要執行此步驟
client = RubyAmazonBedrock :: Client . new (
region : ENV [ "AWS_REGION" ] ,
access_key_id : ENV [ "AWS_ACCESS_KEY_ID" ] ,
secret_access_key : ENV [ "AWS_SECRET_ACCESS_KEY" ]
)
您也可以透過傳遞profile
關鍵字參數來使用 AWS 命名設定檔。使用命名設定檔時,不需要指定region
、 access_key_id
和access_token
。
client = RubyAmazonBedrock :: Client . new (
profile : "AWS_PROFILE"
)
options 參數透過允許額外的、特定於模型的配置來增強方法的靈活性。如果未明確提供,則該方法預設為一組標準模型參數。您可以查看有關模型可選參數的更多詳細資訊。
Amazon Bedrock 是一項完全託管的服務,可透過 API 提供來自領先 AI 新創公司和 Amazon 的 FM,因此您可以從各種 FM 中進行選擇,找到最適合您的使用案例的模型。
企業使用 AI21 的 Jurassic 系列領先的法學碩士,利用現有的組織數據來建立生成式人工智慧驅動的應用程式和服務。 Jurassic 支援跨行業用例,包括長文本和短文本生成、上下文問答、摘要和分類。 Jurassic 旨在遵循自然語言指令,接受了大量網路文本語料庫的訓練,並支援英語以外的六種語言。看更多...
這些模型的選項有:
{
temperature : 0.5 , # float
max_tokens : 200 , # integer
stop_sequences : [ ] , # [string]
top_p : 0.5 , # float
count_penalty : 0 , # integer
presence_penalty : 0 , # integer
frequency_penalty : 0 # integer
}
有關參數資料類型和值的更多文檔,您可以[查看更多詳細資訊...][https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-jurassic2.html]
如何呼叫AI21模型:
侏羅紀 2 超
支援:開卷答案、總結、草稿產生、資訊擷取、構思
語言:英語、西班牙語、法語、德語、葡萄牙語、義大利語、荷蘭語
client . invoke_model ( id : 'ai21.j2-ultra-v1' , prompt : "What's natural language processing?" , options : { } )
# Response
{ :id => 1234 ,
:prompt =>
{ :text => "Describe how an LLM works" ,
:tokens =>
[ { :generatedToken => { :token => "▁Describe" , :logprob => - 10.674324035644531 , :raw_logprob => - 10.674324035644531 } ,
:completions =>
[ { :data =>
{ :text =>
" n Natural language processing (NLP) is a field of computer science, artificial intelligence, and linguistics concerned with the interactions between computers and human (natural) languages, in particular how to program computers to process and analyze large amounts of natural language data. The goal of NLP is to provide computers with the ability to read, understand, and generate human language." ,
:tokens =>
[ { :generatedToken => { :token => "<|newline|>" , :logprob => 0.0 , :raw_logprob => - 0.00046850196667946875 } ,
侏羅紀2中期
client . invoke_model ( id : 'ai21.j2-mid-v1' , prompt : "What's GenAI?" , options : { } )
# Response
{ :id => 1234 ,
:prompt =>
{ :text => "What's GenAI?" ,
:tokens =>
[ { :generatedToken => { :token => "▁What's" , :logprob => - 9.553738594055176 , :raw_logprob => - 9.553738594055176 } ,
:topTokens => nil ,
:textRange => { :start => 0 , :end => 6 } } ,
:completions =>
[ { :data =>
{ :text =>
" n GenAI is a proposed standard for a generic AI language, which would allow AI systems to communicate and reason with each other in a common language. The goal of GenAI is to create a universal language that can be used by all AI systems, regardless of their specific task or underlying architecture. This would make it easier for AI systems to work together and learn from each other, and it would also make it easier for humans to interact with and understand AI systems." ,
:tokens =>
[ { :generatedToken => { :token => "<|newline|>" , :logprob => 0.0 , :raw_logprob => - 0.0009662011871114373 } ,
Amazon Titan Foundation 模型經過大型資料集的預訓練,使其成為強大的通用模型。按原樣使用它們,或透過使用您自己的資料針對特定任務微調模型來自訂它們,而無需註釋大量資料。看更多...
這些模型的選項有:
{
temperature : 0.5 , # float
top_p : 0.5 , # float
max_tokens : 512 , # integer
stop_sequences : [ ] # [string]
}
有關參數資料類型和值的更多文檔,您可以[查看更多詳細資訊...][https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-titan-text. html]
如何呼叫 Amazon Titan 文字模型:
Titan Text G1 - 精簡版
https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=amazon.titan-text-lite-v1
支援:文字生成、程式碼生成、富文本格式、編排(代理)、微調。
client . invoke_model ( id : 'amazon.titan-text-lite-v1' , prompt : 'Generate a story about rubies and gems' , options : { } )
# Response
{ :inputTextTokenCount => 8 ,
:results =>
[ { :tokenCount => 294 ,
:outputText =>
" n Once upon a time, there was a king who was very fond of rubies and gems. He had a collection of the most beautiful rubies and gems ..." ,
:completionReason => "FINISH" } ] }
Titan Text G1 - Express
https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=amazon.titan-text-express-v1
client . invoke_model ( id : 'amazon.titan-text-express-v1' , prompt : 'Generate a post about cats formatted with HTML tags' , options : { } )
# Response
{ :inputTextTokenCount => 9 ,
:results =>
[ { :tokenCount => 330 ,
:outputText =>
" n <h1>Cats</h1> n n Cats are small, furry, carnivorous mammals that are loved by many people around the world. They come in a variety of colors, shapes, and sizes, and have unique personalities that make them great companions. n n " ,
:completionReason => "FINISH" } ] }
支援:文字生成、程式碼生成、富文本格式、編排(代理)、微調
Anthropic 提供 Claude 系列大型語言模型,專為對話、摘要、問答、工作流程自動化、編碼等而建置。早期客戶報告說,克勞德產生有害輸出的可能性要小得多,更容易與人交談,並且更容易操縱 - 因此您可以輕鬆獲得所需的輸出。克勞德還可以對性格、語氣和行為進行指導。
這些模型的選項有:
{
temperature : 0.3 , # float
top_p : 0.5 , # float
top_k : 0.5 , # float
max_tokens : 1000 , # integer
stop_sequences : [ ] # [string]
}
有關參數資料類型和值的更多文檔,您可以[查看更多詳細資訊...][https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-claude.html]
如何調用人擇模型:
克勞德即時1.2
https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=anthropic.claude-instant-v1
支援:問答、資訊擷取、移除 PII、內容產生、多項選擇分類、角色扮演、文字比較、摘要、附引文的文件問答
client . invoke_model ( id : 'anthropic.claude-instant-v1' , prompt : 'What is a neural network?' )
# Response
{ :completion =>
" A neural network is a type of machine learning model inspired by the human brain. The key elements of neural networks are: n n - Neurons..."
: stop_reason = > "stop_sequence" ,
:stop => " n n Human:" }
克勞德1.3
https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=anthropic.claude-v1
支援:問答、資訊擷取、移除 PII、內容產生、多項選擇分類、角色扮演、文字比較、摘要、附引文的文件問答
client . invoke_model ( id : 'anthropic.claude-v1' , prompt : "You will be acting as a AI customer success agent for a company called Acme Dynamics." )
# Response
{ :completion =>
" Okay, thanks for providing the context. My name is Claude, I'm an AI assistant created by Anthropic to be helpful, harmless, and honest. How can I assist you as a customer success agent for Acme Dynamics today?" ,
:stop_reason => "stop_sequence" ,
:stop => " n n Human:" }
克勞德 2
https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=anthropic.claude-v2
支援:問答、資訊擷取、移除 PII、內容產生、多項選擇分類、角色扮演、文字比較、摘要、附引文的文件問答
client . invoke_model ( id : 'anthropic.claude-v2' , prompt : "I'm going to provide some text. I want to remove all person
ally identifying information from this text and replace it with XXX. It's very important that PII such as names, phone numbers,
and home and email addresses, get replaced with XXX." )
# Response
{ :completion =>
" Here is the text with personally identifying information replaced with XXX: n n XXX lives at XXX in the city of XXX. XXX can be reached at phone..." ,
:stop_reason => "stop_sequence" ,
:stop => " n n Human:" }
Cohere 模型是用於業務用例的文本生成模型。 Cohere 模型基於支援可靠業務應用程式的資料進行訓練,例如文字生成、摘要、文案寫作、對話、提取和問答。
命令模型的選項有:
{
temperature : 0.3 , # float
top_p : 0.5 , # float
top_k : 0.5 , # float
max_tokens : 1000 , # integer
stop_sequences : [ ] , # [string]
num_generations : 2 , # integer
return_likelihoods : 'ALL' , # string
stream : true , # boolean
truncate : 'END' # srtring
}
有關參數資料類型和值的更多文檔,您可以[查看更多詳細資訊...][https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-cohere-command. html]
如何呼叫Cohere命令模型:
命令
https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=cohere.command-text-v14
支援:摘要、文案、對話、摘錄、問答。
client . invoke_model ( id : 'cohere.command-text-v14' , prompt : 'Generate a twit about why Ruby on Rails is a great tool for building a startup. Write a few hashtags' )
# Response
{ :generations =>
[ { :finish_reason => "COMPLETE" ,
:id => "b82658a5-8f36-4a94-a1f1-7802aa418904" ,
:text =>
" Sure! Here's a tweet: n n Ruby on Rails is a powerful framework for building web applications. It is highly scalable, has robust community support, and is the perfect choice for startups looking to build fast and ship often. #RoR #RubyOnRails #Startup #Tech #Efficient n n What do you think?" } ] ,
:id => "d5d5149f-ea5a-47ae-ae37-8324882b06c7" ,
:prompt => "Generate a twit about why Ruby on Rails is a great tool for building a startup. Write a few hashtags:" }
指揮燈
https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=cohere.command-light-text-v14
支援:摘要、文案、對話、摘錄、問答。
client . invoke_model ( id : 'cohere.command-light-text-v14' , prompt : 'Generate a facebook post about GenAI models available at Amazon Bedrock' )
# Response
{ :generations =>
[ { :finish_reason => "MAX_TOKENS" ,
:id => "b58eaa37-915e-4f26-b37c-6b4875516648" ,
:text =>
" Introducing the latest innovation at Amazon Bedrock - our state-of-the-art GenAI models! ? n n Our team has been working tirelessly to bring you the best in artificial intelligence, and we're excited to share the results with you. With our GenAI models, you can now: n n - Develop applications that can understand and respond to human language with incredible accuracy, thanks to our state-of-the-art Large Language Models (LLMs). n -" } ] ,
:id => "0d2f2c74-cae9-434b-b5d5-f44aaeb1a587" ,
:prompt => "Generate a facebook post about GenAI models available at Amazon Bedrock:" }
嵌入模型的選項有:
{
input_type : 'classification' , # string
truncate : 'END' # string
}
有關參數資料類型和值的更多文檔,您可以[查看更多詳細資訊...][https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-embed.html]
如何呼叫Cohere嵌入模型:
嵌入英語
https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=cohere.embed-english-v3
支援:語意搜尋、檢索增強生成 (RAG)、分類、聚類。
# WIP
# client.invoke_model(id: 'cohere.embed-english-v3', prompt: 'Your prompt goes here ...')
嵌入多語言
https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=cohere.embed-multilingual-v3
# WIP
# client.invoke_model(id: 'cohere.embed-multilingual-v3', prompt: 'Your prompt goes here ...')
Meta 正在尋求釋放大型語言模型的力量。我們最新版本的 Llama 現在可供個人、創作者、研究人員和各種規模的企業使用,以便他們能夠負責任地實驗、創新和擴展他們的想法。
這些模型的選項有:
{
max_tokens : 128 , # integer
temperature : 0.9 , # float
top_p : 0.7 # float
}
如何呼叫元模型:
駱駝 2 聊天 13B
https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=meta.llama2-13b-chat-v1
支援:Llama 2 旨在用於英語的商業和研究用途。微調的聊天模型適用於基於聊天的應用程式。
client . invoke_model ( id : 'meta.llama2-13b-chat-v1' , prompt : 'Generate an Instagram Post about the Metaverse' )
# Resopnse
{ :generation =>
" n n Hey #Instagram community! I'm super excited to share my latest discovery with you all - the #Metaverse! ? n n Have you heard of it? It's like a virtual world where you can be anyone and do anything! ? From exploring new planets to attending virtual concerts, the possibilities are endless! ? n n I've been spending some time in the #Metaverse lately, and let me tell you, it's a game changer! Not only is it a ton of fun, but it's also a great way to connect with people from all over the world. ? n n I've made some amazing friends in the #Metaverse, and we've had some incredible adventures together! ?? n n So, what are you waiting for? Come join me in the #Metaverse and let's explore this amazing virtual world together! ? n n #Metaverse #VirtualReality #VR #Gaming #Adventure #Fun #Community #Friends #Instagram #SocialMedia" ,
:prompt_token_count => 11 ,
:generation_token_count => 275 ,
:stop_reason => "stop" }
駱駝 2 聊天 70B
https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=meta.llama2-70b-chat-v1
支援:Llama 2 旨在用於英語的商業和研究用途。微調的聊天模型適用於基於聊天的應用程式。
client . invoke_model ( id : 'meta.llama2-70b-chat-v1' , prompt : 'Generate a Facebook add to promote a new website that is selling Ruby on Rails and AI courses' )
# Response
{ :generation =>
". n n The ad should be targeted at people who are interested in learning Ruby on Rails and AI, and should highlight the benefits of taking the courses on the website. n n Here is a sample ad that could be used to promote the website: n n Headline: Unlock Your Potential with Ruby on Rails and AI Courses n n Body: Are you interested in learning Ruby on Rails and AI? Look no further! Our website offers a range of courses that will teach you everything you need to know to get started with these exciting technologies. n n Our Ruby on Rails courses will show you how to build powerful web applications using the popular framework, while our AI courses will teach you the fundamentals of machine learning and deep learning. ..." ,
:prompt_token_count => 22 ,
:generation_token_count => 512 ,
:stop_reason => "length" }
Stability AI 是全球領先的開源生成人工智慧公司,與公共和私營部門合作夥伴合作,為全球受眾帶來下一代基礎設施。
這些模型的選項有:
{
cfg_scale : 20 , # integer
seed : 1 , # integer
steps : 10 # integer
}
如何呼叫穩定性AI模型:
特大號0.8
https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/providers?model=stability.stable-diffusion-xl-v0
支援:圖像生成、圖像編輯
client . invoke_model ( id : 'stability.stable-diffusion-xl-v0' , prompt : 'Generate an image of an orca jumping out of the water' , options : { file_path : 'path/to/your/image.jpg' } )
# NOTE: If file_path is not provided the image will be saved at 'image.jpg'
# Success Response
{
result : :success ,
file_path : 'path/to/your/image.jpg'
}
# Failure Response
{
result : :failure ,
error : ErrorClass
}
生成圖像範例
SDXL 1.0
client . invoke_model ( id : 'stability.stable-diffusion-xl-v1' , prompt : 'Generate an image of a white gold ring with a ruby on it' , options : { file_path : 'path/to/your/image.jpg' } )
# NOTE: If file_path is not provided the image will be saved at 'image.jpg'
# Success Response
{
result : :success ,
file_path : 'path/to/your/image.jpg'
}
# Failure Response
{
result : :failure ,
error : ErrorClass
}
生成圖像範例