Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable #211

Open
bigdataer01 opened this issue Dec 4, 2023 · 3 comments

Comments

@bigdataer01
Copy link

在本地使用如下命令启动了api server
MODEL=../chatglm-ggml.bin uvicorn chatglm_cpp.openai_api:app --host 127.0.0.1 --port 8000

使用openai_client访问的时候报错
OPENAI_API_BASE=http://127.0.0.1:8000/v1 python3 examples/openai_client.py --stream --prompt 你好

报错如下:
openai.OpenAIError: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable

@chibinjiang
Copy link

随便给 OPENAI_API_KEY 赋值就行:
OPENAI_API_KEY=hello OPENAI_API_BASE=http://127.0.0.1:8000/v1 python openai_client.py --stream --prompt 你好

@beiwei30
Copy link

openai 1.5.0 中,指定 OPENAI_API_KEY=hello 后 openai_client.py 超时

openai.APITimeoutError: Request timed out.

openai_client.py 执行了 openai migrate 做了升级:

diff --git a/examples/openai_client.py b/examples/openai_client.py
index 372a541..e1ddfa5 100644
--- a/examples/openai_client.py
+++ b/examples/openai_client.py
@@ -1,6 +1,8 @@
 import argparse

-import openai
+from openai import OpenAI
+
+client = OpenAI()

 parser = argparse.ArgumentParser()
 parser.add_argument("--stream", action="store_true")
@@ -9,11 +11,11 @@ args = parser.parse_args()

 messages = [{"role": "user", "content": args.prompt}]
 if args.stream:
-    response = openai.ChatCompletion.create(model="default-model", messages=messages, stream=True)
+    response = client.chat.completions.create(model="default-model", messages=messages, stream=True)
     for chunk in response:
         content = chunk["choices"][0]["delta"].get("content", "")
         print(content, end="", flush=True)
     print()
 else:
-    response = openai.ChatCompletion.create(model="default-model", messages=messages)
+    response = client.chat.completions.create(model="default-model", messages=messages)
     print(response["choices"][0]["message"]["content"])

@beiwei30
Copy link

补充一下,安装 0.28.1 是工作的

pip install openai==0.28.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants