← 返回蜂巢洞察

使用 LangChain 和 Amazon Bedrock 构建 Streamlit 应用程序

构建强大的机器学习模型是一回事,而使它们变得有用又是另一回事。其中很大一部分是能 … Read More

Socrates

构建强大的机器学习模型是一回事,而使它们变得有用又是另一回事。其中很大一部分是能够构建应用程序以向最终用户公开其功能。流行的例子包括 ChatGPT、Midjourney 等。

Streamlit 是一个开源 Python 库这使得构建机器学习和数据科学的 Web 应用程序变得容易。它有一套丰富的可视化组件API,包括几个聊天工具元素,使得构建会话代理或聊天机器人变得非常方便,特别是与 LLM(大型语言模型)结合使用时。

这也是本博文的示例 – 部署到 Amazon EKS。但这还不是全部!

我们将使用 Streamlit 和 LangChain ,这是一个用于开发由语言模型支持的应用程序的框架。 LangChain 的好处是它支持许多平台和 LLM,包括 Amazon Bedrock(将用于我们的应用程序)。

聊天应用程序的一个关键部分是能够引用历史对话 – 至少在特定时间范围(窗口)内。在LangChain中,这被称为内存。就像LLM一样,您可以插入不同的系统作为内存LangChain应用程序的组件。其中包括 Redis,它是此用例的绝佳选择,因为它是具有灵活数据结构的高性能内存数据库。 Redis 与 结合已经成为实时应用程序(包括聊天)的首选Pub/SubWebSocket< /a>.此应用程序将使用 Amazon ElastiCache Serverless for Redis,该选项可简化缓存管理并即时扩展。这是宣布的在 re:Invent 2023 上,让我们趁它还新鲜的时候探索一下吧!

说实话,该应用程序可以部署在其他计算选项上,例如 Amazon ECS,但我认为由于它需要调用 Amazon Bedrock,因此这是一个很好的机会来介绍如何使用 EKS Pod 身份 (也在 re:Invent 2023 上宣布!!)

GitHub 应用程序的存储库

这是一个简化的高级图表:

<图>

<源尺寸=“(最小分辨率:4dppx)和(最大宽度:700px)50vw,(-webkit-min-device-pixel-ratio:4)和(最大宽度:700px)50vw,(最小分辨率:3dppx)和(最大宽度:700px)67vw,(-webkit-min-device-pixel-ratio:3)和(最大宽度:700px)65vw,(最小分辨率:2.5dppx)和(最大宽度:700px)80vw,(-webkit-最小设备像素比:2.5)和(最大宽度:700px)80vw,(最小分辨率:2dppx)和(最大宽度:700px)100vw,(-webkit-最小设备像素比:2)和(最大宽度:700px)100vw,700px“srcset =”https://miro.medium.com/v2/resize:fit:640/format:webp/0*6Df3EOAqI9 -DpLR4.png 640w,https://miro.medium.com/v2/resize:fit:720/format:webp/0*6Df3EOAqI9-DpLR4.png 720w,https://miro.medium.com/v2/resize :fit:750/format:webp/0*6Df3EOAqI9-DpLR4.png 750w,https://miro.medium.com/v2/resize:fit:786/format:webp/0*6Df3EOAqI9-DpLR4.png 786w,https ://miro.medium.com/v2/resize:fit:828/format:webp/0*6Df3EOAqI9-DpLR4.png 828w,https://miro.medium.com/v2/resize:fit:1100/format: webp/0*6Df3EOAqI9-DpLR4.png 1100w,https://miro.medium.com/v2/resize:fit:1400/format:webp/0*6Df3EOAqI9-DpLR4.png 1400w" type="image/webp"/ >
<源data-testid =“og”尺寸=“(最小分辨率:4dppx)和(最大宽度:700px)50vw,(-webkit-min-device-pixel-ratio:4)和(最大宽度:700px) )50vw,(最小分辨率:3dppx)和(最大宽度:700px)67vw,(-webkit-min-device-pixel-ratio:3)和(最大宽度:700px)65vw,(最小分辨率:2.5 dppx)和(最大宽度:700px)80vw,(-webkit-min-device-pixel-ratio:2.5)和(最大宽度:700px)80vw,(最小分辨率:2dppx)和(最大宽度:700px ) 100vw, (-webkit-min-device-pixel-ratio: 2) 和 (最大宽度: 700px) 100vw, 700px" srcset="https://miro.medium.com/v2/resize:fit:640/ 0*6Df3EOAqI9-DpLR4.png 640w,https://miro.medium.com/v2/resize:fit:720/0*6Df3EOAqI9-DpLR4.png 720w,https://miro.medium.com/v2/resize:适合:750/0*6Df3EOAqI9-DpLR4.png 750w,https://miro.medium.com/v2/resize:fit:786/0*6Df3EOAqI9-DpLR4.png 786w,https://miro.medium.com/ v2/resize:fit:828/0*6Df3EOAqI9-DpLR4.png 828w,https://miro.medium.com/v2/resize:fit:1100/0*6Df3EOAqI9-DpLR4.png 1100w,https://miro。 medium.com/v2/resize:fit:1400/0*6Df3EOAqI9-DpLR4.png 1400w"/>

