ZToken Pro
API登录ZToken官网
API登录ZToken官网
  1. 示例代码
  • 欢迎使用ZToken
  • 概述
  • HelloWord-第一个示例
  • API最新消息
  • 模型和价格说明
  • 渠道路由策略
  • base_url地址到底是哪个?
  • 联系我们&bug反馈
  • 发票与报销
  • 推广合伙人赚钱计划
  • OpenAI接口
    • OpenAI概述
    • 使用OpenAI官方SDK
    • 使用 Codex CLI
    • Audio
    • Chat
    • Completions
    • Embeddings
    • Fine Tuning
    • Batches
    • Files
    • Images
    • Models
    • Moderations
    • Videos
    • Responses
    • Realtime
    • Assistants
    • Vector stores
  • Anthropic接口
    • Claude概述
    • 使用Claude官方SDK
    • 使用 Claude Code
    • Messages
    • Models
  • Google接口
    • Gemini概述
    • 使用Gemini官方SDK
    • 使用 Gemini CLI
    • Generating content
    • Interactions API
    • Files
    • Gemini文本生成
    • Gemini深度思考
    • Gemini函数调用
    • Gemini图片生成
    • Gemini图片理解
    • Gemini文档理解
    • Gemini视频理解
    • Gemini音频理解
    • Gemini代码执行
    • Gemini网页上下文
    • Gemini支持谷歌搜索
    • Gemini结构化输出
  • xAI接口
    • Grok概述
    • Chat
    • Responses
    • Images
    • Videos
  • DeepSeek接口
  • 阿里千问接口
  • 字节豆包接口
  • MiniMax接口
  • 快手可灵接口
  • ZToken系统接口
    • 查询余额
  • 模型说明
    • 其它模型示例
    • 费用计算说明
  • 其它说明
    • 主要概念
    • 常见问题
    • 错误码
  • 示例代码
    • 兼容openai的python库
    • 兼容openai的Node.js库
    • 兼容openai的其它各种库
    • 流式示例stream
    • 函数调用
    • 文字生成图片
    • 图片理解
    • python
    • js
    • java
    • php
    • c++
    • object-c
    • c#
    • curl
    • audio_transcriptions
    • translation
    • tts
    • langchain
  • batch
    • batch示例
    • batch特别注意事项
  • video
    • video示例
    • video特别注意事项
  • 文章列表
    • ZToken-AI工具配置使用指南
    • 在 Cursor 中配置ZToken API
    • 大模型怎么实现连续对话(记忆上下文)
    • 为什么调用chatgpt的api接口没有返回??怎么查问题
  • 废弃
  1. 示例代码

函数调用

直接上代码,要替换成自己在ztoken的key
import os
import openai
import requests
import time
import json
import time

API_SECRET_KEY = "xxxx";
BASE_URL = "https://api.ztoken.pro/v1"

openai.api_key = API_SECRET_KEY
openai.api_base = BASE_URL

# Example dummy function hard coded to return the same weather
# In production, this could be your backend API or an external API
def get_current_weather(location, unit="fahrenheit"):
    """Get the current weather in a given location"""
    weather_info = {
        "location": location,
        "temperature": "72",
        "unit": unit,
        "forecast": ["sunny", "windy"],
    }
    return json.dumps(weather_info)

def run_conversation():
    # Step 1: send the conversation and available functions to GPT
    messages = [{"role": "user", "content": "What's the weather like in Boston?"}]
    functions = [
        {
            "name": "get_current_weather",
            "description": "Get the current weather in a given location",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "The city and state, e.g. San Francisco, CA",
                    },
                    "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
                },
                "required": ["location"],
            },
        }
    ]
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo-0613",
        messages=messages,
        functions=functions,
        # function_call="auto",  # auto is default, but we'll be explicit
        function_call={"name": "get_current_weather"},  # auto is default, but we'll be explicit
    )
    response_message = response["choices"][0]["message"]

    # Step 2: check if GPT wanted to call a function
    if response_message.get("function_call"):
        # Step 3: call the function
        # Note: the JSON response may not always be valid; be sure to handle errors
        available_functions = {
            "get_current_weather": get_current_weather,
        }  # only one function in this example, but you can have multiple
        function_name = response_message["function_call"]["name"]
        function_to_call = available_functions[function_name]
        function_args = json.loads(response_message["function_call"]["arguments"])
        function_response = function_to_call(
            location=function_args.get("location"),
            unit=function_args.get("unit"),
        )

        # Step 4: send the info on the function call and function response to GPT
        response_message["content"]="Testing" #注意这里要传入content,content不能为空,否则openai会报错
        messages.append(response_message)  # extend conversation with assistant's reply
        messages.append(
            {
                "role": "function",
                "name": function_name,
                "content": function_response,
            }
        )  # extend conversation with function response
        second_response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo-0613",
            messages=messages,
        )  # get a new response from GPT where it can see the function response
        return second_response

#print(run_conversation())

if __name__ == '__main__':
    run_conversation();
修改于 2026-07-03 14:17:35
上一页
流式示例stream
下一页
文字生成图片
Built with