Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.
Utilizzo InvokeFlow
con un AWS SDK
Gli esempi di codice seguenti mostrano come utilizzare InvokeFlow
.
Gli esempi di operazioni sono estratti di codice da programmi più grandi e devono essere eseguiti nel contesto. È possibile visualizzare questa operazione nel contesto nel seguente esempio di codice:
- JavaScript
-
- SDK per JavaScript (v3)
-
import { fileURLToPath } from "node:url";
import {
BedrockAgentRuntimeClient,
InvokeFlowCommand,
} from "@aws-sdk/client-bedrock-agent-runtime";
/**
* Invokes an alias of a flow to run the inputs that you specify and return
* the output of each node as a stream.
*
* @param {{
* flowIdentifier: string,
* flowAliasIdentifier: string,
* prompt?: string,
* region?: string
* }} options
* @returns {Promise<import("@aws-sdk/client-bedrock-agent").FlowNodeOutput>} An object containing information about the output from flow invocation.
*/
export const invokeBedrockFlow = async ({
flowIdentifier,
flowAliasIdentifier,
prompt = "Hi, how are you?",
region = "us-east-1",
}) => {
const client = new BedrockAgentRuntimeClient({ region });
const command = new InvokeFlowCommand({
flowIdentifier,
flowAliasIdentifier,
inputs: [
{
content: {
document: prompt,
},
nodeName: "FlowInputNode",
nodeOutputName: "document",
},
],
});
let flowResponse = {};
const response = await client.send(command);
for await (const chunkEvent of response.responseStream) {
const { flowOutputEvent, flowCompletionEvent } = chunkEvent;
if (flowOutputEvent) {
flowResponse = { ...flowResponse, ...flowOutputEvent };
console.log("Flow output event:", flowOutputEvent);
} else if (flowCompletionEvent) {
flowResponse = { ...flowResponse, ...flowCompletionEvent };
console.log("Flow completion event:", flowCompletionEvent);
}
}
return flowResponse;
};
// Call function if run directly
import { parseArgs } from "node:util";
import {
isMain,
validateArgs,
} from "@aws-doc-sdk-examples/lib/utils/util-node.js";
const loadArgs = () => {
const options = {
flowIdentifier: {
type: "string",
required: true,
},
flowAliasIdentifier: {
type: "string",
required: true,
},
prompt: {
type: "string",
},
region: {
type: "string",
},
};
const results = parseArgs({ options });
const { errors } = validateArgs({ options }, results);
return { errors, results };
};
if (isMain(import.meta.url)) {
const { errors, results } = loadArgs();
if (!errors) {
invokeBedrockFlow(results.values);
} else {
console.error(errors.join("\n"));
}
}
- Python
-
- SDK per Python (Boto3)
-
Invoca un flusso.
def invoke_flow(self, flow_id, flow_alias_id, input_data, execution_id):
"""
Invoke an Amazon Bedrock flow and handle the response stream.
Args:
param flow_id: The ID of the flow to invoke.
param flow_alias_id: The alias ID of the flow.
param input_data: Input data for the flow.
param execution_id: Execution ID for continuing a flow. Use the value None on first run.
Return: Response from the flow.
"""
try:
request_params = None
if execution_id is None:
# Don't pass execution ID for first run.
request_params = {
"flowIdentifier": flow_id,
"flowAliasIdentifier": flow_alias_id,
"inputs": input_data,
"enableTrace": True
}
else:
request_params = {
"flowIdentifier": flow_id,
"flowAliasIdentifier": flow_alias_id,
"executionId": execution_id,
"inputs": input_data,
"enableTrace": True
}
response = self.agents_runtime_client.invoke_flow(**request_params)
if "executionId" not in request_params:
execution_id = response['executionId']
result = ""
# Get the streaming response
for event in response['responseStream']:
result = result + str(event) + '\n'
print(result)
except ClientError as e:
logger.error("Couldn't invoke flow %s.", {e})
raise
return result
Per un elenco completo delle guide per sviluppatori AWS SDK e degli esempi di codice, consulta. Utilizzo di Amazon Bedrock con un AWS SDK Questo argomento include anche informazioni su come iniziare e dettagli sulle versioni precedenti dell'SDK.