如何利用langchian调用百度大模型API

Baidu AI Cloud Qianfan Platform 使用LangChain进行聊天模型集成

概述

百度智能云的千帆平台是一个一站式的大模型开发和服务运营平台,为企业开发者提供了包括文心一言(ERNIE-Bot)和第三方开源模型在内的多种模型。主要分为三类模型:

  1. Embedding
  2. Chat
  3. Completion

本文介绍如何使用LangChain与千帆平台的聊天模型进行集成,具体对应LangChain的langchain/chat_models包。

API 初始化

在使用百度千帆平台的大模型服务前,需要初始化相关参数,可以通过环境变量或者直接传参进行初始化:

export QIANFAN_AK=XXX
export QIANFAN_SK=XXX
支持的模型
  • ERNIE-Bot-turbo(默认)
  • ERNIE-Bot
  • ERNIE-Speed-128K
  • BLOOMZ-7B
  • Llama-2-7b-chat
  • Llama-2-13b-chat
  • Llama-2-70b-chat
  • Qianfan-BLOOMZ-7B-compressed
  • Qianfan-Chinese-Llama-2-7B
  • ChatGLM2-6B-32K
  • AquilaChat-7B
基本设置和调用

使用示例代码初始化并调用聊天模型:

import os
from langchain_community.chat_models import QianfanChatEndpoint
from langchain_core.language_models.chat_models import HumanMessage

os.environ["QIANFAN_AK"] = "Your_api_key"
os.environ["QIANFAN_SK"] = "Your_secret_Key"

chat = QianfanChatEndpoint(streaming=True)
messages = [HumanMessage(content="Hello")]
response = chat.invoke(messages)

print(response.content)
异步调用

可以使用异步方法进行调用:

await chat.ainvoke(messages)
批量调用

支持批量处理消息:

responses = chat.batch([messages])
print(responses[0].content)
流式处理

支持流式处理消息输出:

try:
    for chunk in chat.stream(messages):
        print(chunk.content, end="", flush=True)
except TypeError as e:
    print(e)
使用不同模型

默认使用ERNIE-Bot-turbo,如果需要使用其他模型,可以在初始化时指定:

chatBot = QianfanChatEndpoint(
    streaming=True,
    model="ERNIE-Bot",
)

messages = [HumanMessage(content="Hello")]
response = chatBot.invoke(messages)
print(response.content)
模型参数

目前只有ERNIE-Bot和ERNIE-Bot-turbo支持以下参数,可以在调用时指定:

  • temperature
  • top_p
  • penalty_score

示例代码:

response = chat.invoke(
    [HumanMessage(content="Hello")],
    **{"top_p": 0.4, "temperature": 0.1, "penalty_score": 1}
)
print(response.content)

相关推荐

  1. 如何利用langchian调用模型API

    2024-06-09 11:04:03       27 阅读
  2. EXCEL VBA调用API翻译

    2024-06-09 11:04:03       46 阅读
  3. OCR api调用代码

    2024-06-09 11:04:03       44 阅读
  4. [AI 模型] 文心一言

    2024-06-09 11:04:03       24 阅读
  5. 图像增强与特效-API调用实践-AI

    2024-06-09 11:04:03       37 阅读
  6. 模型文心一言api 请求错误码 一览表

    2024-06-09 11:04:03       26 阅读
  7. EXCEL VBA调用api识别身份证

    2024-06-09 11:04:03       51 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-09 11:04:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-09 11:04:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-09 11:04:03       82 阅读
  4. Python语言-面向对象

    2024-06-09 11:04:03       91 阅读

热门阅读

  1. 链桨PaddleDTX-基于区块链的联邦学习

    2024-06-09 11:04:03       33 阅读
  2. 图像特征提取 python

    2024-06-09 11:04:03       29 阅读
  3. vue脚手架 笔记01

    2024-06-09 11:04:03       27 阅读
  4. 金融数据分析----code详解版

    2024-06-09 11:04:03       20 阅读
  5. 深入理解交叉熵损失CrossEntropyLoss - 损失函数

    2024-06-09 11:04:03       27 阅读
  6. 深入浅出服务发现:构建动态微服务架构的基石

    2024-06-09 11:04:03       23 阅读
  7. 事件驱动架构:新时代的软件设计范式

    2024-06-09 11:04:03       31 阅读
  8. C/C++开发,,pthreads-win32官网,pthreads-win32

    2024-06-09 11:04:03       24 阅读