与 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
}
生成图像示例