AI Agent智能应用从0到1定制开发讠果

慈云数据 2024-03-18 技术支持 63 0

在这里插入图片描述

AI Agent智能应用从0到1定制开发

扌并讠果:Ukoou·ㄷㅁΜ

AI Agent智能应用从0到1定制开发 - LLM的共同特点:**

  • 大规模参数:LLM通常包含数十亿甚至数千亿的参数,使其能够学习语言数据中的复杂模式。
  • 基于深度学习:大多数LLM基于Transformer架构,利用自注意力机制处理序列数据。
  • 预训练与微调:LLM通过大规模的预训练学习语言通用模式,再通过微调适应特定任务。
  • 多功能性:能够执行多种NLP任务,如文本生成、翻译、情感分析等。
  • 迁移学习:预训练的LLM可以轻松迁移到新的任务和领域,减少了对大量标注数据的依赖。

    AI Agent智能应用从0到1定制开发 - langchain是什么以及发展过程

    LangChain 是一种创新框架,正在彻底改变我们开发由语言模型驱动应用程序的方式。 通过引入先进的原理,LangChain 正在重新定义传统 API 所能实现的限制。此外,LangChain 应用程序具有智能代理的特性,使语言模型能够与环境进行互动和自适应。LangChain 由多个模块组成。正如其名称所示,LangChain 的主要目的是将这些模块进行链式连接。这意味着我们可以将每个模块都串联在一起,并使用这个链式结构一次性调用所有模块。这些模块由以下部分组成:Model正如介绍中所讨论的那样,模型主要涵盖大语言模型(LLM)。大语言模型是指具有大量参数并在大规模无标签文本上进行训练的神经网络模型。科技巨头们推出了各种各样的大型语言模型,比如:

    • 谷歌的 BERT
    • OpenAI 的 GPT-3
    • 谷歌 LaMDA
    • 谷歌 PaLM
    • Meta AI 的 LLaMA
    • OpenAI 的 GPT-4
    • ……

      借助 LangChain,与大语言模型的交互变得更加便捷。 LangChain 提供的接口和功能有助于将 LLM 的强大能力轻松集成到你的工作应用程序中。LangChain 利用 asyncio 库为 LLM 提供异步支持。对于需要同时并发调用多个 LLM 的网络绑定场景,LangChain 还提供了异步支持。通过释放处理请求的线程,服务器可以将其分配给其他任务,直到响应准备就绪,从而最大限度地提高资源利用率。目前,LangChain 支持 OpenAI、PromptLayerOpenAI、ChatOpenAI 和 Anthropic 等模型的异步支持,但在未来的计划中将扩展对其他 LLM 的异步支持。你可以使用 agenerate 方法来异步调用 OpenAI LLM。此外,你还可以编写自定义的 LLM 包装器,而不仅限于 LangChain 所支持的模型。我在我的应用程序中使用了 OpenAI,并主要使用了 Davinci、Babbage、Curie 和 Ada 模型来解决我的问题。每个模型都有其自身的优点、令牌使用量和使用案例

      AI Agent智能应用从0到1定制开发 - 自定义prompts模板

      基本上有两种不同的提示模板可用-字符串提示模板和聊天提示模板。字符串提示模板提供一个简单的字符串格式提示,而聊天提示模板生成一个更结构化的提示,可用于与聊天API一起使用。

      在本指南中,我们将使用字符串提示模板创建自定义提示。

      要创建一个自定义的字符串提示模板,需要满足两个要求:

      1. 它具有input_variables属性,公开了提示模板预期的输入变量。
      2. 它公开了一个format方法,该方法接受与预期的input_variables相对应的关键字参数,并返回格式化后的提示。

      我们将创建一个自定义的提示模板,它以函数名作为输入,并格式化提示以提供函数的源代码。为了实现这一点,让我们首先创建一个根据函数名返回函数源代码的函数。

      import inspect
      def get_source_code(function_name):
          # Get the source code of the function
          return inspect.getsource(function_name)
      

      Next, we’ll create a custom prompt template that takes in the function name as input, and formats the prompt template to provide the source code of the function.

      from langchain.prompts import StringPromptTemplate
      from pydantic import BaseModel, validator
      class FunctionExplainerPromptTemplate(StringPromptTemplate, BaseModel):
          """A custom prompt template that takes in the function name as input, and formats the prompt template to provide the source code of the function."""
          @validator("input_variables")
          def validate_input_variables(cls, v):
              """Validate that the input variables are correct."""
              if len(v) != 1 or "function_name" not in v:
                  raise ValueError("function_name must be the only input_variable.")
              return v
          def format(self, **kwargs) -> str:
              # Get the source code of the function
              source_code = get_source_code(kwargs["function_name"])
              # Generate the prompt to be sent to the language model
              prompt = f"""
              Given the function name and source code, generate an English language explanation of the function.
              Function Name: {kwargs["function_name"].__name__}
              Source Code:
              {source_code}
              Explanation:
              """
              return prompt
          def _prompt_type(self):
              return "function-explainer"
      

      使用自定义提示模板​

      现在我们已经创建了一个自定义提示模板,我们可以使用它来为我们的任务生成提示。

      fn_explainer = FunctionExplainerPromptTemplate(input_variables=["function_name"])
      # 为函数"get_source_code"生成一个提示
      prompt = fn_explainer.format(function_name=get_source_code)
      print(prompt)
      
              给定函数名和源代码,生成该函数的英语语言解释。
              函数名:get_source_code
              源代码:
              def get_source_code(function_name):
          # 获取函数的源代码
          return inspect.getsource(function_name)
              解释:
      
微信扫一扫加客服

微信扫一扫加客服

点击启动AI问答
Draggable Icon