太原建设网站制作,企业做网站需要多少钱,广告设计与制作专升本考什么,网站全局搜索如何做本篇文章#xff0c;聊聊怎么比较稳定的使用 Anthropic Claude 3#xff0c;以及基于目前表现非常好的模型#xff0c;来做一些有趣的 AI Native 小工具。
写在前面
在实际体验了半个多月#xff0c;月初上线的 Anthropic Claude Pro 后#xff0c;发现 Claude 3 系列模…本篇文章聊聊怎么比较稳定的使用 Anthropic Claude 3以及基于目前表现非常好的模型来做一些有趣的 AI Native 小工具。
写在前面
在实际体验了半个多月月初上线的 Anthropic Claude Pro 后发现 Claude 3 系列模型能力确实很不错Opus、Sonnet、Haiku在指令遵循和响应速度上都有比较明显的变化。 不过个人注册的账号和去年的 ChatGPT Plus 一样借给朋友体验使用因为不同的客户端登录登录 IP 切换很容易遇到风控问题比如需要不确定时间的协助登录确认更麻烦的会遇到账号封禁要提工单解除账号限制。
前一阵亚马逊 AWS 的 Bedrock 基础模型FM平台上更新了 Anthropic Claude 3 模型。周末花了一些时间我对上一篇文章中提到的 Dify 做了一些模型适配工作现在只需要几条命令我们就能够通过 Docker 快速地启动一套基于 Claude 3 的 AI 小东西啦比如 Chat Claude3、Claude3 AI Apps、Claude 3 Bot 的懒人工具啦。 针对 Dify 的相关代码改动已经提交至 Dify 官方仓库目测官方应用这些功能调整应该还需要一段时间。所以想快速尝鲜的同学可以先访问和使用我的仓库来获取配置或者自行构建 Docker 镜像soulteary/dify。
当然如果你想使用更现成一些的应用可以下载网盘中的 Docker 镜像来快速体验如何分分钟搭建属于你自己的 “Claude3 AI 应用”。
还是先从准备工作开始。
准备工作
需要准备的内容一共有三项
能够运行 Docker 应用的设备环境本地或云主机都行。下载我构建好的 Dify 应用镜像或使用我提供的源代码仓库进行手动构建。申请亚马逊 AWS Bedrock Claude 3 模型使用权限。
准备工作Docker 运行环境
借助 Docker我们能够在投入很少额外资源的情况下快速获得纯净、可复现的、一致性非常棒的环境。
不论你的设备硬件或云主机是否包含显卡都可以根据自己的操作系统喜好参考这两篇来完成基础环境的配置《基于 Docker 的深度学习环境Windows 篇》、《基于 Docker 的深度学习环境入门篇》。当然安装 Docker 之后还可以做很多其他的有趣的事情比如之前几十篇有关 Docker 的实践在此就不赘述啦。
如果你已经有了一个可以运行 Docker 的环境我们可以来准备下一项内容。
准备工作下载或构建修改后的 Dify 应用镜像
晚些时候我会上传构建好的镜像到网盘你可以在专栏的评论区找到资源下载地址。镜像下载完毕后可以使用下面的命令加载镜像
docker load -i dify-api-claude3.tar如果你更喜欢从头构建可以执行下面的命令
git clone https://github.com/soulteary/dify.git
cd dify/api
git checkout wow/so-lazy
docker build -t soulteary/dify-api:claude3 .等待命令执行完毕镜像相关的准备工作就完成啦。
准备工作AWS Bedrock 的 Claude3 使用权限申请
账号注册等一系列“一路 Next”的简单操作我们这里就不花时间赘述啦。
当我们登录 “AWS Bedrock”打开模型访问权限管理页面默认情况下可能并没有 “Anthropic Claude3” 模型的使用权限。这个时候提交工单申请开通权限即可。 等权限开通后模型权限页面就能够看到“一切就绪”啦。 访问“用户凭证管理”页面创建一个凭证后下载凭证文件credentials后我们的准备工作就都完成啦。
实践开始
这里同样包括三个步骤
使用 Docker 启动 Dify完成应用基础配置让我们能够在浏览器中管理和配置后续搭建的具体 AI 应用。在 Dify 里完成 Claude3 或者其他的模型资源的配置让我们的程序能够调用这些模型来进行内容生成。使用 Dify 简单的折腾几个常见应用比如 ChatBot最简单的知识库。
快速完成 Dify 的配置和启动
想要快速的使用 Docker 启动 Dify我们只需要准备两个文件分别是 Docker 编排文件 docker-compose.yml 和 Nginx 配置文件nginx.conf。
version: 3.1
services:# API serviceapi:image: soulteary/dify-api:claude3restart: alwaysenvironment:# Startup mode, api starts the API server.MODE: api# The log level for the application. Supported values are DEBUG, INFO, WARNING, ERROR, CRITICALLOG_LEVEL: INFO# A secret key that is used for securely signing the session cookie and encrypting sensitive information on the database. You can generate a strong key using openssl rand -base64 42.SECRET_KEY: sk-9f73s3ljTXVcMT3Blb3ljTqtsKiGHXVcMT3BlbkFJLK7U# The base URL of console application web frontend, refers to the Console base URL of WEB service if console domain is# different from api or web app domain.# example: http://cloud.dify.aiCONSOLE_WEB_URL: # Password for admin user initialization.# If left unset, admin user will not be prompted for a password when creating the initial admin account.INIT_PASSWORD: # The base URL of console application api server, refers to the Console base URL of WEB service if console domain is# different from api or web app domain.# example: http://cloud.dify.aiCONSOLE_API_URL: # The URL prefix for Service API endpoints, refers to the base URL of the current API service if api domain is# different from console domain.# example: http://api.dify.aiSERVICE_API_URL: # The URL prefix for Web APP frontend, refers to the Web App base URL of WEB service if web app domain is different from# console or api domain.# example: http://udify.appAPP_WEB_URL: # File preview or download Url prefix.# used to display File preview or download Url to the front-end or as Multi-model inputs;# Url is signed and has expiration time.FILES_URL: # When enabled, migrations will be executed prior to application startup and the application will start after the migrations have completed.MIGRATION_ENABLED: true# The configurations of postgres database connection.# It is consistent with the configuration in the db service below.DB_USERNAME: postgresDB_PASSWORD: difyai123456DB_HOST: dbDB_PORT: 5432DB_DATABASE: dify# The configurations of redis connection.# It is consistent with the configuration in the redis service below.REDIS_HOST: redisREDIS_PORT: 6379REDIS_USERNAME: REDIS_PASSWORD: difyai123456REDIS_USE_SSL: false# use redis db 0 for redis cacheREDIS_DB: 0# The configurations of celery broker.# Use redis as the broker, and redis db 1 for celery broker.CELERY_BROKER_URL: redis://:difyai123456redis:6379/1# Specifies the allowed origins for cross-origin requests to the Web API, e.g. https://dify.app or * for all origins.WEB_API_CORS_ALLOW_ORIGINS: *# Specifies the allowed origins for cross-origin requests to the console API, e.g. https://cloud.dify.ai or * for all origins.CONSOLE_CORS_ALLOW_ORIGINS: *# The type of storage to use for storing user files. Supported values are local and s3, Default: localSTORAGE_TYPE: local# The path to the local storage directory, the directory relative the root path of API service codes or absolute path. Default: storage or /home/john/storage.# only available when STORAGE_TYPE is local.STORAGE_LOCAL_PATH: storage# The S3 storage configurations, only available when STORAGE_TYPE is s3.S3_ENDPOINT: https://xxx.r2.cloudflarestorage.comS3_BUCKET_NAME: difyaiS3_ACCESS_KEY: ak-difyaiS3_SECRET_KEY: sk-difyaiS3_REGION: us-east-1# The type of vector store to use. Supported values are weaviate, qdrant, milvus.VECTOR_STORE: weaviate# The Weaviate endpoint URL. Only available when VECTOR_STORE is weaviate.WEAVIATE_ENDPOINT: http://weaviate:8080# The Weaviate API key.WEAVIATE_API_KEY: WVF5YThaHlkYwhGUSmCRgsX3tD5ngdN8pkih# Mail configuration, support: resend, smtpMAIL_TYPE: # default send from email address, if not specifiedMAIL_DEFAULT_SEND_FROM: YOUR EMAIL FROM (eg: no-reply no-replydify.ai)SMTP_SERVER: SMTP_PORT: 587SMTP_USERNAME: SMTP_PASSWORD: SMTP_USE_TLS: true# the api-key for resend (https://resend.com)RESEND_API_KEY: RESEND_API_URL: https://api.resend.com# The DSN for Sentry error reporting. If not set, Sentry error reporting will be disabled.SENTRY_DSN: # The sample rate for Sentry events. Default: 1.0SENTRY_TRACES_SAMPLE_RATE: 1.0# The sample rate for Sentry profiles. Default: 1.0SENTRY_PROFILES_SAMPLE_RATE: 1.0depends_on:- db- redisvolumes:# Mount the storage directory to the container, for storing user files.- ./volumes/app/storage:/app/api/storage# worker service# The Celery worker for processing the queue.worker:image: soulteary/dify-api:claude3restart: alwaysenvironment:# Startup mode, worker starts the Celery worker for processing the queue.MODE: worker# --- All the configurations below are the same as those in the api service. ---# The log level for the application. Supported values are DEBUG, INFO, WARNING, ERROR, CRITICALLOG_LEVEL: INFO# A secret key that is used for securely signing the session cookie and encrypting sensitive information on the database. You can generate a strong key using openssl rand -base64 42.# same as the API serviceSECRET_KEY: sk-9f73s3ljTXVcMT3Blb3ljTqtsKiGHXVcMT3BlbkFJLK7U# The configurations of postgres database connection.# It is consistent with the configuration in the db service below.DB_USERNAME: postgresDB_PASSWORD: difyai123456DB_HOST: dbDB_PORT: 5432DB_DATABASE: dify# The configurations of redis cache connection.REDIS_HOST: redisREDIS_PORT: 6379REDIS_USERNAME: REDIS_PASSWORD: difyai123456REDIS_DB: 0REDIS_USE_SSL: false# The configurations of celery broker.CELERY_BROKER_URL: redis://:difyai123456redis:6379/1# The type of storage to use for storing user files. Supported values are local and s3, Default: localSTORAGE_TYPE: localSTORAGE_LOCAL_PATH: storage# The type of vector store to use. Supported values are weaviate, qdrant, milvus.VECTOR_STORE: weaviate# The Weaviate endpoint URL. Only available when VECTOR_STORE is weaviate.WEAVIATE_ENDPOINT: http://weaviate:8080# The Weaviate API key.WEAVIATE_API_KEY: WVF5YThaHlkYwhGUSmCRgsX3tD5ngdN8pkih# Mail configuration, support: resendMAIL_TYPE: # default send from email address, if not specifiedMAIL_DEFAULT_SEND_FROM: YOUR EMAIL FROM (eg: no-reply no-replydify.ai)# the api-key for resend (https://resend.com)RESEND_API_KEY: RESEND_API_URL: https://api.resend.comdepends_on:- db- redisvolumes:# Mount the storage directory to the container, for storing user files.- ./volumes/app/storage:/app/api/storage# Frontend web application.web:image: langgenius/dify-web:0.5.9restart: alwaysenvironment:EDITION: SELF_HOSTED# The base URL of console application api server, refers to the Console base URL of WEB service if console domain is# different from api or web app domain.# example: http://cloud.dify.aiCONSOLE_API_URL: # The URL for Web APP api server, refers to the Web App base URL of WEB service if web app domain is different from# console or api domain.# example: http://udify.appAPP_API_URL: # The DSN for Sentry error reporting. If not set, Sentry error reporting will be disabled.SENTRY_DSN: # The postgres database.db:image: postgres:15-alpinerestart: alwaysenvironment:PGUSER: postgres# The password for the default postgres user.POSTGRES_PASSWORD: difyai123456# The name of the default postgres database.POSTGRES_DB: dify# postgres data directoryPGDATA: /var/lib/postgresql/data/pgdatavolumes:- ./volumes/db/data:/var/lib/postgresql/datahealthcheck:test: [ CMD, pg_isready ]interval: 1stimeout: 3sretries: 30# The redis cache.redis:image: redis:6-alpinerestart: alwaysvolumes:# Mount the redis data directory to the container.- ./volumes/redis/data:/data# Set the redis password when startup redis server.command: redis-server --requirepass difyai123456healthcheck:test: [ CMD, redis-cli, ping ]# The Weaviate vector store.weaviate:image: semitechnologies/weaviate:1.19.0restart: alwaysvolumes:# Mount the Weaviate data directory to the container.- ./volumes/weaviate:/var/lib/weaviateenvironment:# The Weaviate configurations# You can refer to the [Weaviate](https://weaviate.io/developers/weaviate/config-refs/env-vars) documentation for more information.QUERY_DEFAULTS_LIMIT: 25AUTHENTICATION_ANONYMOUS_ACCESS_ENABLED: falsePERSISTENCE_DATA_PATH: /var/lib/weaviateDEFAULT_VECTORIZER_MODULE: noneCLUSTER_HOSTNAME: node1AUTHENTICATION_APIKEY_ENABLED: trueAUTHENTICATION_APIKEY_ALLOWED_KEYS: WVF5YThaHlkYwhGUSmCRgsX3tD5ngdN8pkihAUTHENTICATION_APIKEY_USERS: hellodify.aiAUTHORIZATION_ADMINLIST_ENABLED: trueAUTHORIZATION_ADMINLIST_USERS: hellodify.ai# The nginx reverse proxy.# used for reverse proxying the API service and Web service.nginx:image: nginx:latestrestart: alwaysvolumes:- ./nginx.conf:/etc/nginx/nginx.confdepends_on:- api- webports:- 80:80上面是 docker-compose.yml 文件的内容保存在 soulteary/dify/docker/docker-compose.yml默认配置中我们将使用 Nginx 将各种服务串起来在 80 端口提供访问服务。你可以根据你自己的需求来进行配置调整。当然如果你是 traefik 用户可以参考这个配置文件soulteary/dify/docker/docker-compose.traefik.yml。搞定 Docker 配置后大概完成了这个阶段 50% 的工作。
让我们继续来完成剩下 50% 的工作在 docker-compose.yml 相同目录下创建一个名为 nginx.conf 的文件soulteary/dify/docker/nginx.conf
user nginx;
worker_processes auto;error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;events {worker_connections 1024;
}http {include /etc/nginx/mime.types;default_type application/octet-stream;log_format main $remote_addr - $remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for;access_log /var/log/nginx/access.log main;sendfile on;keepalive_timeout 65;client_max_body_size 15M;server {listen 80;server_name _;proxy_set_header Host $host;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;proxy_http_version 1.1;proxy_set_header Connection ;proxy_buffering off;proxy_read_timeout 3600s;proxy_send_timeout 3600s;location backend {proxy_pass http://api:5001;}location frontend {proxy_pass http://web:3000;}location /console/api {try_files $uri $uri/ backend;}location /api {try_files $uri $uri/ backend;}location /v1 {try_files $uri $uri/ backend;}location /files {try_files $uri $uri/ backend;}location / {try_files $uri $uri/ frontend;}}
}创建并保存了上面两个配置文件后我们执行 docker compose up -d稍等片刻等待服务运行完毕就能够在浏览器中访问 Dify 啦。
# docker compose up -d
[] Running 7/7✔ Container docker-weaviate-1 Started 0.6s ✔ Container docker-web-1 Started 0.6s ✔ Container docker-db-1 Started 0.8s ✔ Container docker-redis-1 Started 0.7s ✔ Container docker-worker-1 Started 1.0s ✔ Container docker-api-1 Started 1.0s ✔ Container docker-nginx-1 Started 当 Docker 中的程序都跑起来之后我们在浏览器中访问运行 Docker 程序的设备 IP 和端口比如 http://IP:80 本地运行的话会是http://127.0.0.1就能够看到 Dify 的管理员账号设置界面啦。 设置完毕管理员账号后登录程序就能够访问 Dify 的控制面板啦我们可以在这里随意的创建基于 Prompt 的 AI 应用。 当然在搭建 AI 应用前我们还需要进行一个步骤配置可以使用的模型资源。
配置 Claude 3 等模型服务
点击界面右上角的用户头像在下拉菜单中点击“设置”在弹出窗口中选择左侧的“模型供应商”菜单能够看到 Dify 支持配置使用的所有模型类型。 如果你的 AWS Claude 3 模型使用权限申请就绪那么可以点击 “AWS Bedrock” 模型打开设置界面。 在设置页面中填写我们从上文中获取到的 AWS 用户凭据信息Access Key 和 Secret Access Key你开通资源访问的云服务资源区域如果你没有获取所有的模型访问权限为了能够配置成功你需要在界面最后一栏中填写一个你拥有权限的模型名称。 全部配置结束后点击“模型供应商”这个弹窗顶部的“系统模型设置”按钮在弹出的下拉菜单中更新默认的系统推理模型建议选择能力相对比较强的模型可以大幅提升你使用 Dify 过程中的用户体验。 好啦接下来我们就可以正式开始搭建 AI 应用啦。
先快速搭建一个 AI 应用
回到主界面点击“创建应用”按钮能够看到一个“开始创建一个新应用”的弹窗。 我们可以在弹出的窗口中选择要创建应用的类型以及应用的名称和图标这里我们先创建一个聊天机器人。 点击创建后我们会自动进入应用编辑页面默认情况下会弹出应用模型配置下拉菜单我们可以根据自己的需求调整应用使用的模型。调试阶段我这里选择的是响应速度更快、成本也更经济的 Claude3 Haiku。 在选择好模型之后我们点击模型按钮能够看到模型的具体配置菜单。这里建议“采样相关参数”只开启一个即可通常情况下尤其是在调试阶段我们只使用“温度”就行增加多样性当结果差不多符合预期后再调整 top-p、top-k 来提高相关性。。 通常情况下模型尤其海外模型对于英文指令遵循能力是更强的所以这里我们可以在 system (prompt) 中设置一个简单的要求
# 只使用中文回答内容必须是要一句简短的电影台词来进行响应。Respond only in Chinese. User response must be replied to using only a simple movie line.然后我们点击页面下方的“添加功能”按钮在弹出的功能选项框里可以根据需求启用一些好玩的功能我这里启用了 “对话开场白”、“下一步问题建议”。 如果你也启动了“开场白”功能那么你可以和我一样在界面中新出现的聊天增强区域写几个“开场问题”。 搞定后页面右侧的调试预览区域就能够看到机器人的预设交互啦。 我们随便点击一个问题按钮问题会自动发送到模型然后模型会携带我们配置好的参数进行内容回复当我们交互完毕模型会根据上面的聊天内容生成几个推荐的问题。是不是还蛮简单的 点击左侧侧边栏的“概览”能够看到这个应用的模型调用情况以及看到 “页面应用地址”、“端口 API 地址”。前者能够让我们打开一个独立的页面应用来提供服务或者避免在调试预览的体验过程中误动模型参数。 后者则可以让我们将这个应用的能力使用编程的方式在其他的地方进行调用。目前 Dify 的 SDK 还在建设中如果你也是 Golang 爱好者可以使用我之前写的 soulteary/dify-go-sdk 来快速调用模型应用的接口完成更复杂的应用。 在应用调用日志界面我们能够看到所有的调用记录我个人认为这里后续可能是一个重点功能因为我们可以通过收集、整理点赞、点踩、编辑或标注内容来从具体场景应用进行模型训练内容的收集和整理。如果官方支持比较晚的话我可能会考虑实现一个小工具来低成本完成这个事情。
最后
这篇文章先写到这里下一篇相关的文章我们来聊聊进阶玩法一如文章开头中图片中的例子们。
–EOF 我们有一个小小的折腾群里面聚集了一些喜欢折腾、彼此坦诚相待的小伙伴。
我们在里面会一起聊聊软硬件、HomeLab、编程上、生活里以及职场中的一些问题偶尔也在群里不定期的分享一些技术资料。
关于交友的标准请参考下面的文章
苏洋致新朋友为生活投票不断寻找更好的朋友
当然通过下面这篇文章添加好友时请备注实名和公司或学校、注明来源和目的珍惜彼此的时间
苏洋关于折腾群入群的那些事 本文使用「署名 4.0 国际 (CC BY 4.0)」许可协议欢迎转载、或重新修改使用但需要注明来源。 署名 4.0 国际 (CC BY 4.0)
本文作者: 苏洋
创建时间: 2024年03月18日 统计字数: 15595字 阅读时间: 32分钟阅读 本文链接: https://soulteary.com/2024/03/18/play-with-anthropopic-claude-3-using-dify-and-aws-bedrock.html