沃卡AI使用指南
  1. Python中使用配置
沃卡AI使用指南
  • 引言
  • 关于沃卡中转API
    • 背景介绍
    • 为什么建议您用中转API?
    • 选择沃卡API的理由
  • 简易教程(新手必看)
    • 卡密购买
    • API站内使用教程
    • API接口文档(开发者使用)
  • Python中使用配置
    • Langchain中使用
    • 官方 openai 库中使用
    • 通过request非流式请求示例(chat示例)
    • 通过 流式请求示例(chat示例)
    • 通request请求示例(图片分析示例)
  • 主流AI应用配置教程(免费)
    • 【必读】API地址(Base Url)
    • Chatbox(移动端推荐)
    • ChatGPT Sidebar
    • CherryStudio(桌面应用推荐)
    • Cline
    • Cursor(写代码推荐)
    • Dify(工作流推荐)
    • GPT Academic(学术科研推荐)
    • LobeChat
    • NextChat
    • 沉浸式翻译(翻译推荐)
    • SillyTavern
    • Pot翻译
  • 支持模型列表
    • 大语言模型
    • 文生图模型
    • 文/图生视频模型
    • 其他模型(向量、文本、语音等)
  • 价格详情说明
    • 模型价格说明
    • Token的计算规则
  • 联系我们
    • 微信客服联系方式
    • QQ群
    • 站点列表汇总
  1. Python中使用配置

官方 openai 库中使用

import openai

def query_gpt4(question):
    openai.api_key = "sk-xxx"
    #openai.base_url = url
    openai.base_url = 'https://4.0.wokaai.com/v1/'


    try:
        response = openai.chat.completions.create(
            model="gpt-4",  # 确认使用 GPT-4 模型
            messages=[
                {"role": "system", "content": "You are a helpful assistant."},
                {"role": "user", "content": question}
            ]
        )
        print(response)
        return response['choices'][0].message['content']
    except Exception as e:
        return str(e)

# 问题
question = "为什么太阳那么红?"

# 获取并打印回答
answer = query_gpt4(question)
print(answer)

# 使用方法
若出现module 'openai' has no attribute 'chat'报错,请更新openai库:pip3 install --upgrade openai
若出现module 'attr' has no attribute 'frozen'报错,请更新输入:pip3 install --upgrade attrs
上一页
Langchain中使用
下一页
通过request非流式请求示例(chat示例)
Built with