啊鸡入坑 AI 之《LangChain 上手》

简介

起因

最近看了一个《基于 Langchain 与 ChatGLM 等语言模型的本地知识库问答》项目,想搞来玩一下,但是该项目的本地部署大语言模型对显卡有要求,并且 docker 镜像也有 30G,所以暂时用 OpenAI 的接口来学习 LangChain。

LangChain 简介

LangChain 是一个基于大语言模型(LLMs)用于构建端到端语言模型应用的框架,它可以让开发者使用语言模型来实现各种复杂的任务,例如文本到图像的生成、文档问答、聊天机器人等。 langchain的主要特点有: 提供了一套工具、组件和接口,可以简化创建由LLMs和聊天模型提供支持的应用程序的过程。

申请 OpenAI Token 小坑

在申请 OpenAI 账号的时候遇到很多坑:
  • 要清空缓存翻墙,而且还要多换几次,因为翻墙的 IP 有可能已被拉入黑名单(这个可以忍)
    • 需要绑定国外的手机号(忍了)
      • 需要有国外的信用卡,虚拟信用卡还被封(怎么搞都搞不好,去他妹的)
        而且我之前就申请过 OpenAI 账号,所以免费的使用额度已经过期了,所以还是另想他法吧。

        开搞

        1. 申请 Azure OpenAI

        微软的 Azure OpenAI Service 服务允许绑定国内的信用卡,具体的教程可以看这个:

        2. 创建 Deployment

        在申请完 Azure OpenAI 之后,需要创建部署:
        notion image
        可选模型如下:
        • gpt-35-turbo
          • gpt-35-turbo-16k
            • text-embedding-ada-002
              • 其中 GPT-4 需要额外申请资格,见申请链接

                3. 简单上手(LangChain 中文入门教程)

                💡
                Azure OpenAI 的模型返回时不时会被 Content filtering 挡掉,想关掉的话需要去额外申请 Modified Content Filters and Abuse monitoring
                notion image
                notion image

                3.1 创建代码

                参考https://github.com/liaokongVFX/LangChain-Chinese-Getting-Started-Guide,复制一份:
                本地运行代码见:https://github.com/JiyuShao/LangChain-Azure-OpenAI-Chinese-Guide

                3.2 改写 OpenAI 调用方式

                主要改写有以下几点:
                挂载 Google Drive
                notion image
                从 Google Drive 中读取环境变量
                切换 LangChain OpenAI LLM 为 Azure OpenAI LLM

                3.3 调试第一次问答

                在使用 LangChain 的 AzureOpenAI 官方示例改写完成后发现仍然报错如下:
                notion image
                原来是由于示例中使用的是较旧的模型text-davinci-003,而新的模型gpt-35-turbo-16k 不支持 Completion 操作导致的,需要从 AzureOpenAI 切换到 AzureChatOpenAI
                notion image

                3.4 对超长文本进行总结

                在挂载好之后则可以进行代码执行了,《孔乙己》执行成功
                notion image
                notion image
                但是发现输出的结果是英语,所以打算定制一下 Prompt(见链接
                notion image
                notion image
                结果发现第一次还是用的我们定制的 prompt,第二次就不是了
                原来是提问的 prompt 和 refine 的 prompt 是不同的,修改一下就可以了,顺便打印出了中间产物
                notion image
                notion image

                3.5 构建本地知识库问答机器人

                首先要有一个 text-embedding-ada-002 模型部署
                运行试一下,发现报错,应该是 Azure OpenAI 的 Embedding 模型有 Token 限制
                notion image
                notion image
                限制一下 Embedding 的 Chunk Size 就可以了,网上人遇到类似的问题
                notion image
                notion image

                3.6 用 Zapier 连接万种工具

                添加 Zapier NLA Provider
                notion image
                添加 Actions,注意 Require preview before running AI Action 要取消选中
                notion image
                notion image
                执行代码,成功发送邮件
                notion image
                notion image
                notion image
                notion image

                Reference


                © Jiyu Shao 2018 - 2025