返回顶部
热门问答 更多热门问答
技术文章 更多技术文章
链载Ai 显示全部楼层 发表于 1 小时前 |阅读模式 打印 上一主题 下一主题

ingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;margin-bottom: 20px;color: rgb(58, 58, 58);">本篇文章,聊聊怎么比较稳定的使用 Anthropic Claude 3,以及基于目前表现非常好的模型,来做一些有趣的 AI Native 小工具。

ingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 22px;font-weight: bold;display: table;margin-top: 2.2em;margin-bottom: 35px;color: rgb(58, 58, 58);border-bottom: 10px solid rgb(241, 173, 42);padding-right: 10px;padding-left: 10px;">写在前面

ingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;margin-bottom: 20px;color: rgb(58, 58, 58);">在实际体验了半个多月,月初上线的 Anthropic Claude Pro 后,发现 Claude 3 系列模型能力确实很不错(Opus、Sonnet、Haiku),在指令遵循和响应速度上都有比较明显的变化。

ingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;margin-bottom: 20px;color: rgb(58, 58, 58);">不过,个人注册的账号和去年的 ChatGPT Plus 一样,借给朋友体验使用,因为不同的客户端登录(登录 IP 切换),很容易遇到风控问题,比如:需要不确定时间的协助登录确认,更麻烦的会遇到账号封禁,要提工单解除账号限制。

ingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;margin-bottom: 20px;color: rgb(58, 58, 58);">前一阵,亚马逊 AWS 的 Bedrock 基础模型(FM)平台上更新了 Anthropic Claude 3 模型[1]。周末花了一些时间,我对上一篇文章[2]中提到的 Dify 做了一些模型适配工作,现在只需要几条命令,我们就能够通过 Docker 快速地启动一套基于 Claude 3 的 AI 小东西啦,比如 Chat Claude3、Claude3 AI Apps、Claude 3 Bot 的懒人工具啦。

ingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;margin-bottom: 20px;color: rgb(58, 58, 58);">针对 Dify 的相关代码改动已经提交至Dify 官方仓库[3],目测官方应用这些功能调整应该还需要一段时间。所以,想快速尝鲜的同学,可以先访问和使用我的仓库来获取配置或者自行构建 Docker 镜像:soulteary/dify[4]

ingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;margin-bottom: 20px;color: rgb(58, 58, 58);">当然,如果你想使用更现成一些的应用,可以下载网盘中的 Docker 镜像,来快速体验如何分分钟搭建属于你自己的 “Claude3 AI 应用”。

ingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;margin-bottom: 20px;color: rgb(58, 58, 58);">还是先从准备工作开始。

ingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;font-size: 22px;font-weight: bold;display: table;margin-top: 2.2em;margin-bottom: 35px;color: rgb(58, 58, 58);border-bottom: 10px solid rgb(241, 173, 42);padding-right: 10px;padding-left: 10px;">准备工作

ingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif;margin-bottom: 20px;color: rgb(58, 58, 58);">需要准备的内容,一共有三项:

1.能够运行 Docker 应用的设备环境(本地或云主机都行)。2.下载我构建好的 Dify 应用镜像,或使用我提供的源代码仓库进行手动构建。3.申请亚马逊 AWS Bedrock Claude 3 模型使用权限。

准备工作:Docker 运行环境

借助 Docker,我们能够在投入很少额外资源的情况下,快速获得纯净、可复现的、一致性非常棒的环境。

不论你的设备硬件或云主机是否包含显卡,都可以根据自己的操作系统喜好,参考这两篇来完成基础环境的配置《基于 Docker 的深度学习环境:Windows 篇[5]》、《基于 Docker 的深度学习环境:入门篇[6]》。当然,安装 Docker 之后,还可以做很多其他的有趣的事情,比如:之前几十篇有关 Docker 的实践[7],在此就不赘述啦。

如果你已经有了一个可以运行 Docker 的环境,我们可以来准备下一项内容。

准备工作:下载或构建修改后的 Dify 应用镜像

晚些时候,我会上传构建好的镜像到网盘,你可以在专栏的评论区找到资源下载地址。镜像下载完毕后,可以使用下面的命令加载镜像:

dockerload-idify-api-claude3.tar

如果你更喜欢从头构建,可以执行下面的命令:

gitclonehttps://github.com/soulteary/dify.git
cddify/api
gitcheckoutwow/so-lazy
dockerbuild-tsoulteary/dify-api:claude3.

等待命令执行完毕,镜像相关的准备工作就完成啦。

准备工作:AWS Bedrock 的 Claude3 使用权限申请

账号注册等一系列“一路 Next”的简单操作,我们这里就不花时间赘述啦。

