NIM 跨平台 C++ SDK
载入中...
搜索中...
未找到
nim_ai_helper.h
浏览该文件的文档.
1
7#ifndef NIM_AI_HELPER_H
8#define NIM_AI_HELPER_H
9
10#include <functional>
11#include <list>
12#include <map>
13#include <string>
15#include "nim_define_include.h"
16#include "nim_user_helper.h"
17
18namespace nim {
19
23 std::string msg_;
25 int32_t type_{0};
26};
27
33 std::string msg_;
35 int32_t type_{0};
36};
37
41 bool antispam_enabled_{true};
44 void ToJsonValue(nim_cpp_wrapper_util::Json::Value& values) const;
45 std::string ToJsonString() const;
46};
47
61};
62
66 std::string prompt_;
68 int32_t max_tokens_{0};
70 float top_p_{0.0};
72 float temperature_{0.0};
73
74 void SetPrompt(const std::string& prompt) {
75 prompt_ = prompt;
76 field_valid_flags_ |= kAIModelConfigFieldFlagPrompt;
77 }
78 std::string GetPrompt() const { return prompt_; }
79 void SetMaxTokens(int32_t max_tokens) {
80 max_tokens_ = max_tokens;
81 field_valid_flags_ |= kAIModelConfigFieldFlagMaxTokens;
82 }
83 int32_t GetMaxTokens() const { return max_tokens_; }
84 void SetTopP(float top_p) {
85 top_p_ = top_p;
86 field_valid_flags_ |= kAIModelConfigFieldFlagTopP;
87 }
88 float GetTopP() const { return top_p_; }
89 void SetTemperature(float temperature) {
90 temperature_ = temperature;
91 field_valid_flags_ |= kAIModelConfigFieldFlagTemperature;
92 }
93 float GetTemperature() const { return temperature_; }
94
95 bool ExistValue(NIMAIModelConfigFieldFlag flag) const { return (field_valid_flags_ & flag) != 0; }
96
97protected:
98 unsigned int field_valid_flags_{0};
99};
100
102
105 std::string account_id_;
109 std::list<NIMAIModelCallMessage> messages_;
111 std::string prompt_variables_;
115 bool streaming_{false};
116 void ToJsonValue(nim_cpp_wrapper_util::Json::Value& values) const;
117 nim_cpp_wrapper_util::Json::Value ToJsonValue() const;
118};
119
123 std::string name_;
125 std::string description_;
127 std::string icon_;
129 std::string url_;
131 std::string title_;
133 time_t time_{0};
135 values[kNIMAICallKeyAIRAGName] = name_;
136 values[kNIMAICallKeyAIRAGIcon] = icon_;
137 values[kNIMAICallKeyAIRAGUrl] = url_;
138 values[kNIMAICallKeyAIRAGTitle] = title_;
139 values[kNIMAICallKeyAIRAGTime] = static_cast<int64_t>(time_);
140 values[kNIMAICallKeyAIRAGDescription] = description_;
141 }
144 name_ = values[kNIMAICallKeyAIRAGName].asString();
146 icon_ = values[kNIMAICallKeyAIRAGIcon].asString();
148 url_ = values[kNIMAICallKeyAIRAGUrl].asString();
150 title_ = values[kNIMAICallKeyAIRAGTitle].asString();
152 time_ = values[kNIMAICallKeyAIRAGTime].asInt64();
154 description_ = values[kNIMAICallKeyAIRAGDesc].asString();
156 description_ = values[kNIMAICallKeyAIRAGDescription].asString();
157 }
158};
159
163 std::string account_id_;
165 std::string request_id_;
169 std::list<NIMAIMessageRAGInfo> rags_;
171 time_t timestamp_{0};
173 bool streaming_{false};
176 void FromJsonString(const std::string& json_string) {
178 ParseJsonValue(json_string, values);
180 account_id_ = values[kNIMAICallKeyAccountID].asString();
182 request_id_ = values[kNIMAICallKeyRequestID].asString();
183 if (values.isMember(kNIMAICallKeyContent)) {
184 const auto& content = values[kNIMAICallKeyContent];
185 if (content.isMember(kNIMAICallKeyContentKeyMsg))
186 content_.msg_ = content[kNIMAICallKeyContentKeyMsg].asString();
187 if (content.isMember(kNIMAICallKeyContentKeyType))
188 content_.type_ = content[kNIMAICallKeyContentKeyType].asInt();
189 }
190 if (values.isMember(kNIMAICallKeyAIRAGs) && values[kNIMAICallKeyAIRAGs].isArray()) {
191 for (const auto& rag : values[kNIMAICallKeyAIRAGs]) {
192 NIMAIMessageRAGInfo rag_info;
193 rag_info.FromJsonValue(rag);
194 rags_.push_back(rag_info);
195 }
196 }
198 timestamp_ = values[kNIMAICallKeyContentKeyTimestamp].asInt64();
200 streaming_ = values[kNIMAICallKeyStreaming].asBool();
202 streaming_message_status_ = static_cast<NIMAIStreamingMessageStatus>(values[kNIMAICallKeyStreamingMessageStatus].asInt());
203 }
204};
205
209 std::string content_;
211 time_t chunk_time_{0};
213 int type_{0};
215 uint32_t index_{0};
218 values[kNIMAICallKeyStreamingMessageChunkChunkTime] = static_cast<int64_t>(chunk_time_);
221 }
231 }
232};
233
237 std::string message_;
239 int type_{0};
244 message_ = values[kNIMAICallKeyContentKeyMsg].asString();
246 type_ = values[kNIMAICallKeyContentKeyType].asInt();
249 }
250 }
251};
252
258 std::string account_id_;
260 std::string request_id_;
264 std::list<NIMAIMessageRAGInfo> rags_;
266 time_t timestamp_{0};
268 if (values.isMember(kNIMResCode))
269 code_ = static_cast<NIMResCode>(values[kNIMResCode].asInt());
271 account_id_ = values[kNIMAICallKeyAccountID].asString();
273 request_id_ = values[kNIMAICallKeyRequestID].asString();
274 if (values.isMember(kNIMAICallKeyContent))
275 content_.FromJsonValue(values[kNIMAICallKeyContent]);
276 if (values.isMember(kNIMAICallKeyAIRAGs) && values[kNIMAICallKeyAIRAGs].isArray()) {
277 for (const auto& rag : values[kNIMAICallKeyAIRAGs]) {
278 NIMAIMessageRAGInfo rag_info;
279 rag_info.FromJsonValue(rag);
280 rags_.push_back(rag_info);
281 }
282 }
283 if (values.isMember(kNIMMsgKeyTime))
284 timestamp_ = values[kNIMMsgKeyTime].asInt64();
285 }
286};
287
291 std::string model_;
293 std::list<std::string> prompt_keys_;
294
295 void SetModel(const std::string& model) {
296 model_ = model;
297 field_valid_flags_ |= kAIModelConfigFieldFlagModel;
298 }
299 std::string GetModel() const { return model_; }
300 void SetPromptKeys(const std::list<std::string>& prompt_keys) {
301 prompt_keys_ = prompt_keys;
302 field_valid_flags_ |= kAIModelConfigFieldFlagPromptKeys;
303 }
304 std::list<std::string> GetPromptKeys() const { return prompt_keys_; }
305};
306
309
310};
311
318
319 void SetModelType(NIMAIModelType model_type) {
320 model_type_ = model_type;
321 value_available_flag_ |= kUserNameCardKeyModelType;
322 }
323 NIMAIModelType GetModelType() const { return model_type_; }
324
325 void SetModelConfig(const NIMAIModelConfig& model_config) {
326 model_config_ = model_config;
327 value_available_flag_ |= kUserNameCardKeyModelConfig;
328 }
329 NIMAIModelConfig GetModelConfig() const { return model_config_; }
330
331 std::string ToJsonString() const;
332 nim_cpp_wrapper_util::Json::Value ToJsonObject() const;
333};
334
341NIM_SDK_CPPWRAPPER_DLL_API bool ParseAIUserNameCards(const std::string& namecards_json, std::list<AIUserNameCard>& namecards);
342
349NIM_SDK_CPPWRAPPER_DLL_API bool ParseAIUserNameCards(const nim_cpp_wrapper_util::Json::Value& namecards_json, std::list<AIUserNameCard>& namecards);
350
357NIM_SDK_CPPWRAPPER_DLL_API bool ParseAIUserNameCard(const std::string& namecard_json, AIUserNameCard& namecard);
358
365NIM_SDK_CPPWRAPPER_DLL_API void ParseAIUserNameCard(const nim_cpp_wrapper_util::Json::Value& namecard_json, AIUserNameCard& namecard);
366
373NIM_SDK_CPPWRAPPER_DLL_API void ParseAIUserModelConfig(const nim_cpp_wrapper_util::Json::Value& model_config_json, NIMAIModelConfig& model_config);
374
375} // namespace nim
376
377#endif // NIM_AI_HELPER_H
Represents a JSON value.
Definition: value.h:196
bool isMember(const char *key) const
Definition: json_value.cpp:1241
bool isArray() const
Definition: json_value.cpp:1386
UInt asUInt() const
Definition: json_value.cpp:684
bool asBool() const
Definition: json_value.cpp:839
Int asInt() const
Definition: json_value.cpp:657
String asString() const
Embedded zeroes are possible.
Definition: json_value.cpp:632
namespace nim
bool ParseJsonValue(const std::string &content, nim_cpp_wrapper_util::Json::Value &values)
解析JsonString
Definition: nim_json_util.cpp:70
@ kUserNameCardKeyModelConfig
Model config 字段有效
Definition: nim_user_helper.h:44
@ kUserNameCardKeyModelType
Model type 字段有效
Definition: nim_user_helper.h:42
void ParseAIUserModelConfig(const nim_cpp_wrapper_util::Json::Value &model_config_json, NIMAIModelConfig &model_config)
解析数字人模型配置
Definition: nim_ai_helper.cpp:180
bool ParseAIUserNameCards(const std::string &namecards_json, std::list< AIUserNameCard > &namecards)
Definition: nim_ai_helper.cpp:115
NIMAIModelConfigFieldFlag
Definition: nim_ai_helper.h:48
@ kAIModelConfigFieldFlagTemperature
Temperature 字段有效
Definition: nim_ai_helper.h:56
@ kAIModelConfigFieldFlagPromptKeys
Prompt keys 字段有效
Definition: nim_ai_helper.h:60
@ kAIModelConfigFieldFlagModel
Model 字段有效
Definition: nim_ai_helper.h:58
@ kAIModelConfigFieldFlagMaxTokens
Max tokens 字段有效
Definition: nim_ai_helper.h:52
@ kAIModelConfigFieldFlagTopP
Top p 字段有效
Definition: nim_ai_helper.h:54
@ kAIModelConfigFieldFlagPrompt
Prompt 字段有效
Definition: nim_ai_helper.h:50
bool ParseAIUserNameCard(const std::string &namecard_json, AIUserNameCard &namecard)
Definition: nim_ai_helper.cpp:137
AIUserNameCardFieldFlag
AI 数字人名片字段有效性校验
Definition: nim_ai_helper.h:308
static const char * kNIMAICallKeyAIRAGIcon
string AI RAG 信息图标
Definition: nim_ai_def.h:25
static const char * kNIMAICallKeyStreamingMessageChunkType
AI 流式消息分片类型,当前仅支持 0 表示文本
Definition: nim_ai_def.h:45
static const char * kNIMAICallKeyAIRAGTitle
string AI RAG 信息标题
Definition: nim_ai_def.h:29
NIMAIModelType
AI 数字人消息模型类型
Definition: nim_ai_def.h:103
static const char * kNIMAICallKeyStreamingMessageChunkContent
AI 流式消息分片内容
Definition: nim_ai_def.h:39
NIMAIModelRoleType
AI 数字人消息角色类型
Definition: nim_ai_def.h:93
@ kNIMMessageAIModelRoleTypeSystem
系统
Definition: nim_ai_def.h:95
static const char * kNIMAICallKeyAIRAGUrl
string AI RAG 信息地址
Definition: nim_ai_def.h:27
static const char * kNIMAICallKeyStreamingMessageChunk
AI 流式消息分片信息
Definition: nim_ai_def.h:37
static const char * kNIMAICallKeyAIRAGDescription
time_t AI RAG 信息描述
Definition: nim_ai_def.h:35
static const char * kNIMAICallKeyContentKeyMsg
请求/响应的文本内容
Definition: nim_ai_def.h:52
static const char * kNIMAICallKeyRequestID
AI proxy 请求 ID
Definition: nim_ai_def.h:13
static const char * kNIMAICallKeyStreamingMessageChunkIndex
AI 流式消息分片序号,从 0 开始
Definition: nim_ai_def.h:47
static const char * kNIMAICallKeyAIRAGs
JSON array AI RAG(Retrieval-Augmented Generation) 信息
Definition: nim_ai_def.h:21
static const char * kNIMAICallKeyAIRAGName
string AI RAG 信息名称
Definition: nim_ai_def.h:23
static const char * kNIMAICallKeyContentKeyTimestamp
分片时间
Definition: nim_ai_def.h:56
static const char * kNIMAICallKeyAIRAGTime
time_t AI RAG 信息时间
Definition: nim_ai_def.h:31
static const char * kNIMAICallKeyStreaming
bool AI streaming message
Definition: nim_ai_def.h:17
static const char * kNIMAICallKeyStreamingMessageChunkChunkTime
AI 流式消息当前分片时间,chunkTime >= messageTime
Definition: nim_ai_def.h:43
static const char * kNIMAICallKeyAccountID
string AI account id
Definition: nim_ai_def.h:11
static const char * kNIMAICallKeyStreamingMessageStatus
int AI streaming message status
Definition: nim_ai_def.h:19
static const char * kNIMAICallKeyContentKeyType
类型, 暂时只有 0, 代表文本, 预留扩展能力
Definition: nim_ai_def.h:54
static const char * kNIMAICallKeyAIRAGDesc
time_t AI RAG 信息描述
Definition: nim_ai_def.h:33
static const char * kNIMAICallKeyContent
请求大模型的内容
Definition: nim_ai_def.h:50
NIM 公共数据类型定义总的包含文件
static const char * kNIMResCode
int, NIMResCode
Definition: nim_global_def.h:20
NIMAIStreamingMessageStatus
AI 流式消息状态
Definition: nim_msglog_def.h:547
@ kNIMAIStreamingMessageNone
非流式状态
Definition: nim_msglog_def.h:551
定义导出宏
#define NIM_SDK_CPPWRAPPER_DLL_API
Definition: nim_sdk_cpp_wrapper.h:38
static const char * kNIMMsgKeyTime
long,消息时间戳(毫秒)
Definition: nim_talk_def.h:196
SDK用户信息辅助方法
NIMResCode
Definition: public_defines.h:21
@ kNIMResSuccess
没有错误,一切正常
Definition: public_defines.h:26
AI 数字人用户信息
Definition: nim_ai_helper.h:313
NIMAIModelType model_type_
模型选择
Definition: nim_ai_helper.h:315
void SetModelType(NIMAIModelType model_type)
Definition: nim_ai_helper.h:319
NIMAIModelConfig GetModelConfig() const
Definition: nim_ai_helper.h:329
NIMAIModelConfig model_config_
模型相关配置文件
Definition: nim_ai_helper.h:317
void SetModelConfig(const NIMAIModelConfig &model_config)
Definition: nim_ai_helper.h:325
NIMAIModelType GetModelType() const
Definition: nim_ai_helper.h:323
消息 AI RAG 信息
Definition: nim_ai_helper.h:121
std::string title_
引用资源的标题
Definition: nim_ai_helper.h:131
std::string description_
引用资源的描述
Definition: nim_ai_helper.h:125
void ToJsonValue(nim_cpp_wrapper_util::Json::Value &values) const
Definition: nim_ai_helper.h:134
std::string name_
引用资源的名称
Definition: nim_ai_helper.h:123
std::string icon_
引用资源的图标
Definition: nim_ai_helper.h:127
void FromJsonValue(const nim_cpp_wrapper_util::Json::Value &values)
Definition: nim_ai_helper.h:142
std::string url_
引用资源的链接
Definition: nim_ai_helper.h:129
Definition: nim_ai_helper.h:103
NIMAIModelCallContent content_
请求大模型的内容
Definition: nim_ai_helper.h:107
std::string prompt_variables_
提示词变量占位符替换
Definition: nim_ai_helper.h:111
NIMAIModelConfigParams model_config_params_
请求接口模型相关参数配置, 如果参数不为空,则默认覆盖控制相关配置
Definition: nim_ai_helper.h:113
std::string account_id_
机器人账号 ID
Definition: nim_ai_helper.h:105
std::list< NIMAIModelCallMessage > messages_
上下文内容
Definition: nim_ai_helper.h:109
调用大模型的请求内容
Definition: nim_ai_helper.h:21
std::string msg_
请求/响应的文本内容
Definition: nim_ai_helper.h:23
int32_t type_
类型, 暂时只有 0, 代表文本, 预留扩展能力
Definition: nim_ai_helper.h:25
AI 数字人消息上下文参数
Definition: nim_ai_helper.h:29
std::string msg_
上下文的内容
Definition: nim_ai_helper.h:33
AI 数字人代理请求结果
Definition: nim_ai_helper.h:161
void FromJsonString(const std::string &json_string)
Definition: nim_ai_helper.h:176
std::list< NIMAIMessageRAGInfo > rags_
数字人回复内容的引用资源列表
Definition: nim_ai_helper.h:169
std::string request_id_
本次响应的标识
Definition: nim_ai_helper.h:165
NIMAIModelCallContent content_
请求 AI 的回复
Definition: nim_ai_helper.h:167
std::string account_id_
数字人的账号 ID
Definition: nim_ai_helper.h:163
AI 数字人模型配置参数
Definition: nim_ai_helper.h:64
bool ExistValue(NIMAIModelConfigFieldFlag flag) const
Definition: nim_ai_helper.h:95
void SetTemperature(float temperature)
Definition: nim_ai_helper.h:89
float GetTopP() const
Definition: nim_ai_helper.h:88
void SetPrompt(const std::string &prompt)
Definition: nim_ai_helper.h:74
std::string prompt_
提示词
Definition: nim_ai_helper.h:66
std::string GetPrompt() const
Definition: nim_ai_helper.h:78
int32_t GetMaxTokens() const
Definition: nim_ai_helper.h:83
void SetMaxTokens(int32_t max_tokens)
Definition: nim_ai_helper.h:79
float GetTemperature() const
Definition: nim_ai_helper.h:93
void SetTopP(float top_p)
Definition: nim_ai_helper.h:84
AI 数字人模型类型
Definition: nim_ai_helper.h:289
std::list< std::string > prompt_keys_
提示词对应的变量
Definition: nim_ai_helper.h:293
std::list< std::string > GetPromptKeys() const
Definition: nim_ai_helper.h:304
std::string model_
具体大模型版本模型名
Definition: nim_ai_helper.h:291
void SetModel(const std::string &model)
Definition: nim_ai_helper.h:295
void SetPromptKeys(const std::list< std::string > &prompt_keys)
Definition: nim_ai_helper.h:300
std::string GetModel() const
Definition: nim_ai_helper.h:299
消息 AI 流式消息分片信息
Definition: nim_ai_helper.h:207
void ToJsonValue(nim_cpp_wrapper_util::Json::Value &values) const
Definition: nim_ai_helper.h:216
std::string content_
流式消息回复分片文本
Definition: nim_ai_helper.h:209
void FromJsonValue(const nim_cpp_wrapper_util::Json::Value &values)
Definition: nim_ai_helper.h:222
消息 AI 流式消息内容
Definition: nim_ai_helper.h:235
void FromJsonValue(const nim_cpp_wrapper_util::Json::Value &values)
Definition: nim_ai_helper.h:242
NIMAIModelStreamingCallChunk chunk_
流式消息最后分片的信息
Definition: nim_ai_helper.h:241
std::string message_
流式消息经过拼接后的完整内容
Definition: nim_ai_helper.h:237
AI 数字人流式消息结果
Definition: nim_ai_helper.h:254
std::string account_id_
数字人的账号 ID
Definition: nim_ai_helper.h:258
std::string request_id_
本次响应的标识
Definition: nim_ai_helper.h:260
std::list< NIMAIMessageRAGInfo > rags_
数字人回复内容的引用资源列表
Definition: nim_ai_helper.h:264
void FromJsonValue(const nim_cpp_wrapper_util::Json::Value &values)
Definition: nim_ai_helper.h:267
NIMAIModelStreamingCallContent content_
请求 AI 的回复
Definition: nim_ai_helper.h:262
AI 数字人代理请求反垃圾配置
Definition: nim_ai_helper.h:39
std::string antispam_business_id_
指定易盾业务id
Definition: nim_ai_helper.h:43
云信用户名片
Definition: nim_user_helper.h:50