InsCode GPT API
接口地址
https://inscode-api.csdn.net/api/v1/gpt
协议
post
、EventStream
请求参数列表
参数名 | 类型 | 是否必填 | 默认值 | 说明 |
---|---|---|---|---|
prompt | String | 否 | gpt参数,prompt与messages 二选一,messages的优先级比较高 | |
messages | Json | 否 | 支持传递上下文:比如[{'role':'user','content':'使用java语言写一个冒泡排序,不用写代码解释'}] | |
max_tokens | Integer | 否 | 最多生成token数量,默认2048,最多不超过4000 | |
temperature | Number | 否 | 默认0.7 | |
extract_code | Boolean | 否 | 默认: false,true 表示提取代码片 | |
prefix | String | 否 | 需要在用户提问的问题前面添加的前缀 | |
stream_token | Boolean | 否 | 是否使用 steam 模式,默认:true | |
apikey | String | 是 | 从运行环境中获得的 API KEY |
请求 Body 例子:
json
{
"messages": [
{
"role": "user",
"content": "用 java 写个快速排序"
}
],
"stream_token": false,
"apikey": "YOUR_API_KEY"
}
返回数据例子:
text
data:{"id":"chatcmpl-7tWBcVaUrNFYpSOMJOTBCKMUYEsEO","object":"chat.completion.chunk","created":1693466120,"model":"gpt-35-turbo","choices":[{"index":0,"finish_reason":null,"delta":{"role":"assistant"}}],"usage":null}
data:{"id":"chatcmpl-7tWBcVaUrNFYpSOMJOTBCKMUYEsEO","object":"chat.completion.chunk","created":1693466120,"model":"gpt-35-turbo","choices":[{"index":0,"finish_reason":null,"delta":{"content":"下"}}],"usage":null}
... ...
data:{"id":"chatcmpl-7tWBcVaUrNFYpSOMJOTBCKMUYEsEO","object":"chat.completion.chunk","created":1693466120,"model":"gpt-35-turbo","choices":[{"index":0,"finish_reason":"stop","delta":{}}],"usage":null}
data:[DONE]
示例
Java
post
java
private static void unStreamType() throws Exception {
String apikey = "YOUR_API_KEY";
URL url = new URL("https://inscode-api.csdn.net/api/v1/gpt");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
String requestBody = "{" +
" \"messages\": [" +
" {" +
" \"role\": \"user\"," +
" \"content\": \"用 java 写个快速排序\"" +
" }" +
" ]," +
" \"stream_token\": false," +
" \"apikey\": \"" + apikey + "\"" +
"}";
os.write(requestBody.getBytes());
os.flush();
os.close();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
}
Event Stream
java
private static void StreamType() throws Exception {
String apikey = "YOUR_API_KEY";
URL url = new URL("https://inscode-api.csdn.net/api/v1/gpt");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
String requestBody = "{" +
" \"messages\": [" +
" {" +
" \"role\": \"user\"," +
" \"content\": \"用 java 写个快速排序\"" +
" }" +
" ]," +
" \"stream_token\": true," +
" \"apikey\": \"" + apikey + "\"" +
"}";
os.write(requestBody.getBytes());
os.flush();
os.close();
Scanner scanner = new Scanner(conn.getInputStream());
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
}
}
Python
SDK
InsCode 提供了 Python 的 SDK 便于快速使用一次性返回内容的 GPT 场景,如文本生成。该 SDK 仅限在 InsCode 环境中使用(开发、发布到社区或部署都可以),无需填写 apikey。
python
import inscode
prompt = '根据关键词创作文章'
content = 'Python'
response = inscode.ai(prompt, content)
post
python
import requests
apikey = "YOUR_API_KEY"
url = "https://inscode-api.csdn.net/api/v1/gpt"
headers = {
"Content-Type": "application/json"
}
data = {
"messages": [
{
"role": "user",
"content": "用 java 写个快速排序"
}
],
"stream_token": False,
"apikey": apikey
}
response = requests.post(url, json=data, headers=headers)
if response.status_code == 200:
print(response.text)
else:
print("Request failed")
Event Stream
python
import json
import requests
from sseclient import SSEClient # pip install sseclient-py
apikey = "YOUR_API_KEY"
url = "https://inscode-api.csdn.net/api/v1/gpt"
headers = {
"Content-Type": "application/json"
}
data = {
"messages": [
{
"role": "user",
"content": "用 java 写个快速排序"
}
],
"stream_token": False,
"apikey": apikey
}
response = requests.post(url, json=data, headers=headers, stream=True)
client = SSEClient(response)
for event in client.events():
print(event.data)
JavaScript
post
javascript
import axios from 'axios';
axios
.post(
'https://inscode-api.csdn.net/api/v1/gpt',
{
'messages': [
{
'role': 'user',
'content': '用 java 写个快速排序'
}
],
'stream_token': false,
'apikey': 'YOUR_API_KEY'
},
{
headers: {
'Content-Type': 'application/json'
}
}
)
.then(({data: res}) => {
console.log(res);
});
Event Stream
javascript
import { fetchEventSource } from '@microsoft/fetch-event-source';
const source = fetchEventSource(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
messages: [
{
role: 'user',
content: "question"
}
],
apikey: "apiKey"
}),
onopen: (response) => {
},
onmessage: (msg) => {
if (msg.data === '[DONE]') {
};
const data = JSON.parse(msg.data);
},
onerror: (err) => {
console.log("error", err);
}
});