当我们登录 “AWS Bedrock”,打开模型访问权限管理页面[8],默认情况下可能并没有 “Anthropic Claude3” 模型的使用权限。这个时候提交工单申请开通权限即可。

等权限开通后,模型权限页面就能够看到“一切就绪”啦。

访问“用户凭证管理[9]”页面,创建一个凭证后,下载凭证文件(credentials)后,我们的准备工作就都完成啦。

实践开始

这里同样包括三个步骤:

1.使用 Docker 启动 Dify,完成应用基础配置,让我们能够在浏览器中管理和配置后续搭建的具体 AI 应用。2.在 Dify 里完成 Claude3 或者其他的模型资源的配置,让我们的程序能够调用这些模型来进行内容生成。3.使用 Dify 简单的折腾几个常见应用,比如 ChatBot,最简单的知识库。

快速完成 Dify 的配置和启动

想要快速的使用 Docker 启动 Dify,我们只需要准备两个文件,分别是 Docker 编排文件docker-compose.yml和 Nginx 配置文件nginx.conf

version:'3.1'
services:
#APIservice
api:
image:soulteary/dify-api:claude3
restart:always
environment:
#Startupmode,'api'startstheAPIserver.
MODE:api
#Theloglevelfortheapplication.Supportedvaluesare`DEBUG`,`INFO`,`WARNING`,`ERROR`,`CRITICAL`
LOG_LEVEL:INFO
#Asecretkeythatisusedforsecurelysigningthesessioncookieandencryptingsensitiveinformationonthedatabase.Youcangenerateastrongkeyusing`opensslrand-base6442`.
SECRET_KEY:sk-9f73s3ljTXVcMT3Blb3ljTqtsKiGHXVcMT3BlbkFJLK7U
#ThebaseURLofconsoleapplicationwebfrontend,referstotheConsolebaseURLofWEBserviceifconsoledomainis
#differentfromapiorwebappdomain.
#example:http://cloud.dify.ai
CONSOLE_WEB_URL:''
#Passwordforadminuserinitialization.
#Ifleftunset,adminuserwillnotbepromptedforapasswordwhencreatingtheinitialadminaccount.
INIT_PASSWORD:''
#ThebaseURLofconsoleapplicationapiserver,referstotheConsolebaseURLofWEBserviceifconsoledomainis
#differentfromapiorwebappdomain.
#example:http://cloud.dify.ai
CONSOLE_API_URL:''
#TheURLprefixforServiceAPIendpoints,referstothebaseURLofthecurrentAPIserviceifapidomainis
#differentfromconsoledomain.
#example:http://api.dify.ai
SERVICE_API_URL:''
#TheURLprefixforWebAPPfrontend,referstotheWebAppbaseURLofWEBserviceifwebappdomainisdifferentfrom
#consoleorapidomain.
#example:http://udify.app
APP_WEB_URL:''
#FilepreviewordownloadUrlprefix.
#usedtodisplayFilepreviewordownloadUrltothefront-endorasMulti-modelinputs;
#Urlissignedandhasexpirationtime.
FILES_URL:''
#Whenenabled,migrationswillbeexecutedpriortoapplicationstartupandtheapplicationwillstartafterthemigrationshavecompleted.
MIGRATION_ENABLED:'true'
#Theconfigurationsofpostgresdatabaseconnection.
#Itisconsistentwiththeconfigurationinthe'db'servicebelow.
DB_USERNAME:postgres
DB_PASSWORD:difyai123456
DB_HOST:db
DB_PORT:5432
DB_DATABASE:dify
#Theconfigurationsofredisconnection.
#Itisconsistentwiththeconfigurationinthe'redis'servicebelow.
REDIS_HOST:redis
REDIS_PORT:6379
REDIS_USERNAME:''
REDIS_PASSWORD:difyai123456
REDIS_USE_SSL:'false'
#useredisdb0forrediscache
REDIS_DB:0
#Theconfigurationsofcelerybroker.
#Useredisasthebroker,andredisdb1forcelerybroker.
CELERY_BROKER_URL:redis://:difyai123456@redis:6379/1
#Specifiestheallowedoriginsforcross-originrequeststotheWebAPI,e.g.https://dify.appor*forallorigins.
WEB_API_CORS_ALLOW_ORIGINS:'*'
#Specifiestheallowedoriginsforcross-originrequeststotheconsoleAPI,e.g.https://cloud.dify.aior*forallorigins.
CONSOLE_CORS_ALLOW_ORIGINS:'*'
#Thetypeofstoragetouseforstoringuserfiles.Supportedvaluesare`local`and`s3`,Default:`local`
STORAGE_TYPE:local
#Thepathtothelocalstoragedirectory,thedirectoryrelativetherootpathofAPIservicecodesorabsolutepath.Default:`storage`or`/home/john/storage`.
#onlyavailablewhenSTORAGE_TYPEis`local`.
STORAGE_LOCAL_PATH:storage
#TheS3storageconfigurations,onlyavailablewhenSTORAGE_TYPEis`s3`.
S3_ENDPOINT:'https://xxx.r2.cloudflarestorage.com'
S3_BUCKET_NAME:'difyai'
S3_ACCESS_KEY:'ak-difyai'
S3_SECRET_KEY:'sk-difyai'
S3_REGION:'us-east-1'
#Thetypeofvectorstoretouse.Supportedvaluesare`weaviate`,`qdrant`,`milvus`.
VECTOR_STORE:weaviate
#TheWeaviateendpointURL.OnlyavailablewhenVECTOR_STOREis`weaviate`.
WEAVIATE_ENDPOINT:http://weaviate:8080
#TheWeaviateAPIkey.
WEAVIATE_API_KEY:WVF5YThaHlkYwhGUSmCRgsX3tD5ngdN8pkih
#Mailconfiguration,support:resend,smtp
MAIL_TYPE:''
#defaultsendfromemailaddress,ifnotspecified
MAIL_DEFAULT_SEND_FROM:'YOUREMAILFROM(eg:no-reply<no-reply@dify.ai>)'
SMTP_SERVER:''
SMTP_PORT:587
SMTP_USERNAME:''
SMTP_PASSWORD:''
SMTP_USE_TLS:'true'
#theapi-keyforresend(https://resend.com)
RESEND_API_KEY:''
RESEND_API_URL:https://api.resend.com
#TheDSNforSentryerrorreporting.Ifnotset,Sentryerrorreportingwillbedisabled.
SENTRY_DSN:''
#ThesamplerateforSentryevents.Default:`1.0`
SENTRY_TRACES_SAMPLE_RATE:1.0
#ThesamplerateforSentryprofiles.Default:`1.0`
SENTRY_PROFILES_SAMPLE_RATE:1.0
depends_on:
-db
-redis
volumes:
#Mountthestoragedirectorytothecontainer,forstoringuserfiles.
-./volumes/app/storage:/app/api/storage

