跳到主要内容

如何通过调用API方式添加样本?

你还可以通过API的方式在样本集中添加样本,以便在评估中使用。

在添加样本时,需要用到你的API key,它可以在EvalsOne的设置页面中找到;以及样本集的ID,它可以在样本集的API添加方式说明标签中找到。

以下是一些常见编程语言中调用API添加样本的示例代码:

通过Curl添加样本的例子:

curl -X POST https://api.evalsone.com/api/sample/add \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <YOUR_API_KEY>" \
-d '{
"sid": "<SAMPLE_SID>",
"messages": [{"system": "example instruct"}, {"user": "example user message"}],
"completion": "<Model generated completion>",
"ideal": ["ideal answer 1', 'ideal answer 2'],
"context": ["Context 1", "Context 2"]
}'

你需要把其中的<YOUR_API_KEY>和<SAMPLE_SID>分别替换成你自己的API key和样本集ID。

参数名称是否必须格式要求含义
sid字符串样本的唯一标识符
messages消息列表一组包含对话消息的列表,每个消息为包含'system'或'user'键的Json对象
completion字符串模型生成的完成内容,可选
ideal字符串列表理想答案
context字符串列表上下文背景信息

通过Python添加样本的例子:

通过Python SDK添加

通过EvalsOne提供的Python SDK,只需几行代码就可以在样本集中添加样本。

首先安装EvalsOne提供的Python SDK:

pip install evalsone

然后使用以下代码添加样本:

eo_client = EvalsOne(api_key='<YOUR_API_KEY>', sid='<SAMPLE_SET_ID>')
sample_data = {'messages': {"role": "user", "content": "Introduce your develop team."}}
response = eo_client.add_sample(sample_data)

通过requests库添加

或者,你也可以直接使用requests库来发送POST请求:

import requests
import json

url = "https://api.evalsone.com/api/sample/add"
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer <YOUR_API_KEY>"
}
data = {
"sid": "<SAMPLE_SID>",
"messages": [{"system": "example instruct"}, {"user": "example user message"}],
"completion": "<Model generated completion>", # Optional, completed answer, string
"ideal": ["ideal answer 1", "ideal answer 2"], # Optional, ideal answer list, string or list of string
"context": ["Context 1", "Context 2"] # Optional, context list, string or list of string
}

requests.post(url, headers=headers, data=json.dumps(data))

通过PHP添加样本的例子:

<?php
$url = "https://api.evalsone.com/api/sample/add";
$headers = array(
"Content-Type: application/json",
"Authorization: Bearer <YOUR_API_KEY>"
);
$data = array(
"sid" => "<SAMPLE_SID>",
"messages" => array(array("system" => "example instruct"), array("user" => "example user message")),
"completion" => "<Model generated completion>", // Optional, generated completion, string
"ideal" => array("ideal answer 1", "ideal answer 2"), // Optional, ideal answer list, string or list of string
"context" => array("Context 1", "Context 2") // Optional, context list, string or list of string
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);
echo $response;
?>

通过NodeJS添加样本的例子:

const axios = require('axios');

const url = "https://api.evalsone.com/api/sample/add";
const headers = {
"Content-Type": "application/json",
"Authorization": "Bearer <YOUR_API_KEY>"
};
const data = {
sid: "<SAMPLE_SID>",
messages: [{system: "example instruct"}, {user: "example user message"}],
completion: "<Model generated completion>", // Optional, generated completion, string
ideal: ["ideal answer 1", "ideal answer 2"], // Optional, ideal answer list, string or list of string
context: ["Context 1", "Context 2"] // Optional List
};

axios.post(url, data, { headers: headers })
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error('There was an error!', error);
});