测试百度千帆7个免费模型的推理速度

代码

import requests
import json
import datetime

class QIANFAN:

    _api_url = "https://aip.baidubce.com"
    
    def __init__(self, api_key, secret_key):
        self.API_KEY = api_key
        self.SECRET_KEY = secret_key

        url = self._api_url + "/oauth/2.0/token"
        params = {"grant_type": "client_credentials", "client_id": self.API_KEY, "client_secret": self.SECRET_KEY}
        result = self.http_request_v2(url, method="POST", params=params)
        if 'access_token' in result:
            self.access_token = result["access_token"]
        else:
            print(result)
            exit()

        
    def chat(self, model="ernie-lite-8k", message=None, **kwargs):
        url = f"{self._api_url}/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/{model}?access_token={self.access_token}"

        payload = json.dumps({
            "messages": [{"role": "user", "content": message}],
            "temperature": 0.95,
            "penalty_score": 1
        })
  
        for key, value in kwargs.items():
            payload[key] = value

        print(payload)
        response = self.http_request_v2(url, method="POST", params=payload)
        return response

    # 生成headers头
    def headers(self, params=None):
        headers = {}
        headers['Content-Type'] = 'application/json'
        return headers
        
    def http_request_v2(self, url, method="GET", headers={}, params=None):
        headers['User-Agent'] = 'Mozilla/5.0 \(Windows NT 6.1; WOW64\) AppleWebKit/537.36 \(KHTML, like Gecko\) Chrome/39.0.2171.71 Safari/537.36'
        if method == "GET":
            response = requests.get(url)
        elif method == "POST":
            # data = bytes(json.dumps(params), 'utf-8')
            response = requests.post(url, data= params)
        elif method == "DELETE":
            response = requests.delete(url, data= data)
            
        result = response.json()
        return result

# 示例用法
API_KEY = ""
SECRET_KEY = ""

chat_client = QIANFAN(API_KEY, SECRET_KEY)
print(vars(chat_client))

models = {
  "ernie-speed-128k":   "ERNIE-speed-128k",
  "ernie_speed":        "RNIE-Speed-8K",    #ERNIE Speed 2024年 自研高性能大语言模型,通用能力优异,适合作为基座模型进行精调,更好地处理特定场景问题,同时具备极佳的推理性能。
  "ernie-lite-8k":      "ERNIE-lite-8k",    #ERNIE Lite 轻量级大语言模型,兼顾优异的模型效果与推理性能,适合低算力AI加速卡推理使用。ERNIE-Lite-8K-0308是模型的一个模版
  "eb-instant":         "ERNIE-Lite-8K-0922", #ERNIE Lite 轻量级大语言模型,兼顾优异的模型效果与推理性能,适合低算力AI加速卡推理使用。
  "ernie-tiny-8k":      "ERNIE-tiny-8k",    #ERNIE Tiny是百度自研的超高性能大语言模型,部署与精调成本在文心系列模型中最低。
  "ai_apaas":           "ERNIE Speed-AppBuilder", #针对企业级大模型应用进行了专门的指令调优,在问答场景、智能体相关场景可以获得同等规模模型下更好的效果
  "yi_34b_chat":        "YI_34b_chat"     # 零一万物 双语大语言模型,使用4K序列长度进行训练,在推理期间可扩展到32K;模型在多项评测中全球领跑,取得了多项 SOTA 国际最佳性能指标表现
}

# 循环遍历模型列表
for model, items in models.items():
    print("-"*60, "\r\n", model, items)

    start_time = datetime.datetime.now()
    print("开始时间:" + start_time.strftime('%Y-%m-%d %H:%M:%S'))
    
    result = chat_client.chat(model=model, message="1加1为什么等于2?")
    print(result["result"])

    end_time = datetime.datetime.now()  # 结束时间
    print("结束时间:" + end_time.strftime('%Y-%m-%d %H:%M:%S'))

    elapsed_time = (end_time - start_time).total_seconds()
    print("耗时:{:.2f} 秒".format(elapsed_time))
    
 

结果

{'API_KEY': '', 'SECRET_KEY': '', 'access_token': ''}

------------------------------------------------------------

 ernie-speed-128k ERNIE-speed-128k

