{"id":25562,"date":"2024-01-12T18:30:11","date_gmt":"2024-01-12T09:30:11","guid":{"rendered":"https:\/\/www.skyarch.net\/blog\/?p=25562"},"modified":"2024-01-12T18:30:11","modified_gmt":"2024-01-12T09:30:11","slug":"amazon-bedrock%e3%81%a8%e8%a7%a6%e3%82%8c%e5%90%88%e3%81%8a%e3%81%86-%e3%80%9cstreamlit%e3%81%a7%e3%83%81%e3%83%a3%e3%83%83%e3%83%88%e3%83%9c%e3%83%83%e3%83%88%e4%bd%9c%e3%81%a3%e3%81%a6%e3%81%bf","status":"publish","type":"post","link":"https:\/\/www.skyarch.net\/blog\/amazon-bedrock%e3%81%a8%e8%a7%a6%e3%82%8c%e5%90%88%e3%81%8a%e3%81%86-%e3%80%9cstreamlit%e3%81%a7%e3%83%81%e3%83%a3%e3%83%83%e3%83%88%e3%83%9c%e3%83%83%e3%83%88%e4%bd%9c%e3%81%a3%e3%81%a6%e3%81%bf\/","title":{"rendered":"Amazon Bedrock\u3068\u89e6\u308c\u5408\u304a\u3046 \u301cStreamlit\u3067\u30c1\u30e3\u30c3\u30c8\u30dc\u30c3\u30c8\u4f5c\u3063\u3066\u307f\u305f\u301c"},"content":{"rendered":"<h1>\u306f\u3058\u3081\u306b<\/h1>\n<p>Amazon Bedrock\u306b\u89e6\u308c\u3066\u307f\u3088\u3046\uff01\u306e\u7b2c\u4e09\u5f3e\u3067\u3059\u3002<\/p>\n<p>\u5c11\u3057\u756a\u5916\u7de8\u3068\u306a\u308a\u307e\u3059\u304c\u3001\u3053\u308c\u307e\u3067AWS\u306e\u30b3\u30f3\u30bd\u30fc\u30eb\u753b\u9762\u3084SDK\u3092\u4f7f\u7528\u3057\u3066Bedrock\u306e\u691c\u8a3c\u3092\u884c\u3063\u3066\u304d\u307e\u3057\u305f\u304c\u3001Streamlit\u3068\u3044\u3046Python\u306e\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3092\u7528\u3044\u3066\u7c21\u6613\u7684\u306a\u30c1\u30e3\u30c3\u30c8\u30dc\u30c3\u30c8\u3092\u4f5c\u3063\u3066\u307f\u3088\u3046\u3068\u601d\u3044\u307e\u3059\u3002<br \/>\n\u691c\u8a3c\u3059\u308b\u4e0a\u3067\u3082\u30aa\u30ea\u30b8\u30ca\u30eb\u306aUI\u304c\u3042\u308b\u3068\u3084\u308b\u6c17\u304c\u51fa\u307e\u3059\u3002\u7b11<\/p>\n<p>\u7b2c\u4e00\u5f3e\u30d6\u30ed\u30b0\u306f\u3053\u3061\u3089\u3000-&gt; <a href=\"https:\/\/www.skyarch.net\/blog\/amazon-bedrock%e3%81%a8%e8%a7%a6%e3%82%8c%e5%90%88%e3%81%8a%e3%81%86-%e3%80%9c%e3%82%b3%e3%83%b3%e3%82%bd%e3%83%bc%e3%83%ab%e7%94%bb%e9%9d%a2%e3%82%92%e7%9c%ba%e3%82%81%e3%82%88%e3%81%86%e3%80%9c\/\" title=\"Amazon Bedrock\u3068\u89e6\u308c\u5408\u304a\u3046 \u301c\u30b3\u30f3\u30bd\u30fc\u30eb\u753b\u9762\u3092\u773a\u3081\u3088\u3046\u301c\">Amazon Bedrock\u3068\u89e6\u308c\u5408\u304a\u3046 \u301c\u30b3\u30f3\u30bd\u30fc\u30eb\u753b\u9762\u3092\u773a\u3081\u3088\u3046\u301c<\/a><br \/>\n\u7b2c\u4e8c\u5f3e\u30d6\u30ed\u30b0\u306f\u3053\u3061\u3089\u3000-&gt; <a href=\"https:\/\/www.skyarch.net\/blog\/amazon-bedrock%e3%81%a8%e8%a7%a6%e3%82%8c%e5%90%88%e3%81%8a%e3%81%86-%e3%80%9craku%e3%81%abrag%e3%82%92%e6%a7%8b%e7%af%89%e3%81%97%e3%82%88%e3%81%86%e3%80%9c\/\" title=\"Amazon Bedrock\u3068\u89e6\u308c\u5408\u304a\u3046 \u301cRAKU\u306bRAG\u3092\u69cb\u7bc9\u3057\u3088\u3046\u301c\">Amazon Bedrock\u3068\u89e6\u308c\u5408\u304a\u3046 \u301cRAKU\u306bRAG\u3092\u69cb\u7bc9\u3057\u3088\u3046\u301c<\/a><\/p>\n<h2>\u76ee\u6b21<\/h2>\n<ul>\n<li><a href=\"#streamlit\">Streamlit\u3068\u306f\uff1f<\/a><\/li>\n<li><a href=\"#image\">\u5b8c\u6210\u30a4\u30e1\u30fc\u30b8<\/a><\/li>\n<li><a href=\"#todo\">\u65e9\u901f\u4f5c\u3063\u3066\u307f\u305f<\/a><\/li>\n<li><a href=\"#tenbo\">\u5c55\u671b<\/a><\/li>\n<\/ul>\n<h2 id=\"streamlit\">Streamlit\u3068\u306f\uff1f<\/h2>\n<p>Python\u306eWeb\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u5411\u304d\u306e\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3068\u3044\u3048\u3070\u3001Django\u3084Flask\u304c\u6709\u540d\u304b\u3068\u601d\u3044\u307e\u3059\u3002<br \/>\n\u5f0a\u793e\u3067\u3082Flask\u3092\u63a1\u7528\u3057\u30a2\u30d7\u30ea\u958b\u767a\u306e\u5b9f\u7e3e\u3082\u3054\u3056\u3044\u307e\u3059\u3002\uff08\u5c11\u3057\u5ba3\u4f1d\uff09<br \/>\nStreamlit\u306f\u3001\u6a5f\u68b0\u5b66\u7fd2\u3084\u30c7\u30fc\u30bf\u30b5\u30a4\u30a8\u30f3\u30b9\u5411\u304d\u306eWeb\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u958b\u767a\u306b\u5411\u3044\u3066\u3044\u308b\u3088\u3046\u3067<br \/>\nPoC\u3067\u30b5\u30af\u30c3\u3068\u30b0\u30e9\u30d5\u63cf\u753b\u3092\u3057\u305f\u3044\uff01\u306e\u3088\u3046\u306a\u5834\u9762\u3067\u5927\u6d3b\u8e8d\u3059\u308b\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\nHTML\u3084CSS\u3092\u66f8\u304f\u5fc5\u8981\u3082\u306a\u304f\u3001\u30aa\u30b7\u30e3\u30ec\u306a\u753b\u9762\u3092\u4f5c\u308b\u3053\u3068\u304c\u51fa\u6765\u307e\u3059\u3002<\/p>\n<p>\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306f<a href=\"https:\/\/streamlit.io\/\">\u3053\u3061\u3089<\/a>\u3067\u3059\u3002<br \/>\nAPI\u30ea\u30d5\u30a1\u30ec\u30f3\u30b9\u3092\u773a\u3081\u3066\u308b\u3060\u3051\u3067\u3082\u697d\u3057\u3044\u3067\u3059\u3002<br \/>\n\u3053\u306e\u3088\u3046\u306a\u5f62\u3067\u30a4\u30e1\u30fc\u30b8\u4ed8\u304d\u3067\u8a18\u8f09\u3057\u3066\u3042\u308b\u305f\u3081\u975e\u5e38\u306b\u5b9f\u88c5\u304c\u697d\u3067\u3059\u3002<br \/>\n<a href=\"https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2023\/12\/Input_widgets_-_Streamlit_Docs.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2023\/12\/Input_widgets_-_Streamlit_Docs.png\" alt=\"\" width=\"2568\" height=\"1484\" class=\"alignnone size-full wp-image-25565\" srcset=\"https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2023\/12\/Input_widgets_-_Streamlit_Docs.png 2568w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2023\/12\/Input_widgets_-_Streamlit_Docs-300x173.png 300w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2023\/12\/Input_widgets_-_Streamlit_Docs-1024x592.png 1024w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2023\/12\/Input_widgets_-_Streamlit_Docs-768x444.png 768w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2023\/12\/Input_widgets_-_Streamlit_Docs-1536x888.png 1536w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2023\/12\/Input_widgets_-_Streamlit_Docs-2048x1184.png 2048w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2023\/12\/Input_widgets_-_Streamlit_Docs-728x421.png 728w\" sizes=\"auto, (max-width: 2568px) 100vw, 2568px\" \/><\/a><\/p>\n<h2 id=\"image\">\u5b8c\u6210\u30a4\u30e1\u30fc\u30b8<\/h2>\n<p>\u307e\u305a\u306f\u5b8c\u6210\u30a4\u30e1\u30fc\u30b8\u3067\u3059\u3002<\/p>\n<h3>\u901a\u5e38\u306e\u30c1\u30e3\u30c3\u30c8\u30dc\u30c3\u30c8\u6a5f\u80fd<\/h3>\n<ol>\n<li>\u30bb\u30ec\u30af\u30c8\u30dc\u30c3\u30af\u30b9\u3088\u308aBedrock\u306e\u57fa\u76e4\u30e2\u30c7\u30eb\u3092\u9078\u629e\u3057\u307e\u3059<\/li>\n<li>Configurations\u3067\u30ec\u30b9\u30dd\u30f3\u30b9Max\u30c8\u30fc\u30af\u30f3\u3084Temperature\u3001Top_p\u306e\u5024\u3092\u8a2d\u5b9a<\/li>\n<li>\u5165\u529b\u6b04\u304b\u3089prompt\u3092\u5165\u529b\u3057\u3001\u30c1\u30e3\u30c3\u30c8\u30dc\u30c3\u30c8\u3092\u958b\u59cb<\/li>\n<\/ol>\n<a href=\"https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2023\/12\/68a72ca9b5c1f2e835d1496347633d73.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2023\/12\/68a72ca9b5c1f2e835d1496347633d73.png\" alt=\"\" width=\"2866\" height=\"1528\" class=\"alignnone size-full wp-image-25567\" srcset=\"https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2023\/12\/68a72ca9b5c1f2e835d1496347633d73.png 2866w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2023\/12\/68a72ca9b5c1f2e835d1496347633d73-300x160.png 300w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2023\/12\/68a72ca9b5c1f2e835d1496347633d73-1024x546.png 1024w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2023\/12\/68a72ca9b5c1f2e835d1496347633d73-768x409.png 768w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2023\/12\/68a72ca9b5c1f2e835d1496347633d73-1536x819.png 1536w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2023\/12\/68a72ca9b5c1f2e835d1496347633d73-2048x1092.png 2048w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2023\/12\/68a72ca9b5c1f2e835d1496347633d73-728x388.png 728w\" sizes=\"auto, (max-width: 2866px) 100vw, 2866px\" \/><\/a>\n<h3>RAG\u53c2\u7167\u6a5f\u80fd<\/h3>\n<ol>\n<li>RAG\u306e\u4f7f\u7528\u6709\u306b\u9078\u629e\u3057\u3001Knowledge base ID\u306e\u5165\u529b\uff08Bedrock\u306e\u30b3\u30f3\u30bd\u30fc\u30eb\u753b\u9762\u3088\u308a\u30b3\u30d4\u30da\uff09<\/li>\n<li>\u3042\u3068\u306f\u901a\u5e38\u306e\u624b\u9806\u3068\u540c\u69d8<\/li>\n<\/ol>\n<a href=\"https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2023\/12\/075ded52a029b931a4d349e0ab94c5b7.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2023\/12\/075ded52a029b931a4d349e0ab94c5b7.png\" alt=\"\" width=\"2878\" height=\"1564\" class=\"alignnone size-full wp-image-25568\" srcset=\"https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2023\/12\/075ded52a029b931a4d349e0ab94c5b7.png 2878w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2023\/12\/075ded52a029b931a4d349e0ab94c5b7-300x163.png 300w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2023\/12\/075ded52a029b931a4d349e0ab94c5b7-1024x556.png 1024w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2023\/12\/075ded52a029b931a4d349e0ab94c5b7-768x417.png 768w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2023\/12\/075ded52a029b931a4d349e0ab94c5b7-1536x835.png 1536w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2023\/12\/075ded52a029b931a4d349e0ab94c5b7-2048x1113.png 2048w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2023\/12\/075ded52a029b931a4d349e0ab94c5b7-728x396.png 728w\" sizes=\"auto, (max-width: 2878px) 100vw, 2878px\" \/><\/a>\n<h2 id=\"todo\">\u65e9\u901f\u4f5c\u3063\u3066\u307f\u305f<\/h2>\n<p>\u901a\u5e38\u3067\u3042\u308c\u3070\u30ed\u30fc\u30ab\u30eb\u3067\u3082\u554f\u984c\u306a\u304f\u958b\u767a&amp;\u52d5\u4f5c\u78ba\u8a8d\u304c\u51fa\u6765\u308b\u306e\u3067\u3059\u304c\u3001\u79c1\u306e\u30de\u30b7\u30f3\u5074\u306e\u554f\u984c\u306b\u3088\u308a\u52d5\u4f5c\u78ba\u8a8d\u304c\u53d6\u308c\u306a\u304b\u3063\u305f\u305f\u3081Cloud9\u3092\u4f7f\u3063\u3066\u958b\u767a&amp;\u52d5\u4f5c\u78ba\u8a8d\u3092\u884c\u3044\u307e\u3057\u305f\u3002<br \/>\n\u30a8\u30f3\u30b8\u30cb\u30a2\u3068\u3057\u3066\u306f\u539f\u56e0\u3092\u7a81\u304d\u6b62\u3081\u308b\u3079\u304d\u3067\u3059\u304c\u3001\u65e9\u304f\u52d5\u304b\u3057\u3066\u307f\u305f\u3044\u6b32\u304c\u5f37\u304f\u65e9\u3005\u306b\u8abf\u67fb\u3092\u8ae6\u3081\u307e\u3057\u305f\uff08\u8a00\u3044\u8a33\uff09<\/p>\n<p>Cloud9\u3092\u69cb\u7bc9\u5f8c\u3001\u3069\u3046\u3084\u3089\u30b9\u30da\u30c3\u30af\u304c\u5c0f\u3055\u3044\u3053\u3068\u304c\u539f\u56e0\u3088\u3046\u3067\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3067\u4f55\u5ea6\u304b\u6b62\u307e\u3063\u3066\u3057\u307e\u3063\u305f\u306e\u3067\u300ct3.small\u300d\u306b\u30b9\u30b1\u30fc\u30eb\u30a2\u30c3\u30d7\u3055\u305b\u307e\u3057\u305f\u3002<br \/>\n\u307e\u305f\u691c\u8a3c\u3067\u4f7f\u7528\u3059\u308b8080\u30dd\u30fc\u30c8\u3092\u7279\u5b9aIP\u306e\u307f\u8a31\u53ef\u306eSG\u3092\u30a2\u30bf\u30c3\u30c1\u3055\u305b\u307e\u3059\u3002<\/p>\n<p>\u6b21\u306b\u4e0b\u8a18\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3066\u6e96\u5099\u306f\u5b8c\u4e86\u3067\u3059\u3002<\/p>\n<pre>\nstreamlit==1.29.0\nboto3==1.34.10\n<\/pre>\n<p>\u4e0b\u8a18\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306f\u3001\u30b5\u30a4\u30c9\u30d0\u30fc\u306e\u8a2d\u5b9a\u51e6\u7406\u306e\u629c\u7c8b\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\n\u305f\u3063\u305f\u3053\u308c\u3060\u3051\u3067\u30b5\u30a4\u30c9\u30d0\u30fc\u90e8\u5206\u306e\u5b9f\u88c5\u306f\u7d42\u308f\u308a\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\n\u57fa\u76e4\u30e2\u30c7\u30eb\u306e\u8a2d\u5b9a\u5024\u3092\u30bb\u30c3\u30b7\u30e7\u30f3\u306b\u8a70\u3081\u3066\u3001\u5f8c\u307b\u3069\u547c\u3073\u51fa\u3059Bedrock\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u306b\u6e21\u3059\u5f62\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>\ndef init_page():\n    \n    st.set_page_config(page_title=\"ChatBot\", page_icon=\"\ud83e\udd17\")\n    # \u30b5\u30a4\u30c9\u30d0\u30fc\u3092\u8868\u793a\n    st.sidebar.title(\"\u57fa\u76e4\u30e2\u30c7\u30eb\u8a2d\u5b9a\")\n    \n    # \u5de6\u30b5\u30a4\u30c9\u30d0\u30fc\n    with st.sidebar:\n        bedrock_model = st.selectbox(\"Bedrock\u306e\u30e2\u30c7\u30eb\u3092\u9078\u629e\u3057\u3066\u304f\u3060\u3055\u3044\", (BEDROCK_MODEL_LIST))\n        st.session_state[\"bedrock_model\"] = bedrock_model\n    \n        with st.expander(label=\"RAG\", expanded=False):\n            rag_on = st.toggle(\"Knowledge base\")\n            if rag_on:\n                st.session_state[\"knowledge_base_id\"] = st.text_input(label=\"Knowledge base ID\", type=\"default\")\n            st.session_state[\"rag_on\"] = rag_on\n    \n        with st.expander(label=\"Configurations\", expanded=True):\n            max_tokens_to_sample = st.slider(label=\"Maximum length\", min_value=0, max_value=2048, value=300)\n            st.session_state[\"max_tokens_to_sample\"] = max_tokens_to_sample\n    \n            temperature = st.slider(label=\"Temperature\", min_value=0.0, max_value=1.0, value=0.1, step=0.1)\n            st.session_state[\"temperature\"] = temperature\n    \n            top_p = st.slider(label=\"top_p\", min_value=0.00, max_value=1.00, value=0.90, step=0.01)\n            st.session_state[\"top_p\"] = top_p\n<\/pre>\n<p>\u6b21\u306b\u30c1\u30e3\u30c3\u30c8\u30dc\u30c3\u30c8\u306eI\/F\u90e8\u5206\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\n\u3053\u3061\u3089\u3082Streamlit\u306e\u90e8\u54c1\u3092\u6700\u5927\u9650\u306b\u5229\u7528\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\nRAG\u306e\u4f7f\u7528\u6709\u7121\u306b\u3088\u3063\u3066\u547c\u3073\u51fa\u3059\u95a2\u6570\u3092\u5206\u3051\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre>\ndef show_page():\n    \n    # \u30e6\u30fc\u30b6\u30fc\u304b\u3089\u306e\u65b0\u3057\u3044\u5165\u529b\u3092\u53d6\u5f97(Bedrock\u7528)\n    if prompt := st.chat_input(\"\u4f55\u304b\u8cea\u554f\u3092\u3069\u3046\u305e\"):\n        st.session_state.messages.append({\"role\": \"Human\", \"content\": prompt})\n        with st.chat_message(\"Human\"):\n            st.markdown(prompt)\n    \n        with st.chat_message(\"Assistant\"):\n            with st.spinner(\"\u56de\u7b54\u3092\u751f\u6210\u4e2d...\"):\n                message_placeholder = st.empty()\n                full_response = \"\"\n    \n                if st.session_state[\"rag_on\"]:\n                    full_response = _retrieve_and_generate(prompt, message_placeholder)\n                else:\n                    full_response = _invoke_model_with_response_stream_claude(prompt, message_placeholder, full_response)\n                \n        st.session_state.messages.append({\"role\": \"Assistant\", \"content\": full_response})\n        st.session_state.Clear = True  # \u30c1\u30e3\u30c3\u30c8\u5c65\u6b74\u306e\u30af\u30ea\u30a2\u30dc\u30bf\u30f3\u3092\u6709\u52b9\u306b\u3059\u308b\n\n    ....\n<\/pre>\n<p>RAG\u3092\u4f7f\u7528\u3057\u306a\u3044\u5834\u5408\u306e\u547c\u3073\u51fa\u3057\u65b9\u6cd5\u3067\u3059\u3002<br \/>\n\u30b9\u30c8\u30ea\u30fc\u30df\u30f3\u30b0\u5fdc\u7b54\u53ef\u80fd\u306aAPI\u3092\u4f7f\u7528\u3057\u3066\u3001\u753b\u9762\u8868\u793a\u3082\u305d\u308c\u3063\u307d\u304f\u5de5\u592b\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre>\ndef _invoke_model_with_response_stream_claude(input, message_placeholder, full_response):\n    # Bedrock\u304b\u3089\u306e\u30b9\u30c8\u30ea\u30fc\u30df\u30f3\u30b0\u5fdc\u7b54\u3092\u51e6\u7406\n\n    bedrock = boto3.client(service_name=\"bedrock-runtime\", region_name=REGION_NAME)\n    messages = [m[\"role\"] + \":\" + m[\"content\"] for m in st.session_state.messages]\n\n    body = json.dumps(\n        {\n            \"prompt\": \"\\n\\n\" + \"\\n\\n\".join(messages) + \"\\n\\nAssistant:\",\n            \"max_tokens_to_sample\": st.session_state[\"max_tokens_to_sample\"],\n            \"temperature\": st.session_state[\"temperature\"],\n            \"top_p\": st.session_state[\"top_p\"],\n        }\n    )\n\n    response = bedrock.invoke_model_with_response_stream(modelId=st.session_state[\"bedrock_model\"], body=body)\n    stream = response.get(\"body\")\n    if stream:\n        for event in stream:\n            chunk = event.get(\"chunk\")\n            if chunk:\n                full_response += json.loads(chunk.get(\"bytes\").decode())[\"completion\"]\n                message_placeholder.markdown(full_response + \"\u258c\")\n\n        message_placeholder.markdown(full_response)\n\n    return full_response\n<\/pre>\n<p>RAG\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u306e\u547c\u3073\u51fa\u3057\u65b9\u6cd5\u3067\u3059\u3002<br \/>\n\u3053\u3061\u3089\u306f\u30b9\u30c8\u30ea\u30fc\u30df\u30f3\u30b0\u5fdc\u7b54\u53ef\u80fd\u306aAPI\u304c\u898b\u3064\u304b\u3089\u306a\u304b\u3063\u305f\u306e\u3067\u3001\u30ec\u30b9\u30dd\u30f3\u30b9\u7d50\u679c\u304b\u3089\u5fc5\u8981\u306a\u90e8\u5206\u3092\u629c\u304d\u51fa\u3057\u3066\u3001\u753b\u9762\u8868\u793a\u3055\u305b\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre>\ndef _retrieve_and_generate(input, message_placeholder):\n    # Bedrock\u304b\u3089\u306eRAG\u5fdc\u7b54\u3092\u51e6\u7406\n\n    bedrock_agent_runtime = boto3.client(\"bedrock-agent-runtime\", region_name=REGION_NAME)\n    full_response = bedrock_agent_runtime.retrieve_and_generate(\n        input={\"text\": input},\n        retrieveAndGenerateConfiguration={\n            \"type\": \"KNOWLEDGE_BASE\",\n            \"knowledgeBaseConfiguration\": {\n                \"knowledgeBaseId\": st.session_state[\"knowledge_base_id\"],\n                \"modelArn\": f'arn:aws:bedrock:{REGION_NAME}::foundation-model\/{st.session_state[\"bedrock_model\"]}',\n            },\n        },\n    )[\"output\"][\"text\"]\n\n    message_placeholder.markdown(full_response)\n    return full_response\n<\/pre>\n<p>\u5c11\u3057\u7701\u7565\u3057\u3066\u3054\u7d39\u4ecb\u3057\u3066\u3044\u307e\u3059\u304c\u5168\u90e8\u3067150\u884c\u7a0b\u5ea6\u3067\u3053\u306e\u3088\u3046\u306a\u30c1\u30e3\u30c3\u30c8\u30dc\u30c3\u30c8\u3092\u4f5c\u6210\u3059\u308b\u3053\u3068\u304c\u51fa\u6765\u307e\u3059\u3002<br \/>\n\u304a\u597d\u307f\u306e\u30c7\u30b6\u30a4\u30f3\u3067\u5b9f\u88c5\u3057\u3066\u305c\u3072\u30c8\u30e9\u30a4\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<h2 id=\"tenbo\">\u5c55\u671b<\/h2>\n<p>\u3053\u3053\u307e\u3067\u3067\u7c21\u5358\u306b\u30c1\u30e3\u30c3\u30c8\u30dc\u30c3\u30c8\u30b7\u30b9\u30c6\u30e0\u304c\u4f5c\u308c\u308b\u3053\u3068\u306f\u304a\u5206\u304b\u308a\u306b\u306a\u3063\u305f\u304b\u3068\u601d\u3044\u307e\u3059\u304c\u3001\u5b9f\u969b\u306b\u672c\u756a\u904b\u7528\u3059\u308b\u4e0a\u3067\u306f\u8db3\u308a\u3066\u3044\u306a\u3044\u6a5f\u80fd\u304c\u3044\u304f\u3064\u304b\u3042\u308b\u304b\u3068\u601d\u3044\u307e\u3059\u3002\uff08PoC\u3067\u30b5\u30af\u30c3\u3068\u4f5c\u308b\u5206\u306b\u306f\u7533\u3057\u5206\u306a\u3044\u3067\u3059\u304c\uff09<br \/>\nStreamlit\u3092AWS\u4e0a\u306b\u69cb\u7bc9\u3059\u308b\u306a\u3089\u3001\u3053\u306e\u3088\u3046\u306a\u30b7\u30b9\u30c6\u30e0\u69cb\u6210\u3067\u69cb\u7bc9\u304c\u30d9\u30b9\u30c8\u304b\u306a\u3068\u8003\u3048\u3066\u3044\u307e\u3059\u306e\u3067\u89e3\u8aac\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<a href=\"https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2023\/12\/9b96c0a2e954fc1ca2dccb8ab4503c3f.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2023\/12\/9b96c0a2e954fc1ca2dccb8ab4503c3f.png\" alt=\"\" width=\"4115\" height=\"1998\" class=\"alignnone size-full wp-image-25571\" srcset=\"https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2023\/12\/9b96c0a2e954fc1ca2dccb8ab4503c3f.png 4115w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2023\/12\/9b96c0a2e954fc1ca2dccb8ab4503c3f-300x146.png 300w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2023\/12\/9b96c0a2e954fc1ca2dccb8ab4503c3f-1024x497.png 1024w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2023\/12\/9b96c0a2e954fc1ca2dccb8ab4503c3f-768x373.png 768w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2023\/12\/9b96c0a2e954fc1ca2dccb8ab4503c3f-1536x746.png 1536w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2023\/12\/9b96c0a2e954fc1ca2dccb8ab4503c3f-2048x994.png 2048w, https:\/\/www.skyarch.net\/blog\/wp-content\/uploads\/2023\/12\/9b96c0a2e954fc1ca2dccb8ab4503c3f-728x353.png 728w\" sizes=\"auto, (max-width: 4115px) 100vw, 4115px\" \/><\/a>\n<ul>\n<li>ALB -&gt; ECS on Fargate\uff1aStreamlit\u3092Private\u30b5\u30d6\u30cd\u30c3\u30c8\u306eFargate\u4e0a\u3067\u52d5\u4f5c<\/li>\n<li>Code\u5144\u5f1f\uff1a\u30b3\u30f3\u30c6\u30ca\u30a4\u30e1\u30fc\u30b8\u306e\u4f5c\u6210\u53ca\u3073Fargate\u3078\u306e\u30c7\u30d7\u30ed\u30a4<\/li>\n<li>DynamoDB\uff1a\u4f1a\u8a71\u306e\u5c65\u6b74\u4fdd\u5b58\u306e\u305f\u3081\u306b\u4f7f\u7528<\/li>\n<li>VPC\u30a8\u30f3\u30c9\u30dd\u30a4\u30f3\u30c8\uff1aDynamoDB\u3084Bedrock\u3001CloudWatch\u3078\u306f\u5168\u3066AWS\u5185\u7d4c\u7531\u3067\u30a2\u30af\u30bb\u30b9<\/li>\n<li>Cognito\uff1a\u8a8d\u8a3c\u6a5f\u80fd<\/li>\n<\/ul>\n<p>\u3056\u3063\u3068\u601d\u3044\u3064\u304f\u9650\u308a\u3067\u66f8\u3044\u3066\u307f\u307e\u3057\u305f\u304c\u3053\u306e\u3088\u3046\u306a\u5f62\u304c\u30d9\u30b9\u30c8\u3067\u3057\u3087\u3046\u304b\u3002<br \/>\nCDK\u3067\u5b9a\u7fa9\u3057\u3066\u304a\u3044\u3066\u3001\u30c1\u30e3\u30c3\u30c8\u30dc\u30c3\u30c8\u30b7\u30b9\u30c6\u30e0\u3068\u3057\u3066\u3059\u3050\u306b\u74b0\u5883\u69cb\u7bc9\u304c\u51fa\u6765\u308c\u3070\u5b8c\u74a7\u3067\u3059\u306d\u3002<\/p>\n<p>Streamlit\u3092\u4f7f\u3046\u6a5f\u4f1a\u304c\u3042\u308b\u65b9\u306b\u5c11\u3057\u3067\u3082\u53c2\u8003\u306b\u306a\u308c\u3070\u5e78\u3044\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u306f\u3058\u3081\u306b Amazon Bedrock\u306b\u89e6\u308c\u3066\u307f\u3088\u3046\uff01\u306e\u7b2c\u4e09\u5f3e\u3067\u3059\u3002 \u5c11\u3057\u756a\u5916\u7de8\u3068\u306a\u308a\u307e\u3059\u304c\u3001\u3053\u308c\u307e\u3067AWS\u306e\u30b3\u30f3\u30bd\u30fc\u30eb\u753b\u9762\u3084SDK\u3092\u4f7f\u7528\u3057\u3066Bedrock\u306e\u691c\u8a3c\u3092\u884c\u3063\u3066\u304d\u307e\u3057\u305f\u304c\u3001Streamlit\u3068\u3044\u3046Python\u306e&#8230;<\/p>\n","protected":false},"author":135,"featured_media":25563,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_locale":"ja","_original_post":"https:\/\/www.skyarch.net\/blog\/?p=25562","footnotes":""},"categories":[1276,1277],"tags":[],"class_list":{"0":"post-25562","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-amazon-bedrock","8":"category-ai","9":"ja"},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts\/25562","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/users\/135"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/comments?post=25562"}],"version-history":[{"count":17,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts\/25562\/revisions"}],"predecessor-version":[{"id":25604,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/posts\/25562\/revisions\/25604"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/media\/25563"}],"wp:attachment":[{"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/media?parent=25562"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/categories?post=25562"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skyarch.net\/blog\/wp-json\/wp\/v2\/tags?post=25562"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}