Skip to content

Commit

Permalink
add invoke code
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasyu888 committed May 24, 2024
1 parent d8d3d8c commit cbce2d3
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions bedrock/invoke.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import boto3
from botocore.exceptions import ClientError


def invoke_agent(agents_runtime_client, agent_id, agent_alias_id, session_id, prompt):
"""
Sends a prompt for the agent to process and respond to.
:param agent_id: The unique identifier of the agent to use.
:param agent_alias_id: The alias of the agent to use.
:param session_id: The unique identifier of the session. Use the same value across requests
to continue the same conversation.
:param prompt: The prompt that you want Claude to complete.
:return: Inference response from the model.
"""

try:
response = agents_runtime_client.invoke_agent(
agentId=agent_id,
agentAliasId=agent_alias_id,
sessionId=session_id,
inputText=prompt,
)

completion = ""

for event in response.get("completion"):
chunk = event["chunk"]
completion = completion + chunk["bytes"].decode()

except ClientError as e:
print(f"Couldn't invoke agent. {e}")
raise

return completion


runtime_client=boto3.client(
service_name="bedrock-agent-runtime", region_name="us-east-1"
)

invoke_agent(
agents_runtime_client=runtime_client,
agent_id="7O1Q74HUYJ",
agent_alias_id="YQ6O9PQNP8",
session_id="my_session",
prompt="give me some observations about NF1fl/fl;Dhh-Cre"
)

0 comments on commit cbce2d3

Please sign in to comment.