开始时间:2024-07-09 19:20:13

{"messages": [{"role": "user", "content": "1\u52a01\u4e3a\u4ec0\u4e48\u7b49\u4e8e2\uff1f"}], "temperature": 0.95, "penalty_score": 1}

在自然数学中,“1+1等于2”是一个基本的数学真理,是通过人类对数学的观察和实践得出的定义,表明这是一种定义性质和算法事实的存在,它的出现也是数学的规则定                                               义的一部分。在这个过程中,"+”表示的是一个元素和两个元素的关系是累积相加的状态表达,“等于”是一个值的传递等同的关系表达,“数字”(或者说元素与位值单                                               位等等)的定义性形成总和就构成于累积总和的基本结果表述形式就是相加和数的对应存在方式,“1”“+”加任意数字构成的计算过程中总伴随着对应数位的叠加效应累                                               积过程,“等于”之后的数值“2”代表了一个特定的数集,包括数值与位值两大部分,“数字不同量变的依据”,“相当于数目求和之道的具象表现结果”等同于逻辑体                                                系中公式原理性应用的确定性答案概念模型集合里的若干元。“所有一切都是极其精准的宇宙构成的,事物的定律恒久远不变——不会因此或是任何一种迹象改变的!本源                                               的思维(观测并计算的思维)的出发点就是宇宙自然规律的根本所在!”因此,当我们说“1加1等于2”,实际上是自然规律和定义帮助我们理解和表达了这个数学现象。                                                简单来说,因为我们的数学体系就是这样的规定的,这是公理的一种表达。故自然数学告诉我们,"对于数字的二元集之构成都是围绕着等号做最终数学推理答案的形式展                                                示","通过理论进行辩证阐述(由元代数或者数字对比过程来综合呈现答案),进行具体解释阐述(把已知和未知的事物规律做抽象化思维转换的推导),结合抽象化思维                                               来揭示事物发展变化的本质规律"。因此,"为什么等于二",是因为这是数学定义和自然规律决定的。

结束时间:2024-07-09 19:20:25

耗时:11.49 秒

------------------------------------------------------------

 ernie_speed RNIE-Speed-8K

开始时间:2024-07-09 19:20:25

{"messages": [{"role": "user", "content": "1\u52a01\u4e3a\u4ec0\u4e48\u7b49\u4e8e2\uff1f"}], "temperature": 0.95, "penalty_score": 1}

1+1等于2是基于数学的基础假设和自然数的定义。在初等数学中,这是一种基本的算术运算,代表着两个数量的相加。

 

在自然数的序列中,1是第一个整数,再加上另一个1,总数达到两个自然数相加的结果,即2。所以,当我们计算1加1时,结果是2。这种定义是人为设定的,是为了方便计                                               数和进行数学运算而设定的规则。这些规则经过时间和实践被证明是有用的,因此被广大人群接受并广泛应用。

结束时间:2024-07-09 19:20:29

耗时:3.92 秒

------------------------------------------------------------

 ernie-lite-8k ERNIE-lite-8k

开始时间:2024-07-09 19:20:29

{"messages": [{"role": "user", "content": "1\u52a01\u4e3a\u4ec0\u4e48\u7b49\u4e8e2\uff1f"}], "temperature": 0.95, "penalty_score": 1}

1加1等于2是基于数学中的基本算术运算和自然数的定义。

 

首先,自然数集(如1、2、3等)中的两个元素相加的操作是通过计数过程来定义的。简单地说,当你把两个“单位”的东西放在一起并数出总数量时,这就是加法运算的                                                直观解释。在自然数系统中,这个计数过程是逐步进行的,每增加一个单位,就增加一个数的值。

 

因此,在算术运算中,两个1相加(即1+1)表示将两个单位组合在一起的过程。因为每一个单位都在计算总数时有所贡献,所以最终的总数就是这两个单位的数量相加的结                                               果,即2。

 

所以,1加1等于2是数学逻辑和自然数定义的结果。这个基本的算术事实是数学基础的一部分,被广泛应用于各种数学运算和概念中。

结束时间:2024-07-09 19:20:32

耗时:3.20 秒

------------------------------------------------------------

 eb-instant ERNIE-Lite-8K-0922