#workerservice
#TheCeleryworkerforprocessingthequeue.
worker:
image:soulteary/dify-api:claude3
restart:always
environment:
#Startupmode,'worker'startstheCeleryworkerforprocessingthequeue.
MODE:worker

#---Alltheconfigurationsbelowarethesameasthoseinthe'api'service.---

#Theloglevelfortheapplication.Supportedvaluesare`DEBUG`,`INFO`,`WARNING`,`ERROR`,`CRITICAL`
LOG_LEVEL:INFO
#Asecretkeythatisusedforsecurelysigningthesessioncookieandencryptingsensitiveinformationonthedatabase.Youcangenerateastrongkeyusing`opensslrand-base6442`.
#sameastheAPIservice
SECRET_KEY:sk-9f73s3ljTXVcMT3Blb3ljTqtsKiGHXVcMT3BlbkFJLK7U
#Theconfigurationsofpostgresdatabaseconnection.
#Itisconsistentwiththeconfigurationinthe'db'servicebelow.
DB_USERNAME:postgres
DB_PASSWORD:difyai123456
DB_HOST:db
DB_PORT:5432
DB_DATABASE:dify
#Theconfigurationsofrediscacheconnection.
REDIS_HOST:redis
REDIS_PORT:6379
REDIS_USERNAME:''
REDIS_PASSWORD:difyai123456
REDIS_DB:0
REDIS_USE_SSL:'false'
#Theconfigurationsofcelerybroker.
CELERY_BROKER_URL:redis://:difyai123456@redis:6379/1
#Thetypeofstoragetouseforstoringuserfiles.Supportedvaluesare`local`and`s3`,Default:`local`
STORAGE_TYPE:local
STORAGE_LOCAL_PATH:storage
#Thetypeofvectorstoretouse.Supportedvaluesare`weaviate`,`qdrant`,`milvus`.
VECTOR_STORE:weaviate
#TheWeaviateendpointURL.OnlyavailablewhenVECTOR_STOREis`weaviate`.
WEAVIATE_ENDPOINT:http://weaviate:8080
#TheWeaviateAPIkey.
WEAVIATE_API_KEY:WVF5YThaHlkYwhGUSmCRgsX3tD5ngdN8pkih
#Mailconfiguration,support:resend
MAIL_TYPE:''
#defaultsendfromemailaddress,ifnotspecified
MAIL_DEFAULT_SEND_FROM:'YOUREMAILFROM(eg:no-reply<no-reply@dify.ai>)'
#theapi-keyforresend(https://resend.com)
RESEND_API_KEY:''
RESEND_API_URL:https://api.resend.com
depends_on:
-db
-redis
volumes:
#Mountthestoragedirectorytothecontainer,forstoringuserfiles.
-./volumes/app/storage:/app/api/storage

