0
点赞
收藏
分享

微信扫一扫

【GameFramework框架内置模块】13、流程(Procedure)

pipu 03-23 22:00 阅读 2

本篇文章,聊聊怎么比较稳定的使用 Anthropic Claude 3,以及基于目前表现非常好的模型,来做一些有趣的 AI Native 小工具。

写在前面

在实际体验了半个多月,月初上线的 Anthropic Claude Pro 后,发现 Claude 3 系列模型能力确实很不错(Opus、Sonnet、Haiku),在指令遵循和响应速度上都有比较明显的变化。

Anthropic Claude Pro 氪金体验

不过,个人注册的账号和去年的 ChatGPT Plus 一样,借给朋友体验使用,因为不同的客户端登录(登录 IP 切换),很容易遇到风控问题,比如:需要不确定时间的协助登录确认,更麻烦的会遇到账号封禁,要提工单解除账号限制。

前一阵,亚马逊 AWS 的 Bedrock 基础模型(FM)平台上更新了 Anthropic Claude 3 模型。周末花了一些时间,我对上一篇文章中提到的 Dify 做了一些模型适配工作,现在只需要几条命令,我们就能够通过 Docker 快速地启动一套基于 Claude 3 的 AI 小东西啦,比如 Chat Claude3、Claude3 AI Apps、Claude 3 Bot 的懒人工具啦。

可以快速搭建类似这样的 AI 应用

针对 Dify 的相关代码改动已经提交至 Dify 官方仓库,目测官方应用这些功能调整应该还需要一段时间。所以,想快速尝鲜的同学,可以先访问和使用我的仓库来获取配置或者自行构建 Docker 镜像:soulteary/dify。

当然,如果你想使用更现成一些的应用,可以下载网盘中的 Docker 镜像,来快速体验如何分分钟搭建属于你自己的 “Claude3 AI 应用”。

还是先从准备工作开始。

准备工作

需要准备的内容,一共有三项:

  1. 能够运行 Docker 应用的设备环境(本地或云主机都行)。
  2. 下载我构建好的 Dify 应用镜像,或使用我提供的源代码仓库进行手动构建。
  3. 申请亚马逊 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” 模型的使用权限。这个时候提交工单申请开通权限即可。

默认情况,可能没有 Claude 3 模型权限

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

获取到的 Claude 3 模型权限

