零一万物大模型开放平台 第二天 方式2:SDK API 接口兼容 OpenAI 的 Python SDK,只需要简单配置即可使用。 安装 OpenAI SDK。请确保使用的 Python 版本至少为 3.7.1,OpenAI SDK 版本不低于 1.0.0。 以下是我花了一天终于调通的: PS D:> python - v Python 3.12.4 (tags/v3.12.4:8e8a4ba, Jun 6 2024, 19:30:16) [MSC v.1940 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. 出现Bug: 所在位置 行:2 字符: 1

  • from openai import OpenAI

此语言版本中不支持“from”关键字。 解决方案: 适用于 Windows 的 Python 版本 |Python.org 安装: Python 3.8.10 - 2021 年 5 月 3 日 请注意,Python 3.8.10 不能在 Windows XP 或更早版本上使用。 下载 Windows 安装程序(64 位) 继续: pip install openai 显示: Installing collected packages: typing-extensions, sniffio, h11, distro, colorama, annotated-types, tqdm, pydantic-core, httpcore, anyio, pydantic, httpx, openai Successfully installed annotated-types-0.7.0 anyio-4.4.0 colorama-0.4.6 distro-1.9.0 h11-0.14.0 httpcore-1.0.5 httpx-0.27.0 openai-1.34.0 pydantic-2.7.4 pydantic-core-2.18.4 sniffio-1.3.1 tqdm-4.66.4 typing-extensions-4.12.2 继续: PS D:> import openai

from openai import OpenAI

API_BASE = "https://api.lingyiwanwu.com/v1" API_KEY = "fa75942eb8914a57bdddb44af543164f"

client = OpenAI(api_key=API_KEY, base_url=API_BASE)

completion = client.ChatCompletion.create( model="yi-spark", messages=[{"role": "user", "content": "Hi, who are you?"}] )

print(completion) 显示: ChatCompletion(id='cmpl-3c255018', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='Hello! I am an AI-powered language model based on the transformers architecture developed by OpenAI. My purpose is to be a helpful resource for you, capable of answering questions and offering insightful information across a wide range of topics. How may I be of service to you today?', role='assistant', function_call=None, tool_calls=None))], created=14505603, model='yi-large', object='chat.completion', system_fingerprint=None, usage=CompletionUsage(completion_tokens=59, prompt_tokens=16, total_tokens=75)) 与昨天的结果一致: 问: "Hi, who are you?" 答: 'Hello! I am an AI-powered language model based on the transformers architecture developed by OpenAI. My purpose is to be a helpful resource for you, capable of answering questions and offering insightful information across a wide range of topics. How may I be of service to you today?' 翻译: 问:“嗨,你是谁?” 答:“你好!我是一个基于OpenAI开发的transformers架构的人工智能语言模型。我的目的是成为你的有益资源,能够回答你的问题并提供跨广泛话题的有洞察力的信息。今天我能为你提供什么帮助呢?” 至此完成方式2:SDK 发起请求 补充一下: 前提是: 前置条件 请注册「零一万物大模型开放平台」并且完成 用户信息 认证。 如需创建或查看 API Key,前往「API Key 管理」。