【大模型】Qwen, DeepSeek, GLM的API接口调用(官方示例+LangChain示例)


平常工作中,我们可能需要调用大模型API接口来提供服务,这里对 Qwen, DeepSeek, GLM系列大模型的API接口调用方式进行一个记录,包括有大模型厂商官方提供的调用示例以及LangChain提供的调用示例。

1. 通义千问Qwen

1.1 API_KEY

在这里插入图片描述

这里,我申请的API_KEY信息如下:

api_key="API_KEY" 
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1"

1.2 调用示例

pip install -U openai

(1)官网调用示例(来自阿里云百炼官网)

import os
from openai import OpenAI

try:
    client = OpenAI(
        # 若没有配置环境变量,请用百炼API Key将下行替换为:api_key="sk-xxx",        
        # api_key=os.getenv("DASHSCOPE_API_KEY"),        
        api_key="API_KEY",        
        base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",    
        )

    completion = client.chat.completions.create(
        model="qwen-plus",  # 模型列表:https://help.aliyun.com/zh/model-studio/getting-started/models        
        messages=[
            {'role': 'system', 'content': 'You are a helpful assistant.'},            
            {'role': 'user', 'content': '你是谁?'}
            ]
    )
    print(completion.choices[0].message.content)
except Exception as e:
    print(f"错误信息:{e}")
    print("请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code")

(2)Langchain调用示例https://python.langchain.com/docs/integrations/chat/tongyi/

Langchain可用模型列表:https://python.langchain.com/docs/integrations/chat/

  • Langchain: API调用
# pip install dashscopeimport os

os.environ["DASHSCOPE_API_KEY"] = "API_KEY"
from langchain_community.chat_models.tongyi import ChatTongyi
from langchain_core.messages import HumanMessage

chatLLM = ChatTongyi(
    streaming=True,)
res = chatLLM.stream([HumanMessage(content="hi")], streaming=True)
for r in res:
    print("chat resp:", r)
  • Langchain: Tool Calling
from langchain_community.chat_models.tongyi import ChatTongyi
from langchain_core.tools import tool

import os

os.environ["DASHSCOPE_API_KEY"] = "API_KEY"
@tooldef multiply(first_int: int, second_int: int) -> int:
    """Multiply two integers together."""    return first_int * second_int


llm = ChatTongyi(model="qwen-turbo")

llm_with_tools = llm.bind_tools([multiply])

msg = llm_with_tools.invoke("What's 5 times forty two")

print(msg)

2. DeepSeek

2.1 API_KEY

申请API_KEY官网:https://platform.deepseek.com/api_keys

这里,我申请的API_KEY信息如下:

deepseek_api_key: API_KEY
deepseek_api_base: "https://api.deepseek.com"

2.2 调用示例

(1)DeepSeek官网调用示例https://api-docs.deepseek.com/zh-cn/

# pip3 install openai
from openai import OpenAI

client = OpenAI(api_key="API_KEY", base_url="https://api.deepseek.com")

response = client.chat.completions.create(
    model="deepseek-chat",    
    messages=[
        {"role": "system", "content": "You are a helpful assistant"},        {"role": "user", "content": "Hello"},],    
    stream=False)

print(response.choices[0].message.content)

(2)Langchain调用示例https://python.langchain.com/docs/integrations/chat/deepseek/

Langchain可用模型列表:https://python.langchain.com/docs/integrations/chat/

from langchain_deepseek import ChatDeepSeek
import os

os.environ["DEEPSEEK_API_KEY"] = "API_KEY"
llm = ChatDeepSeek(
    model="deepseek-chat",    temperature=0,    max_tokens=None,    timeout=None,    max_retries=2,    # other params...)

messages = [
    (
        "system",        
        "You are a helpful assistant that translates English to Chinese. Translate the user sentence.",    
        ),    
     ("human", "I love programming."),]
ai_msg = llm.invoke(messages)
print(ai_msg.content)

3. GLM

3.1 API_KEY

申请API_KEY官网:https://www.bigmodel.cn/usercenter/proj-mgmt/apikeys

这里,我申请的API_KEY信息如下:

zhipu_api_key: API_KEY

3.2 调用示例

(1)GLM官网调用示例https://open.bigmodel.cn/dev/api/normal-model/glm-4

from zhipuai import ZhipuAI
client = ZhipuAI(api_key="API_KEY")  # 请填写您自己的APIKey
response = client.chat.completions.create(
    model="glm-4-plus",  # 请填写您要调用的模型名称
    messages=[
        {"role": "user", "content": "作为一名营销专家,请为我的产品创作一个吸引人的口号"},
        {"role": "assistant", "content": "当然,要创作一个吸引人的口号,请告诉我一些关于您产品的信息"},
        {"role": "user", "content": "智谱AI开放平台"},
        {"role": "assistant", "content": "点燃未来,智谱AI绘制无限,让创新触手可及!"},
        {"role": "user", "content": "创作一个更精准且吸引人的口号"}
    ],
)
print(response.choices[0].message)

(2)Langchain调用示例https://python.langchain.com/docs/integrations/chat/zhipuai/

# pip install --upgrade httpx httpx-sse PyJWT

from langchain_community.chat_models import ChatZhipuAI
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
import os

os.environ["ZHIPUAI_API_KEY"] = "API_KEY"
chat = ChatZhipuAI(
    model="glm-4",    temperature=0.5,)

messages = [
    AIMessage(content="Hi."),    
    SystemMessage(content="Your role is a poet."), 
    HumanMessage(content="Write a short poem about AI in four lines."),
    ]

response = chat.invoke(messages)
print(response.content)  # Displays the AI-generated poem
### QwenLangChain的集成 #### 集成概述 Qwen是一个强大的预训练语言模型,而LangChain作为一个用于构建基于语言模型应用的开源框架[^2],能够极大地简化大模型的操作流程,使得后端程序员仅需调用特定API就能完成复杂任务[^1]。因此,在二者集成交互的过程中,主要目标在于如何让Qwen更好地适配LangChain所提供的接口和服务。 #### 实现方式 为了实现两者的无缝对接,通常需要遵循以下几个方面: - **环境配置**:确保本地或云端环境中已安装并正确配置了LangChain库以及Qwen所需的依赖包。 - **初始化实例**:创建Qwen对象作为预测器,并将其注册到LangChain的工作流中去。 - **定义交互逻辑**:编写具体的业务处理函数,这些函数负责接收输入请求、调用Qwen执行推理运算并将结果返回给前端界面或其他下游服务。 下面给出一段Python代码示例来说明这一过程: ```python from langchain import LangChain import qwen # 假设qwen为对应的SDK名称 def init_qwen_langchain(): lc = LangChain() # 初始化Qwen客户端 model = qwen.QwenClient(api_key='your_api_key') @lc.register('ask_question') def ask_question(question): response = model.predict(text=question) return {"answer":response} if __name__ == '__main__': app = init_qwen_langchain() ``` 此段脚本展示了怎样利用装饰器机制将自定义的任务处理器绑定至LangChain实例上;当接收到名为`ask_question`的消息时,则触发内部对Qwen API发起询问的动作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值