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[kNIMAICallKeyAIRAGDescription].asString();
155 }
156};
157
161 std::string account_id_;
163 std::string request_id_;
167 std::list<NIMAIMessageRAGInfo> rags_;
169 time_t timestamp_{0};
171 bool streaming_{false};
174 void FromJsonString(const std::string& json_string) {
176 ParseJsonValue(json_string, values);
178 account_id_ = values[kNIMAICallKeyAccountID].asString();
180 request_id_ = values[kNIMAICallKeyRequestID].asString();
181 if (values.isMember(kNIMAICallKeyContent)) {
182 const auto& content = values[kNIMAICallKeyContent];
183 if (content.isMember(kNIMAICallKeyContentKeyMsg))
184 content_.msg_ = content[kNIMAICallKeyContentKeyMsg].asString();
185 if (content.isMember(kNIMAICallKeyContentKeyType))
186 content_.type_ = content[kNIMAICallKeyContentKeyType].asInt();
187 }
188 if (values.isMember(kNIMAICallKeyAIRAGs) && values[kNIMAICallKeyAIRAGs].isArray()) {
189 for (const auto& rag : values[kNIMAICallKeyAIRAGs]) {
190 NIMAIMessageRAGInfo rag_info;
191 rag_info.FromJsonValue(rag);
192 rags_.push_back(rag_info);
193 }
194 }
196 timestamp_ = values[kNIMAICallKeyContentKeyTimestamp].asInt64();
198 streaming_ = values[kNIMAICallKeyStreaming].asBool();
200 streaming_message_status_ = static_cast<NIMAIStreamingMessageStatus>(values[kNIMAICallKeyStreamingMessageStatus].asInt());
201 }
202};
203
207 std::string content_;
209 time_t chunk_time_{0};
211 int type_{0};
213 uint32_t index_{0};
216 values[kNIMAICallKeyStreamingMessageChunkChunkTime] = static_cast<int64_t>(chunk_time_);
219 }
229 }
230};
231
235 std::string message_;
237 int type_{0};
242 message_ = values[kNIMAICallKeyContentKeyMsg].asString();
244 type_ = values[kNIMAICallKeyContentKeyType].asInt();
247 }
248 }
249};
250
256 std::string account_id_;
258 std::string request_id_;
262 std::list<NIMAIMessageRAGInfo> rags_;
264 time_t timestamp_{0};
266 if (values.isMember(kNIMResCode))
267 code_ = static_cast<NIMResCode>(values[kNIMResCode].asInt());
269 account_id_ = values[kNIMAICallKeyAccountID].asString();
271 request_id_ = values[kNIMAICallKeyRequestID].asString();
272 if (values.isMember(kNIMAICallKeyContent))
273 content_.FromJsonValue(values[kNIMAICallKeyContent]);
274 if (values.isMember(kNIMAICallKeyAIRAGs) && values[kNIMAICallKeyAIRAGs].isArray()) {
275 for (const auto& rag : values[kNIMAICallKeyAIRAGs]) {
276 NIMAIMessageRAGInfo rag_info;
277 rag_info.FromJsonValue(rag);
278 rags_.push_back(rag_info);
279 }
280 }
281 if (values.isMember(kNIMMsgKeyTime))
282 timestamp_ = values[kNIMMsgKeyTime].asInt64();
283 }
284};
285
289 std::string model_;
291 std::list<std::string> prompt_keys_;
292
293 void SetModel(const std::string& model) {
294 model_ = model;
295 field_valid_flags_ |= kAIModelConfigFieldFlagModel;
296 }
297 std::string GetModel() const { return model_; }
298 void SetPromptKeys(const std::list<std::string>& prompt_keys) {
299 prompt_keys_ = prompt_keys;
300 field_valid_flags_ |= kAIModelConfigFieldFlagPromptKeys;
301 }
302 std::list<std::string> GetPromptKeys() const { return prompt_keys_; }
303};
304
307
308};
309
316
317 void SetModelType(NIMAIModelType model_type) {
318 model_type_ = model_type;
319 value_available_flag_ |= kUserNameCardKeyModelType;
320 }
321 NIMAIModelType GetModelType() const { return model_type_; }
322
323 void SetModelConfig(const NIMAIModelConfig& model_config) {
324 model_config_ = model_config;
325 value_available_flag_ |= kUserNameCardKeyModelConfig;
326 }
327 NIMAIModelConfig GetModelConfig() const { return model_config_; }
328
329 std::string ToJsonString() const;
330 nim_cpp_wrapper_util::Json::Value ToJsonObject() const;
331};
332
339NIM_SDK_CPPWRAPPER_DLL_API bool ParseAIUserNameCards(const std::string& namecards_json, std::list<AIUserNameCard>& namecards);
340
347NIM_SDK_CPPWRAPPER_DLL_API bool ParseAIUserNameCards(const nim_cpp_wrapper_util::Json::Value& namecards_json, std::list<AIUserNameCard>& namecards);
348
355NIM_SDK_CPPWRAPPER_DLL_API bool ParseAIUserNameCard(const std::string& namecard_json, AIUserNameCard& namecard);
356
363NIM_SDK_CPPWRAPPER_DLL_API void ParseAIUserNameCard(const nim_cpp_wrapper_util::Json::Value& namecard_json, AIUserNameCard& namecard);
364
371NIM_SDK_CPPWRAPPER_DLL_API void ParseAIUserModelConfig(const nim_cpp_wrapper_util::Json::Value& model_config_json, NIMAIModelConfig& model_config);
372
373} // namespace nim
374
375#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:306
static const char * kNIMAICallKeyAIRAGIcon
string AI RAG 信息图标
Definition: nim_ai_def.h:25
static const char * kNIMAICallKeyStreamingMessageChunkType
AI 流式消息分片类型,当前仅支持 0 表示文本
Definition: nim_ai_def.h:43
static const char * kNIMAICallKeyAIRAGTitle
string AI RAG 信息标题
Definition: nim_ai_def.h:29
NIMAIModelType
AI 数字人消息模型类型
Definition: nim_ai_def.h:101
static const char * kNIMAICallKeyStreamingMessageChunkContent
AI 流式消息分片内容
Definition: nim_ai_def.h:37
NIMAIModelRoleType
AI 数字人消息角色类型
Definition: nim_ai_def.h:91
@ kNIMMessageAIModelRoleTypeSystem
系统
Definition: nim_ai_def.h:93
static const char * kNIMAICallKeyAIRAGUrl
string AI RAG 信息地址
Definition: nim_ai_def.h:27
static const char * kNIMAICallKeyStreamingMessageChunk
AI 流式消息分片信息
Definition: nim_ai_def.h:35
static const char * kNIMAICallKeyAIRAGDescription
time_t AI RAG 信息时间
Definition: nim_ai_def.h:33
static const char * kNIMAICallKeyContentKeyMsg
请求/响应的文本内容
Definition: nim_ai_def.h:50
static const char * kNIMAICallKeyRequestID
AI proxy 请求 ID
Definition: nim_ai_def.h:13
static const char * kNIMAICallKeyStreamingMessageChunkIndex
AI 流式消息分片序号,从 0 开始
Definition: nim_ai_def.h:45
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:54
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:41
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:52
static const char * kNIMAICallKeyContent
请求大模型的内容
Definition: nim_ai_def.h:48
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:311
NIMAIModelType model_type_
模型选择
Definition: nim_ai_helper.h:313
void SetModelType(NIMAIModelType model_type)
Definition: nim_ai_helper.h:317
NIMAIModelConfig GetModelConfig() const
Definition: nim_ai_helper.h:327
NIMAIModelConfig model_config_
模型相关配置文件
Definition: nim_ai_helper.h:315
void SetModelConfig(const NIMAIModelConfig &model_config)
Definition: nim_ai_helper.h:323
NIMAIModelType GetModelType() const
Definition: nim_ai_helper.h:321
消息 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:159
void FromJsonString(const std::string &json_string)
Definition: nim_ai_helper.h:174
std::list< NIMAIMessageRAGInfo > rags_
数字人回复内容的引用资源列表
Definition: nim_ai_helper.h:167
std::string request_id_
本次响应的标识
Definition: nim_ai_helper.h:163
NIMAIModelCallContent content_
请求 AI 的回复
Definition: nim_ai_helper.h:165
std::string account_id_
数字人的账号 ID
Definition: nim_ai_helper.h:161
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:287
std::list< std::string > prompt_keys_
提示词对应的变量
Definition: nim_ai_helper.h:291
std::list< std::string > GetPromptKeys() const
Definition: nim_ai_helper.h:302
std::string model_
具体大模型版本模型名
Definition: nim_ai_helper.h:289
void SetModel(const std::string &model)
Definition: nim_ai_helper.h:293
void SetPromptKeys(const std::list< std::string > &prompt_keys)
Definition: nim_ai_helper.h:298
std::string GetModel() const
Definition: nim_ai_helper.h:297
消息 AI 流式消息分片信息
Definition: nim_ai_helper.h:205
void ToJsonValue(nim_cpp_wrapper_util::Json::Value &values) const
Definition: nim_ai_helper.h:214
std::string content_
流式消息回复分片文本
Definition: nim_ai_helper.h:207
void FromJsonValue(const nim_cpp_wrapper_util::Json::Value &values)
Definition: nim_ai_helper.h:220
消息 AI 流式消息内容
Definition: nim_ai_helper.h:233
void FromJsonValue(const nim_cpp_wrapper_util::Json::Value &values)
Definition: nim_ai_helper.h:240
NIMAIModelStreamingCallChunk chunk_
流式消息最后分片的信息
Definition: nim_ai_helper.h:239
std::string message_
流式消息经过拼接后的完整内容
Definition: nim_ai_helper.h:235
AI 数字人流式消息结果
Definition: nim_ai_helper.h:252
std::string account_id_
数字人的账号 ID
Definition: nim_ai_helper.h:256
std::string request_id_
本次响应的标识
Definition: nim_ai_helper.h:258
std::list< NIMAIMessageRAGInfo > rags_
数字人回复内容的引用资源列表
Definition: nim_ai_helper.h:262
void FromJsonValue(const nim_cpp_wrapper_util::Json::Value &values)
Definition: nim_ai_helper.h:265
NIMAIModelStreamingCallContent content_
请求 AI 的回复
Definition: nim_ai_helper.h:260
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