You need to enable JavaScript to run this app.
导航
最佳实践
最近更新时间:2024.04.01 16:48:40首次发布时间:2024.02.07 16:01:17

Case1:内置常用接口的调用方法

注意

目前仅支持 python>=3.5

'''
Usage:

1. python3 -m pip install --user volcengine
2. VOLC_ACCESSKEY=XXXXX VOLC_SECRETKEY=YYYYY python main.py
3  api document: "https://www.volcengine.com/docs/82379/1222542"
4  使用说明:
   只需要添加ak,sk,即可测试各接口
   测试不同接口: 只需要去掉不同接口的注释即可
'''

import os
from volcengine.maas.v2 import MaasService
from volcengine.maas import MaasException, ChatRole


# 测试tokenize分词接口
def test_tokenize(maas, endpoint_id, req):
    try:
        resp = maas.tokenize(endpoint_id, req)
    except MaasException as e:
        print(e)
    return resp


# 构造分词请求
def construct_tokenize_req(prompt):
    tokenizeReq = {
        "text": prompt,
    }
    return tokenizeReq


# 测试api接口
def test_chat(maas, endpoint_id, req):
    try:
        resp = maas.chat(endpoint_id, req)
        print(resp)
    except MaasException as e:
        print(e)
#
# # 测试流式api接口-原始接口
# def test_stream_chat(maas, endpoint_id, req):
#     try:
#         resps = maas.stream_chat(endpoint_id, req)
#         for resp in resps:
#             print(resp)
#     except MaasException as e:
#         print(e)

# 测试流式api接口
def test_stream_chat(maas, endpoint_id, req):
    try:
        resps = maas.stream_chat(endpoint_id, req)
        output = ""
        for resp in resps:
            # print(resp)
            if resp['choices'][0]['message']['content']:
                output = output + resp['choices'][0]['message']['content']
            # print(resp.choice.message.content)
        print(output)
    except MaasException as e:
        print(e)

# 构造正常调用请求
def construct_req(prompt):
    req = {
        "parameters": {
            "max_new_tokens": 1000,  # 输出文本的最大tokens限制
            "min_new_tokens": 1,  # 输出文本的最小tokens限制
            "temperature": 0.01,  # 用于控制生成文本的随机性和创造性,Temperature值越大随机性越大,取值范围0~1
            "top_p": 0.7,  # 用于控制输出tokens的多样性,TopP值越大输出的tokens类型越丰富,取值范围0~1
            "top_k": 0,  # 选择预测值最大的k个token进行采样,取值范围0-1000,0表示不生效
            "max_prompt_tokens": 3000,  # 最大输入 token 数,如果给出的 prompt 的 token 长度超过此限制,取最后 max_prompt_tokens 个 token 输入模型。
            "repetition_penalty": 1.1  # 重复token输出的惩罚项
        },
        # 如果是单轮对话,构造message的方式
        "messages": [
            {
                "role": ChatRole.USER,
                "content": prompt
            }
        ]
        # # 如果是多轮对话,构造message的方式
        # "messages": [
        #     {
        #         "role": ChatRole.USER,
        #         "content": "我对北京的美食很感兴趣,你能给我一些推荐吗?"
        #     }, {
        #         "role": ChatRole.ASSISTANT,
        #         "content": "没问题,北京有很多著名的美食,比如烤鸭、老北京炸酱面、涮羊肉等。你有特别喜欢的口味或者菜系吗?"
        #     }, {
        #         "role": ChatRole.USER,
        #         "content": "我比较喜欢川菜,有没有川菜馆推荐呢?"
        #     }
        # ]
    }
    return req


# 测试分类接口
def test_classification(maas, endpoint_id, req):
    try:
        resp = maas.classification(endpoint_id, req)
        print(resp)
    except MaasException as e:
        print(e)


# 构造分类接口的调用请求
def construct_classification_req():
    req = {
        "query": "中国的第一个经济特区是?",
        "labels": ["北京", "珠海", "深圳", "厦门", "上海"]  # 输出的结果都在labels的选项内
    }
    return req


if __name__ == '__main__':
    # 如果调用的时候,遇到Read time out问题,可以尝试调大connection_timeout和socket_timeout
    maas = MaasService('maas-api.ml-platform-cn-beijing.volces.com', 'cn-beijing', connection_timeout=600,
                       socket_timeout=600)

    # 设置客户或者个人在方舟账号的ak,sk
    maas.set_ak(os.getenv("VOLC_ACCESSKEY"))
    maas.set_sk(os.getenv("VOLC_SECRETKEY"))

    endpoint_id = "{YOUR_ENDPOINT_ID}"

    prompt = '''
        为一群10-15岁的孩子编写一篇介绍太空探索历史的文章。
        '''

    # chat接口调用
    test_chat(maas, endpoint_id, construct_req(prompt))

    # # 流式chat接口调用(当token长度过长,输出很慢的时候,可以尝试走流式chat接口)
    test_stream_chat(maas, endpoint_id, construct_req(prompt))

    # # 测试分类接口调用(严格要求输出结果,必须在labels的选项内的时候,可以调用)
    test_classification(maas, endpoint_id, construct_classification_req())

    # # 测试分词接口
    response = test_tokenize(maas, endpoint_id, construct_tokenize_req(prompt))
    print(response)
    print("总共的token数:"+str(response['total_tokens']))

Case2:读取一个完整的excel文档,并给出情感分类的准确率

注意

目前仅支持 python>=3.5

