使用 Amazon Bedrock Converse API 节省时间!

花韻仙語
发布: 2024-11-27 09:00:48
转载
1039人浏览过

通过 bedrock,您可以访问一系列不同的大型语言模型(例如 claude、mistral、llama 和 amazon titan),并且随时都有新版本可用。

有选择固然很棒,但必须为每个模型编写不同的请求代码却很痛苦。

这就是为什么在比较不同基础模型的输出时,amazon bedrock converse api 将为您节省大量时间和精力!

一致性是关键!

converse api 是一个一致的接口,适用于所有支持消息/系统提示的模型。这意味着您只需编写一次代码,即可用它来试验不同的模型。

这是一个如何工作的示例,这个练习应该花费

配置模型访问

开始之前,请务必检查您想要使用的模型在您所在的地区是否可用,并且您已启用对它们的访问,这是我正在使用的模型,您可以选择这些模型或选择您自己的模型:
anthropic.claude-v2
anthropic.claude-3-俳句
克劳德 3.5 十四行诗
小米斯特拉尔

使用 Amazon Bedrock Converse API 节省时间!

1) 我们可以使用 aws 控制台中的 cloudshell 完成所有操作。

使用 Amazon Bedrock Converse API 节省时间!

2) 当 cloudshell 准备就绪时,安装 boto3,它是适用于 python 的 aws 开发工具包
pip 安装 boto3

使用 Amazon Bedrock Converse API 节省时间!

3) 从 github 下载名为 converse_demo.py 的文件 您可以使用 wget 并提供文件的原始路径来执行此操作:

wget https://raw.githubusercontent.com/fayekins/demos/refs/heads/main/converse_demo.py
登录后复制

使用 Amazon Bedrock Converse API 节省时间!

converse_demo.py

#first we import boto3 and json 
import boto3, json

#create a boto3 session - stores config state and allows you to create service clients
session = boto3.session()

#create a bedrock runtime client instance - used to send api calls to ai models in bedrock
bedrock = session.client(service_name='bedrock-runtime')

#here's our prompt telling the model what we want it to do, we can change this later
system_prompts = [{"text": "you are an app that creates reading lists for book groups."}]

#define an empty message list - to be used to pass the messages to the model
message_list = []

#here’s the message that i want to send to the model, we can change this later if we want
initial_message = {
            "role": "user",
               "content": [{"text": "create a list of five novels suitable for a book group who are interested in classic novels."}],
               }

#the message above is appended to the message_list
message_list.append(initial_message)

#make an api call to the bedrock converse api, we define the model to use, the message, and inference parameters to use as well
response = bedrock.converse(
modelid="anthropic.claude-v2",
messages=message_list,
system=system_prompts,
inferenceconfig={
            "maxtokens": 2048,
            "temperature": 0,
            "topp": 1
            },
)

#invoke converse with all the parameters we provided above and after that, print the result 
response_message = response['output']['message']
print(json.dumps(response_message, indent=4))
登录后复制

4) 像这样运行python代码:

python converse_demo.py
登录后复制

它应该给你类似这样的输出:

使用 Amazon Bedrock Converse API 节省时间!

5) 我们还可以使用不同的模型运行相同的代码,方法是替换代码中的模型 id,如下所示:

anthropic.claude-3-haiku-20240307-v1:0

比较第二个模型的输出,略有不同:

使用 Amazon Bedrock Converse API 节省时间!

6) 我们可以用另一个版本再次测试:

anthropic.claude-3-5-sonnet-20240620-v1:0

使用 Amazon Bedrock Converse API 节省时间!

当 claude 的新版本发布时,我们可以请求访问,然后只需在代码中替换模型的名称即可!

访问被拒绝错误

如果您看到与此类似的错误,则仅意味着您正在尝试使用您尚无权访问的模型。只需请求访问该模型,并在授予访问权限后重试。

使用 Amazon Bedrock Converse API 节省时间!

7) 我还尝试使用不同的模型提供商,将模型 id 更改为:

mistral.mistral-small-2402-v1:0

使用 Amazon Bedrock Converse API 节省时间!

因此,converse api 为您提供了一个简单、一致的 api,可与所有支持消息的 amazon bedrock 模型配合使用。这意味着您可以编写一次代码并将其与不同的模型一起使用来比较结果!

所以下次您与 bedrock 合作时,帮自己一个忙,尝试一下 converse api,稍后再感谢我!

以上就是使用 Amazon Bedrock Converse API 节省时间!的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:dev.to网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号