Closed homo11451419 closed 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 截图
Tools or Programming Language 使用的工具或编程语言 python
你代码有问题, 有返回的. 代码问题请自行解决 你在这里打印一下 chunk就会看到返回了
tks,but事POST请求(悲),用不了openai
Describe the bug 描述bug 如题。
To Reproduce 复现方法
Screenshots 截图
Tools or Programming Language 使用的工具或编程语言 python