'''
Usage:

1. python3 -m pip install --user volcengine
2. VOLC_ACCESSKEY=XXXXX VOLC_SECRETKEY=YYYYY python main.py
3  api document: “https://www.volcengine.com/docs/82379/1222542”
'''

import os
from volcengine.maas.v2 import MaasService
from volcengine.maas import MaasException, ChatRole
import pandas as pd
import numpy as np


# 测试tokenize分词接口
def test_tokenize(maas, endpoint_id, req):
    try:
        resp = maas.tokenize(endpoint_id, req)
    except MaasException as e:
        print(e)
    return resp


# 构造分词请求
def construct_tokenize_req(prompt):
    tokenizeReq = {
        "text": prompt,
    }
    return tokenizeReq


# 测试api接口
def test_chat(maas, endpoint_id, req):
    try:
        resp = maas.chat(endpoint_id, req)
        print(resp)
        return resp['choices'][0]['message']['content']
    except MaasException as e:
        print(e)

#
# # 测试流式api接口-原始接口
# def test_stream_chat(maas, endpoint_id, req):
#     try:
#         resps = maas.stream_chat(endpoint_id, req)
#         for resp in resps:
#             print(resp)
#     except MaasException as e:
#         print(e)

# 测试流式api接口
def test_stream_chat(maas, endpoint_id, req):
    try:
        resps = maas.stream_chat(endpoint_id, req)
        output = ""
        for resp in resps:
            # print(resp)
            if resp['choices'][0]['message']['content']:
                output = output + resp['choices'][0]['message']['content']
            # print(resp.choice.message.content)
        print(output)
    except MaasException as e:
        print(e)


# 构造正常调用请求
def construct_req(prompt):
    req = {
        "parameters": {
            "max_new_tokens": 1000,  # 输出文本的最大tokens限制
            "min_new_tokens": 1,  # 输出文本的最小tokens限制
            "temperature": 0.01,  # 用于控制生成文本的随机性和创造性,Temperature值越大随机性越大,取值范围0~1
            "top_p": 0.7,  # 用于控制输出tokens的多样性,TopP值越大输出的tokens类型越丰富,取值范围0~1
            "top_k": 0,  # 选择预测值最大的k个token进行采样,取值范围0-1000,0表示不生效
            "max_prompt_tokens": 3000,  # 最大输入 token 数,如果给出的 prompt 的 token 长度超过此限制,取最后 max_prompt_tokens 个 token 输入模型。
            "repetition_penalty": 1.1  # 重复token输出的惩罚项
        },
        # 如果是单轮对话,构造message的方式
        "messages": [
            {
                "role": ChatRole.USER,
                "content": prompt
            }
        ]
        # # 如果是多轮对话,构造message的方式
        # "messages": [
        #     {
        #         "role": ChatRole.USER,
        #         "content": "我对北京的美食很感兴趣,你能给我一些推荐吗?"
        #     }, {
        #         "role": ChatRole.ASSISTANT,
        #         "content": "没问题,北京有很多著名的美食,比如烤鸭、老北京炸酱面、涮羊肉等。你有特别喜欢的口味或者菜系吗?"
        #     }, {
        #         "role": ChatRole.USER,
        #         "content": "我比较喜欢川菜,有没有川菜馆推荐呢?"
        #     }
        # ]
    }
    return req


# 测试分类接口
def test_classification(maas, endpoint_id, req):
    try:
        resp = maas.classification(endpoint_id, req)
        print(resp)
    except MaasException as e:
        print(e)


# 构造分类接口的调用请求
def construct_classification_req():
    req = {
        "query": "中国的第一个经济特区是?",
        "labels": ["北京", "珠海", "深圳", "厦门", "上海"]  # 输出的结果都在labels的选项内
    }
    return req


def eval_accuracy(labels, responses):
    return np.sum([labels[i] == responses[i] for i in range(len(labels))]) / (len(labels) * 1.0)


if __name__ == '__main__':
    # 如果调用的时候,遇到Read time out问题,可以尝试调大connection_timeout和socket_timeout
    maas = MaasService('maas-api.ml-platform-cn-beijing.volces.com', 'cn-beijing', connection_timeout=600,
                       socket_timeout=600)

    # 设置客户或者个人在方舟账号的ak,sk
    maas.set_ak(os.getenv("VOLC_ACCESSKEY"))
    maas.set_sk(os.getenv("VOLC_SECRETKEY"))

    endpoint_id = "{YOUR_ENDPOINT_ID}"

    prompt = '''
        假如你是一个情感分析大师,请判断如下情感,是属于积极还是消极,要求:
        1 输出只有有两种可能,积极 或者 消极
        情感: %s
        输出:
    '''

    # excel文件名
    excel_file_name = '/Users/bytedance/Downloads/fangzhou_emotion.xlsx'
    # excel的sheet_name
    sheet_name = 'emotion'

    # 读取excel,假定excel_file_name,总共只有两列,列名分别是:['情感', 'label'], excel的内容如下:
    #             情感	                   label
    # 今天的天气真好!	                       积极
    # 这场雨下得让人心情烦	                   消极
    # 亲爱的用户,感谢您的注册,祝您使用愉快!	   积极
    data = pd.read_excel(excel_file_name, sheet_name=sheet_name)
    labels = []
    responses = []
    for index, row in data.iterrows():
        print("当前case:", index)
        req_input = (prompt % (row['情感']))
        response = test_chat(maas, endpoint_id, construct_req(req_input))
        labels.append(row['label'])
        responses.append(response)
    acc = eval_accuracy(labels, responses)
    print("准确率:", acc)