Metadata-Version: 2.1
Name: xunfei-spark-python
Version: 0.1.0
Summary: 科大讯飞星火大模型SDK
Home-page: https://github.com/sunmh207/xunfei-spark-python
Author: Stanley Sun
Author-email: stanley.java@gmail.com
License: MIT
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Python :: Implementation :: PyPy
Requires-Python: >=3.7.0
Description-Content-Type: text/markdown
Requires-Dist: websocket-client==1.7.0


# 科大讯飞星火模型 Python SDK

## 介绍
科大讯飞星火大模型的Python版SDK，目前支持3.5版本的API。 详细参数说明可参考官网[WebAPI接口文档](https://www.xfyun.cn/doc/spark/Web.html)。

## 快速体验

1. 下载本项目代码，并安装依赖
```
git clone https://github.com/sunmh207/xunfei-spark-python.git
cd xunfei-spark-python
pip install -r requirements.txt
```

2. 复制文件.env.dist 为.env，并将.env文件中的`{your_app_id}`, `{your_api_secret}`, `{your_api_key}`替换为自己的应用信息
```
cp .env.dist .env
```

3. 运行demo
```
python demo.py
```
![demo](docs/imgs/demo.png)

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


## 项目中使用

### 1.安装
```
pip install xunfei-spark-python==0.1.0
```

### 2.使用说明

#### 问答接口

```python
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模式问答接口
    
```python
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()
```
