chatanywhere / GPT_API_free

Free ChatGPT API Key,免费ChatGPT API,支持GPT4 API(免费),ChatGPT国内可用免费转发API,直连无需代理。可以搭配ChatBox等软件/插件使用,极大降低接口使用成本。国内即可无限制畅快聊天。
MIT License
24.71k stars 1.87k forks source link

无返回,但扣费并有输出token #271

Closed homo11451419 closed 3 months ago

homo11451419 commented 3 months ago

Describe the bug 描述bug 如题。

To Reproduce 复现方法

import openai
import json

client = openai.OpenAI(
    api_key = 'ket',
    base_url = 'https://api.chatanywhere.com.cn'
)

# 定义一个简单的函数,它将在function call中被调用
def get_current_time():
    return time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())

# 定义一个函数来处理OpenAI的function call
def handle_function_call(name, arguments):
    if name == "get_current_time":
        return get_current_time()
    return None

# 定义流式输出的生成器
def stream_gpt_response(prompt):
    response = client.chat.completions.create(
        model="gpt-4o",  # 使用gpt-4o模型
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": prompt}
        ],
        stream=True,
        functions=[
            {
                "name": "get_current_time",
                "description": "Returns the current GMT time.",
                "parameters": {
                    "type": "object",
                    "properties": {}
                }
            }
        ]
    )

    # 处理流式输出
    for chunk in response:
        if 'choices' in chunk and len(chunk['choices']) > 0:
            choice = chunk['choices'][0]
            if 'delta' in choice:
                delta = choice['delta']
                if 'content' in delta:
                    yield delta['content']
                elif 'function_call' in delta:
                    name = delta['function_call']['name']
                    arguments = delta['function_call']['arguments']
                    result = handle_function_call(name, arguments)
                    yield result

if __name__ == "__main__":
    user_input = input("You: ")
    print("GPT: ", end='', flush=True)
    for text in stream_gpt_response(user_input):
        print(text, end='', flush=True)

Screenshots 截图

image image

Tools or Programming Language 使用的工具或编程语言 python

ynpl commented 3 months ago

你代码有问题, 有返回的. 代码问题请自行解决 你在这里打印一下 chunk就会看到返回了 image image

homo11451419 commented 3 months ago

tks,but事POST请求(悲),用不了openai