访问“用户凭证管理”页面,创建一个凭证后,下载凭证文件(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:
  # API service
  api:
    image: soulteary/dify-api:claude3
    restart: always
    environment:
      # Startup mode, 'api' starts the API server.
      MODE: api
      # The log level for the application. Supported values are `DEBUG`, `INFO`, `WARNING`, `ERROR`, `CRITICAL`
      LOG_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.ai
      CONSOLE_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.ai
      CONSOLE_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.ai
      SERVICE_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.app
      APP_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: postgres
      DB_PASSWORD: difyai123456
      DB_HOST: db
      DB_PORT: 5432
      DB_DATABASE: dify
      # The configurations of redis connection.
      # It is consistent with the configuration in the 'redis' service below.
      REDIS_HOST: redis
      REDIS_PORT: 6379
      REDIS_USERNAME: ''
      REDIS_PASSWORD: difyai123456
      REDIS_USE_SSL: 'false'
      # use redis db 0 for redis cache
      REDIS_DB: 0
      # The configurations of celery broker.
      # Use redis as the broker, and redis db 1 for celery broker.
      CELERY_BROKER_URL: redis://:difyai123456@redis: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: `local`
      STORAGE_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.com'
      S3_BUCKET_NAME: 'difyai'
      S3_ACCESS_KEY: 'ak-difyai'
      S3_SECRET_KEY: 'sk-difyai'
      S3_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, smtp
      MAIL_TYPE: ''
      # default send from email address, if not specified
      MAIL_DEFAULT_SEND_FROM: 'YOUR EMAIL FROM (eg: no-reply <no-reply@dify.ai>)'
      SMTP_SERVER: ''
      SMTP_PORT: 587
      SMTP_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.0`
      SENTRY_TRACES_SAMPLE_RATE: 1.0
      # The sample rate for Sentry profiles. Default: `1.0`
      SENTRY_PROFILES_SAMPLE_RATE: 1.0
    depends_on:
      - db
      - redis
    volumes:
      # 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:claude3
    restart: always
    environment:
      # 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`, `CRITICAL`
      LOG_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 service
      SECRET_KEY: sk-9f73s3ljTXVcMT3Blb3ljTqtsKiGHXVcMT3BlbkFJLK7U
      # The configurations of postgres database connection.
      # It is consistent with the configuration in the 'db' service below.
      DB_USERNAME: postgres
      DB_PASSWORD: difyai123456
      DB_HOST: db
      DB_PORT: 5432
      DB_DATABASE: dify
      # The configurations of redis cache connection.
      REDIS_HOST: redis
      REDIS_PORT: 6379
      REDIS_USERNAME: ''
      REDIS_PASSWORD: difyai123456
      REDIS_DB: 0
      REDIS_USE_SSL: 'false'
      # The configurations of celery broker.
      CELERY_BROKER_URL: redis://:difyai123456@redis:6379/1
      # The type of storage to use for storing user files. Supported values are `local` and `s3`, Default: `local`
      STORAGE_TYPE: local
      STORAGE_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: resend
      MAIL_TYPE: ''
      # default send from email address, if not specified
      MAIL_DEFAULT_SEND_FROM: 'YOUR EMAIL FROM (eg: no-reply <no-reply@dify.ai>)'
      # the api-key for resend (https://resend.com)
      RESEND_API_KEY: ''
      RESEND_API_URL: https://api.resend.com
    depends_on:
      - db
      - redis
    volumes:
      # 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.9
    restart: always
    environment:
      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.ai
      CONSOLE_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.app
      APP_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-alpine
    restart: always
    environment:
      PGUSER: postgres
      # The password for the default postgres user.
      POSTGRES_PASSWORD: difyai123456
      # The name of the default postgres database.
      POSTGRES_DB: dify
      # postgres data directory
      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

  # The redis cache.
  redis:
    image: redis:6-alpine
    restart: always
    volumes:
      # Mount the redis data directory to the container.
      - ./volumes/redis/data:/data
    # Set the redis password when startup redis server.
    command: redis-server --requirepass difyai123456
    healthcheck:
      test: [ "CMD", "redis-cli", "ping" ]

  # The Weaviate vector store.
  weaviate:
    image: semitechnologies/weaviate:1.19.0
    restart: always
    volumes:
      # Mount the Weaviate data directory to the container.
      - ./volumes/weaviate:/var/lib/weaviate
    environment:
      # 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: 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'

  # The nginx reverse proxy.
  # used for reverse proxying the API service and Web service.
  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,默认配置中我们将使用 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 应用。

Dify 控制面板默认界面

当然,在搭建 AI 应用前,我们还需要进行一个步骤,配置可以使用的模型资源。

配置 Claude 3 等模型服务

点击界面右上角的用户头像,在下拉菜单中点击“设置”,在弹出窗口中选择左侧的“模型供应商”菜单,能够看到 Dify 支持配置使用的所有模型类型。

打开设置选项中的“模型供应商”

如果你的 AWS Claude 3 模型使用权限申请就绪,那么可以点击 “AWS Bedrock” 模型,打开设置界面。

AWS Bedrock 模型设置界面

在设置页面中,填写我们从上文中获取到的 AWS 用户凭据信息(Access KeySecret Access Key),你开通资源访问的云服务资源区域,如果你没有获取所有的模型访问权限,为了能够配置成功,你需要在界面最后一栏中填写一个你拥有权限的模型名称。

成功获取 AWS Bedrock 模型列表

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

更新默认系统推理模型

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

先快速搭建一个 AI 应用

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

创建应用

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

先选择“助手”类型的应用

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

更新应用的模型后端服务

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

调整模型参数

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

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

Respond only in Chinese. User response must be replied to using only a simple movie line.

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

添加一些有趣的交互功能

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

填写一些预设的交互内容

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

调试验证刚刚配置好的应用

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

调试验证刚刚配置好的应用

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

使用 Dify 配置生成的 AI 应用

后者,则可以让我们将这个应用的能力使用编程的方式,在其他的地方进行调用。目前 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

举报

相关推荐

0 条评论