Skip to main content

科大讯飞星火大模型SDK

Project description

科大讯飞星火模型 Python SDK

介绍

科大讯飞星火大模型的Python版SDK,目前支持3.5版本的API。 详细参数说明可参考官网WebAPI接口文档

快速体验

  1. 下载本项目代码,并安装依赖
git clone https://github.com/sunmh207/xunfei-spark-python.git
cd xunfei-spark-python
pip install -r requirements.txt
  1. 复制文件.env.dist 为.env,并将.env文件中的{your_app_id}, {your_api_secret}, {your_api_key}替换为自己的应用信息
cp .env.dist .env
  1. 运行demo
python demo.py

demo

备注: 使用前请确保已经在科大讯飞官网注册账号、创建应用,且获得了app_id, api_secret, api_key,获取key的地址:https://console.xfyun.cn/services/bm35

项目中使用

1.安装

pip install xunfei-spark-python==0.1.0

2.使用说明

问答接口

from xunfei.spark.client import Spark
from xunfei.spark.response import ChatResponse

if __name__ == '__main__':
    config = {
        "app_id": "{your_app_id}",
        "api_secret": "{your_api_secret}",
        "api_key": "{your_api_key}",
        "gpt_url": "wss://spark-api.xf-yun.com/v3.5/chat",
        "domain": "generalv3.5",
    }
    spark = Spark(**config)
    messages = [{"role": "user", "content": "你是谁开发的?"}]

    response: ChatResponse = spark.chat(messages=messages, temperature=0.5, max_tokens=1024)
    print('AI:', response.content)
    print('Token使用量:', response.usage)

Stream模式问答接口

from xunfei.spark.client import Spark

def main():
    config = {
        "app_id": "{your_app_id}",
        "api_secret": "{your_api_secret}",
        "api_key": "{your_api_key}",
        "gpt_url": "wss://spark-api.xf-yun.com/v3.5/chat",
        "domain": "generalv3.5",
    }
    spark = Spark(**config)
    messages = [{"role": "user", "content": "你是谁开发的?"}]

    for message in spark.chat_stream(messages=messages, temperature=0.5, max_tokens=1024):
        if message:
            if message.get('type') == 'chunk':
                print(f"客户端接受到的消息: {message['content']}")
            elif message.get('type') == 'stop':
                print("结束")
                break

if __name__ == '__main__':
    main()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xunfei-spark-python-0.1.0.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

xunfei_spark_python-0.1.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file xunfei-spark-python-0.1.0.tar.gz.

File metadata

  • Download URL: xunfei-spark-python-0.1.0.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.8

File hashes

Hashes for xunfei-spark-python-0.1.0.tar.gz
Algorithm Hash digest
SHA256 503dadb6d74af9a93bbda2306097df0f85775ab704412d488bf57288057b66b2
MD5 70f5806389f850eea8e3b774b0709be4
BLAKE2b-256 fefb8935219ab3b06c66af7c98be0cba77439f1f2149f699a5334c5a692c72cc

See more details on using hashes here.

File details

Details for the file xunfei_spark_python-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for xunfei_spark_python-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 55fd9868c347a0a52de901bad4de0cb046ccdb229b81a8f8842c3f3b9c3df8b1
MD5 8bb7ed4b716ee4bf4d71c36bcac5e615
BLAKE2b-256 24fab32dea31138daf5b4383872df6bea49cacbbcc74f746a70fb517c8a3046e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page