#Frontendwebapplication.
web:
image:langgenius/dify-web:0.5.9
restart:always
environment:
EDITION:SELF_HOSTED
#ThebaseURLofconsoleapplicationapiserver,referstotheConsolebaseURLofWEBserviceifconsoledomainis
#differentfromapiorwebappdomain.
#example:http://cloud.dify.ai
CONSOLE_API_URL:''
#TheURLforWebAPPapiserver,referstotheWebAppbaseURLofWEBserviceifwebappdomainisdifferentfrom
#consoleorapidomain.
#example:http://udify.app
APP_API_URL:''
#TheDSNforSentryerrorreporting.Ifnotset,Sentryerrorreportingwillbedisabled.
SENTRY_DSN:''

#Thepostgresdatabase.
db:
image:postgres:15-alpine
restart:always
environment:
PGUSER:postgres
#Thepasswordforthedefaultpostgresuser.
POSTGRES_PASSWORD:difyai123456
#Thenameofthedefaultpostgresdatabase.
POSTGRES_DB:dify
#postgresdatadirectory
PGDATA:/var/lib/postgresql/data/pgdata
volumes:
-./volumes/db/data:/var/lib/postgresql/data
healthcheck:
test:["CMD","pg_isready"]
interval:1s
timeout:3s
retries:30

#Therediscache.
redis:
image:redis:6-alpine
restart:always
volumes:
#Mounttheredisdatadirectorytothecontainer.
-./volumes/redis/data:/data
#Settheredispasswordwhenstartupredisserver.
command:redis-server--requirepassdifyai123456
healthcheck:
test:["CMD","redis-cli","ping"]

#TheWeaviatevectorstore.
weaviate:
image:semitechnologies/weaviate:1.19.0
restart:always
volumes:
#MounttheWeaviatedatadirectorytothecontainer.
-./volumes/weaviate:/var/lib/weaviate
environment:
#TheWeaviateconfigurations
#Youcanrefertothe[Weaviate](https://weaviate.io/developers/weaviate/config-refs/env-vars)documentationformoreinformation.
QUERY_DEFAULTS_LIMIT:25
AUTHENTICATION_ANONYMOUS_ACCESS_ENABLED:'false'
PERSISTENCE_DATA_PATH:'/var/lib/weaviate'
DEFAULT_VECTORIZER_MODULE:'none'
CLUSTER_HOSTNAME:'node1'
AUTHENTICATION_APIKEY_ENABLED:'true'
AUTHENTICATION_APIKEY_ALLOWED_KEYS:'WVF5YThaHlkYwhGUSmCRgsX3tD5ngdN8pkih'
AUTHENTICATION_APIKEY_USERS:'hello@dify.ai'
AUTHORIZATION_ADMINLIST_ENABLED:'true'
AUTHORIZATION_ADMINLIST_USERS:'hello@dify.ai'

#Thenginxreverseproxy.
#usedforreverseproxyingtheAPIserviceandWebservice.
nginx:
image:nginx:latest
restart:always
volumes:
-./nginx.conf:/etc/nginx/nginx.conf
depends_on:
-api
-web
ports:
-"80:80"

上面是docker-compose.yml文件的内容,保存在soulteary/dify/docker/docker-compose.yml[10],默认配置中我们将使用 Nginx 将各种服务串起来,在80端口提供访问服务。你可以根据你自己的需求,来进行配置调整。当然,如果你是 traefik 用户,可以参考这个配置文件:soulteary/dify/docker/docker-compose.traefik.yml[11]。搞定 Docker 配置后,大概完成了这个阶段 50% 的工作。

让我们继续来完成剩下 50% 的工作,在docker-compose.yml相同目录下,创建一个名为nginx.conf的文件(soulteary/dify/docker/nginx.conf[12]):

usernginx;
worker_processesauto;

error_log/var/log/nginx/error.lognotice;
pid/var/run/nginx.pid;


events{
worker_connections1024;
}