开始时间:2024-07-09 19:20:32

{"messages": [{"role": "user", "content": "1\u52a01\u4e3a\u4ec0\u4e48\u7b49\u4e8e2\uff1f"}], "temperature": 0.95, "penalty_score": 1}

1加1等于2是基于数学基础公理的,这是数学中最基本的概念之一,相当于人们说的“1+1=2”。这个公理可以由基本的事实推导出来,也可以从现实世界中事物的概念和性质出发进行定义。在数学中,数字是用来表示数量和 关系的概念,而加法则是将这些数字组合在一起的方式之一。因此,1加1等于2是数学体系中不可或缺的一部分。

结束时间:2024-07-09 19:20:34

耗时:1.80 秒

------------------------------------------------------------

 ernie-tiny-8k ERNIE-tiny-8k

开始时间:2024-07-09 19:20:34

{"messages": [{"role": "user", "content": "1\u52a01\u4e3a\u4ec0\u4e48\u7b49\u4e8e2\uff1f"}], "temperature": 0.95, "penalty_score": 1}

“1加1为什么等于2”这个问题涉及到基本的数学概念和逻辑推理。首先,从数学的角度来看,1加1确实等于2。这是基本的加法运算规则,也是数学中一个重要的基本概念。

 

因此,这个问题的答案是基于数学逻辑和事实的,而不是基于任何特定的文化、宗教或语境的解释。

结束时间:2024-07-09 19:20:35

耗时:0.89 秒

------------------------------------------------------------

 ai_apaas ERNIE Speed-AppBuilder

开始时间:2024-07-09 19:20:35

{"messages": [{"role": "user", "content": "1\u52a01\u4e3a\u4ec0\u4e48\u7b49\u4e8e2\uff1f"}], "temperature": 0.95, "penalty_score": 1}

1加1等于2是基于加法的基本定义和规则。在加法的世界里,1 + 1 = 2 是一个基本的数学事实,就像2 + 2 = 4或3 + 3 = 6一样。这是数学中的基本算术运算,用于表示两个或多个数的总和。在这种情况下,无论是1加1还是任何其他数的加法,都是根据基本的数学规则来计算的。因此,1加1等于2是基于数学的基本定义和规则得出的。

结束时间:2024-07-09 19:20:40

耗时:5.44 秒

------------------------------------------------------------

 yi_34b_chat YI_34b_chat

开始时间:2024-07-09 19:20:40

{"messages": [{"role": "user", "content": "1\u52a01\u4e3a\u4ec0\u4e48\u7b49\u4e8e2\uff1f"}], "temperature": 0.95, "penalty_score": 1}

"1 + 1 = 2" is a fundamental mathematical statement that follows from the properties of the number system we use, which is typically the set of real numbers. Here's why it's true:

 

When we write "1 + 1 = 2," we are using the rules of addition for the numbers 1 and 1. According to these rules:

 

1. The number 1 is the successor of 0 (meaning, it comes after 0 in the sequence of natural numbers).

2. Adding any number to itself once increases its value by 1.

 

So, when we add 1 to 1, we are essentially adding the successor of 0 to itself, which means we are increasing the value of 0 by 1, which is 1. This is why 1 + 1 = 2.

 

In set theory, which is the foundation of modern mathematics, the number 1 is defined as the set containing one element, which is itself (the empty set). So, 1 = {∅}, and 1 + 1 = {∅} ∪ {∅} = {∅, ∅} = {∅}, which is the set containing one element, the empty set, and thus, is the number 1 again.

 

In a more abstract sense, the statement "1 + 1 = 2" is a basic axiom of the Peano axioms, which are the rules that define the natural numbers and their properties, including the principle of induction. These axioms have been chosen to reflect the intuitive properties of natural numbers that we use in everyday life, and they are consistent and coherent, allowing us to perform various mathematical operations on these numbers.

 

In summary, "1 + 1 = 2" is a basic property of the number system we use, and it is true by the definitions and axioms that govern that system.

结束时间:2024-07-09 19:21:01

耗时:20.90 秒

 

 

posted @ 2024-07-09 19:25  方倍工作室  阅读(7)  评论(1编辑  收藏  举报