olivebot/utils/openai_api/openai_api.py
2024-12-11 15:29:38 +08:00

22 lines
767 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import openai
# 设置OpenAI API的密钥
# openai.api_key = os.getenv("OPENAI_API_KEY")
openai.base_url = "http://127.0.0.1:8000/v1/chat/completions"
# 定义API请求的数据
data = {
"model": "chatglm3-6b",
"prompt": "Say this is a test",
"temperature": 0.5, # 控制输出结果的随机性范围从0.0到1.0,越高越随机
"max_tokens": 75, # 控制输出文本的长度
"top_p": 1, # 一个更复杂的参数与temperature类似但更加精细控制
"n": 1, # 要返回的最完整的文本段落数
"stream": False # 是否以流的形式返回输出
}
# 发送API请求
response = openai.Completion.create(**data)
# 打印响应结果
print(response.get("choices")[0]["text"])