【AI探索】豆包AI之火山引擎API初尝试(从安装volcengine-python-sdk报错到调用成功)
文章目录
前言
使用过豆包AI,其反馈不论是速度还是质量都可圈可点,在中文大模型里很值得推荐。于是就想是否可以使用API调用一下。
一、火山引擎是什么?
火山方舟是火山引擎推出的大模型服务平台,提供模型训练、推理、评测、精调等全方位功能与服务,并重点支撑大模型生态。 火山方舟通过稳定可靠的安全互信方案,保障模型提供方的模型安全与模型使用者的信息安全,加速大模型能力渗透到千行百业,助力模型提供方和使用者实现商业新增长。
字节自研大模型于2024年5月正式命名为豆包大模型。
目前,火山方舟SDK V3在易用性、功能丰富性、稳定性、性能等各方面远超V1/V2,且已经上线并稳定运行一段时间。火山方舟平台SDK V1/V2 版本将于2024年11月30日正式下线,不再支持调用,2024年11月19日以后创建的推理接入点将不能使用SDK V1/V2访问。建议用户尽快核实SDK版本,并于旧版本下线前参照本迁移指引完成SDK V1/V2至SDK V3版本切换和业务迁移。
V3版本增强说明
易用性:V3接口完全兼容 OpenAI 协议,支持直接使用社区兼容 OpenAI 协议的 SDK 调用,同时更容易和 LangChain 等开源生态结合。
功能更丰富:V3接口对 function calling、n-sample等功能的支持更完善。
稳定性:V3接口模型推理服务链路优化,整体稳定性更好;推理服务的模型版本切换支持平滑切换(无中断)。
性能:V3接口模型推理服务平均 TTFT (用户提交查询后开始看到模型输出的速度)降低 50-100ms。
官方文档:https://www.volcengine.com/docs/82379/1319847
二、安装火山引擎
1. 找不到文件?
(base) C:\Users\Administrator>pip install volcengine-python-sdk[ark] -i https://pypi.tuna.tsinghua.edu.cn/simple
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting volcengine-python-sdk[ark]
Using cached https://pypi.tuna.tsinghua.edu.cn/packages/24/2c/5336b3f1eeca5d69a2bf346047aafe1da42da1d5bf8f356959a9193ca19a/volcengine-python-sdk-1.0.112.tar.gz (2.9 MB)
ERROR: Could not install packages due to an OSError: [Errno 2] No such file or directory: 'C:\\Users\\Administrator\\AppData\\Local\\Temp\\pip-install-l_vj1y3e\\volcengine-python-sdk_07bb35afc504470297970c846c6e3dc4\\volcenginesdktransitrouter\\models\\transit_router_traffic_qos_marking_policy_for_describe_transit_router_traffic_qos_marking_policies_output.py'
尝试清除缓存
(base) C:\Users\Administrator>pip cache purge
Files removed: 1425
再运行以上命令行,问题依旧。
2. 加上–user参数
pip install volcengine-python-sdk[ark] -i https://pypi.tuna.tsinghua.edu.cn/simple --user
问题依旧
3. 查官方文档,超长文件名惹的祸
由于 Windows 系统有最长路径限制,可能会导致安装失败,请按照以下方式设置:
- 按下 Win+R ,输入 regedit 打开注册表编辑器。
- 设置 \HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem 路径下的变量 LongPathsEnabled 为 1 即可。
三、无法运行 setup.py
1. setup.py
报错
解决了长路径问题,发现还是安装不了,setup.py仍旧不给力
(base) C:\Users\Administrator>pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ volcengine-python-sdk[ark] --user
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/
Collecting volcengine-python-sdk[ark]
Using cached https://pypi.tuna.tsinghua.edu.cn/packages/24/2c/5336b3f1eeca5d69a2bf346047aafe1da42da1d5bf8f356959a9193ca19a/volcengine-python-sdk-1.0.112.tar.gz (2.9 MB)
Preparing metadata (setup.py) ... error
error: subprocess-exited-with-error
× python setup.py egg_info did not run successfully.
│ exit code: 1
╰─> [1 lines of output]
ERROR: Can not execute `setup.py` since setuptools is not available in the build environment.
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed
× Encountered error while generating package metadata.
╰─> See above for output.
note: This is an issue with the package mentioned above, not pip.
hint: See above for details.
2. 更新pip,setuptools
python.exe -m pip install –upgrade pip
pip install –upgrade setuptools
pip install ez_setup
(base) C:\Users\Administrator>pip install ez_setup
Collecting ez_setup
Downloading ez_setup-0.9.tar.gz (6.6 kB)
Preparing metadata (setup.py) ... error
error: subprocess-exited-with-error
× python setup.py egg_info did not run successfully.
│ exit code: 1
╰─> [1 lines of output]
ERROR: Can not execute `setup.py` since setuptools is not available in the build environment.
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed
× Encountered error while generating package metadata.
╰─> See above for output.
note: This is an issue with the package mentioned above, not pip.
hint: See above for details.
升级完运行,问题依旧!要骂人了。
四、降低版本再试
1. 检查可用版本
输入一个不存在的版本,看下都有哪些版本?
pip install volcengine-python-sdk[ark]==1000
(base) C:\Users\Administrator>pip install volcengine-python-sdk[ark]==1000
ERROR: Could not find a version that satisfies the requirement volcengine-python-sdk==1000 (from versions: 1.0.0, 1.0.1, 1.0.2, 1.0.3, 1.0.4, 1.0.5, 1.0.6, 1.0.7, 1.0.8, 1.0.9, 1.0.10, 1.0.11, 1.0.12, 1.0.13, 1.0.14, 1.0.15, 1.0.16, 1.0.17, 1.0.18, 1.0.19, 1.0.20, 1.0.21, 1.0.22, 1.0.23, 1.0.24, 1.0.25, 1.0.26, 1.0.27, 1.0.28, 1.0.29, 1.0.30, 1.0.31, 1.0.32, 1.0.33, 1.0.34, 1.0.35, 1.0.36, 1.0.37, 1.0.38, 1.0.39, 1.0.40, 1.0.41, 1.0.42, 1.0.43, 1.0.44, 1.0.45, 1.0.46, 1.0.47, 1.0.48, 1.0.49, 1.0.50, 1.0.51, 1.0.52, 1.0.53, 1.0.54, 1.0.55, 1.0.56, 1.0.57, 1.0.58, 1.0.59, 1.0.60, 1.0.61, 1.0.62, 1.0.63, 1.0.64, 1.0.65, 1.0.66, 1.0.67, 1.0.68, 1.0.69, 1.0.70, 1.0.71, 1.0.72, 1.0.73, 1.0.74, 1.0.75, 1.0.76, 1.0.77, 1.0.78, 1.0.79, 1.0.80, 1.0.81, 1.0.82, 1.0.83, 1.0.84, 1.0.85, 1.0.86, 1.0.87, 1.0.88, 1.0.89, 1.0.90, 1.0.91, 1.0.92, 1.0.93, 1.0.94, 1.0.95, 1.0.96, 1.0.97, 1.0.98, 1.0.99, 1.0.100, 1.0.101, 1.0.102, 1.0.103, 1.0.104, 1.0.105, 1.0.106, 1.0.107, 1.0.108, 1.0.109, 1.0.110, 1.0.111, 1.0.112)
ERROR: No matching distribution found for volcengine-python-sdk==1000
2. 尝试安装低版本
(base) C:\Users\Administrator>pip install volcengine-python-sdk[ark]==1.0.100
Collecting volcengine-python-sdk==1.0.100 (from volcengine-python-sdk[ark]==1.0.100)
Downloading volcengine-python-sdk-1.0.100.tar.gz (2.4 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.4/2.4 MB 237.1 kB/s eta 0:00:00
Preparing metadata (setup.py) ... error
error: subprocess-exited-with-error
× python setup.py egg_info did not run successfully.
│ exit code: 1
╰─> [1 lines of output]
ERROR: Can not execute `setup.py` since setuptools is not available in the build environment.
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed
× Encountered error while generating package metadata.
╰─> See above for output.
note: This is an issue with the package mentioned above, not pip.
hint: See above for details.
问题依旧没有解决,要奔溃了!
五、删除backports文件夹后搞定
把文章开头提到的文件手动下载下来解压看看,有setup.py文件啊
https://pypi.tuna.tsinghua.edu.cn/packages/24/2c/5336b3f1eeca5d69a2bf346047aafe1da42da1d5bf8f356959a9193ca19a/volcengine-python-sdk-1.0.112.tar.gz
(base) C:\Users\Administrator>cd C:\Users\Administrator\Downloads\volcengine-python-sdk-1.0.112\volcengine-python-sdk-1.0.112
(base) C:\Users\Administrator\Downloads\volcengine-python-sdk-1.0.112\volcengine-python-sdk-1.0.112>python setup.py install --user
Traceback (most recent call last):
File "C:\Users\Administrator\Downloads\volcengine-python-sdk-1.0.112\volcengine-python-sdk-1.0.112\setup.py", line 3, in <module>
from setuptools import setup, find_packages # noqa: H301
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "d:\ProgramData\anaconda3\Lib\site-packages\setuptools\__init__.py", line 27, in <module>
from .dist import Distribution
File "d:\ProgramData\anaconda3\Lib\site-packages\setuptools\dist.py", line 19, in <module>
from . import (
File "d:\ProgramData\anaconda3\Lib\site-packages\setuptools\_entry_points.py", line 6, in <module>
from jaraco.text import yield_lines
File "d:\ProgramData\anaconda3\Lib\site-packages\setuptools\_vendor\jaraco\text\__init__.py", line 12, in <module>
from jaraco.context import ExceptionTrap
File "d:\ProgramData\anaconda3\Lib\site-packages\setuptools\_vendor\jaraco\context.py", line 17, in <module>
from backports import tarfile
ImportError: cannot import name 'tarfile' from 'backports' (d:\ProgramData\anaconda3\Lib\site-packages\backports\__init__.py)
发现提示:
cannot import name 'tarfile' from 'backports'
再次百度搜索,有说到Anaconda目录Lib\site-packages\backports 下把backports文件夹删除了,为了保险期间,给该目录改个名字先。
改完再次运行安装命令
(base) C:\Users\Administrator>pip install volcengine-python-sdk --user
Collecting volcengine-python-sdk
Using cached volcengine-python-sdk-1.0.112.tar.gz (2.9 MB)
Preparing metadata (setup.py) ... done
Requirement already satisfied: certifi>=2017.4.17 in d:\programdata\anaconda3\lib\site-packages (from volcengine-python-sdk) (2024.8.30)
Requirement already satisfied: python-dateutil>=2.1 in d:\programdata\anaconda3\lib\site-packages (from volcengine-python-sdk) (2.9.0.post0)
Requirement already satisfied: six>=1.10 in d:\programdata\anaconda3\lib\site-packages (from volcengine-python-sdk) (1.16.0)
Requirement already satisfied: urllib3>=1.23 in d:\programdata\anaconda3\lib\site-packages (from volcengine-python-sdk) (2.2.3)
Building wheels for collected packages: volcengine-python-sdk
Building wheel for volcengine-python-sdk (setup.py) ... done
Created wheel for volcengine-python-sdk: filename=volcengine_python_sdk-1.0.112-py3-none-any.whl size=9939019 sha256=effba3546d0fcdc34f52d32ebf27a6794de4c8f9c18047c0925ea30acccb0631
Stored in directory: c:\users\administrator\appdata\local\pip\cache\wheels\83\8a\94\b1ba7c3898d4d5a60b5b3340463d7f9f58271047d9b9bddd07
Successfully built volcengine-python-sdk
Installing collected packages: volcengine-python-sdk
Successfully installed volcengine-python-sdk-1.0.112
终于安装成功了!我擦,搁这里等我呢!
六、API测试
登录火山引擎控制中心,左侧最下有个API Key 管理,创建后赋值api_key,替换程序内参数。
另外,每个大模型接入还需申请接入点,访问https://www.volcengine.com/docs/82379/1302008,根据连接跳转,创建推理接入点(Endpoint),创建后替换model后面的参数。
from volcenginesdkarkruntime import Ark
client = Ark(
base_url="https://ark.cn-beijing.volces.com/api/v3",api_key="e7d03c16-b90d-xxxx-8b23-xxxxxxxxxxxx"
)
# Non-streaming:
print("----- standard request -----")
completion = client.chat.completions.create(
model="ep-20241207225428-xxxxx",
messages = [
{"role": "system", "content": "你是豆包,是由字节跳动开发的 AI 人工智能助手"},
{"role": "user", "content": "常见的十字花科植物有哪些?"},
],
)
print(completion.choices[0].message.content)
运行结果如下:
----- standard request -----
十字花科植物是一个庞大的植物家族,以下是一些常见的十字花科植物:
1. **白菜**:包括大白菜、小白菜等,是常见的蔬菜。
2. **萝卜**:有白萝卜、红萝卜、胡萝卜等。
3. **甘蓝**:如卷心菜、紫甘蓝等。
4. **芥菜**:包括芥菜头、雪里蕻等。
5. **油菜**:可用于榨油或作为蔬菜食用。
6. **花椰菜**:又称菜花。
7. **西兰花**:营养丰富。
8. **芜菁**:也叫蔓菁。
9. **荠菜**:是一种野菜。
10. **西洋菜**:常见于水生环境。
11. **独行菜**:有一定的药用价值。
12. **紫罗兰**:观赏花卉。
13. **诸葛菜**:又名二月兰。
14. **播娘蒿**:具有一定的药用功效。
这些植物在农业、园艺和日常生活中都有广泛的应用。
大功告成
总结
话说火山引擎的安装FAQ文档写得太简陋了。这么好的AI引擎,却在安装过程中折腾了这么好久,真是坏人心情。好在一起最终都搞定了。
写个博文,给新来的小伙伴做个debug参考,免得走弯路!
如果有帮到你,不妨点个赞再走。
作者:IT里的交易员