OLLamaClient/OLlamaClientPP/OLlamaClientPP.cpp

133 lines
8.1 KiB
C++
Raw Permalink Normal View History

2025-06-05 10:49:37 +08:00
#include <iostream>
#include "Utils/Utils.h"
std::string GenerateByAI(std::string model, std::string msg)
{
msg = StringHelper::Replace(msg, "\r", " ");
msg = StringHelper::Replace(msg, "\n", " ");
msg = StringHelper::Replace(msg, "\\", "/");
std::string currStr = "";
std::string tmpStr = "";
auto json = StringHelper::Format(R"({"model":"%s","messages":[{"role":"user","content":"%s"}],"stream":true})", model.c_str(), msg.c_str());
auto result = HttpHelper::PostStream(
"http://localhost:11434/api/chat",
Convert::AnsiToUtf8(json),
"Content-Type: application/json",
"",
[&](std::string s)
{
tmpStr += s;
int idx = StringHelper::IndexOf(tmpStr, "\n");
if (idx >= 0)
{
std::string pak = tmpStr.substr(0, idx + 1);
auto j = json::parse(pak);
bool done = j["done"];
if (done == false)
{
if (j.contains("message"))
{
auto message = j["message"];
if (message.contains("content"))
{
std::string content = Convert::Utf8ToAnsi(message["content"]);
printf("%s", content.c_str());
currStr += content;
}
}
}
tmpStr = tmpStr.substr(idx + 1);
}
}
);
return currStr;
}
const char* _testMessage = R"(你需要扮演一名抖音客服,你的目标是解决客户的疑问并尽可能拿到客户的联系方式或者引导客户来联系我们的服务人员,你需要参考下面的文档来建立工作内容的知识,并参考聊天的上下文给出回复消息,你只需要输出回复的消息即可,不允许输出其他任何内容
:U代表客户,S代表你自己()
:
S:
U:,?
S:便
U:
S:便
U:
S:,便
U:19921461944
:
:
1.
便
2.
3.
便
4. :
便
便
1XX产品可以贴吗
V或二维码便
便WX号或二维码便
2//
便便V或二维码
3
便V或二维码
4
便V
5,
5.
便
6.
WX添加您只是给您介绍下
7.
[]
[]
8.
9. V
wx13636662468[]
10. /
便
WX无法搜索到
:
11.
5-105-10
12.
1-2
13.
使线
14.
TPU材质的PETTPU材质也有
15.
16.
17.
INTEGOMADICO3MSKINTEC等等品牌可以选择
18.
19.
20.
21.
wx13636662468[]
22.
,)";
int main()
{
system("ollama list");
std::string msg = GenerateByAI("qwen2.5-coder:32b", _testMessage);
printf("\n----------------------------------\n");
}