探索AmazonBedrock的功能与应用
立即解锁
发布时间: 2025-09-02 00:44:43 阅读量: 12 订阅数: 21 AIGC 


生成式AI实战:从理论到应用
# 探索 Amazon Bedrock 的功能与应用
## 1. 流式响应
流式响应对于响应式聊天界面特别有用,可维持交互式应用程序的实时性。以下是使用 Bedrock 的 InvokeModelWithResponseStream API 的 Python 代码示例:
```python
response = bedrock_runtime.invoke_model_with_response_stream(
modelId=modelId,
body=body)
stream = response.get('body')
if stream:
for event in stream:
chunk=event.get('chunk')
if chunk:
print(json.loads(chunk.get('bytes').decode))
```
## 2. 大语言模型推理 API
基础模型会公开一组生成配置参数,这些参数会在推理过程中影响模型的输出。这些配置参数能让你控制模型的响应,包括令牌的多样性和数量。大多数模型支持温度(temperature)、top_k 和 top_p。
以下是使用 `invoke_model()` API 的 Bedrock 推理 API 请求示例,其中包含了使用 Bedrock 模型的提示配置参数:
```python
import boto3
import json
bedrock_runtime = boto3.client(
service_name='bedrock-runtime'
)
prompt = "<your prompt here>"
body = json.dumps({
"inputText": "This is where you place your input text",
"textGenerationConfig": {
"temperature":0,
"topP":1
}
})
modelId = '...' # Amazon Bedrock 基础模型:
# Amazon Titan Text
# Anthropic Claude
# AI21 Jurassic
# Cohere Command
# Meta Llama2
# 等等
response = bedrock_runtime.invoke_model(
body=body,
modelId=modelId)
response_body = json.loads(response.get('body').read())
print(response_body.get('results')[0].get('outputText'))
```
## 3. 生成 SQL 代码
许多文本生成模型(包括 Amazon Bedrock 中的模型)都在大量文本数据(包括代码示例)上进行了预训练。以下是使用 Amazon Bedrock 生成 SQL 查询的示例:
```python
prompt = """
I have a table called 'students' with fields 'id', 'age', 'year_enrollment',
'subject', 'grade'. Write me a SQL Query that returns the 'id' with the highest
'age'.
"""
body = json.dumps({"inputText": prompt})
modelId = '...'
response = bedrock_runtime.invoke_model(
body=body,
modelId=modelId)
response_body = json.loads(response.get('body').read())
print(response_body.get('results')[0].get('outputText'))
```
## 4. 文本摘要
生成文本摘要也是生成式 AI 的一个流行用例。以下是构建提示并发送 API 请求的步骤:
1. 构建提示:
```python
prompt = """
Please provide a summary of the following text. Do not add any information that
is not mentioned in the text below.
<text>
AWS took all of that feedback from customers, and today we are excited to
announce Amazon Bedrock, a new service that makes generative foundation models
accessible via an API. Bedrock is the easiest way for customers to build and
scale generative AI-based applications using FMs, democratizing access for all
builders.
</text>
"""
```
2. 定义 API 请求体:
```python
body = json.dumps(
{
"inputText": prompt,
"textGenerationConfig":{
"maxTokenCount":128,
"temperature":0,
"topP":1
}
}
)
```
3. 发送 API 请求:
```python
import json
response = bedrock_runtime.invoke_model_with_response_stream(
body=body,
modelId=modelId)
stream = response.get('body')
output = []
if stream:
for event in stream:
chunk = event.get('chunk')
if chunk:
chunk_obj = json.loads(chunk.get('bytes').decode())
text = chunk_obj['outputText']
output.append(text)
print(''.join(output))
```
## 5. 嵌入生成
嵌入是生成式 AI 和机器学习中的关键概念。可以使用 Amazon Bedrock 模型为任何输入字符串检索嵌入向量,然后比较向量之间的距离以找到最相关的文本字符串。常见用例包括语义搜索、推荐和分类。
以下是生成嵌入向量的代码示例:
```python
def get_embedding(body, modelId, accept, contentType):
response = bedrock_runtime.invoke_model(
body=body,
modelId=modelId)
response_body = json.loads(response.get('body').read())
embedding = response_body.get('embedding')
return embedding
body = json.dumps(
{
"inputText": "<your prompt here>"
}
)
modelId = '...'
embedding = get_embedding(body, modelId)
print(embedding)
```
为了可视化句子之间的语义相似性,可以生成热力图:
```python
import sklearn
from sklearn.preprocessing import normalize
import numpy as np
import seaborn as sns
def plot_similarity_heatmap(text_labels, embeddings, rotation):
inner_product = np.inner(embeddings, embeddings)
sns.set(font_scale=1.1)
graph = sns.heatmap(
inner_product,
xticklabels=text_labels,
yticklabels=text_labels,
vmin=np.min(inner_product),
vmax=1,
cmap="BuPu",
)
graph.set_xticklabels(text_labels, rotation=rotation)
graph.set_title("Semantic Textual Similarity Between Sentences")
phrases = [
# Animals
"Shelbee's dog, Molly, is so cute.",
"Antje hates cats.",
"Chris's first dog was very cute.",
# U.S. Cities
"Chicago is the place where I'm from.",
"I work in San Francisco.",
"Washington D.C. is a great place to visit.",
# Color
"What is your favorite color?",
"Is Molly brown?",
"Are your eyes blue?"
]
embeddings = []
for phrase in phrases:
query_response = get_embedding(
body=json.dumps({"inputText": phrase}),
modelId="...")
embeddings.append(query_response)
# 内积之前进行归一化
embeddings = normalize(np.array(embeddings), axis=1)
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
plot_similarity_heatmap(phrases, embeddings, 90)
```
## 6. 模型微调
当你决定自定义模型时,可以使用 Amazon Bedrock 对基础模型进行微调。以下是微调模型的步骤:
1. 准备数据集:将数据集以 JSON Lines 格式存储在 S3 中,例如:
```json
{"prompt": "I love going to the movies", "completion": "Positive"}
{"prompt": "This new shirt is gorgeous", "completion": "Positive"}
{"prompt": "The weather is awful", "completion": "Negative"}
{"prompt": "This movie is terrible", "completion": "Negative"}
```
2. 调用 `create_model_customization_job()` 开始微调:
```python
import boto3
bedrock = boto3.client(service_name='bedrock')
input_training_data = "s3://<BUCKET>/train.jsonl"
output_data = "s3://<BUCKET>/output/"
bedrock.create_model_customization_job(
jobName="my-job",
customModelName="my-fine-tuned-model",
baseModelIdentifier="...", # Bedrock 基础模型
trainingDataConfig={"s3Uri": input_training_data},
outputDataConfig={"s3Uri": output_data},
```
0
0
复制全文
相关推荐







