智增增API
API登录演示ChatAPI应用示例API最新消息
API登录演示ChatAPI应用示例API最新消息
Github地址
智增增官网
  1. 示例代码
  • 欢迎使用智增增
  • 概述
  • HelloWord-第一个示例
  • API最新消息
  • OpenAI接口列表
    • OpenAI概述
    • Introduction介绍
    • Audio 音频
    • Chat 聊天
    • Completions 补全(Legacy)
    • Embeddings 嵌入
    • Fine-tuning 微调
    • Batch 批处理
    • Files 文件
    • Images 图像
    • Models 模型
    • Moderations 审核
  • Anthropic接口列表
    • Claude概述
    • Claude消息
    • Claude深度思考
  • Google接口列表
    • Gemini概述
    • Gemini文本生成
    • Gemini深度思考
    • Gemini函数调用
    • Gemini图片生成
    • Gemini支持谷歌搜索
  • Xai接口列表
    • Grok概述
    • Grok的chat
    • Grok深度思考
  • 自有API接口
    • 查询余额
    • Modify fine-tune
  • 模型说明
    • 模型和价格说明
    • 其它模型示例
    • 费用计算说明
    • 深度思考
    • 模型微调
  • 其它说明
    • 主要概念
    • 常见问题
    • base_url地址到底是哪个?
    • 更新记录
    • 退款说明
    • 错误码
  • 接口示例
    • 模型调用示例
    • API应用示例
    • 示例场景
    • 示例代码
      • audio_transcriptions(语音识别)
      • c#语言(支持Unity)
      • c++语言
      • curl
      • gpt-4-vision.图片理解
      • java语言(支持android)
      • js
      • langchain的支持
      • object-c语言(支持苹果IOS)
      • php
      • python
      • translation(识别并翻译成英文)
      • tts.speech.语音合成
      • 兼容openai的Node.js库
      • 兼容openai的python库
      • 兼容openai的其它各种库
      • 函数调用
      • 文字生成图片
      • 流式示例stream
  • fine-tune.微调
    • 微调常见错误
    • 微调示例
    • finetune特别注意事项
  • assistant.助手
    • assistant示例
  • batch.批处理
    • batch示例
    • batch特别注意事项
  • Documentation 使用手册
  • 文章列表
    • 智增增-AI工具配置使用指南
    • 大模型怎么实现连续对话(记忆上下文)
    • ChatGPT-Next-Web使用指南
    • 为什么调用chatgpt的api接口没有返回??怎么查问题
  1. 示例代码

函数调用

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

API_SECRET_KEY = "xxxx";
BASE_URL = "https://api.zhizengzeng.com/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();
修改于 2024-05-15 01:28:24
上一页
兼容openai的其它各种库
下一页
文字生成图片
Built with