[AI] Python calls the iFlytek Spark large model interface to easily generate text

With the emergence of chatGPT, the general large model has become a hot spot of research. Due to well-known reasons, calls in the Asia-Pacific region are often banned. In China, the iFlytek Spark large model is a very excellent Chinese pre-training model. This article will introduce how to use Python to call the iFlytek Spark large model interface to implement functions such as text generation.

1. Install API library

You need to install the library. Enter the following command on the command line to install:

pip3 install websocket
pip3 install websocket-client

2. Obtain the iFlytek Spark model API key

Before using the iFlytek Spark model API, you need to obtain an API key first. Please visit the official website of iFlytek Open Platform (https://www.xfyun.cn/), register an account and log in, enter the "My Application" page and create a new application

Request an API key in the ticket:
Insert image description here

3. Call the iFlytek Spark large model API to generate text

Refer to the official API: https://www.xfyun.cn/doc/spark/Web.html
The following demonstrates how to use Python to call the iFlytek Spark large model interface for text generation. The required libraries need to be imported, and the API key set:Insert image description here

import _thread as thread
import base64
import datetime
import hashlib
import hmac
import json
from urllib.parse import urlparse
import ssl
from datetime import datetime
from time import mktime
from urllib.parse import urlencode
from wsgiref.handlers import format_date_time

import websocket
global result
import os
 class Ws_Param(object):
        # 初始化
        def __init__(self, APPID, APIKey, APISecret, gpt_url):
            self.APPID = APPID
            self.APIKey = APIKey
            self.APISecret = APISecret
            self.host = urlparse(gpt_url).netloc
            self.path = urlparse(gpt_url).path
            self.gpt_url = gpt_url

        # 生成url
        def create_url(self):
            # 生成RFC1123格式的时间戳
            now = datetime.now()
            date = format_date_time(mktime(now.timetuple()))

            # 拼接字符串
            signature_origin = "host: " + self.host + "\n"
            signature_origin += "date: " + date + "\n"
            signature_origin += "GET " + self.path + " HTTP/1.1"

            # 进行hmac-sha256进行加密
            signature_sha = hmac.new(self.APISecret.encode('utf-8'), signature_origin.encode('utf-8'),
                                    digestmod=hashlib.sha256).digest()

            signature_sha_base64 = base64.b64encode(signature_sha).decode(encoding='utf-8')

            authorization_origin = f'api_key="{
      
      self.APIKey}", algorithm="hmac-sha256", headers="host date request-line", signature="{
      
      signature_sha_base64}"'

            authorization = base64.b64encode(authorization_origin.encode('utf-8')).decode(encoding='utf-8')

            # 将请求的鉴权参数组合为字典
            v = {
    
    
                "authorization": authorization,
                "date": date,
                "host": self.host
            }
            # 拼接鉴权参数,生成url
            url = self.gpt_url + '?' + urlencode(v)
            # 此处打印出建立连接时候的url,参考本demo的时候可取消上方打印的注释,比对相同参数时生成的url与自己代码生成的url是否一致
            return url


        # 收到websocket错误的处理
        def on_error(ws, error):
            print("### error:", error)


        # 收到websocket关闭的处理
        def on_close(ws):
            print("### closed ###")


        # 收到websocket连接建立的处理
        def on_open(ws):
            thread.start_new_thread(run, (ws,))


        def run(ws, *args):
            data = json.dumps(gen_params(appid=ws.appid, question=ws.question))
            ws.send(data)


        # 收到websocket消息的处理
        def on_message(ws, message):
            global result 
            # print(message)
            data = json.loads(message)
            code = data['header']['code']
            if code != 0:
                print(f'请求错误: {
      
      code}, {
      
      data}')
                ws.close()
            else:
                choices = data["payload"]["choices"]
                status = choices["status"]
                content = choices["text"][0]["content"]
                result += content
                print(content, end='')
                if status == 2:
                    ws.close()")

4. Code explanation

This code defines a Ws_Paramclass called which is used to handle WebSocket requests. Here is an explanation of each method in the code:

  1. __init__(self, APPID, APIKey, APISecret, gpt_url):Initialization method, used to set instance variables of the class. Among them, APPID, APIKey, and APISecret respectively represent the application ID, API Key, and API Secret of the iFlytek open platform; gpt_url represents the URL of the iFlytek speech synthesis service.

  2. create_url(self):Generate the requested URL. Generate a timestamp in RFC1123 format based on the current time; then, splice the signature string, including host, date and GET request line; then, use the hmac-sha256 algorithm to encrypt the signature string; base64 encode the encrypted signature string , and add it to the authentication parameters to generate the complete URL.

  3. on_error(ws, error):How to handle WebSocket errors received. This method is called when an error occurs with the WebSocket connection.

  4. on_close(ws): Receive the processing method of WebSocket closing. This method is called when the WebSocket connection is closed.

  5. on_open(ws): Receive the processing method of WebSocket connection establishment. This method is called when the WebSocket connection is established. Here, a new thread is started to run runthe function.

  6. run(ws, *args):Running function used to send requests to the iFlytek speech synthesis service. Generate request parameters based on the appid and question attributes of the WebSocket instance; then, convert the request parameters to a JSON string and send it over WebSocket.

  7. on_message(ws, message):Handling method for receiving WebSocket messages. This method is called when a message is received from the iFlytek speech synthesis service. Parse the received message; then, determine whether the request is successful based on the code in the message; if successful, add the returned content to the global variable result and print it out; if the code is not 0, it means the request failed and close at this time WebSocket connection.

5.Auto save

It is best to save each interface call and summarize some commonly used techniques, such as naming, specifying markdown format, etc.:

# -*- coding: utf-8 -*-
import sys
import os
sys.path.append(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
import requests, json,time
from ai import SparkApi
from util import file_util
def write(prefix, titile):
    result = SparkApi.api(prefix + titile)   
    result = result.replace('我们','').replace('首先,','').replace('其次,','').replace('最后,','')
    file_util.write(os.path.join(r'./data', titile + '.md'), result)
def article(titile):   
    prefix = '假如你是一个公众号博主,请以markdown格式写一篇1500字的文章并起10个吸引人的标题,从第二级标题开始,'  
    write(prefix, titile)
def replace(titile):
    result = file_util.read(os.path.join(r'./data', titile + '.md'))
    result = result.replace('我们','').replace('首先,','').replace('其次,','').replace('最后,','')
    file_util.write(os.path.join(r'./data', titile + '.md'), result)
def code(titile, code_str):
    prefix = '假如你是一个公众号博主,请以markdown格式写一篇1500字的文章,解释如下代码:'  
    write(prefix + code_str, titile)
if __name__ ==  '__main__':
    startTime=time.time()        
    article("python调用讯飞星火大模型接口")
    endTime=time.time()
    print(str(endTime-startTime))''')

Guess you like

Origin blog.csdn.net/luansj/article/details/132195821