-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
182 lines (153 loc) · 6.27 KB
/
utils.py
File metadata and controls
182 lines (153 loc) · 6.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env python3
"""
Shared utilities for the MemMachine Workshop chatbots.
Contains Bedrock client management, model formatting, response cleaning,
CSS loading, and common UI components used by both chatbot variants.
"""
import json
import logging
import os
import re
import time
from pathlib import Path
import boto3
import streamlit as st
from dotenv import load_dotenv
load_dotenv()
logger = logging.getLogger(__name__)
# ---------------------------------------------------------------------------
# Configuration
# ---------------------------------------------------------------------------
AWS_REGION = os.getenv("AWS_REGION", "us-west-2")
MODEL_ID = os.getenv("BEDROCK_MODEL_ID", "openai.gpt-oss-20b-1:0")
AVAILABLE_MODELS = {
"openai.gpt-oss-20b-1:0": "OpenAI GPT-OSS 20B",
"anthropic.claude-3-sonnet-20240229-v1:0": "Anthropic Claude 3 Sonnet",
"anthropic.claude-3-haiku-20240307-v1:0": "Anthropic Claude 3 Haiku",
"us.deepseek.r1-v1:0": "DeepSeek R1",
"qwen.qwen3-32b-v1:0": "Qwen 3 32B",
"mistral.mixtral-8x7b-instruct-v0:1": "Mistral Mixtral 8x7B Instruct",
"mistral.mistral-7b-instruct-v0:2": "Mistral 7B Instruct",
}
TYPING_SPEED = 0.02
# ---------------------------------------------------------------------------
# Bedrock client
# ---------------------------------------------------------------------------
@st.cache_resource
def get_bedrock_client():
"""Get or create Bedrock runtime client."""
return boto3.client("bedrock-runtime", region_name=AWS_REGION)
# ---------------------------------------------------------------------------
# Bedrock model invocation
# ---------------------------------------------------------------------------
def _build_request_body(model_id: str, prompt: str) -> str:
"""Build the JSON request body for a given Bedrock model."""
if model_id.startswith("anthropic."):
return json.dumps({
"anthropic_version": "bedrock-2023-05-31",
"messages": [{"role": "user", "content": prompt}],
"max_tokens": 1000,
"temperature": 0.7,
})
if model_id.startswith("us.deepseek.") or model_id.startswith("qwen."):
return json.dumps({
"messages": [{"role": "user", "content": prompt}],
"max_tokens": 1000,
"temperature": 0.7,
"top_p": 0.9,
})
if model_id.startswith("meta.") or model_id.startswith("mistral."):
return json.dumps({
"messages": [{"role": "user", "content": prompt}],
"max_tokens": 1000,
"temperature": 0.7,
})
if model_id.startswith("amazon.titan"):
return json.dumps({
"inputText": prompt,
"textGenerationConfig": {
"maxTokenCount": 1000,
"temperature": 0.7,
},
})
# Default (OpenAI and others)
return json.dumps({
"messages": [{"role": "user", "content": prompt}],
"max_tokens": 1000,
"temperature": 0.7,
})
def _extract_response_text(response_body: dict) -> str:
"""Extract the generated text from a Bedrock response."""
if "choices" in response_body:
choices = response_body["choices"]
if isinstance(choices, list) and choices:
choice = choices[0]
if "message" in choice:
return choice["message"]["content"]
if "text" in choice:
return choice["text"].strip()
if "content" in response_body:
content = response_body["content"]
if isinstance(content, list):
return "".join(
part["text"] for part in content if part.get("type") == "text"
).strip()
return content[0]["text"]
if "results" in response_body:
return response_body["results"][0]["outputText"]
if "generation" in response_body:
return response_body["generation"]
return str(response_body)
def call_bedrock(prompt: str, model_id: str | None = None) -> str:
"""Call an AWS Bedrock model and return the response text."""
if model_id is None:
model_id = st.session_state.get("model_id", MODEL_ID)
bedrock_runtime = get_bedrock_client()
try:
body = _build_request_body(model_id, prompt)
response = bedrock_runtime.invoke_model(
modelId=model_id,
body=body,
contentType="application/json",
accept="application/json",
)
response_body = json.loads(response["body"].read())
return _extract_response_text(response_body)
except Exception as e:
logger.exception("Bedrock invocation failed")
return f"Error calling Bedrock: {e}"
# ---------------------------------------------------------------------------
# Response cleaning
# ---------------------------------------------------------------------------
def clean_response(response: str) -> str:
"""Remove reasoning tags and clean up response text."""
response = re.sub(r"<reasoning>.*?</reasoning>", "", response, flags=re.DOTALL | re.IGNORECASE)
response = re.sub(r"</?reasoning>", "", response, flags=re.IGNORECASE)
response = re.sub(r"\n\s*\n\s*\n", "\n\n", response)
return response.strip()
# ---------------------------------------------------------------------------
# UI helpers
# ---------------------------------------------------------------------------
def typewriter_effect(text: str, speed: float = TYPING_SPEED):
"""Generator that yields text word-by-word to create a typing effect."""
words = text.split(" ")
for i, word in enumerate(words):
yield word if i == 0 else " " + word
time.sleep(speed)
def load_css():
"""Load CSS from styles.css file."""
css_path = Path(__file__).parent / "styles.css"
try:
with css_path.open(encoding="utf-8") as f:
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
except FileNotFoundError:
pass
def test_bedrock_connection() -> tuple[bool, str]:
"""Test connectivity to AWS Bedrock."""
try:
bedrock = boto3.client("bedrock", region_name=AWS_REGION)
bedrock.list_foundation_models()
return True, "AWS Bedrock connection: OK"
except Exception as e:
logger.warning("Bedrock connection test failed: %s", e)
return False, f"AWS Bedrock connection failed: {e}"