图表

我们走吧!!

基本设置

将 Docker 映像推送到 ECR 并将应用程序部署到 EKS

克隆 GitHub 存储库:

 

git 克隆 https://github.com/abhirockzz/streamlit-langchain-chatbot-bedrock-redis-memory
cdstreamlit-langchain-chatbot-bedrock-redis-内存

创建 ECR 存储库

 

{ elem.addEventListener(event, (e) => fn(e, elem), false); }); } } const onLoad = function () { let lastKnownScrollPosition = window.scrollY; let ticking = true; attachEvent('#header nav', 'click', function () { document.querySelector('[data-aw-toggle-menu]')?.classList.remove('expanded'); document.body.classList.remove('overflow-hidden'); document.getElementById('header')?.classList.remove('h-screen'); document.getElementById('header')?.classList.remove('expanded'); document.getElementById('header')?.classList.remove('bg-page'); document.querySelector('#header nav')?.classList.add('hidden'); document.querySelector('#header > div > div:last-child')?.classList.add('hidden'); }); attachEvent('[data-aw-toggle-menu]', 'click', function (_, elem) { elem.classList.toggle('expanded'); document.body.classList.toggle('overflow-hidden'); document.getElementById('header')?.classList.toggle('h-screen'); document.getElementById('header')?.classList.toggle('expanded'); document.getElementById('header')?.classList.toggle('bg-page'); document.querySelector('#header nav')?.classList.toggle('hidden'); document.querySelector('#header > div > div:last-child')?.classList.toggle('hidden'); }); attachEvent('[data-aw-toggle-color-scheme]', 'click', function () { if (defaultTheme.endsWith(':only')) { return; } Observer.removeAnimationDelay(); document.documentElement.classList.toggle('dark'); localStorage.theme = document.documentElement.classList.contains('dark') ? 'dark' : 'light'; }); attachEvent('[data-aw-social-share]', 'click', function (_, elem) { const network = elem.getAttribute('data-aw-social-share'); const url = encodeURIComponent(elem.getAttribute('data-aw-url')); const text = encodeURIComponent(elem.getAttribute('data-aw-text')); let href; switch (network) { case 'facebook': href = `https://www.facebook.com/sharer.php?u=${url}`; break; case 'twitter': href = `https://twitter.com/intent/tweet?url=${url}&text=${text}`; break; case 'linkedin': href = `https://www.linkedin.com/shareArticle?mini=true&url=${url}&title=${text}`; break; case 'whatsapp': href = `https://wa.me/?text=${text}%20${url}`; break; case 'mail': href = `mailto:?subject=%22${text}%22&body=${text}%20${url}`; break; default: return; } const newlink = document.createElement('a'); newlink.target = '_blank'; newlink.href = href; newlink.click(); }); const screenSize = window.matchMedia('(max-width: 767px)'); screenSize.addEventListener('change', function () { document.querySelector('[data-aw-toggle-menu]')?.classList.remove('expanded'); document.body.classList.remove('overflow-hidden'); document.getElementById('header')?.classList.remove('h-screen'); document.getElementById('header')?.classList.remove('expanded'); document.getElementById('header')?.classList.remove('bg-page'); document.querySelector('#header nav')?.classList.add('hidden'); document.querySelector('#header > div > div:last-child')?.classList.add('hidden'); }); function applyHeaderStylesOnScroll() { const header = document.querySelector('#header[data-aw-sticky-header]'); if (!header) return; if (lastKnownScrollPosition > 60 && !header.classList.contains('scroll')) { header.classList.add('scroll'); } else if (lastKnownScrollPosition <= 60 && header.classList.contains('scroll')) { header.classList.remove('scroll'); } ticking = false; } applyHeaderStylesOnScroll(); attachEvent([document], 'scroll', function () { lastKnownScrollPosition = window.scrollY; if (!ticking) { window.requestAnimationFrame(() => { applyHeaderStylesOnScroll(); }); ticking = true; } }); }; const onPageShow = function () { document.documentElement.classList.add('motion-safe:scroll-smooth'); const elem = document.querySelector('[data-aw-toggle-menu]'); if (elem) { elem.classList.remove('expanded'); } document.body.classList.remove('overflow-hidden'); document.getElementById('header')?.classList.remove('h-screen'); document.getElementById('header')?.classList.remove('expanded'); document.querySelector('#header nav')?.classList.add('hidden'); }; window.onload = onLoad; window.onpageshow = onPageShow; document.addEventListener('astro:after-swap', () => { initTheme(); onLoad(); onPageShow(); }); })();