From 32692beeff4f346b6a3be427a04b9cc25e574dd5 Mon Sep 17 00:00:00 2001 From: Guangya Liu Date: Mon, 16 Sep 2024 23:00:54 -0400 Subject: [PATCH] agent framework for bedrock --- aws/bedrock2.py | 7 ++- aws/react-bedrock2.py | 135 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 140 insertions(+), 2 deletions(-) create mode 100644 aws/react-bedrock2.py diff --git a/aws/bedrock2.py b/aws/bedrock2.py index bf95604..9f25e64 100644 --- a/aws/bedrock2.py +++ b/aws/bedrock2.py @@ -44,11 +44,14 @@ "max_tokens": 512, "temperature": 0.5, "messages": [ - { "role": "user", "content": [{"type": "text", "text": "Tell me about AWS Bedrock"}], - } + }, + { + "role": "assistant", + "content": [{"type": "text", "text": "Bedrock is an AI Platform"}], + }, ], } diff --git a/aws/react-bedrock2.py b/aws/react-bedrock2.py new file mode 100644 index 0000000..2118908 --- /dev/null +++ b/aws/react-bedrock2.py @@ -0,0 +1,135 @@ +# This code is Apache 2 licensed: +# https://www.apache.org/licenses/LICENSE-2.0 + +from dotenv import load_dotenv +load_dotenv() + +import boto3 +import json + +from botocore.exceptions import ClientError + +import re +import httpx + +class ChatBot: + def __init__(self, system=""): + self.client = boto3.client(service_name="bedrock-runtime", region_name="us-west-2") + self.system = system + self.messages = [] + def __call__(self, message): + self.messages.append({"role": "user", "content": message}) + print(self.messages) + result = self.execute() + self.messages.append({"role": "assistant", "content": result}) + print("after call >>>>>> ", self.messages) + + return result + + def execute(self): + native_request = { + "system": self.system, + "anthropic_version": "bedrock-2023-05-31", + "max_tokens": 512, + "temperature": 0.5, + "messages": self.messages, + } + + + # Convert the native request to JSON. + request = json.dumps(native_request) + + model_id = "anthropic.claude-v2" + + + try: + # Invoke the model with the request. + response = self.client.invoke_model(modelId=model_id, body=request) + + except (ClientError, Exception) as e: + print(f"ERROR: Can't invoke '{model_id}'. Reason: {e}") + exit(1) + + # Decode the response body. + model_response = json.loads(response["body"].read()) + + # Extract and print the response text. + response_text = model_response["content"][0]["text"] + return response_text + +prompt = """ +You run in a loop of Thought, Action, PAUSE, Observation. +At the end of the loop you output an Answer +Use Thought to describe your thoughts about the question you have been asked. +Use Action to run one of the actions available to you - then return PAUSE. +Observation will be the result of running those actions. + +Your available actions are: + +calculate: +e.g. calculate: 4 * 7 / 3 +Runs a calculation and returns the number - uses Python so be sure to use floating point syntax if necessary + +wikipedia: +e.g. wikipedia: LLM +Returns a summary from searching Wikipedia + +Example session: + +Question: What is the capital of Hebei? +Thought: I should look up Hebei on Wikipedia +Action: wikipedia: Hebei +PAUSE + +You will be called again with this: + +Observation: Hebei is a province in China. The capital is Shijiazhuang. + +You then output: + +Answer: The capital of Hebei is Shijiazhuang +""".strip() + + +action_re = re.compile('^Action: (\w+): (.*)$') + +def query(question, max_turns=3): + i = 0 + bot = ChatBot(prompt) + next_prompt = question + while i < max_turns: + i += 1 + result = bot(next_prompt) + # print(result) + actions = [action_re.match(a) for a in result.split('\n') if action_re.match(a)] + if actions: + # There is an action to run + action, action_input = actions[0].groups() + if action not in known_actions: + raise Exception("Unknown action: {}: {}".format(action, action_input)) + print(" -- running {} {}".format(action, action_input)) + observation = known_actions[action](action_input) + print("Observation:", observation) + next_prompt = "Observation: {}".format(observation) + else: + return + + +def wikipedia(q): + return httpx.get("https://en.wikipedia.org/w/api.php", params={ + "action": "query", + "list": "search", + "srsearch": q, + "format": "json" + }).json()["query"]["search"][0]["snippet"] + + +def calculate(what): + return eval(what) + +known_actions = { + "wikipedia": wikipedia, + "calculate": calculate, +} + +query("What is the captical of Hebei")