http{
include/etc/nginx/mime.types;
default_typeapplication/octet-stream;

log_formatmain'$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.logmain;

sendfileon;
keepalive_timeout65;
client_max_body_size15M;

server{
listen80;
server_name_;

proxy_set_headerHost$host;
proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
proxy_set_headerX-Forwarded-Proto$scheme;
proxy_http_version1.1;
proxy_set_headerConnection"";
proxy_bufferingoff;
proxy_read_timeout3600s;
proxy_send_timeout3600s;

location@backend{
proxy_passhttp://api:5001;
}

location@frontend{
proxy_passhttp://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 啦。

#dockercomposeup-d
[+]Running7/7
✔Containerdocker-weaviate-1Started0.6s
✔Containerdocker-web-1Started0.6s
✔Containerdocker-db-1Started0.8s
✔Containerdocker-redis-1Started0.7s
✔Containerdocker-worker-1Started1.0s
✔Containerdocker-api-1Started1.0s
✔Containerdocker-nginx-1Started

当 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 KeySecret Access Key),你开通资源访问的云服务资源区域,如果你没有获取所有的模型访问权限,为了能够配置成功,你需要在界面最后一栏中填写一个你拥有权限的模型名称。

全部配置结束后,点击“模型供应商”这个弹窗顶部的“系统模型设置”按钮,在弹出的下拉菜单中,更新默认的系统推理模型,建议选择能力相对比较强的模型,可以大幅提升你使用 Dify 过程中的用户体验。

好啦,接下来我们就可以正式开始搭建 AI 应用啦。

先快速搭建一个 AI 应用

回到主界面,点击“创建应用”按钮,能够看到一个“开始创建一个新应用”的弹窗。

我们可以在弹出的窗口中,选择要创建应用的类型,以及应用的名称和图标,这里我们先创建一个聊天机器人。

点击创建后,我们会自动进入应用编辑页面,默认情况下会弹出应用模型配置下拉菜单,我们可以根据自己的需求调整应用使用的模型。调试阶段,我这里选择的是响应速度更快、成本也更经济的 Claude3 Haiku。

在选择好模型之后,我们点击模型按钮,能够看到模型的具体配置菜单。这里建议“采样相关参数”只开启一个即可,通常情况下,尤其是在调试阶段,我们只使用“温度”就行(增加多样性),当结果差不多符合预期后,再调整top-ptop-k来提高相关性。。

通常情况下,模型(尤其海外模型)对于英文指令遵循能力是更强的,所以这里我们可以在system (prompt)中设置一个简单的要求:

#只使用中文回答内容,必须是要一句简短的电影台词来进行响应。

RespondonlyinChinese.Userresponsemustberepliedtousingonlyasimplemovieline.

然后我们点击页面下方的“添加功能”按钮,在弹出的功能选项框里,可以根据需求启用一些好玩的功能,我这里启用了 “对话开场白”、“下一步问题建议”。

如果你也启动了“开场白”功能,那么你可以和我一样,在界面中新出现的聊天增强区域写几个“开场问题”。

搞定后,页面右侧的调试预览区域,就能够看到机器人的预设交互啦。

我们随便点击一个问题按钮,问题会自动发送到模型,然后模型会携带我们配置好的参数进行内容回复,当我们交互完毕,模型会根据上面的聊天内容生成几个推荐的问题。是不是还蛮简单的?

点击左侧侧边栏的“概览”,能够看到这个应用的模型调用情况,以及看到 “页面应用地址”、“端口 API 地址”。前者能够让我们打开一个独立的页面应用,来提供服务或者避免在调试预览的体验过程中,误动模型参数。

后者,则可以让我们将这个应用的能力使用编程的方式,在其他的地方进行调用。目前 Dify 的 SDK 还在建设中,如果你也是 Golang 爱好者,可以使用我之前写的soulteary/dify-go-sdk[13]来快速调用模型应用的接口,完成更复杂的应用。

在应用调用日志界面,我们能够看到所有的调用记录,我个人认为这里后续可能是一个重点功能,因为我们可以通过收集、整理(点赞、点踩、编辑或标注内容),来从具体场景应用进行模型训练内容的收集和整理。如果官方支持比较晚的话,我可能会考虑实现一个小工具,来低成本完成这个事情。

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

链载AI是专业的生成式人工智能教程平台。提供Stable Diffusion、Midjourney AI绘画教程,Suno AI音乐生成指南,以及Runway、Pika等AI视频制作与动画生成实战案例。从提示词编写到参数调整,手把手助您从入门到精通。
  • 官方手机版

  • 微信公众号

  • 商务合作

  • Powered by Discuz! X3.5 | Copyright © 2025-2025. | 链载Ai
  • 桂ICP备2024021734号 | 营业执照 | |广西笔趣文化传媒有限公司|| QQ