Spark API and Client
接口说明 注意: 该接口可以正式使用。如您需要申请使用,请点击前往产品页面
一 安装
python3 -m pip install -U sparkapi-python
二 配置
拷贝
example.env到~/.sparkapi.env并修改成自己的配置
SPARK_APP_ID=<your-app-id>
SPARK_API_SECRET=<your-api-secret>
SPARK_API_KEY=<your-api-key>
SPARK_API_MODEL='v3.5' # v1.5, v2.0, v3.0, v3.5
SPARK_CHAT_MAX_TOKENS=4096 # V1.5取值为[1,4096], V2.0、V3.0和V3.5取值为[1,8192],默认为2048。
SPARK_CHAT_TEMPERATURE=0.5 # 取值范围 (0,1] ,默认值0.5
SPARK_CHAT_TOP_K=4 # 取值为[1,6],默认为4
三 快速开始
3.1 命令行模式
sparkapi --help
"
Usage: sparkapi [OPTIONS] COMMAND [ARGS]...
Spark API and Client
Options:
--version Show the version and exit.
-e, --env-file TEXT Environment file [default: ~/.sparkapi.env]
-?, -h, --help Show this message and exit.
Commands:
Chat Chat with SparkDesk
ImageGeneration Generate images based on user input prompt
ImageUnderstanding Understanding the image and engaging in conversation
Contact: suqingdong <suqingdong1114@gmail.com>
"
3.1.1. Chat [星火认知大模型]
sparkapi Chat --help
"
Usage: sparkapi Chat [OPTIONS]
Chat with SparkDesk
Options:
-m, --model [v1.5|v2.0|v3.0|v3.5]
The model version to use
--chat Star a chat
-p, --prompt TEXT Prompt to get completion from
-h, -?, --help Show this message and exit.
"
prompt模式
# 使用默认模型
sparkapi Chat -p 你是谁?
"
您好,我是科大讯飞研发的认知智能大模型,我的名字叫讯飞星火认知大模型。我可以和人类进行自然交流,解答问题,高效完成各领域认知智能需求。
"
# 指定模型
sparkapi Chat -p 你是谁? -m v1.5
"
作为一个认知智能模型,可以回答你的问题和提供帮助。
"
chat模式
sparkapi Chat --chat
>>> User: 你是谁?
>>> AI: 您好,我是科大讯飞研发的认知智能大模型,我的名字叫讯飞星火认知大模型。我可以和人类进行自然交流,解答问题,高效完成各领域认知智能需求。
>>> User: 使用Python编写斐波那契数列函数, 仅输出代码块
>>> AI: ```python
def fibonacci(n):
if n <= 0:
return "输入错误,请输入大于0的整数"
elif n == 1 or n == 2:
return 1
else:
a, b = 1, 1
for _ in range(3, n + 1):
a, b = b, a + b
return b
# 测试
print(fibonacci(10))
>>> User:
3.1.2. ImageGeneration [图像生成]
sparkapi ImageGeneration --help
"
Usage: python -m sparkapi.bin.cli ImageGeneration [OPTIONS]
Generate images based on user input prompt
Options:
-m, --model TEXT The model version to use [default: image_generation]
-p, --prompt TEXT The prompt to use
-o, --outfile TEXT The output file path [default: out.png]
-?, -h, --help Show this message and exit.
"
example
sparkapi ImageGeneration -p 一只可爱的小狗在奔跑 -o dog.png
"save file: dog.png"
# 指定宽高
sparkapi ImageGeneration -p 一只可爱的小狗在奔跑 -o dog.1280x720.png --width 1280 --height 720
"save file: dog.1280x720.png"
3.1.3. ImageUnderstanding [图像理解]
sparkapi ImageUnderstanding --help
"
Usage: python -m sparkapi.bin.cli ImageUnderstanding [OPTIONS]
Understanding the image and engaging in conversation
Options:
-m, --model TEXT The model version to use [default: image_generation]
--chat Start a chat
-f, --file PATH The image file to use
-p, --prompt TEXT The prompt to use
-h, -?, --help Show this message and exit.
"
prompt模式
sparkapi ImageUnderstanding -f dog.png -p 描述下这张图片
"
这是一张非常可爱的小狗的图片。小狗看起来非常活泼和快乐,它的眼睛闪烁着好奇的光芒,嘴巴微微张开,仿佛在欢快地叫唤或是呼吸新鲜空气。
它的毛发呈现出金黄色,与背景中的阳光形成了和谐的对比。阳光从画面的上方斜射下来,为整个场景增添了一种温暖和明亮的感觉。
小狗的四肢健壮,正在奔跑中,尾巴高高翘起,显示出它的活力和快乐。
整体上,这张图片给人一种温馨、愉悦的感觉,仿佛是在一个美好的夏日午后拍摄的。
"
chat模式
sparkapi ImageUnderstanding --chat
"
>>> Image:./dog.png
>>> User: 描述一下这张图片
>>> AI: 这是一张非常可爱的小狗的图片。小狗呈现出金黄色,眼睛大而明亮,嘴巴微微张开,舌头伸出,看起来非常开心和活泼。它的耳朵长且柔软,尾巴高高翘起。背景是模糊的自然景色,阳光从背后照射下来,为整张图片增添了一种温暖和明亮的感觉。整体上,这张图片给人一种轻松愉快的感觉,仿佛小狗正在享受一个美好的下午时光。
>>> User: 精简成4点
>>> AI: 1. 图片展示了一只活泼的金黄色小狗在阳光明媚的自然背景下奔跑。
2. 小狗的眼睛大而明亮,嘴巴微微张开,舌头伸出,看起来非常开心和兴奋。
3. 背景是模糊的自然景色,阳光从背后照射下来,为整张图片增添了一种温暖和明亮的感觉。
4. 整体上,这张图片给人一种轻松愉快的感觉,仿佛小狗正在享受一个美好的下午时光。
>>> User:
"
3.2 Python中使用
from sparkapi.config import SparkConfig
from sparkapi.core.chat.api import SparkAPI as ChatAPI
from sparkapi.core.image_generation.api import SparkAPI as ImageGenerationAPI
from sparkapi.core.image_understanding.api import SparkAPI as ImageUnderstandingAPI
# Chat
api = ChatAPI(**SparkConfig().model_dump())
result = api.get_completion('你好')
print(''.join(result))
# ImageGeneration
api = ImageGenerationAPI(**SparkConfig(api_model='image_generation').model_dump())
result = api.get_completion('帮我生成一张二次元风景图', outfile='out.png')
print(result)
# ImageUnderstanding
api = ImageUnderstandingAPI(**SparkConfig(api_model='image_understanding').model_dump())
result = api.get_completion('out.png', '解释一下这张图片')
print(''.join(result))
四 Changelog
[1.0.7] - 2024-06-28
- Add support for model
v4.0
[1.0.5] - 2024-03-05
- Add support for
ImageGenerationandImageUnderstanding
[1.0.4] - 2024-03-04
- Add support for model
v3.5 - Bug fixed:
SparkAPI.get_completion_from_messages
[1.0.3] - 2023-10-26
- Add support for model
v3.0
Metadata
Release files for sparkapi-python 1.0.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| sparkapi-python-1.0.7.tar.gz | 13.8 kB | Without provenance | Details for sparkapi-python-1.0.7.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| sparkapi_python-1.0.7-py3-none-any.whl | Python 3 | none | any | Details for sparkapi_python-1.0.7-py3-none-any.whl |
Total release size: 30.7 kB
Release files / sparkapi-python-1.0.7.tar.gz
| Download URL | sparkapi-python-1.0.7.tar.gz |
|---|---|
| Size | 13.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d5deb3b405767f7dde5ecdf2602866d5e813d21f834bb9606fb9684d3bf7eebc
|
|
BLAKE2b-256 checksum How to use checksums |
f48c533262915bf643affefc60bbf6e69edd26b17b8abb33d96de0ce4b95524c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.8.8
|
Release files / sparkapi_python-1.0.7-py3-none-any.whl
| Download URL | sparkapi_python-1.0.7-py3-none-any.whl |
|---|---|
| Size | 16.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3a3e5135627e10fe302ffa8910ff2ad9f00ccf911ff9ca658e8d5bd309cda529
|
|
BLAKE2b-256 checksum How to use checksums |
793a672f431dda35224285d77700d47a4d20791735a7b7dfe786537779697582
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.8.8
|
Security for sparkapi-python 1.0.7
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.