更新中……

前置学习:OpenAI API学习

Web实现发送HTTP请求

官方API:

代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// API Key
var APIKey = '***';

// 参数
var url = 'https://api.openai.com/v1/chat/completions'
var model = "gpt-3.5-turbo";
var messages = [
{ "role": "system", "content": "You are ChatGPT, a helpful assistant"},
{ "role": "user", "content": "Hello, how are you?" }
];
var temperature = 0.7;


// 获取回复
var responseData;
function ShowResponse(data) {
responseData = data;
var responseContent = responseData.choices[0].message.content;
console.log(responseContent);
console.log(responseData);
}


// 创建请求体
var header = new Headers({
"Content-Type": "application/json",
"Authorization": "Bearer " + APIKey
})

var body = {
"model": model,
"messages": messages,
"temperature": temperature
}

// 发送请求
fetch(url, {
method: "POST",
headers: header,
body: JSON.stringify(body)
}).then(
function (response) {
response.json().then(
data => ShowResponse(data),
error => console.log(error)
);
});

使用浏览器测试

做个网页试试

Unity实现

UE5实现

使用中继服务器

用这种方法有以下问题:

  1. 国内使用要科学上网;
  2. API key不好隐藏;
  3. 无法限制用户使用次数;

创建 API Key 页面