本周工作主要是对现有功能进行测试,并修复出现的问题。
用户和训练数据的关系绑定出现了问题,每次启动时关联表会自动清空。怀疑是因为关系定义不规范,导致初始化时将正常数据视作不规范数据清除,但没有找到原因。优化表结构,通过双向一对多的"多"方维护关系,省去关联表,解决了这一问题。
public class TrainingData {
@Id
private String id;
private Date time;
@ManyToOne
@JoinColumn(name = "user_id")
private User user;
private double score;
@OneToMany
@JoinTable(name = "training_data_dialogs",
joinColumns = {@JoinColumn(name = "training_data_id", referencedColumnName = "id")},
inverseJoinColumns = {@JoinColumn(name = "dialogs_id", referencedColumnName = "id")}
)
private List<Dialog> dialogs;
}
public class User {
//为避免long传向前台出现精度丢失,通过注释将long化为String
@Id
@JsonIgnore
private long id;
@Size(max = 50)
private String nickName;
@Column(unique = true)
@JsonSerialize(using= ToStringSerializer.class)
private long phone;
//男:false 女:true
private Boolean gender;
//切断循环引用
@OneToMany(mappedBy = "user")
@JsonIgnore
@JsonIgnoreProperties("user")
private List<TrainingData> trainingDataList;
}
此外,因为商汤InternLLM终止提供大模型服务,导致大模型迁移至阿里魔搭平台,同时导致大模型api变更,后端也进行了针对性的代表修改:
@Override
public String invokeModel(String chat){
InvokeModelRequest request = new InvokeModelRequest(chat);
// 创建httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建请求对象
HttpPost httpPost = new HttpPost( MODEL_BASE_URL + "/chat");
JSONObject jsonObject = (JSONObject) JSONObject.toJSON(request);
StringEntity entity = null;
try {
entity = new StringEntity(jsonObject.toString());
} catch (UnsupportedEncodingException e) {
return e.toString();
}
//指定请求编码方式
entity.setContentEncoding("utf-8");
//数据格式
entity.setContentType("application/json");
httpPost.setEntity(entity);
//发送请求
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpPost);
} catch (IOException e) {
return e.toString();
}
//解析返回结果
int statusCode = response.getStatusLine().getStatusCode();
HttpEntity entity1 = response.getEntity();
String body = null;
try {
body = EntityUtils.toString(entity1);
} catch (IOException e) {
return e.toString();
}
if(statusCode != 200){
return "出现错误:" + statusCode + " body: " + body;
}
//关闭资源
try {
response.close();
httpClient.close();
} catch (IOException e) {
return e.toString();
}
String result = body.substring(body.lastIndexOf("\"text\":") + 11, body.indexOf('\"', body.lastIndexOf("\"text\":") + 11));
return result;
}