千帆大模型已于2024年4月25日正式免費,調用這個免費的模型以實現自己的AI對話功能,遵循以下步驟:
了解千帆大模型:
千帆大模型是百度智能云推出的一個平臺,提供了一系列AI能力和工具,用于快速開發和應用AI技術。該平臺提供了從數據管理、數據標注、模型開發、模型納管到部署上線的全生命周期AI能力研發與應用管理服務。
注冊與登錄:
如果已經是開發者,可以直接登錄百度智能云千帆大模型平臺。如果沒有賬號,需要先進行注冊,并按照提示完成賬號的創建和登錄。
數據準備:
在開始之前,需要準備用于訓練和測試的數據。這些數據應該包含對話的輸入和期望的輸出。千帆大模型平臺支持多種數據格式和導入方式,可以根據實際情況選擇合適的方式進行數據導入。
數據標注:
對于導入的數據,需要進行標注,以便模型能夠理解和學習對話的模式和規則。在千帆大模型平臺上,可以使用平臺提供的工具進行數據的標注工作。
模型訓練:
在完成數據標注后,可以使用千帆大模型平臺提供的工具進行模型的訓練。訓練過程中,可以根據需要調整模型的參數和配置,以獲得更好的對話效果。
模型評估與優化:
訓練完成后,可以對模型進行評估,查看其在測試數據上的表現。如果模型的表現不佳,可以根據評估結果進行模型的優化和調整。
部署與測試:
當模型訓練和優化完成后,可以將其部署到千帆大模型平臺上,并進行實際的測試。通過與模型進行對話,驗證模型的性能和效果,并根據需要進行進一步的調整和優化。
集成與應用:
如果模型的表現符合預期,可以將其集成到自己的應用中,實現自己的AI對話功能。在集成過程中,需要注意與千帆大模型平臺的接口和協議進行匹配和對接。需要注意的是,雖然千帆大模型已經免費,但在使用過程中可能會產生一些額外的費用,如數據存儲、計算資源等。因此,在使用之前需要了解平臺的收費標準和計費方式,以便做好預算和規劃。
python實現與千帆大模型交互
import?sys
sys.path.append(r'C:\Users\XXX\AppData\Local\Programs\Python\Python312-32\Lib\site-packages')
import?requests
import?json
def?getcookies(): ?
??url?=?"?https://aip.baidubce.com/oauth/2.0/token"??
??params?= { ?
? ? ??'grant_type':?'client_credentials', ?
? ? ??'client_id':'4O0GgKpCfUT5mxZ4s3f', ?#?替換為你的客戶端ID ?
? ? ??'client_secret':?'RKETeu3iLn4YbCclMUei21ZVVnD6Y'?#?替換為你的客戶端密鑰??
? ? } ?
??headers?= { ?
? ? ??'Content-Type':?'application/x-www-form-urlencoded', ?
? ? ??'Accept':?'application/json'??
? ? } ?
? ? ?
??response?=?requests.post(url,?params=params,?headers=headers) ?
??if?response.status_code?==?200: ?
? ? ??data?=?response.json() ?
? ? ??if'access_token'?in?data: ?
? ? ? ? ??print(data['access_token'])
? ? ? ? ??return?data['access_token'] ?
? ? ??else: ?
? ? ? ? ??print("服務器響應中未找到?access_token") ?
? ? ? ? ??return?None??
??else: ?
? ? ??print(f"請求失敗,狀態碼:{response.status_code}") ?
? ? ??returnNone
? ?
def?getvalue(content,?token): ?
??token = getcookies() ? ? ? ?
#如果成功獲取的token格式如下,一個月獲取一次
?#?token?=?"24.0a2e1101e19ebe5e500a0962b30468fb.2592000.1700073516.282335-70004478"
??url?=?"?https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie-speed-128k?access_token="?+?token
? #content為填充的潤色語句,要求可以自行改進
??payload?=?json.dumps({
? ? ??"messages": [
? ? ? ? ? ? { ? ?
? ? ? ? ? ? ??"content":?"原文內容:{{"?+?content?+?"}}\
要求:\
1.?使用更專業與精準的詞匯,提取關鍵內容,重構文本表達更加簡潔、準確和有層次。\
2.?調整結構順序,使文字更具吸引力、可讀性和說服力,內容簡短精煉,字數不超過原始內容的70%。\
3.?重構后的文本相較于原文有較大的提升和改良,無需展示修改過程。\
4.?直接按新序號展示最終修訂后的內容,無需添加任何說明。",
"role":?"user"
? ? ? ? ? ? }
? ? ? ? ]
? ? })
??headers?= {
? ? ??'Content-Type':?'application/json'
? ? }
? ?
??response?=?requests.request("POST",?url,?headers=headers,?data=payload)
? ?
??#print(response.text)
??return?response.text
C++調用pyhton文件中的函數
頭文件定義
#ifndef?PYTHONCALLER_H
#define?PYTHONCALLER_H
#include?<Python.h>
#include?<string>
class?PythonCaller?{
private:
? PyObject*?pModule;
? PyObject*?pGetvalueFunc;
? PyObject*?pGetcookiesFunc;
public:
??PythonCaller();
??~PythonCaller();
??bool?Initialize(std::string?path,?std::string?name);
??void?Finalize();
??std::string?CallGetValue(const?std::string&?url,?const?std::string&?cookies);
??std::string?CallGetCookies();
};
#endif??// PYTHONCALLER_H
#else
#endif
源文件定義
#include?"PythonCaller.h"
PythonCaller::PythonCaller() :?pModule(nullptr) {}
PythonCaller::~PythonCaller() {
??Finalize();
}
bool?PythonCaller::Initialize(std::string?path,?std::string?name) {
??Py_Initialize();
??PyRun_SimpleString("import sys");
??PyRun_SimpleString(("sys.path.append('"?+ path +?"')").c_str());
? PyObject* pName =?PyUnicode_DecodeFSDefault(name.c_str());
? pModule =?PyImport_Import(pName);
??Py_DECREF(pName);
??if?(pModule ==?nullptr) {
? ? ??return0;
? ? }
? pGetvalueFunc =?PyObject_GetAttrString(pModule,"getvalue");
??if?(!pGetvalueFunc || !PyCallable_Check(pGetvalueFunc)) {
? ? ??Py_XDECREF(pGetvalueFunc);
? ? ??return0;
? ? }
? pGetcookiesFunc =?PyObject_GetAttrString(pModule,"getcookies");
??if?(!pGetcookiesFunc || !PyCallable_Check(pGetcookiesFunc)) {
? ? ??Py_XDECREF(pGetcookiesFunc);
? ? ??return0;
? ? }
??return?1;
}
void?PythonCaller::Finalize() {
??if?(pModule !=?nullptr) {
? ? ??Py_DECREF(pModule);
? ? ? pModule =?nullptr;??//將?pModule?置為?nullptr
? ? }
??if?(pGetvalueFunc !=?nullptr) {
? ? ??Py_DECREF(pGetvalueFunc);
? ? ? pGetvalueFunc =?nullptr;??//將?pModule?置為?nullptr
? ? }
??if?(pGetcookiesFunc !=?nullptr) {
? ? ??Py_DECREF(pGetcookiesFunc);
? ? ? pGetcookiesFunc =?nullptr;??//將?pModule?置為?nullptr
? ? }
??Py_Finalize();
}
std::string PythonCaller::CallGetValue(const?std::string&?url,?const?std::string&?cookies) {
??if?(!pGetvalueFunc || !PyCallable_Check(pGetvalueFunc)) {
? ? ??Py_XDECREF(pGetvalueFunc);
? ? ??return"";
? ? }
? ?
? PyObject* pArgs =?PyTuple_New(2);
??PyTuple_SetItem(pArgs,?0,?PyUnicode_FromString(url.c_str()));
??PyTuple_SetItem(pArgs,?1,?PyUnicode_FromString(cookies.c_str()));//?設置第二個參數為字符串
? PyObject* pValue =?PyObject_CallObject(pGetvalueFunc, pArgs);
??Py_DECREF(pArgs);
??if?(pValue ==?nullptr) {
? ? ??PyErr_Print();
? ? ??return"Error: Call to 'getvalue' failed";
? ? }
??std::string result;
??if?(pValue == Py_None) {
? ? ? result =?"Result of call: None";
? ? }
??else?{
? ? ? PyObject* pStr =?PyObject_Str(pValue);
? ? ??if?(pStr !=?nullptr) {
? ? ? ? ??const char* utf8_str =?PyUnicode_AsUTF8(pStr);
? ? ? ? ? result =?std::string(utf8_str);
? ? ? ? ??Py_DECREF(pStr);
? ? ? ? }
? ? ??else?{
? ? ? ? ? result =?"Error: Unable to convert result to string";
? ? ? ? }
? ? }
??Py_DECREF(pValue);
??return?result;
}
std::string PythonCaller::CallGetCookies() {
??if?(!pGetcookiesFunc || !PyCallable_Check(pGetcookiesFunc)) {
? ? ??Py_XDECREF(pGetcookiesFunc);
? ? ??return"";
? ? }
? PyObject* pArgs =?PyTuple_New(0);
? PyObject* pValue =?PyObject_CallObject(pGetcookiesFunc, pArgs);
??Py_DECREF(pArgs);
??if?(pValue ==?nullptr) {
? ? ??PyErr_Print();
? ? ??return"Error: Call to 'getvalue' failed";
? ? }
??std::string result;
??if?(pValue == Py_None) {
? ? ? result =?"Result of call: None";
? ? }
??else?{
? ? ? PyObject* pStr =?PyObject_Str(pValue);
? ? ??if?(pStr !=?nullptr) {
? ? ? ? ??const char* utf8_str =?PyUnicode_AsUTF8(pStr);
? ? ? ? ? result =?std::string(utf8_str);
? ? ? ? ??Py_DECREF(pStr);
? ? ? ? }
? ? ??else?{
? ? ? ? ? result =?"Error: Unable to convert result to string";
? ? ? ? }
? ? }
??Py_DECREF(pValue);
??return?result;
}
C++與python函數的交互過程
??if?(mgIspythonCaller ==?0) {
? ? ? mgIspythonCaller =?mgpythonCaller.Initialize(mgSelfPath,?"ai");
? ? }
? ?
??if?(!mgIspythonCaller) {
? ? ??return1;
? ? }
? string str =?mgpythonCaller.CallGetValue(content,?"");
visualstudio中python環境設置
根據程序環境,選擇對應版本的python安裝
下載源碼地址:https://www.lanzoub.com/iAFK0203yd4h